Skip to content

Commit fc2abac

Browse files
committed
Update Gate._mul_with_qubits and _rmul_with_qubits
... to handle operations with single-item pauli expansions using PauliString. Addressed the GateOperation * GateOperation cases in #7588.
1 parent e17c7d9 commit fc2abac

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

cirq-core/cirq/ops/raw_types.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,62 @@ def _commutes_(self, other: Any, *, atol: float = 1e-8) -> None | NotImplemented
483483

484484
def _mul_with_qubits(self, qubits: tuple[cirq.Qid, ...], other):
485485
"""cirq.GateOperation.__mul__ delegates to this method."""
486+
if isinstance(other, Operation):
487+
try:
488+
# Try using pauli expansion if both operations have single-item expansions
489+
pauli_expansion_self = protocols.pauli_expansion(self.on(*qubits))
490+
pauli_expansion_other = protocols.pauli_expansion(other)
491+
492+
if (
493+
pauli_expansion_self is not None
494+
and len(pauli_expansion_self) == 1
495+
and pauli_expansion_other is not None
496+
and len(pauli_expansion_other) == 1
497+
):
498+
499+
gate_self, coef_self = next(iter(pauli_expansion_self.items()))
500+
gate_other, coef_other = next(iter(pauli_expansion_other.items()))
501+
502+
from cirq.ops.pauli_string import PauliString
503+
504+
return (
505+
coef_self
506+
* PauliString({q: gate_self for q in qubits})
507+
* coef_other
508+
* PauliString({q: gate_other for q in qubits})
509+
)
510+
except TypeError:
511+
return NotImplemented
486512
return NotImplemented
487513

488514
def _rmul_with_qubits(self, qubits: tuple[cirq.Qid, ...], other):
489515
"""cirq.GateOperation.__rmul__ delegates to this method."""
516+
if isinstance(other, Operation):
517+
try:
518+
# Try using pauli expansion if both operations have single-item expansions
519+
pauli_expansion_self = protocols.pauli_expansion(self.on(*qubits))
520+
pauli_expansion_other = protocols.pauli_expansion(other)
521+
522+
if (
523+
pauli_expansion_self is not None
524+
and len(pauli_expansion_self) == 1
525+
and pauli_expansion_other is not None
526+
and len(pauli_expansion_other) == 1
527+
):
528+
529+
gate_self, coef_self = next(iter(pauli_expansion_self.items()))
530+
gate_other, coef_other = next(iter(pauli_expansion_other.items()))
531+
532+
from cirq.ops.pauli_string import PauliString
533+
534+
return (
535+
coef_other
536+
* PauliString({q: gate_other for q in qubits})
537+
* coef_self
538+
* PauliString({q: gate_self for q in qubits})
539+
)
540+
except TypeError:
541+
return NotImplemented
490542
return NotImplemented
491543

492544
def _json_dict_(self) -> dict[str, Any]:

0 commit comments

Comments
 (0)