Skip to content

Commit f1ea0bd

Browse files
authored
Fix numpy annotations np.array -> np.ndarray (#5227)
`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.
1 parent 0482b47 commit f1ea0bd

File tree

8 files changed

+16
-16
lines changed

8 files changed

+16
-16
lines changed

cirq-core/cirq/circuits/qasm_output.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, theta, phi, lmda) -> None:
4343
self.phi = phi % 2
4444

4545
@staticmethod
46-
def from_matrix(mat: np.array) -> 'QasmUGate':
46+
def from_matrix(mat: np.ndarray) -> 'QasmUGate':
4747
pre_phase, rotation, post_phase = linalg.deconstruct_single_qubit_matrix_into_angles(mat)
4848
return QasmUGate(
4949
rotation / np.pi,
@@ -115,7 +115,7 @@ def _value_equality_values_(self):
115115
return self.kak
116116

117117
@staticmethod
118-
def from_matrix(mat: np.array, atol=1e-8) -> 'QasmTwoQubitGate':
118+
def from_matrix(mat: np.ndarray, atol=1e-8) -> 'QasmTwoQubitGate':
119119
"""Creates a QasmTwoQubitGate from the given matrix.
120120
121121
Args:

cirq-core/cirq/experiments/random_quantum_circuit_generation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class CircuitLibraryCombination:
305305
"""
306306

307307
layer: Optional[Any]
308-
combinations: np.array
308+
combinations: np.ndarray
309309
pairs: List[QidPairT]
310310

311311

cirq-core/cirq/ion/ion_decomposition_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def assert_ms_depth_below(operations, threshold):
117117
(2, _random_double_MS_effect()) for _ in range(10)
118118
])
119119
# yapf: enable
120-
def test_two_to_ops(max_ms_depth: int, effect: np.array):
120+
def test_two_to_ops(max_ms_depth: int, effect: np.ndarray):
121121
q0 = cirq.NamedQubit('q0')
122122
q1 = cirq.NamedQubit('q1')
123123

cirq-core/cirq/qis/clifford_tableau.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -240,33 +240,33 @@ def __init__(self, num_qubits, initial_state: int = 0):
240240
self._zs[self.n + i, i] = True
241241

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

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

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

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

260260
@property
261-
def rs(self) -> np.array:
261+
def rs(self) -> np.ndarray:
262262
return self._rs[:-1]
263263

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

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

cirq-core/cirq/sim/density_matrix_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _probs(
222222

223223

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

cirq-google/cirq_google/calibration/engine_simulator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def create_from_characterizations_sqrt_iswap(
373373
ideal_when_missing_parameter=ideal_when_missing_parameter,
374374
)
375375

376-
def final_state_vector(self, program: cirq.Circuit) -> np.array:
376+
def final_state_vector(self, program: cirq.Circuit) -> np.ndarray:
377377
result = self.simulate(program)
378378
return result.state_vector()
379379

cirq-google/cirq_google/calibration/phased_fsim.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ def with_zeta_chi_gamma_compensated(
10331033
(cirq.rz(0.5 * gamma - beta).on(a), cirq.rz(0.5 * gamma + beta).on(b)),
10341034
)
10351035

1036-
def _unitary_(self) -> np.array:
1036+
def _unitary_(self) -> np.ndarray:
10371037
"""Implements Cirq's `unitary` protocol for this object."""
10381038
p = np.exp(-np.pi * 1j * self.phase_exponent)
10391039
return (

examples/stabilizer_code.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def _gaussian_elimination(
114114

115115

116116
def _transfer_to_standard_form(
117-
M: np.array, n: int, k: int
118-
) -> Tuple[np.array, np.array, np.array, int]:
117+
M: np.ndarray, n: int, k: int
118+
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, int]:
119119
"""Puts the stabilizer matrix in its standardized form, as in section 4.1 of the thesis.
120120
121121
Args:

0 commit comments

Comments
 (0)