Qiskit / qiskit-aer

Aer is a high performance simulator for quantum circuits that includes noise models
https://qiskit.github.io/qiskit-aer/
Apache License 2.0
503 stars 361 forks source link

Segfault when using `StatevectorSimulator().run(qc)` #1341

Closed amarjahin closed 3 years ago

amarjahin commented 3 years ago

Informations

What is the current behavior?

Segmentation fault (core dumped).

Steps to reproduce the problem

Here's a MWE (at least as minimal as I can get the bug to occur)

from numpy import zeros, pi
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import StatevectorSimulator
from qiskit.circuit import Parameter, ParameterVector

def quadratic_exp(qc,theta, qubits): 
    i, j = qubits[0], qubits[1]
    for k in range(i, j): 
        qc.cx(k, k+1)
    qc.rz(theta, j)
    for k in range(i, j)[::-1]: 
        qc.cx(k, k+1)
    return None

def change_ansatz(m, init_gauge_p): 
    qc = QuantumCircuit(m, name='change_ansatz') 
    if len(init_gauge_p) != 0:
        qc.x(init_gauge_p)

    for q in init_gauge_p:
        for i in range(0, q): 
            qc.z(i)
    return qc

def PSU(num_qubits,fermion_qubits, gauge_qubits, steps=1, param_name='th', include_all=False):
    m_j, m_i = len(gauge_qubits), len(fermion_qubits)

    # defining the parameters used in this ansatz. 
    length = m_i * m_j
    theta_4 = ParameterVector(f'{param_name}_4', length=length)

    qc = QuantumCircuit(num_qubits, name='PSU')
    for _ in range(steps):
        index_4 = 0
        for i in fermion_qubits:
            for j in gauge_qubits: 
                #####################################################
                qc.rx(pi/2, [i])
                qc.h([j]) 
                quadratic_exp(qc=qc, theta=-(1/steps)*theta_4[index_4]/2, qubits=[i,j])
                qc.h([j]) 
                qc.rx(-pi/2, [i])

                qc.h([i]) 
                qc.rx(pi/2, [j])
                quadratic_exp(qc=qc, theta=(1/steps)*theta_4[index_4]/2, qubits=[i,j])
                qc.rx(-pi/2, [j])
                qc.h([i])

                index_4 = index_4 + 1  
    return qc

m = 16
qc = QuantumCircuit(m)
qc.append(change_ansatz(m, init_gauge_p=[7, 13, 14, 11]).to_instruction(), qargs=[*range(m)])
qc.append(PSU(num_qubits=m, fermion_qubits=[*range(4)], gauge_qubits=[*range(4,m)]).to_instruction()
        ,qargs=[*range(m)])
qc.append(change_ansatz(m, init_gauge_p=[7, 13, 14, 11]).to_instruction(), qargs=[*range(m)])

simulator = StatevectorSimulator()
qc = transpile(qc, simulator) 
params0 = list(zeros(qc.num_parameters))
qc = qc.bind_parameters(params0)
print('here0')
result = simulator.run(qc)
print('here1')
result = result.result()
print('here2')

Here's what I get running this:

here0
here1
------------------------------------------------------------------------
/usr/lib/python3.9/site-packages/cysignals/signals.cpython-39-x86_64-linux-gnu.so(+0x774d)[0x7f259aadb74d]
/usr/lib/python3.9/site-packages/cysignals/signals.cpython-39-x86_64-linux-gnu.so(+0x780c)[0x7f259aadb80c]
/usr/lib/python3.9/site-packages/cysignals/signals.cpython-39-x86_64-linux-gnu.so(+0xa98a)[0x7f259aade98a]
/usr/lib/libc.so.6(+0x3cda0)[0x7f25ce20bda0]
/usr/lib/libstdc++.so.6(_ZNKSs7compareEPKc+0x1d)[0x7f25a0e1b99d]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x42d0c)[0x7f25972b7d0c]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x60e65)[0x7f25972d5e65]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x930bf)[0x7f25973080bf]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x44121)[0x7f25972b9121]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x950d7)[0x7f259730a0d7]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0xac672)[0x7f2597321672]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0xaf86b)[0x7f259732486b]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0xaff48)[0x7f2597324f48]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x20f7bf)[0x7f25974847bf]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x210124)[0x7f2597485124]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x21027a)[0x7f259748527a]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x12fc2a)[0x7f25973a4c2a]
/home/ammar/.local/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so(+0x131af8)[0x7f25973a6af8]
/usr/lib/libpython3.9.so.1.0(+0x14952e)[0x7f25ce4e452e]
/usr/lib/libpython3.9.so.1.0(_PyObject_MakeTpCall+0x2e3)[0x7f25ce4cc813]
/usr/lib/libpython3.9.so.1.0(+0x147f3a)[0x7f25ce4e2f3a]
/usr/lib/libpython3.9.so.1.0(+0x9fe7a)[0x7f25ce43ae7a]
/usr/lib/libpython3.9.so.1.0(_PyObject_MakeTpCall+0x2e3)[0x7f25ce4cc813]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x4de6)[0x7f25ce4c8226]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x3c7)[0x7f25ce4c3807]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x61b)[0x7f25ce4c3a5b]
/usr/lib/libpython3.9.so.1.0(+0x127489)[0x7f25ce4c2489]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x3de)[0x7f25ce4d405e]
/usr/lib/libpython3.9.so.1.0(+0x147e9b)[0x7f25ce4e2e9b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x2f91)[0x7f25ce4c63d1]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x61b)[0x7f25ce4c3a5b]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x2f91)[0x7f25ce4c63d1]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x61b)[0x7f25ce4c3a5b]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(_PyEval_EvalFrameDefault+0x61b)[0x7f25ce4c3a5b]
/usr/lib/libpython3.9.so.1.0(_PyFunction_Vectorcall+0x1bb)[0x7f25ce4d3e3b]
/usr/lib/libpython3.9.so.1.0(+0x147e9b)[0x7f25ce4e2e9b]
/usr/lib/libpython3.9.so.1.0(+0x22d146)[0x7f25ce5c8146]
/usr/lib/libpython3.9.so.1.0(+0x208824)[0x7f25ce5a3824]
/usr/lib/libpthread.so.0(+0x9259)[0x7f25ce1b7259]
/usr/lib/libc.so.6(clone+0x43)[0x7f25ce2cd5e3]
------------------------------------------------------------------------
Attaching gdb to process id 159327.
Cannot find gdb installed
GDB is not installed.
Install gdb for enhanced tracebacks.
------------------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred.
This probably occurred because a *compiled* module has a bug
in it and is not properly wrapped with sig_on(), sig_off().
Python will now terminate.
------------------------------------------------------------------------
Segmentation fault (core dumped)

What is the expected behavior?

I'm not sure if this is qiskit's fault or linux's fault, or my fault. It's also that the error message is not clear (at least to me, someone who isn't a software engineer).

Suggested solutions

I honestly don't know. But as the error message suggest something needs to be wrapped with sig_on(), sig_off?

chriseclectic commented 3 years ago

Does this segfault still occur if you use the main branch?

amarjahin commented 3 years ago

A quick tangential question. I have a problem using qiskit-aer 0.9.0 (what I get by switching to main branch) and qiskit 0.29.1 which requires qiskit-aer 0.8.2. For example when I try import qiskit.providers.aer:

File "/home/ammar/Documents/qcode/fermilab_intern/test_sigfault/venv/lib/python3.9/site-packages/qiskit/providers/aer/__init__.py", line 73, in <module>
    from .aerprovider import AerProvider
  File "/home/ammar/Documents/qcode/fermilab_intern/test_sigfault/venv/lib/python3.9/site-packages/qiskit/providers/aer/aerprovider.py", line 19, in <module>
    from .backends.aer_simulator import AerSimulator
  File "/home/ammar/Documents/qcode/fermilab_intern/test_sigfault/venv/lib/python3.9/site-packages/qiskit/providers/aer/backends/__init__.py", line 17, in <module>
    from .aer_simulator import AerSimulator
  File "/home/ammar/Documents/qcode/fermilab_intern/test_sigfault/venv/lib/python3.9/site-packages/qiskit/providers/aer/backends/aer_simulator.py", line 27, in <module>
    from .controller_wrappers import aer_controller_execute
ImportError: /home/ammar/Documents/qcode/fermilab_intern/test_sigfault/venv/lib/python3.9/site-packages/qiskit/providers/aer/backends/controller_wrappers.cpython-39-x86_64-linux-gnu.so: undefined symbol: slamch_
hhorii commented 3 years ago

I have tested your code.

In my environment, Aer 0.8.2 can finish your code without error.

$ pip list | grep qiskit
qiskit                        0.29.0             
qiskit-aer                    0.8.2              
qiskit-aqua                   0.9.4              
qiskit-ibmq-provider          0.16.0             
qiskit-ignis                  0.6.0              
qiskit-machine-learning       0.2.0              
qiskit-nature                 0.2.0              
qiskit-sphinx-theme           1.8.5              
qiskit-terra                  0.18.1             

main can also finish your code without error, too.

amarjahin commented 3 years ago

Interesting. I tried matching your versions exactly,

$ pip list | grep qiskit
qiskit                        0.29.0
qiskit-aer                    0.8.2
qiskit-aqua                   0.9.4
qiskit-ibmq-provider          0.16.0
qiskit-ignis                  0.6.0
qiskit-terra                  0.18.1

and I still can't get it to run. Actually now I get a much shorter message of just Segmentation fault (core dumped).

Could this then be a problem with howa Aer interact with the OS? Can I ask what OS you use?

Also I think I was wrong about which line produces the segfault. Even if I commented result = result.result() in the above code I get the same error.

Another thing that is interesting is that if I comment line 57:

qc.append(change_ansatz(m, init_gauge_p=[7, 13, 14, 11]).to_instruction(), qargs=[*range(m)])

I don't get the error.

amarjahin commented 3 years ago

I had the chance to test this on a windows laptop, and it works fine on it as well. I still have the problem on Manjaro Linux. I'm not sure, is this Manjaro's problem? Should I close this issue?

amarjahin commented 3 years ago

Okay, I got the above code to run on my machine by adding a qc.barrier() in the function change_ansatz. It now looks like this:

def change_ansatz(m, init_gauge_p): 
    qc = QuantumCircuit(m, name='change_ansatz') 
    if len(init_gauge_p) != 0:
        qc.x(init_gauge_p)
    for q in init_gauge_p: 
        for i in range(0, q): 
            qc.z(i)
        qc.barrier()
    return qc

Since this works, I'm happy to close the issue, though this definitely seems a little weird, since barriers aren't supposed to change anything, do they?

chriseclectic commented 3 years ago

Barriers should only be for terra transpiler since they are ignored by Aer. The transpilation can affect simulation execution though.

The segfault (undefined symbol: slamch_) looks to be something related to LAPACK. I'm not sure why commenting out that line or adding a barrier would affect it though.