Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing in numpy function arguments #5657

Merged
merged 7 commits into from
Jul 6, 2022
Merged
Prev Previous commit
Next Next commit
Turn off type check for TParamValComplex used in numpy expressions
Assume it is not sympy.Expr and hope for the best.
  • Loading branch information
pavoljuhas committed Jun 30, 2022
commit 7cce457f41f55ec1da2165ecc1bcf70c8009af1a
5 changes: 3 additions & 2 deletions cirq-core/cirq/ops/global_phase_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""A no-qubit global phase operation."""
from typing import AbstractSet, Any, Dict, Sequence, Tuple, TYPE_CHECKING, Union

from typing import AbstractSet, Any, cast, Dict, Sequence, Tuple, TYPE_CHECKING, Union

import numpy as np
import sympy
Expand Down Expand Up @@ -89,7 +90,7 @@ def _apply_unitary_(
) -> Union[np.ndarray, NotImplementedType]:
if not self._has_unitary_():
return NotImplemented
args.target_tensor *= self.coefficient
args.target_tensor *= cast(np.generic, self.coefficient)
return args.target_tensor

def _has_stabilizer_effect_(self) -> bool:
Expand Down
6 changes: 4 additions & 2 deletions cirq-core/cirq/ops/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,9 @@ def matrix(self, qubits: Optional[Iterable[TKey]] = None) -> np.ndarray:
"""
qubits = self.qubits if qubits is None else qubits
factors = [self.get(q, default=identity.I) for q in qubits]
return linalg.kron(self.coefficient, *[protocols.unitary(f) for f in factors])
pavoljuhas marked this conversation as resolved.
Show resolved Hide resolved
return linalg.kron(
cast(complex, self.coefficient), *[protocols.unitary(f) for f in factors]
)

def _has_unitary_(self) -> bool:
if self._is_parameterized_():
Expand All @@ -517,7 +519,7 @@ def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs'):
if not self._has_unitary_():
return None
if self.coefficient != 1:
args.target_tensor *= self.coefficient
args.target_tensor *= cast(complex, self.coefficient)
return protocols.apply_unitaries([self[q].on(q) for q in self.qubits], self.qubits, args)

def expectation_from_state_vector(
Expand Down