Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
4.85k stars 2.29k forks source link

`RuntimeWarning in det r = _umath_linalg.det(a, signature=signature)` on MacOS #10248

Open spiccinelli opened 1 year ago

spiccinelli commented 1 year ago

Environment

What is happening?

Defining a PassManager object on MacOS throws a RuntimeWarning,

~/lib/python3.11/site-packages/numpy/linalg/linalg.py:2154: RuntimeWarning: divide by zero encountered in det
  r = _umath_linalg.det(a, signature=signature)
~/lib/python3.11/site-packages/numpy/linalg/linalg.py:2154: RuntimeWarning: invalid value encountered in det
  r = _umath_linalg.det(a, signature=signature)

Adding import numpy as np; np.seterr(all="raise") throws a FloatingPointError: divide by zero encountered in det. Maybe related to this issue?

How can we reproduce the issue?

from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import ConsolidateBlocks

pulse_efficient = PassManager(ConsolidateBlocks(basis_gates=["rz", "sx", "x", "rxx"]))

What should happen?

No warning should be raised.

Any suggestions?

No response

jakelishman commented 1 year ago

Could you show the output of numpy.show_config() (and the version of Numpy)? I suspect this is something BLAS-related, because on my Mac I can't reproduce it. I'm on macOS 13.3.1 and have OpenBLAS available, but I'm wondering if Apple have changed something about Accelerate in 13.4.

Is there more to your stack trace, as well? It'd help to know where in Qiskit code the error comes from.

spiccinelli commented 1 year ago

Thanks for the answer! I have numpy version 1.23.5, numpy.show_config() gives

openblas64__info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
    runtime_library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
blas_ilp64_opt_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
    runtime_library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
openblas64__lapack_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
    runtime_library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
lapack_ilp64_opt_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
    runtime_library_dirs = ['[/usr/local/lib](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib)']
Supported SIMD extensions in this NumPy install:
    baseline = NEON,NEON_FP16,NEON_VFPV4,ASIMD
    found = ASIMDHP
    not found = ASIMDDP,ASIMDFHM

Here's the full stack trace:

FloatingPointError                        Traceback (most recent call last)

      2 from qiskit.transpiler.passes import ConsolidateBlocks
      3 import numpy as np; np.seterr(all="raise")
----> 4 pulse_efficient = PassManager(ConsolidateBlocks(basis_gates=["rz", "sx", "x", "rxx"]))

File ~/lib/python3.11/site-packages/qiskit/transpiler/basepasses.py:30, in MetaPass.__call__(cls, *args, **kwargs)
     29 def __call__(cls, *args, **kwargs):
---> 30     pass_instance = type.__call__(cls, *args, **kwargs)
     31     pass_instance._hash = hash(MetaPass._freeze_init_parameters(cls, args, kwargs))
     32     return pass_instance

File ~/lib/python3.11/site-packages/qiskit/transpiler/passes/optimization/consolidate_blocks.py:69, in ConsolidateBlocks.__init__(self, kak_basis_gate, force_consolidate, basis_gates, approximation_degree, target)
     67     self.decomposer = TwoQubitBasisDecomposer(kak_basis_gate)
     68 elif basis_gates is not None:
---> 69     self.decomposer = unitary_synthesis._decomposer_2q_from_basis_gates(
     70         basis_gates, approximation_degree=approximation_degree
     71     )
     72 else:
     73     self.decomposer = TwoQubitBasisDecomposer(CXGate())

File ~lib/python3.11/site-packages/qiskit/transpiler/passes/synthesis/unitary_synthesis.py:129, in _decomposer_2q_from_basis_gates(basis_gates, pulse_optimize, approximation_degree)
    127     decomposer2q = XXDecomposer(euler_basis=euler_basis, backup_optimizer=backup_optimizer)
    128 elif kak_gate is not None:
--> 129     decomposer2q = TwoQubitBasisDecomposer(
    130         kak_gate,
    131         basis_fidelity=basis_fidelity,
    132         euler_basis=euler_basis,
    133         pulse_optimize=pulse_optimize,
    134     )
    135 return decomposer2q

File ~/lib/python3.11/site-packages/qiskit/quantum_info/synthesis/two_qubit_decompose.py:876, in TwoQubitBasisDecomposer.__init__(self, gate, basis_fidelity, euler_basis, pulse_optimize)
    873 self.basis_fidelity = basis_fidelity
    874 self.pulse_optimize = pulse_optimize
--> 876 basis = self.basis = TwoQubitWeylDecomposition(Operator(gate).data)
    877 self._decomposer1q = OneQubitEulerDecomposer(euler_basis)
    879 # FIXME: find good tolerances

File ~/lib/python3.11/site-packages/qiskit/quantum_info/synthesis/two_qubit_decompose.py:157, in TwoQubitWeylDecomposition.__new__(cls, unitary_matrix, fidelity, _unpickling)
    155 # Make U be in SU(4)
    156 U = np.array(unitary_matrix, dtype=complex, copy=True)
--> 157 detU = np.linalg.det(U)
    158 U *= detU ** (-0.25)
    159 global_phase = cmath.phase(detU) [/](https://file+.vscode-resource.vscode-cdn.net/) 4

File <__array_function__ internals>:180, in det(*args, **kwargs)

File ~/lib/python3.11/site-packages/numpy/linalg/linalg.py:2154, in det(a)
   2152 t, result_t = _commonType(a)
   2153 signature = 'D->D' if isComplexType(t) else 'd->d'
-> 2154 r = _umath_linalg.det(a, signature=signature)
   2155 r = r.astype(result_t, copy=False)
   2156 return r

FloatingPointError: divide by zero encountered in det
pedroripper commented 9 months ago

Does anyone have an update on this issue?

jakelishman commented 9 months ago

Looking at the runtime configuration, I'm assuming this is happening on M1/M2 Macs (the available SIMD instructions are ARM-like). Unfortunately, I've got an Intel Mac, so it's going to be difficult for me to help diagnose the full problem.

If you've got a complete minimal reproducible example (the example given at the top doesn't include the circuit that's being used, which I didn't notice at the time), that might help, though with me not having an ARM Mac, it'll be tricky.

If you do have an example, you could try setting the environment variable NPY_DISABLE_CPU_FEATURES=NEON before launching Python (or importing Numpy), and see if the problem still exists.