Skip to content

Commit

Permalink
Improve __str__ and __repr__ for SingleQubitCliffordGate (issue #6326)
Browse files Browse the repository at this point in the history
Change __repr__ to be simpler following the recommendation in the issue description.
  • Loading branch information
codrut3 committed Jan 4, 2025
1 parent 83a8e0e commit 9fce31c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,12 @@ def equivalent_gate_before(self, after: 'SingleQubitCliffordGate') -> 'SingleQub
return self.merged_with(after).merged_with(self**-1)

def __repr__(self) -> str:
return f'cirq.CliffordGate.from_clifford_tableau({self.clifford_tableau!r})'
return (
f'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, '
f'rs=np.array({self._clifford_tableau.rs.tolist()!r}), '
f'xs=np.array({self._clifford_tableau.xs.tolist()!r}), '
f'zs=np.array({self._clifford_tableau.zs.tolist()!r})))'
)

def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
Expand Down
28 changes: 28 additions & 0 deletions cirq-core/cirq/ops/clifford_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,31 @@ def test_all_single_qubit_clifford_unitaries():
assert cirq.equal_up_to_global_phase(cs[21], (i - 1j * (-x + y - z)) / 2)
assert cirq.equal_up_to_global_phase(cs[22], (i - 1j * (-x - y + z)) / 2)
assert cirq.equal_up_to_global_phase(cs[23], (i - 1j * (-x - y - z)) / 2)


def test_single_qubit_clifford_gate_repr():
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]])))'
)
assert (
repr(cirq.ops.SingleQubitCliffordGate.Y)
== 'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, rs=np.array([True, True]), xs=np.array([[True], [False]]), zs=np.array([[False], [True]])))'
)
assert (
repr(cirq.ops.SingleQubitCliffordGate.Z)
== 'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, rs=np.array([True, False]), xs=np.array([[True], [False]]), zs=np.array([[False], [True]])))'
)
assert (
repr(cirq.ops.SingleQubitCliffordGate.I)
== 'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, rs=np.array([False, False]), xs=np.array([[True], [False]]), zs=np.array([[False], [True]])))'
)
assert (
repr(cirq.ops.SingleQubitCliffordGate.X_sqrt)
== 'cirq.ops.SingleQubitCliffordGate(_clifford_tableau=cirq.CliffordTableau(1, rs=np.array([False, True]), xs=np.array([[True], [True]]), zs=np.array([[False], [True]])))'
)

assert (
str(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]])))'
)

0 comments on commit 9fce31c

Please sign in to comment.