Skip to content

Commit e141008

Browse files
committed
Fix #855: don't call cirq.unitary() on non-unitary matrices
In Cirq 1.6.0, the function [`unitary(...)`](https://github.com/quantumlib/Cirq/blob/fe0dc2187ca3269c178526e8ba41083fa1a467c9/cirq-core/cirq/protocols/unitary_protocol.py#L81) in `cirq-core/cirq/protocols/unitary_protocol.py` changed. Whereas previously, if the value passed in was a NumPy array, it the function returned the array directly without doing anything else, `unitary(...)` was changed in [PR the array really _is_ unitary and raise an exception if it isn't. This arguably corrected a fault in `unitary(...)` because with the previous definition, it would return non-unitary results if given a NumPy array that wasn't already unitary. However, in one of the tests in qsim, it had the result of revealing a small bug: the test case was knowingly invoking `unitary(...)` with a non-unitary matrix. This led to exceptions being raised in Cirq 1.6.0 but not in prior versions. The solution in this case turned out to be very simple: the call to `cirq.unitary(...)` on line 1060 was unnecessary because the previous version of `unitary(...)` simply returned it right away. Removing the call kept the previous behavior (which was to use a non-unitary matrix in that situation) and works in both Cirq 1.5.0 and 1.6.0.
1 parent 7932f78 commit e141008

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

qsimcirq_tests/qsimcirq_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ 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+
return [(prob, op) for prob, op, in self._prob_op_pairs]
10611061

10621062

10631063
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)