Skip to content

Commit 78ab6c1

Browse files
TarunSinghaniatanujkhattarpavoljuhas
authored
Update and correct pow function for PauliSum (quantumlib#6019)
* Update and correct pow function for PauliSum Updated pow function for PauliSum to use binary exponentiation * Format and remainder initialisation correction +Corrected formatting +Corrected initialisation of remainder to identity * Format and remainder initialisation correction +Corrected formatting +Corrected initialisation of remainder to identity * using local variable instead of self and reformatting * reverting to regular linear multiplication and adding tests * Adding test for range (1,9) * trailing blank * remove trailing blank test * Getting rid of extraneous variables * Fix up final formatting issue --------- Co-authored-by: Tanuj Khattar <[email protected]> Co-authored-by: Pavol Juhas <[email protected]>
1 parent 8cda4d3 commit 78ab6c1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

cirq/ops/linear_combinations.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,10 @@ def __pow__(self, exponent: int):
839839
if exponent == 0:
840840
return PauliSum(value.LinearDict({frozenset(): 1 + 0j}))
841841
if exponent > 0:
842-
base = self.copy()
842+
result = self.copy()
843843
for _ in range(exponent - 1):
844-
base *= base
845-
return base
844+
result *= self
845+
return result
846846
return NotImplemented
847847

848848
def __truediv__(self, a: value.Scalar):

cirq/ops/linear_combinations_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,22 @@ def test_pauli_sum_pow():
11561156
for psum in [psum1, psum2, psum3, psum4]:
11571157
assert cirq.approx_eq(psum**0, identity)
11581158

1159+
# tests for exponents greater than two for both even and odd
1160+
psum5 = cirq.Z(q0) * cirq.Z(q1) + cirq.Z(q2) + cirq.Z(q3)
1161+
correctresult = psum5.copy()
1162+
for e in range(1, 9):
1163+
assert correctresult == psum5**e
1164+
correctresult *= psum5
1165+
1166+
psum6 = cirq.X(q0) * cirq.Y(q1) + cirq.Z(q2) + cirq.X(q3)
1167+
assert psum6 * psum6 * psum6 * psum6 * psum6 * psum6 * psum6 * psum6 == psum6**8
1168+
1169+
# test to ensure pow doesn't make any change to the original value
1170+
psum7 = cirq.X(q0) * cirq.Y(q1) + cirq.Z(q2)
1171+
psum7copy = psum7.copy()
1172+
assert psum7**5 == psum7 * psum7 * psum7 * psum7 * psum7
1173+
assert psum7copy == psum7
1174+
11591175

11601176
# Using the entries of table 1 of https://arxiv.org/abs/1804.09130 as golden values.
11611177
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)