Skip to content

Commit e17c7d9

Browse files
committed
Update PauliString.__mul__ and __rmul__ to handle GateOperations
Enhanced PauliString multiplication methods to handle GateOperations that can be interpreted as Pauli strings (e.g. X**2) by using the existing _try_interpret_as_pauli_string function. Addressed part of #7588, for the PauliString * GateOperation cases, e.g. `x*x*x**2`
1 parent fe0dc21 commit e17c7d9

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

cirq-core/cirq/ops/pauli_string.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ def __mul__(self, other):
271271
known = True
272272
elif isinstance(other, (PauliString, numbers.Number)):
273273
known = True
274+
elif (as_pauli_string := _try_interpret_as_pauli_string(other)) is not None:
275+
return self * as_pauli_string
274276
if known:
275277
return PauliString(
276278
cast(PAULI_STRING_LIKE, other),
@@ -297,6 +299,8 @@ def __rmul__(self, other) -> PauliString:
297299

298300
if isinstance(other, raw_types.Operation) and isinstance(other.gate, identity.IdentityGate):
299301
return self # pragma: no cover
302+
elif (as_pauli_string := _try_interpret_as_pauli_string(other)) is not None:
303+
return as_pauli_string * self
300304

301305
# Note: PauliString case handled by __mul__.
302306
return NotImplemented

0 commit comments

Comments
 (0)