Skip to content

Commit

Permalink
Fix broken caching in CliffordGate and add test (#5142)
Browse files Browse the repository at this point in the history
Looks like copy-pasta errors to me.

Also update a test to being parameterized and complete.
  • Loading branch information
dabacon authored Mar 25, 2022
1 parent dace549 commit 23976e2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
12 changes: 6 additions & 6 deletions cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,18 +610,18 @@ def I(cls):
@property
def X(cls):
if getattr(cls, '_X', None) is None:
cls._Z = cls._generate_clifford_from_known_gate(1, pauli_gates.X)
return cls._Z
cls._X = cls._generate_clifford_from_known_gate(1, pauli_gates.X)
return cls._X

@property
def Y(cls):
if getattr(cls, '_X', None) is None:
cls._Z = cls._generate_clifford_from_known_gate(1, pauli_gates.Y)
return cls._Z
if getattr(cls, '_Y', None) is None:
cls._Y = cls._generate_clifford_from_known_gate(1, pauli_gates.Y)
return cls._Y

@property
def Z(cls):
if getattr(cls, '_X', None) is None:
if getattr(cls, '_Z', None) is None:
cls._Z = cls._generate_clifford_from_known_gate(1, pauli_gates.Z)
return cls._Z

Expand Down
41 changes: 29 additions & 12 deletions cirq-core/cirq/ops/clifford_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,18 +529,26 @@ def test_text_diagram_info(gate, sym, exp):
)


def test_from_unitary():
def _test(clifford_gate):
u = cirq.unitary(clifford_gate)
result_gate = cirq.SingleQubitCliffordGate.from_unitary(u)
assert result_gate == clifford_gate

_test(cirq.SingleQubitCliffordGate.I)
_test(cirq.SingleQubitCliffordGate.H)
_test(cirq.SingleQubitCliffordGate.X)
_test(cirq.SingleQubitCliffordGate.Y)
_test(cirq.SingleQubitCliffordGate.Z)
_test(cirq.SingleQubitCliffordGate.X_nsqrt)
@pytest.mark.parametrize(
"clifford_gate",
(
cirq.SingleQubitCliffordGate.I,
cirq.SingleQubitCliffordGate.H,
cirq.SingleQubitCliffordGate.X,
cirq.SingleQubitCliffordGate.Y,
cirq.SingleQubitCliffordGate.Z,
cirq.SingleQubitCliffordGate.X_sqrt,
cirq.SingleQubitCliffordGate.Y_sqrt,
cirq.SingleQubitCliffordGate.Z_sqrt,
cirq.SingleQubitCliffordGate.X_nsqrt,
cirq.SingleQubitCliffordGate.Y_nsqrt,
cirq.SingleQubitCliffordGate.Z_nsqrt,
),
)
def test_from_unitary(clifford_gate):
u = cirq.unitary(clifford_gate)
result_gate = cirq.SingleQubitCliffordGate.from_unitary(u)
assert result_gate == clifford_gate


def test_from_unitary_with_phase_shift():
Expand Down Expand Up @@ -612,6 +620,15 @@ def test_common_clifford_gate(clifford_gate, standard_gate):
cirq.testing.assert_allclose_up_to_global_phase(u_c, u_s, atol=1e-8)


@pytest.mark.parametrize('clifford_gate_name', ("I", "X", "Y", "Z", "H", "S", "CNOT", "CZ", "SWAP"))
def test_common_clifford_gate_caching(clifford_gate_name):
cache_name = f"_{clifford_gate_name}"
delattr(cirq.CliffordGate, cache_name)
assert not hasattr(cirq.CliffordGate, cache_name)
_ = getattr(cirq.CliffordGate, clifford_gate_name)
assert hasattr(cirq.CliffordGate, cache_name)


def test_multi_qubit_clifford_pow():
assert cirq.CliffordGate.X ** -1 == cirq.CliffordGate.X
assert cirq.CliffordGate.H ** -1 == cirq.CliffordGate.H
Expand Down

0 comments on commit 23976e2

Please sign in to comment.