Skip to content

Fix typing in numpy function arguments #5657

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

Merged
merged 7 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
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
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/phased_iswap_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""ISWAPPowGate conjugated by tensor product Rz(phi) and Rz(-phi)."""

from typing import AbstractSet, Any, Dict, List, Optional, Sequence, Tuple, Union
from typing import AbstractSet, Any, cast, Dict, List, Optional, Sequence, Tuple, Union

import numpy as np
import sympy
Expand Down Expand Up @@ -173,7 +173,7 @@ def _pauli_expansion_(self) -> value.LinearDict[str]:
return NotImplemented
expansion = protocols.pauli_expansion(self._iswap)
assert set(expansion.keys()).issubset({'II', 'XX', 'YY', 'ZZ'})
assert np.isclose(expansion['XX'], expansion['YY'])
assert np.isclose(cast(np.generic, expansion['XX']), cast(np.generic, expansion['YY']))

v = (expansion['XX'] + expansion['YY']) / 2
phase_angle = np.pi * self.phase_exponent
Expand Down