Skip to content

Commit c092aa1

Browse files
Finalise support for Numpy 2.0
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: - Qiskitgh-10890 - Qiskitgh-10891 - Qiskitgh-10892 - Qiskitgh-10897 - Qiskitgh-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 <18673315+levbishop@users.noreply.github.com>
1 parent 5421ec6 commit c092aa1

File tree

7 files changed

+27
-8
lines changed

7 files changed

+27
-8
lines changed

qiskit/primitives/backend_sampler_v2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _prepare_memory(results: list[Result], num_bytes: int) -> NDArray[np.uint8]:
213213
# no measure in a circuit
214214
data = np.zeros((exp.shots, num_bytes), dtype=np.uint8)
215215
lst.append(data)
216-
ary = np.array(lst, copy=False)
216+
ary = np.asarray(lst)
217217
return np.unpackbits(ary, axis=-1, bitorder="big")
218218

219219

qiskit/primitives/containers/shape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def array_coerce(arr: ArrayLike | Shaped) -> NDArray | Shaped:
8585
"""
8686
if isinstance(arr, Shaped):
8787
return arr
88-
return np.array(arr, copy=False)
88+
return np.asarray(arr)
8989

9090

9191
def _flatten_to_ints(arg: ShapeInput) -> Iterable[int]:

qiskit/quantum_info/operators/symplectic/clifford.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,17 @@ def __init__(self, data, validate=True, copy=True):
164164

165165
# Initialize StabilizerTable directly from the data
166166
else:
167-
if isinstance(data, (list, np.ndarray)) and np.asarray(data, dtype=bool).ndim == 2:
168-
data = np.array(data, dtype=bool, copy=copy)
167+
if (
168+
isinstance(data, (list, np.ndarray))
169+
and (data_asarray := np.asarray(data, dtype=bool)).ndim == 2
170+
):
171+
# This little dance is to avoid Numpy 1/2 incompatiblities between the availability
172+
# and meaning of the 'copy' argument in 'array' and 'asarray', when the input needs
173+
# its dtype converting. 'asarray' prefers to return 'self' if possible in both.
174+
if copy and np.may_share_memory(data, data_asarray):
175+
data = data_asarray.copy()
176+
else:
177+
data = data_asarray
169178
if data.shape[0] == data.shape[1]:
170179
self.tableau = self._stack_table_phase(
171180
data, np.zeros(data.shape[0], dtype=bool)

qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ def __init__(
142142
if coeffs is None:
143143
coeffs = np.ones(pauli_list.size, dtype=complex)
144144
else:
145-
coeffs = np.array(coeffs, copy=copy, dtype=dtype)
145+
coeffs_asarray = np.asarray(coeffs, dtype=dtype)
146+
coeffs = (
147+
coeffs_asarray.copy()
148+
if copy and np.may_share_memory(coeffs, coeffs_asarray)
149+
else coeffs_asarray
150+
)
146151

147152
if ignore_pauli_phase:
148153
# Fast path used in copy operations, where the phase of the PauliList is already known

qiskit/visualization/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _num_to_latex(raw_value, decimals=15, first_term=True, coefficient=False):
3333
"""
3434
import sympy # runtime import
3535

36-
raw_value = np.around(raw_value, decimals=decimals)
36+
raw_value = np.around(raw_value, decimals=decimals).item()
3737
value = sympy.nsimplify(raw_value, rational=False)
3838

3939
if isinstance(value, sympy.core.numbers.Rational) and value.denominator > 50:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
This release of Qiskit finalizes support for NumPy 2.0. Qiskit will continue to support both
5+
Numpy 1.x and 2.x for the foreseeable future.

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
rustworkx>=0.14.0
2-
numpy>=1.17,<2
2+
numpy>=1.17,<3
33
scipy>=1.5
44
sympy>=1.3
55
dill>=0.3
66
python-dateutil>=2.8.0
77
stevedore>=3.0.0
88
typing-extensions
9-
symengine>=0.11
9+
symengine>=0.11

0 commit comments

Comments
 (0)