diff --git a/cirq-core/cirq/ops/clifford_gate.py b/cirq-core/cirq/ops/clifford_gate.py index 7be2d4503b1..bd11de2e05c 100644 --- a/cirq-core/cirq/ops/clifford_gate.py +++ b/cirq-core/cirq/ops/clifford_gate.py @@ -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 diff --git a/cirq-core/cirq/ops/clifford_gate_test.py b/cirq-core/cirq/ops/clifford_gate_test.py index db0542fee9c..4797f9b8733 100644 --- a/cirq-core/cirq/ops/clifford_gate_test.py +++ b/cirq-core/cirq/ops/clifford_gate_test.py @@ -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]]), ' @@ -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]])))' + )