From 6ab1f13e57a249ab51d6b6b4d4190cb5ef1a0b3b Mon Sep 17 00:00:00 2001 From: Craig Gidney Date: Mon, 28 Oct 2024 14:04:35 -0700 Subject: [PATCH] Fix unitary_matrix vs is_unitary test --- src/stim/gates/gates.pybind.cc | 2 +- src/stim/gates/gates_test.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/stim/gates/gates.pybind.cc b/src/stim/gates/gates.pybind.cc index df54e498..fdb46139 100644 --- a/src/stim/gates/gates.pybind.cc +++ b/src/stim/gates/gates.pybind.cc @@ -50,7 +50,7 @@ pybind11::object gate_tableau(const Gate &self) { return pybind11::none(); } pybind11::object gate_unitary_matrix(const Gate &self) { - if (self.flags & GATE_IS_UNITARY) { + if ((self.flags & GateFlags::GATE_IS_UNITARY) && (self.flags & (GateFlags::GATE_IS_SINGLE_QUBIT_GATE | GateFlags::GATE_TARGETS_PAIRS))) { auto r = self.unitary(); auto n = r.size(); std::complex *buffer = new std::complex[n * n]; diff --git a/src/stim/gates/gates_test.py b/src/stim/gates/gates_test.py index aae2b00c..400f5597 100644 --- a/src/stim/gates/gates_test.py +++ b/src/stim/gates/gates_test.py @@ -59,14 +59,17 @@ def test_gate_data_repr(): def test_gate_data_inverse(): for v in stim.gate_data().values(): assert v.is_unitary == (v.inverse is not None) - if v.is_unitary: - assert np.allclose(v.unitary_matrix.conj().T, v.inverse.unitary_matrix, atol=1e-6) + matrix = v.unitary_matrix + if matrix is not None: + assert v.is_unitary + assert np.allclose(matrix.conj().T, v.inverse.unitary_matrix, atol=1e-6) assert v.inverse == v.generalized_inverse assert stim.gate_data('H').inverse == stim.gate_data('H') assert stim.gate_data('S').inverse == stim.gate_data('S_DAG') assert stim.gate_data('M').inverse is None assert stim.gate_data('CXSWAP').inverse == stim.gate_data('SWAPCX') + assert stim.gate_data('SPP').inverse == stim.gate_data('SPP_DAG') assert stim.gate_data('S').generalized_inverse == stim.gate_data('S_DAG') assert stim.gate_data('M').generalized_inverse == stim.gate_data('M')