Skip to content

Commit

Permalink
Fix crash in newer macOS versions (#3884)
Browse files Browse the repository at this point in the history
There is an OpenBlas threading issue affecting scipy.linalg.eigh that is holding up #3591. This removes the call to eigh and replaces it with eig that uses a different underlying blas call and just takes the real part of the resulting eigenvalues.

This also made explicit two of the tests that were causing issues as they were off by a minus sign in the test suite but were fine when run explicitly.

* fix threading crash

* fix tests to be explicit
  • Loading branch information
nonhermitian authored Feb 26, 2020
1 parent 7e19692 commit fdd5603
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
5 changes: 4 additions & 1 deletion qiskit/quantum_info/operators/channel/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ def _choi_to_kraus(data, input_dim, output_dim, atol=ATOL_DEFAULT):
# Check if hermitian matrix
if is_hermitian_matrix(data, atol=atol):
# Get eigen-decomposition of Choi-matrix
w, v = la.eigh(data)
# This should be a call to la.eigh, but there is an OpenBlas
# threading issue that is causing segfaults.
w, v = la.eig(data)
w = w.real
# Check eigenvalues are non-negative
if len(w[w < -atol]) == 0:
# CP-map Kraus representation
Expand Down
21 changes: 15 additions & 6 deletions test/python/quantum_info/operators/channel/test_kraus.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import numpy as np
from numpy.testing import assert_allclose

from qiskit import QiskitError
from qiskit import QiskitError, QuantumCircuit, QuantumRegister
from qiskit.quantum_info.states import DensityMatrix
from qiskit.quantum_info.operators.channel import Kraus
from qiskit.quantum_info import Kraus, Operator
from .channel_test_case import ChannelTestCase


Expand Down Expand Up @@ -62,10 +62,19 @@ def test_init(self):

def test_circuit_init(self):
"""Test initialization from a circuit."""
circuit, target = self.simple_circuit_no_measure()
op = Kraus(circuit)
target = Kraus(target)
self.assertEqual(op, target)
unitary_x = np.array([[0, 1], [1, 0]])
unitary_h = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
y90 = (1 / np.sqrt(2)) * np.array([[1, -1], [1, 1]])

target = Operator(np.kron(y90, np.kron(unitary_x, unitary_h)))
qr = QuantumRegister(3)
circ = QuantumCircuit(qr)
circ.h(qr[0])
circ.x(qr[1])
circ.ry(np.pi / 2, qr[2])

ans = np.linalg.norm((Kraus(circ)-Kraus(target)).data)
self.assertAlmostEqual(ans, 0.0)

def test_circuit_init_except(self):
"""Test initialization from circuit with measure raises exception."""
Expand Down
21 changes: 15 additions & 6 deletions test/python/quantum_info/operators/channel/test_stinespring.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import numpy as np
from numpy.testing import assert_allclose

from qiskit import QiskitError
from qiskit import QiskitError, QuantumCircuit, QuantumRegister
from qiskit.quantum_info.states import DensityMatrix
from qiskit.quantum_info.operators.channel import Stinespring
from qiskit.quantum_info import Stinespring, Operator
from .channel_test_case import ChannelTestCase


Expand Down Expand Up @@ -56,10 +56,19 @@ def test_init(self):

def test_circuit_init(self):
"""Test initialization from a circuit."""
circuit, target = self.simple_circuit_no_measure()
op = Stinespring(circuit)
target = Stinespring(target)
self.assertEqual(op, target)
unitary_x = np.array([[0, 1], [1, 0]])
unitary_h = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
y90 = (1 / np.sqrt(2)) * np.array([[1, -1], [1, 1]])

target = Operator(np.kron(y90, np.kron(unitary_x, unitary_h)))
qr = QuantumRegister(3)
circ = QuantumCircuit(qr)
circ.h(qr[0])
circ.x(qr[1])
circ.ry(np.pi / 2, qr[2])

ans = np.linalg.norm((Stinespring(circ)-Stinespring(target)).data)
self.assertAlmostEqual(ans, 0.0)

def test_circuit_init_except(self):
"""Test initialization from circuit with measure raises exception."""
Expand Down

0 comments on commit fdd5603

Please sign in to comment.