Skip to content

Commit

Permalink
Merge pull request #351 from QunaSys/numpy-20-compatibility
Browse files Browse the repository at this point in the history
Support numpy 2.0.0
  • Loading branch information
ThomasenQunasys authored Jul 11, 2024
2 parents b9dc865 + 3965f01 commit 5ac2603
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_filter_matrix(
]
amatrix = []
for counts in sampler(pairs):
cv: "npt.NDArray[np.float_]" = np.array(
cv: "npt.NDArray[np.float64]" = np.array(
[float(counts[k]) if k in counts else 0.0 for k in range(dim)]
)
amatrix.append(cv / linalg.norm(cv, ord=1))
Expand All @@ -70,7 +70,7 @@ def readout_mitigation(
def counts_iter() -> Iterable[MeasurementCounts]:
for count in counts:
dim = filter_matrix.shape[0]
cnoisy: "npt.NDArray[np.float_]" = np.array(
cnoisy: "npt.NDArray[np.float64]" = np.array(
[float(count[k]) if k in count else 0.0 for k in range(dim)]
)
cideal = filter_matrix.dot(cnoisy)
Expand Down
2 changes: 1 addition & 1 deletion packages/algo/quri_parts/algo/optimizer/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#: Represents a parameter vector subject to optimization.
#: (A gradient vector is also represented as Params.)
Params: TypeAlias = "npt.NDArray[np.float_]"
Params: TypeAlias = "npt.NDArray[np.float64]"

#: Cost function for optimization.
CostFunction: TypeAlias = Callable[[Params], float]
Expand Down
7 changes: 4 additions & 3 deletions packages/algo/quri_parts/algo/optimizer/lbfgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
self._rho_const = rho_const
self._m = m

self._gtol: Optional[Callable[["npt.NDArray[np.float_]"], bool]] = None
self._gtol: Optional[Callable[["npt.NDArray[np.float64]"], bool]] = None
if gtol is not None:
if not 0.0 < gtol:
raise ValueError("ftol must be a positive float.")
Expand Down Expand Up @@ -282,10 +282,11 @@ def _linesearch12(
cost_prev: float,
extra_condition: Optional[
Callable[
[float, "npt.NDArray[np.float_]", float, "npt.NDArray[np.float_]"], bool
[float, "npt.NDArray[np.float64]", float, "npt.NDArray[np.float64]"],
bool,
]
] = None,
) -> tuple[int, int, float, float, float, "npt.NDArray[np.float_]"]:
) -> tuple[int, int, float, float, float, "npt.NDArray[np.float64]"]:
alpha, fc, gc, fval, old_fval, gval = line_search_wolfe1(
f=cost_function,
fprime=grad_function,
Expand Down
2 changes: 1 addition & 1 deletion packages/algo/quri_parts/algo/optimizer/nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _param_step(
cost_function: CostFunction,
) -> tuple[Params, float, int]:
x_data = np.pi * np.linspace(-1.0, 1.0, num=self._n_points, endpoint=False)
y_data: "npt.NDArray[np.float_]" = np.zeros(self._n_points, dtype=float)
y_data: "npt.NDArray[np.float64]" = np.zeros(self._n_points, dtype=float)

params = state.params.copy()
funcalls = state.funcalls
Expand Down
4 changes: 2 additions & 2 deletions packages/algo/quri_parts/algo/optimizer/tolerance.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def fn(cost: float, cost_prev: float) -> bool:
return fn


def gtol(gtol: float) -> Callable[["npt.NDArray[np.float_]"], bool]:
def gtol(gtol: float) -> Callable[["npt.NDArray[np.float64]"], bool]:
"""Returns a function evaluating gradient function tolerance.
The return value is True when ``amax(abs(grad)) <= gtol``.
"""

def fn(grad: "npt.NDArray[np.float_]") -> bool:
def fn(grad: "npt.NDArray[np.float64]") -> bool:
return cast(float, np.amax(np.abs(grad))) <= gtol

return fn
2 changes: 1 addition & 1 deletion packages/circuit/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ style = "pep440"
[tool.poetry.dependencies]
python = "^3.9.8"
typing-extensions = "^4.1.1"
numpy = "^1.22.0"
numpy = ">=1.22.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.0.1"
Expand Down
8 changes: 4 additions & 4 deletions packages/circuit/quri_parts/circuit/utils/circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ def _place_check(
def _write_gate_string(
gate_string: Sequence[str],
row_idx: int,
circuit_picture: npt.NDArray[np.string_],
) -> npt.NDArray[np.string_]:
circuit_picture: npt.NDArray[np.bytes_],
) -> npt.NDArray[np.bytes_]:
rows, cols = circuit_picture.shape

# Checks if existing circuit block can accomodate ``gate_string``.
Expand Down Expand Up @@ -384,7 +384,7 @@ def _write_gate_string(
return circuit_picture


def _connect_wire(circuit_picture: npt.NDArray[np.string_]) -> npt.NDArray[np.string_]:
def _connect_wire(circuit_picture: npt.NDArray[np.bytes_]) -> npt.NDArray[np.bytes_]:
horizontal_size = circuit_picture.shape[1]
for row in range(2, circuit_picture.shape[0], 4):
p = 0
Expand All @@ -409,7 +409,7 @@ def _connect_wire(circuit_picture: npt.NDArray[np.string_]) -> npt.NDArray[np.st
return circuit_picture


def _interm_layer(qubit_count: int) -> npt.NDArray[np.string_]:
def _interm_layer(qubit_count: int) -> npt.NDArray[np.bytes_]:
arr = np.full((qubit_count * 4, 1), " ")
for i in range(qubit_count):
row_idx = (i + 1) * 4 - 2
Expand Down
2 changes: 1 addition & 1 deletion packages/cirq/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ style = "pep440"

[tool.poetry.dependencies]
python = "^3.9.8"
numpy = "^1.22.0"
numpy = ">=1.22.0"
quri-parts-circuit = "*"
cirq-core = "^1.0.0"

Expand Down
6 changes: 3 additions & 3 deletions packages/cirq/quri_parts/cirq/circuit/circuit_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, psi: float) -> None:
def _num_qubits_(self) -> int:
return 1

def _unitary_(self) -> "np.typing.NDArray[np.complex_]":
def _unitary_(self) -> "np.typing.NDArray[np.complex128]":
return np.array([[1, 0], [0, np.exp(self.psi * 1.0j)]])

def _circuit_diagram_info_(self) -> str:
Expand All @@ -75,7 +75,7 @@ def __init__(self, phi: float, psi: float) -> None:
def _num_qubits_(self) -> int:
return 1

def _unitary_(self) -> "np.typing.NDArray[np.complex_]":
def _unitary_(self) -> "np.typing.NDArray[np.complex128]":
return np.array(
[
[1 / np.sqrt(2), -np.exp(self.psi * 1.0j) / np.sqrt(2)],
Expand All @@ -102,7 +102,7 @@ def __init__(self, theta: float, phi: float, psi: float) -> None:
def _num_qubits_(self) -> int:
return 1

def _unitary_(self) -> "np.typing.NDArray[np.complex_]":
def _unitary_(self) -> "np.typing.NDArray[np.complex128]":
return np.array(
[
[
Expand Down
2 changes: 1 addition & 1 deletion packages/core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ style = "pep440"
[tool.poetry.dependencies]
python = ">=3.9.8,<3.13"
typing-extensions = "^4.1.1"
numpy = "^1.22.0"
numpy = ">=1.22.0"
quri-parts-circuit = "*"
scipy = "^1.11.3"

Expand Down
2 changes: 1 addition & 1 deletion packages/ionq/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ style = "pep440"
[tool.poetry.dependencies]
python = "^3.9.8"
typing-extensions = "^4.1.1"
numpy = "^1.22.0"
numpy = ">=1.22.0"

[tool.poetry.group.dev.dependencies]
quri-parts-circuit = {path = "../circuit", develop = true}
Expand Down
2 changes: 1 addition & 1 deletion packages/pyscf/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ style = "pep440"
[tool.poetry.dependencies]
python = "^3.9.8"
typing-extensions = "^4.1.1"
numpy = "^1.22.0"
numpy = ">=1.22.0"
pyscf = "^2.3.0"
quri-parts-core = "*"
quri-parts-chem = "*"
Expand Down
2 changes: 1 addition & 1 deletion packages/qiskit/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ style = "pep440"

[tool.poetry.dependencies]
python = "^3.9.8,<3.12"
numpy = "^1.22.0"
numpy = ">=1.22.0"
quri-parts-circuit = "*"
quri-parts-core = "*"
pydantic = "^2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/quantinuum/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ style = "pep440"
[tool.poetry.dependencies]
python = "^3.9.8"
typing-extensions = "^4.1.1"
numpy = "^1.22.0"
numpy = ">=1.22.0"

[tool.poetry.group.dev.dependencies]
quri-parts-circuit = {path = "../circuit", develop = true}
Expand Down
12 changes: 6 additions & 6 deletions packages/qulacs/quri_parts/qulacs/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import numpy as np
import qulacs as ql
from numpy import cfloat, zeros
from numpy import complex128, zeros
from numpy.typing import NDArray

from quri_parts.circuit import NonParametricQuantumCircuit
Expand Down Expand Up @@ -53,7 +53,7 @@ def _evaluate_qp_state_to_qulacs_state(state: QulacsStateT) -> ql.QuantumState:

def _get_updated_qulacs_state_from_vector(
circuit: Union[NonParametricQuantumCircuit, _QulacsCircuit],
init_state: NDArray[cfloat],
init_state: NDArray[complex128],
) -> ql.QuantumState:
if len(init_state) != 2**circuit.qubit_count:
raise ValueError("Inconsistent qubit length between circuit and state")
Expand Down Expand Up @@ -85,21 +85,21 @@ def evaluate_state_to_vector(state: QulacsStateT) -> QuantumStateVector:

def run_circuit(
circuit: NonParametricQuantumCircuit,
init_state: NDArray[cfloat],
) -> NDArray[cfloat]:
init_state: NDArray[complex128],
) -> NDArray[complex128]:
"""Act a NonParametricQuantumCircuit onto a state vector and returns a new
state vector."""

qulacs_state = _get_updated_qulacs_state_from_vector(circuit, init_state)
# We need to disable type check due to an error in qulacs type annotation
# https://github.com/qulacs/qulacs/issues/537
new_state_vector: NDArray[cfloat] = qulacs_state.get_vector() # type: ignore
new_state_vector: NDArray[complex128] = qulacs_state.get_vector() # type: ignore

return new_state_vector


def get_marginal_probability(
state_vector: NDArray[cfloat], measured_values: dict[int, int]
state_vector: NDArray[complex128], measured_values: dict[int, int]
) -> float:
"""Compute the probability of obtaining a result when measuring on a subset
of the qubits.
Expand Down
2 changes: 1 addition & 1 deletion packages/qulacs/tests/qulacs/estimator/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@


def create_vector(qubit_count: int, bits: int) -> StateVectorType:
vector: StateVectorType = np.zeros(2**qubit_count, dtype=np.cfloat)
vector: StateVectorType = np.zeros(2**qubit_count, dtype=np.complex128)
vector[bits] = 1.0
return vector

Expand Down
8 changes: 4 additions & 4 deletions packages/stim/quri_parts/stim/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Union

import stim
from numpy import cfloat, zeros
from numpy import complex128, zeros
from numpy.typing import NDArray

from quri_parts.circuit import NonParametricQuantumCircuit
Expand Down Expand Up @@ -46,8 +46,8 @@ def evaluate_state_to_vector(

def run_circuit(
circuit: NonParametricQuantumCircuit,
init_state: NDArray[cfloat],
) -> NDArray[cfloat]:
init_state: NDArray[complex128],
) -> NDArray[complex128]:
"""Act a NonParametricQuantumCircuit onto a state vector and returns a new
state vector.
Expand All @@ -58,6 +58,6 @@ def run_circuit(
tableau.set_state_from_state_vector(init_state, endian="little")
stim_circuit = convert_circuit(circuit)
tableau.do_circuit(stim_circuit)
out_state: NDArray[cfloat] = tableau.state_vector(endian="little")
out_state: NDArray[complex128] = tableau.state_vector(endian="little")

return out_state
4 changes: 2 additions & 2 deletions packages/stim/tests/stim/simulator/test_stim_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import unittest

from numpy import allclose, angle, array, cfloat, exp
from numpy import allclose, angle, array, complex128, exp
from numpy.typing import NDArray

from quri_parts.circuit import NonParametricQuantumCircuit, QuantumCircuit
Expand All @@ -20,7 +20,7 @@

class TestSimulator(unittest.TestCase):
update_circuit: NonParametricQuantumCircuit
expected_output: NDArray[cfloat]
expected_output: NDArray[complex128]

@classmethod
def setUpClass(cls) -> None:
Expand Down
2 changes: 1 addition & 1 deletion packages/tket/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ style = "pep440"

[tool.poetry.dependencies]
python = "^3.9.8"
numpy = "^1.22.0"
numpy = ">=1.22.0"
quri-parts-circuit = "*"
pytket = "^1.0.0"

Expand Down
16 changes: 8 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 5ac2603

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.