Skip to content

Commit

Permalink
Fix unitary_matrix vs is_unitary test
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Oct 28, 2024
1 parent cec9e6c commit 6ab1f13
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/stim/gates/gates.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> *buffer = new std::complex<float>[n * n];
Expand Down
7 changes: 5 additions & 2 deletions src/stim/gates/gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 6ab1f13

Please sign in to comment.