@@ -483,10 +483,62 @@ def _commutes_(self, other: Any, *, atol: float = 1e-8) -> None | NotImplemented
483
483
484
484
def _mul_with_qubits (self , qubits : tuple [cirq .Qid , ...], other ):
485
485
"""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
486
512
return NotImplemented
487
513
488
514
def _rmul_with_qubits (self , qubits : tuple [cirq .Qid , ...], other ):
489
515
"""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
490
542
return NotImplemented
491
543
492
544
def _json_dict_ (self ) -> dict [str , Any ]:
0 commit comments