Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken caching in CliffordGate and add test #5142

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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