From 371b2bace1cc8a31dad1650813d7dd515c8392da Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 7 Aug 2024 21:46:41 +0200 Subject: [PATCH] Make numpy 2.0 compatible --- opensquirrel/common.py | 2 +- opensquirrel/ir.py | 6 +++--- opensquirrel/utils/matrix_expander.py | 16 ++++++++-------- test/ir_equality_test_base.py | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/opensquirrel/common.py b/opensquirrel/common.py index b3c22c51..d4eeab77 100644 --- a/opensquirrel/common.py +++ b/opensquirrel/common.py @@ -17,7 +17,7 @@ def normalize_angle(x: float) -> float: return t -def are_matrices_equivalent_up_to_global_phase(matrix_a: NDArray[np.complex_], matrix_b: NDArray[np.complex_]) -> bool: +def are_matrices_equivalent_up_to_global_phase(matrix_a: NDArray[np.complex128], matrix_b: NDArray[np.complex128]) -> bool: order_magnitude = abs(math.floor(math.log10(ATOL))) matrix_a = np.round(matrix_a, order_magnitude) diff --git a/opensquirrel/ir.py b/opensquirrel/ir.py index 4468b7fd..ff8c8223 100644 --- a/opensquirrel/ir.py +++ b/opensquirrel/ir.py @@ -16,7 +16,7 @@ def repr_round( - value: float | Axis | NDArray[np.complex64], decimals: int = REPR_DECIMALS + value: float | Axis | NDArray[np.complex64] | NDArray[np.complex128], decimals: int = REPR_DECIMALS ) -> float | Axis | NDArray[np.complex64]: return np.round(value, decimals) @@ -157,7 +157,7 @@ def _parse_and_validate_axislike(cls, axis: AxisLike) -> NDArray[np.float64]: return axis.value try: - axis = np.asfarray(axis) + axis = np.asarray(axis, dtype=float) except (ValueError, TypeError) as e: raise TypeError("axis requires an ArrayLike") from e axis = axis.flatten() @@ -354,7 +354,7 @@ def is_identity(self) -> bool: class MatrixGate(Gate): def __init__( self, - matrix: NDArray[np.complex_], + matrix: NDArray[np.complex128], operands: list[Qubit], generator: Callable[..., MatrixGate] | None = None, arguments: tuple[Expression, ...] | None = None, diff --git a/opensquirrel/utils/matrix_expander.py b/opensquirrel/utils/matrix_expander.py index 9e96723b..3763531e 100644 --- a/opensquirrel/utils/matrix_expander.py +++ b/opensquirrel/utils/matrix_expander.py @@ -94,7 +94,7 @@ class MatrixExpander(IRVisitor): def __init__(self, qubit_register_size: int) -> None: self.qubit_register_size = qubit_register_size - def visit_bloch_sphere_rotation(self, rot: BlochSphereRotation) -> NDArray[np.complex_]: + def visit_bloch_sphere_rotation(self, rot: BlochSphereRotation) -> NDArray[np.complex128]: if rot.qubit.index >= self.qubit_register_size: raise IndexError("index out of range") @@ -108,7 +108,7 @@ def visit_bloch_sphere_rotation(self, rot: BlochSphereRotation) -> NDArray[np.co ValueError("result has incorrect shape") return result - def visit_controlled_gate(self, gate: ControlledGate) -> NDArray[np.complex_]: + def visit_controlled_gate(self, gate: ControlledGate) -> NDArray[np.complex128]: if gate.control_qubit.index >= self.qubit_register_size: raise IndexError("index out of range") @@ -117,9 +117,9 @@ def visit_controlled_gate(self, gate: ControlledGate) -> NDArray[np.complex_]: if col_index & (1 << gate.control_qubit.index) == 0: col[:] = 0 col[col_index] = 1 - return np.asarray(expanded_matrix, dtype=np.complex_) + return np.asarray(expanded_matrix, dtype=np.complex128) - def visit_matrix_gate(self, gate: MatrixGate) -> NDArray[np.complex_]: + def visit_matrix_gate(self, gate: MatrixGate) -> NDArray[np.complex128]: # The convention is to write gate matrices with operands reversed. # For instance, the first operand of CNOT is the control qubit, and this is written as # 1, 0, 0, 0 @@ -160,17 +160,17 @@ def visit_matrix_gate(self, gate: MatrixGate) -> NDArray[np.complex_]: Z = np.array([[1, 0], [0, -1]]) -def can1(axis: AxisLike, angle: float, phase: float = 0) -> NDArray[np.complex_]: +def can1(axis: AxisLike, angle: float, phase: float = 0) -> NDArray[np.complex128]: nx, ny, nz = Axis(axis) result = cmath.rect(1, phase) * ( math.cos(angle / 2) * np.identity(2) - 1j * math.sin(angle / 2) * (nx * X + ny * Y + nz * Z) ) - return np.asarray(result, dtype=np.complex_) + return np.asarray(result, dtype=np.complex128) -def get_matrix(gate: Gate, qubit_register_size: int) -> NDArray[np.complex_]: +def get_matrix(gate: Gate, qubit_register_size: int) -> NDArray[np.complex128]: """ Compute the unitary matrix corresponding to the gate applied to those qubit operands, taken among any number of qubits. This can be used for, e.g., @@ -211,4 +211,4 @@ def get_matrix(gate: Gate, qubit_register_size: int) -> NDArray[np.complex_]: [0, 0, 0, 1, 0, 0, 0, 0]]) """ expander = MatrixExpander(qubit_register_size) - return cast(NDArray[np.complex_], gate.accept(expander)) + return cast(NDArray[np.complex128], gate.accept(expander)) diff --git a/test/ir_equality_test_base.py b/test/ir_equality_test_base.py index 7f881cc3..7a9f4803 100644 --- a/test/ir_equality_test_base.py +++ b/test/ir_equality_test_base.py @@ -10,7 +10,7 @@ from opensquirrel.common import are_matrices_equivalent_up_to_global_phase -def check_equivalence_up_to_global_phase(matrix_a: NDArray[np.complex_], matrix_b: NDArray[np.complex_]) -> None: +def check_equivalence_up_to_global_phase(matrix_a: NDArray[np.complex128], matrix_b: NDArray[np.complex128]) -> None: assert are_matrices_equivalent_up_to_global_phase(matrix_a, matrix_b)