Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make opensquirrel compatible with numpy 2.0 #250

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opensquirrel/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions opensquirrel/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions opensquirrel/utils/matrix_expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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.,
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion test/ir_equality_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
Loading