Skip to content

Commit dd3df78

Browse files
babacrypavoljuhas
andauthored
Fix the format of CliffordGate repr. (#7046)
* fix the format of CliffordGate repr. * Add unit tests for CliffordGate.__repr__() * Check the full string of repr without calling _str_full_. * remove test cases * Strip trailing blanks in the expected repr(gate) string --------- Co-authored-by: Pavol Juhas <juhas@google.com>
1 parent bc9fc97 commit dd3df78

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

cirq-core/cirq/ops/clifford_gate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def __pow__(self, exponent: float) -> 'CliffordGate':
433433
return CliffordGate.from_clifford_tableau(base_tableau)
434434

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

438438
def _commutes_(
439439
self, other: Any, *, atol: float = 1e-8

cirq-core/cirq/ops/clifford_gate_test.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,31 @@ def test_all_single_qubit_clifford_unitaries():
923923
assert cirq.equal_up_to_global_phase(cs[23], (i - 1j * (-x - y - z)) / 2)
924924

925925

926+
def test_clifford_gate_repr():
927+
q0, q1, q2 = cirq.LineQubit.range(3)
928+
assert (
929+
repr(cirq.ops.CliffordGate.from_op_list([cirq.ops.X(q0), cirq.CZ(q1, q2)], [q0, q1, q2]))
930+
== """Clifford Gate with Tableau:
931+
stable | destable
932+
---------+----------
933+
- Z0 | + X0
934+
+ Z1 | + X1Z2
935+
+ Z2 | + Z1X2
936+
"""
937+
)
938+
assert (
939+
repr(cirq.ops.CliffordGate.CNOT)
940+
== """Clifford Gate with Tableau:
941+
stable | destable
942+
-------+----------
943+
+ Z0 | + X0X1
944+
+ Z0Z1 | + X1
945+
"""
946+
)
947+
948+
926949
def test_single_qubit_clifford_gate_repr():
950+
# Common gates
927951
assert repr(cirq.ops.SingleQubitCliffordGate.X) == (
928952
'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
929953
'rs=np.array([False, True]), xs=np.array([[True], [False]]), '
@@ -950,8 +974,22 @@ def test_single_qubit_clifford_gate_repr():
950974
'zs=np.array([[False], [True]])))'
951975
)
952976

953-
assert str(cirq.ops.SingleQubitCliffordGate.X) == (
977+
assert repr(cirq.ops.SingleQubitCliffordGate.X) == (
954978
'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
955979
'rs=np.array([False, True]), xs=np.array([[True], [False]]), '
956980
'zs=np.array([[False], [True]])))'
957981
)
982+
983+
# Other gates
984+
qa = cirq.NamedQubit('a')
985+
gate = cirq.ops.SingleQubitCliffordGate.from_clifford_tableau(
986+
cirq.ops.CliffordGate.from_op_list(
987+
[cirq.ops.PhasedXZGate(axis_phase_exponent=0.25, x_exponent=-1, z_exponent=0).on(qa)],
988+
[qa],
989+
).clifford_tableau
990+
)
991+
assert repr(gate) == (
992+
'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
993+
'rs=np.array([False, True]), xs=np.array([[True], [False]]), '
994+
'zs=np.array([[True], [True]])))'
995+
)

0 commit comments

Comments
 (0)