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 and tonybruguier committed Apr 19, 2022
1 parent 4f10a79 commit a9e53a4
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cirq-core/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
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-core/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-core/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-core/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
2 changes: 1 addition & 1 deletion cirq-google/cirq_google/calibration/engine_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def create_from_characterizations_sqrt_iswap(
ideal_when_missing_parameter=ideal_when_missing_parameter,
)

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

Expand Down
2 changes: 1 addition & 1 deletion cirq-google/cirq_google/calibration/phased_fsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ def with_zeta_chi_gamma_compensated(
(cirq.rz(0.5 * gamma - beta).on(a), cirq.rz(0.5 * gamma + beta).on(b)),
)

def _unitary_(self) -> np.array:
def _unitary_(self) -> np.ndarray:
"""Implements Cirq's `unitary` protocol for this object."""
p = np.exp(-np.pi * 1j * self.phase_exponent)
return (
Expand Down
4 changes: 2 additions & 2 deletions examples/stabilizer_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def _gaussian_elimination(


def _transfer_to_standard_form(
M: np.array, n: int, k: int
) -> Tuple[np.array, np.array, np.array, int]:
M: np.ndarray, n: int, k: int
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, int]:
"""Puts the stabilizer matrix in its standardized form, as in section 4.1 of the thesis.
Args:
Expand Down

0 comments on commit a9e53a4

Please sign in to comment.