2828 Optional ,
2929 overload ,
3030 Sequence ,
31- SupportsComplex ,
3231 Tuple ,
3332 TYPE_CHECKING ,
3433 TypeVar ,
@@ -271,9 +270,7 @@ def __mul__(self, other: 'cirq.Operation') -> 'cirq.PauliString[Union[TKey, cirq
271270 pass
272271
273272 @overload
274- def __mul__ (
275- self , other : Union [complex , int , float , numbers .Number ]
276- ) -> 'cirq.PauliString[TKey]' :
273+ def __mul__ (self , other : complex ) -> 'cirq.PauliString[TKey]' :
277274 pass
278275
279276 def __mul__ (self , other ):
@@ -308,10 +305,9 @@ def gate(self) -> 'cirq.DensePauliString':
308305 )
309306
310307 def __rmul__ (self , other ) -> 'PauliString' :
311- if isinstance (other , numbers .Number ):
308+ if isinstance (other , numbers .Complex ):
312309 return PauliString (
313- qubit_pauli_map = self ._qubit_pauli_map ,
314- coefficient = self ._coefficient * complex (cast (SupportsComplex , other )),
310+ qubit_pauli_map = self ._qubit_pauli_map , coefficient = self ._coefficient * other
315311 )
316312
317313 if isinstance (other , raw_types .Operation ) and isinstance (other .gate , identity .IdentityGate ):
@@ -321,10 +317,9 @@ def __rmul__(self, other) -> 'PauliString':
321317 return NotImplemented
322318
323319 def __truediv__ (self , other ):
324- if isinstance (other , numbers .Number ):
320+ if isinstance (other , numbers .Complex ):
325321 return PauliString (
326- qubit_pauli_map = self ._qubit_pauli_map ,
327- coefficient = self ._coefficient / complex (cast (SupportsComplex , other )),
322+ qubit_pauli_map = self ._qubit_pauli_map , coefficient = self ._coefficient / other
328323 )
329324 return NotImplemented
330325
@@ -518,7 +513,7 @@ def _unitary_(self) -> Optional[np.ndarray]:
518513 def _apply_unitary_ (self , args : 'protocols.ApplyUnitaryArgs' ):
519514 if not self ._has_unitary_ ():
520515 return None
521- assert isinstance (self .coefficient , complex )
516+ assert isinstance (self .coefficient , numbers . Complex )
522517 if self .coefficient != 1 :
523518 args .target_tensor *= self .coefficient
524519 return protocols .apply_unitaries ([self [q ].on (q ) for q in self .qubits ], self .qubits , args )
@@ -792,9 +787,11 @@ def __pos__(self) -> 'PauliString':
792787 return self
793788
794789 def __array_ufunc__ (self , ufunc , method , * inputs , ** kwargs ):
795- """Override behavior of numpy's exp method ."""
790+ """Override numpy behavior ."""
796791 if ufunc == np .exp and len (inputs ) == 1 and inputs [0 ] is self :
797792 return math .e ** self
793+ if ufunc == np .multiply and len (inputs ) == 2 and inputs [1 ] is self :
794+ return self * inputs [0 ]
798795 return NotImplemented
799796
800797 def __pow__ (self , power ):
@@ -1174,14 +1171,14 @@ def _as_pauli_string(self) -> PauliString:
11741171 def __mul__ (self , other ):
11751172 if isinstance (other , SingleQubitPauliStringGateOperation ):
11761173 return self ._as_pauli_string () * other ._as_pauli_string ()
1177- if isinstance (other , (PauliString , complex , float , int )):
1174+ if isinstance (other , (PauliString , numbers . Complex )):
11781175 return self ._as_pauli_string () * other
11791176 if (as_pauli_string := _try_interpret_as_pauli_string (other )) is not None :
11801177 return self * as_pauli_string
11811178 return NotImplemented
11821179
11831180 def __rmul__ (self , other ):
1184- if isinstance (other , (PauliString , complex , float , int )):
1181+ if isinstance (other , (PauliString , numbers . Complex )):
11851182 return other * self ._as_pauli_string ()
11861183 if (as_pauli_string := _try_interpret_as_pauli_string (other )) is not None :
11871184 return as_pauli_string * self
@@ -1430,8 +1427,8 @@ def _imul_helper(self, other: 'cirq.PAULI_STRING_LIKE', sign: int):
14301427 pauli_int = _pauli_like_to_pauli_int (qubit , pauli_gate_like )
14311428 phase_log_i += self ._imul_atom_helper (cast (TKey , qubit ), pauli_int , sign )
14321429 self .coefficient *= 1j ** (phase_log_i & 3 )
1433- elif isinstance (other , numbers .Number ):
1434- self .coefficient *= complex ( cast ( SupportsComplex , other ))
1430+ elif isinstance (other , numbers .Complex ):
1431+ self .coefficient *= other
14351432 elif isinstance (other , raw_types .Operation ) and isinstance (
14361433 other .gate , identity .IdentityGate
14371434 ):
0 commit comments