Skip to content

Commit 2acea90

Browse files
authored
Fix #855: don't call cirq.unitary() on non-unitary matrices (#857)
1 parent 7932f78 commit 2acea90

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

qsimcirq_tests/qsimcirq_test.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,17 @@ def __init__(self, *args, **kwargs):
10571057
super().__init__(*args, **kwargs)
10581058

10591059
def _mixture_(self):
1060-
return [(prob, cirq.unitary(op)) for prob, op, in self._prob_op_pairs]
1060+
# Cirq's mixture() function in mixture_protocol.py returns tuples of
1061+
# the form (probability, unitary operation). It does this by applying
1062+
# Cirq's unitary() function to the second elements of the tuples
1063+
# returned from here. Now, the values in self._prob_op_pairs will be
1064+
# tuples of the form (probability, NoiseStep). NoiseStep defines a
1065+
# _unitary_() method that simply returns the array as-is. Thus, when
1066+
# Cirq's mixture() function gets the value returned here and calls
1067+
# unitary() on those NoiseStep objects, the values unitary() returns
1068+
# will not actually be unitary. This is done knowingly. The nonunitary
1069+
# values are eventually normalized in test_multi_qubit_noise().
1070+
return [(prob, op) for prob, op, in self._prob_op_pairs]
10611071

10621072

10631073
@pytest.mark.parametrize(
@@ -2025,11 +2035,11 @@ def test_qsimcirq_identity_expectation_value():
20252035
for w, pauli in objs:
20262036
pauli = pauli[::-1]
20272037
hamiltonian += float(w) * cirq.PauliString(
2028-
cirq.I(cirq.LineQubit(i))
2029-
if p == "I"
2030-
else cirq.Z(cirq.LineQubit(i))
2031-
if p == "Z"
2032-
else None
2038+
(
2039+
cirq.I(cirq.LineQubit(i))
2040+
if p == "I"
2041+
else cirq.Z(cirq.LineQubit(i)) if p == "Z" else None
2042+
)
20332043
for i, p in enumerate(pauli)
20342044
)
20352045

0 commit comments

Comments
 (0)