Skip to content

Commit

Permalink
Finalise support for Numpy 2.0
Browse files Browse the repository at this point in the history
This commit brings the Qiskit test suite to a passing state (with all
optionals installed) with Numpy 2.0.0b1, building on previous commits
that handled much of the rest of the changing requirements:

- gh-10890
- gh-10891
- gh-10892
- gh-10897
- gh-11023

Notably, this commit did not actually require a rebuild of Qiskit,
despite us compiling against Numpy; it seems to happen that the C API
stuff we use via `rust-numpy` (which loads the Numpy C extensions
dynamically during module initialisation) hasn't changed.

The main changes are:

- adapting to the changed `copy=None` and `copy=False` semantics in
  `array` and `asarray`.
- making sure all our implementers of `__array__` accept both `dtype`
  and `copy` arguments.

Co-authored-by: Lev S. Bishop <[email protected]>
  • Loading branch information
jakelishman and levbishop committed Apr 15, 2024
1 parent 5421ec6 commit c092aa1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion qiskit/primitives/backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _prepare_memory(results: list[Result], num_bytes: int) -> NDArray[np.uint8]:
# no measure in a circuit
data = np.zeros((exp.shots, num_bytes), dtype=np.uint8)
lst.append(data)
ary = np.array(lst, copy=False)
ary = np.asarray(lst)
return np.unpackbits(ary, axis=-1, bitorder="big")


Expand Down
2 changes: 1 addition & 1 deletion qiskit/primitives/containers/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def array_coerce(arr: ArrayLike | Shaped) -> NDArray | Shaped:
"""
if isinstance(arr, Shaped):
return arr
return np.array(arr, copy=False)
return np.asarray(arr)


def _flatten_to_ints(arg: ShapeInput) -> Iterable[int]:
Expand Down
13 changes: 11 additions & 2 deletions qiskit/quantum_info/operators/symplectic/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,17 @@ def __init__(self, data, validate=True, copy=True):

# Initialize StabilizerTable directly from the data
else:
if isinstance(data, (list, np.ndarray)) and np.asarray(data, dtype=bool).ndim == 2:
data = np.array(data, dtype=bool, copy=copy)
if (
isinstance(data, (list, np.ndarray))
and (data_asarray := np.asarray(data, dtype=bool)).ndim == 2
):
# This little dance is to avoid Numpy 1/2 incompatiblities between the availability
# and meaning of the 'copy' argument in 'array' and 'asarray', when the input needs
# its dtype converting. 'asarray' prefers to return 'self' if possible in both.
if copy and np.may_share_memory(data, data_asarray):
data = data_asarray.copy()
else:
data = data_asarray
if data.shape[0] == data.shape[1]:
self.tableau = self._stack_table_phase(
data, np.zeros(data.shape[0], dtype=bool)
Expand Down
7 changes: 6 additions & 1 deletion qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ def __init__(
if coeffs is None:
coeffs = np.ones(pauli_list.size, dtype=complex)
else:
coeffs = np.array(coeffs, copy=copy, dtype=dtype)
coeffs_asarray = np.asarray(coeffs, dtype=dtype)
coeffs = (
coeffs_asarray.copy()
if copy and np.may_share_memory(coeffs, coeffs_asarray)
else coeffs_asarray
)

if ignore_pauli_phase:
# Fast path used in copy operations, where the phase of the PauliList is already known
Expand Down
2 changes: 1 addition & 1 deletion qiskit/visualization/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _num_to_latex(raw_value, decimals=15, first_term=True, coefficient=False):
"""
import sympy # runtime import

raw_value = np.around(raw_value, decimals=decimals)
raw_value = np.around(raw_value, decimals=decimals).item()
value = sympy.nsimplify(raw_value, rational=False)

if isinstance(value, sympy.core.numbers.Rational) and value.denominator > 50:
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/numpy-2.0-2f3e35bd42c48518.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
This release of Qiskit finalizes support for NumPy 2.0. Qiskit will continue to support both
Numpy 1.x and 2.x for the foreseeable future.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
rustworkx>=0.14.0
numpy>=1.17,<2
numpy>=1.17,<3
scipy>=1.5
sympy>=1.3
dill>=0.3
python-dateutil>=2.8.0
stevedore>=3.0.0
typing-extensions
symengine>=0.11
symengine>=0.11

0 comments on commit c092aa1

Please sign in to comment.