Skip to content

Fix the format of CliffordGate repr. #7046

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

Merged
merged 7 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def __pow__(self, exponent: float) -> 'CliffordGate':
return CliffordGate.from_clifford_tableau(base_tableau)

def __repr__(self) -> str:
return f"Clifford Gate with Tableau:\n {self.clifford_tableau._str_full_()}"
return f"Clifford Gate with Tableau:\n{self.clifford_tableau._str_full_()}"

def _commutes_(
self, other: Any, *, atol: float = 1e-8
Expand Down
48 changes: 47 additions & 1 deletion cirq-core/cirq/ops/clifford_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,39 @@ def test_all_single_qubit_clifford_unitaries():
assert cirq.equal_up_to_global_phase(cs[23], (i - 1j * (-x - y - z)) / 2)


def test_clifford_gate_repr():
q0, q1, q2 = cirq.LineQubit.range(3)
gates: list[cirq.ops.CliffordGate] = [
# Common gates
cirq.ops.CliffordGate.CNOT,
cirq.ops.CliffordGate.CZ,
cirq.ops.CliffordGate.SWAP,
# Other gates
cirq.ops.CliffordGate.from_op_list(
[cirq.ops.PhasedXZGate(axis_phase_exponent=0.25, x_exponent=-1, z_exponent=0).on(q0)],
[q0],
),
cirq.ops.CliffordGate.from_op_list([cirq.ops.X(q0), cirq.CZ(q1, q2)], [q0, q1, q2]),
]
for gate in gates:
t = gate.clifford_tableau
assert repr(gate) == f"Clifford Gate with Tableau:\n{t._str_full_()}"

# Check repr without calling _str_full_() for one gate of CliffordGate type.
# This verifies that repr output is as expected with correct stabilizers and destabilizers.
assert (
repr(cirq.ops.CliffordGate.CNOT)
== """Clifford Gate with Tableau:
stable | destable
-------+----------
+ Z0 | + X0X1
+ Z0Z1 | + X1
"""
)


def test_single_qubit_clifford_gate_repr():
# Common gates
assert repr(cirq.ops.SingleQubitCliffordGate.X) == (
'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
'rs=np.array([False, True]), xs=np.array([[True], [False]]), '
Expand All @@ -950,8 +982,22 @@ def test_single_qubit_clifford_gate_repr():
'zs=np.array([[False], [True]])))'
)

assert str(cirq.ops.SingleQubitCliffordGate.X) == (
assert repr(cirq.ops.SingleQubitCliffordGate.X) == (
'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
'rs=np.array([False, True]), xs=np.array([[True], [False]]), '
'zs=np.array([[False], [True]])))'
)

# Other gates
qa = cirq.NamedQubit('a')
gate = cirq.ops.SingleQubitCliffordGate.from_clifford_tableau(
cirq.ops.CliffordGate.from_op_list(
[cirq.ops.PhasedXZGate(axis_phase_exponent=0.25, x_exponent=-1, z_exponent=0).on(qa)],
[qa],
).clifford_tableau
)
assert repr(gate) == (
'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
'rs=np.array([False, True]), xs=np.array([[True], [False]]), '
'zs=np.array([[True], [True]])))'
)