Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve __pow__ for SingleQubitCliffordGate (issue #6327) #6919

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,16 +743,21 @@ def _to_phased_xz_gate(self) -> phased_x_z_gate.PhasedXZGate:
return phased_x_z_gate.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)

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

return NotImplemented

def _act_on_(
self,
Expand Down
2 changes: 2 additions & 0 deletions cirq-core/cirq/ops/clifford_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def test_pow():
assert cirq.SingleQubitCliffordGate.Y_nsqrt == cirq.SingleQubitCliffordGate.Y**-0.5
assert cirq.SingleQubitCliffordGate.Z_nsqrt == cirq.SingleQubitCliffordGate.Z**-0.5
assert cirq.SingleQubitCliffordGate.X_sqrt**-1 == cirq.SingleQubitCliffordGate.X_nsqrt
assert cirq.SingleQubitCliffordGate.X_sqrt**3 == cirq.SingleQubitCliffordGate.X**1.5
assert cirq.SingleQubitCliffordGate.Z**2.0 == cirq.SingleQubitCliffordGate.I
assert cirq.inverse(cirq.SingleQubitCliffordGate.X_nsqrt) == (
cirq.SingleQubitCliffordGate.X_sqrt
)
Expand Down