From 7823bdbf51e596babfd242726b5aba04d8abc93f Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Tue, 20 Aug 2024 17:49:21 +0000 Subject: [PATCH] Numpy 2 anticipatory fixes (#1312) Numpy 2 support --- qualtran/bloqs/basic_gates/su2_rotation.py | 4 ++-- .../hamiltonian_simulation/hamiltonian_simulation_by_gqsp.py | 2 +- .../hamiltonian_simulation_by_gqsp_test.py | 2 +- .../sparse_state_preparation_via_rotations.py | 2 +- .../sparse_state_preparation_via_rotations_test.py | 2 +- qualtran/linalg/polynomial/jacobi_anger_approximations.py | 4 ++-- qualtran/linalg/polynomial/qsp_testing.py | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/qualtran/bloqs/basic_gates/su2_rotation.py b/qualtran/bloqs/basic_gates/su2_rotation.py index 483494305..440f84721 100644 --- a/qualtran/bloqs/basic_gates/su2_rotation.py +++ b/qualtran/bloqs/basic_gates/su2_rotation.py @@ -69,7 +69,7 @@ def signature(self) -> Signature: return Signature.build(q=1) @cached_property - def rotation_matrix(self) -> NDArray[np.complex_]: + def rotation_matrix(self) -> NDArray[np.complex128]: if isinstance(self.lambd, sympy.Expr): raise ValueError(f'Symbolic lambda not allowed: {self.lambd}') if isinstance(self.phi, sympy.Expr): @@ -89,7 +89,7 @@ def rotation_matrix(self) -> NDArray[np.complex_]: ) @staticmethod - def from_matrix(mat: NDArray[np.complex_]) -> 'SU2RotationGate': + def from_matrix(mat: NDArray[np.complex128]) -> 'SU2RotationGate': theta = np.arctan2(np.abs(mat[1, 0]), np.abs(mat[0, 0])) if np.isclose(np.cos(theta), 0): alpha = 0 diff --git a/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp.py b/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp.py index 44f677ce8..42ea512ce 100644 --- a/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp.py +++ b/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp.py @@ -111,7 +111,7 @@ def degree(self) -> SymbolicInt: return degree_jacobi_anger_approximation(self.t * self.alpha, precision=self.precision) @cached_property - def approx_cos(self) -> Union[NDArray[np.complex_], Shaped]: + def approx_cos(self) -> Union[NDArray[np.complex128], Shaped]: r"""polynomial approximation for $$e^{i\theta} \mapsto e^{it\cos(\theta)}$$""" if self.is_symbolic(): return Shaped((2 * self.degree + 1,)) diff --git a/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp_test.py b/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp_test.py index 6752ed61c..a883c4fc1 100644 --- a/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp_test.py +++ b/qualtran/bloqs/hamiltonian_simulation/hamiltonian_simulation_by_gqsp_test.py @@ -70,7 +70,7 @@ def test_generalized_qsp_with_exp_cos_approx_on_random_unitaries( def verify_hamiltonian_simulation_by_gqsp( - W: QubitizationWalkOperator, H: NDArray[np.complex_], *, t: float, precision: float + W: QubitizationWalkOperator, H: NDArray[np.complex128], *, t: float, precision: float ): N = H.shape[0] diff --git a/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations.py b/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations.py index 8e68ead00..cd0890a1c 100644 --- a/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations.py +++ b/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations.py @@ -97,7 +97,7 @@ def from_coefficient_map( @classmethod def from_sparse_array( cls, - coeffs: Union[Sequence[complex], NDArray[np.complex_], 'scipy.sparse.sparray'], + coeffs: Union[Sequence[complex], NDArray[np.complex128], 'scipy.sparse.sparray'], phase_bitsize: SymbolicInt, ): """Factory to construct sparse state given the coefficients. diff --git a/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations_test.py b/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations_test.py index 250872dca..121d3c781 100644 --- a/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations_test.py +++ b/qualtran/bloqs/state_preparation/sparse_state_preparation_via_rotations_test.py @@ -28,7 +28,7 @@ def test_examples(bloq_autotester): bloq_autotester(_sparse_state_prep_via_rotations) -def get_prepared_state_vector(bloq: SparseStatePreparationViaRotations) -> NDArray[np.complex_]: +def get_prepared_state_vector(bloq: SparseStatePreparationViaRotations) -> NDArray[np.complex128]: bb = BloqBuilder() state = bb.add(IntState(0, bloq.target_bitsize)) phase_gradient = bb.add(PhaseGradientState(bloq.phase_bitsize)) diff --git a/qualtran/linalg/polynomial/jacobi_anger_approximations.py b/qualtran/linalg/polynomial/jacobi_anger_approximations.py index 6c22b2a85..5dd339d1d 100644 --- a/qualtran/linalg/polynomial/jacobi_anger_approximations.py +++ b/qualtran/linalg/polynomial/jacobi_anger_approximations.py @@ -63,7 +63,7 @@ def term_too_small(n: int) -> bool: return d -def approx_exp_cos_by_jacobi_anger(t: float, *, degree: int) -> NDArray[np.complex_]: +def approx_exp_cos_by_jacobi_anger(t: float, *, degree: int) -> NDArray[np.complex128]: r"""Laurent Polynomial approximation for $e^{i\theta} \mapsto e^{it\cos\theta}$. The approximation is given by @@ -82,7 +82,7 @@ def approx_exp_cos_by_jacobi_anger(t: float, *, degree: int) -> NDArray[np.compl return 1j**coeff_indices * scipy.special.jv(coeff_indices, t) -def approx_exp_sin_by_jacobi_anger(t: float, *, degree: int) -> NDArray[np.complex_]: +def approx_exp_sin_by_jacobi_anger(t: float, *, degree: int) -> NDArray[np.complex128]: r"""Laurent Polynomial approximation for $e^{i\theta} \mapsto e^{it\cos\theta}$. The approximation is given by diff --git a/qualtran/linalg/polynomial/qsp_testing.py b/qualtran/linalg/polynomial/qsp_testing.py index 0e2f1c360..496822c4e 100644 --- a/qualtran/linalg/polynomial/qsp_testing.py +++ b/qualtran/linalg/polynomial/qsp_testing.py @@ -86,7 +86,7 @@ def _polynomial_max_abs_value_on_unit_circle( def scale_down_to_qsp_polynomial( P: Sequence[complex], *, n_points: int = 2**17 -) -> NDArray[np.complex_]: +) -> NDArray[np.complex128]: r"""Scale down the polynomial to be a valid QSP Polynomial $P$ is a QSP polynomial if $|P(e^{i\theta})| \le 1$ for every $\theta \in [0, 2\pi]$.