Skip to content

Commit 5ab7869

Browse files
authored
Improve __pow__ for SingleQubitCliffordGate (issue #6327) (#6919)
1 parent be8b04b commit 5ab7869

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

cirq-core/cirq/ops/clifford_gate.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -743,16 +743,21 @@ def _to_phased_xz_gate(self) -> phased_x_z_gate.PhasedXZGate:
743743
return phased_x_z_gate.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)
744744

745745
def __pow__(self, exponent: Union[float, int]) -> 'SingleQubitCliffordGate':
746-
# First to check if we can get the sqrt and negative sqrt Clifford.
747-
if self._get_sqrt_map().get(exponent, None):
748-
pow_gate = self._get_sqrt_map()[exponent].get(self, None)
746+
if int(exponent) == exponent:
747+
# The single qubit Clifford gates are a group of size 24
748+
ret_gate = super().__pow__(int(exponent) % 24)
749+
return SingleQubitCliffordGate.from_clifford_tableau(ret_gate.clifford_tableau)
750+
elif int(2 * exponent) == 2 * exponent:
751+
# If exponent = k/2 for integer k, then we compute the k-th power of the square root
752+
if exponent < 0:
753+
sqrt_exp = -0.5
754+
else:
755+
sqrt_exp = 0.5
756+
pow_gate = self._get_sqrt_map()[sqrt_exp].get(self, None)
749757
if pow_gate:
750-
return pow_gate
751-
# If not, we try the Clifford Tableau based method.
752-
ret_gate = super().__pow__(exponent)
753-
if ret_gate is NotImplemented:
754-
return NotImplemented
755-
return SingleQubitCliffordGate.from_clifford_tableau(ret_gate.clifford_tableau)
758+
return pow_gate ** (abs(2 * exponent))
759+
760+
return NotImplemented
756761

757762
def _act_on_(
758763
self,

cirq-core/cirq/ops/clifford_gate_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ def test_pow():
219219
assert cirq.SingleQubitCliffordGate.Y_nsqrt == cirq.SingleQubitCliffordGate.Y**-0.5
220220
assert cirq.SingleQubitCliffordGate.Z_nsqrt == cirq.SingleQubitCliffordGate.Z**-0.5
221221
assert cirq.SingleQubitCliffordGate.X_sqrt**-1 == cirq.SingleQubitCliffordGate.X_nsqrt
222+
assert cirq.SingleQubitCliffordGate.X_sqrt**3 == cirq.SingleQubitCliffordGate.X**1.5
223+
assert cirq.SingleQubitCliffordGate.Z**2.0 == cirq.SingleQubitCliffordGate.I
222224
assert cirq.inverse(cirq.SingleQubitCliffordGate.X_nsqrt) == (
223225
cirq.SingleQubitCliffordGate.X_sqrt
224226
)

0 commit comments

Comments
 (0)