Skip to content

Commit

Permalink
Fix the format of CliffordGate repr. (quantumlib#7046)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
babacry and pavoljuhas authored Feb 12, 2025
1 parent bc9fc97 commit dd3df78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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
40 changes: 39 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,31 @@ 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)
assert (
repr(cirq.ops.CliffordGate.from_op_list([cirq.ops.X(q0), cirq.CZ(q1, q2)], [q0, q1, q2]))
== """Clifford Gate with Tableau:
stable | destable
---------+----------
- Z0 | + X0
+ Z1 | + X1Z2
+ Z2 | + Z1X2
"""
)
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 +974,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]])))'
)

0 comments on commit dd3df78

Please sign in to comment.