Skip to content

Commit

Permalink
Fix numpy annotations np.array -> np.ndarray (quantumlib#5227)
Browse files Browse the repository at this point in the history
`np.array` is not a valid type, but rather a factory function for creating arrays. The actual type is `np.ndarray`. This change reduces the number of `check/mypy --next` errors by >60% from 244 to 96 on my machine.
  • Loading branch information
maffoo authored Apr 8, 2022
1 parent f11d470 commit e112624
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cirq/circuits/qasm_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, theta, phi, lmda) -> None:
self.phi = phi % 2

@staticmethod
def from_matrix(mat: np.array) -> 'QasmUGate':
def from_matrix(mat: np.ndarray) -> 'QasmUGate':
pre_phase, rotation, post_phase = linalg.deconstruct_single_qubit_matrix_into_angles(mat)
return QasmUGate(
rotation / np.pi,
Expand Down Expand Up @@ -115,7 +115,7 @@ def _value_equality_values_(self):
return self.kak

@staticmethod
def from_matrix(mat: np.array, atol=1e-8) -> 'QasmTwoQubitGate':
def from_matrix(mat: np.ndarray, atol=1e-8) -> 'QasmTwoQubitGate':
"""Creates a QasmTwoQubitGate from the given matrix.
Args:
Expand Down
2 changes: 1 addition & 1 deletion cirq/experiments/random_quantum_circuit_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class CircuitLibraryCombination:
"""

layer: Optional[Any]
combinations: np.array
combinations: np.ndarray
pairs: List[QidPairT]


Expand Down
2 changes: 1 addition & 1 deletion cirq/ion/ion_decomposition_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def assert_ms_depth_below(operations, threshold):
(2, _random_double_MS_effect()) for _ in range(10)
])
# yapf: enable
def test_two_to_ops(max_ms_depth: int, effect: np.array):
def test_two_to_ops(max_ms_depth: int, effect: np.ndarray):
q0 = cirq.NamedQubit('q0')
q1 = cirq.NamedQubit('q1')

Expand Down
14 changes: 7 additions & 7 deletions cirq/qis/clifford_tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,33 +240,33 @@ def __init__(self, num_qubits, initial_state: int = 0):
self._zs[self.n + i, i] = True

@property
def xs(self) -> np.array:
def xs(self) -> np.ndarray:
return self._xs[:-1, :]

@xs.setter
def xs(self, new_xs: np.array) -> None:
def xs(self, new_xs: np.ndarray) -> None:
assert np.shape(new_xs) == (2 * self.n, self.n)
self._xs[:-1, :] = np.array(new_xs).astype(bool)

@property
def zs(self) -> np.array:
def zs(self) -> np.ndarray:
return self._zs[:-1, :]

@zs.setter
def zs(self, new_zs: np.array) -> None:
def zs(self, new_zs: np.ndarray) -> None:
assert np.shape(new_zs) == (2 * self.n, self.n)
self._zs[:-1, :] = np.array(new_zs).astype(bool)

@property
def rs(self) -> np.array:
def rs(self) -> np.ndarray:
return self._rs[:-1]

@rs.setter
def rs(self, new_rs: np.array) -> None:
def rs(self, new_rs: np.ndarray) -> None:
assert np.shape(new_rs) == (2 * self.n,)
self._rs[:-1] = np.array(new_rs).astype(bool)

def matrix(self) -> np.array:
def matrix(self) -> np.ndarray:
"""Returns the 2n * 2n matrix representation of the Clifford tableau."""
return np.concatenate([self.xs, self.zs], axis=1)

Expand Down
2 changes: 1 addition & 1 deletion cirq/sim/density_matrix_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _probs(


def _validate_density_matrix_qid_shape(
density_matrix: np.array, qid_shape: Tuple[int, ...]
density_matrix: np.ndarray, qid_shape: Tuple[int, ...]
) -> Tuple[int, ...]:
"""Validates that a tensor's shape is a valid shape for qids and returns the
qid shape.
Expand Down

0 comments on commit e112624

Please sign in to comment.