Skip to content

Commit 80c1780

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 7591490 commit 80c1780

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

qiskit/quantum_info/operators/symplectic/clifford.py

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

166166
# Initialize StabilizerTable directly from the data
167167
else:
168-
if isinstance(data, (list, np.ndarray)) and np.asarray(data, dtype=bool).ndim == 2:
169-
data = np.array(data, dtype=bool, copy=copy)
168+
if (
169+
isinstance(data, (list, np.ndarray))
170+
and (data_asarray := np.asarray(data, dtype=bool)).ndim == 2
171+
):
172+
# This little dance is to avoid Numpy 1/2 incompatiblities between the availability
173+
# and meaning of the 'copy' argument in 'array' and 'asarray', when the input needs
174+
# its dtype converting. 'asarray' prefers to return 'self' if possible in both.
175+
if copy and np.may_share_memory(data, data_asarray):
176+
data = data_asarray.copy()
177+
else:
178+
data = data_asarray
170179
if data.shape[0] == data.shape[1]:
171180
self.tableau = self._stack_table_phase(
172181
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
@@ -144,7 +144,12 @@ def __init__(
144144
if coeffs is None:
145145
coeffs = np.ones(pauli_list.size, dtype=complex)
146146
else:
147-
coeffs = np.array(coeffs, copy=copy, dtype=dtype)
147+
coeffs_asarray = np.asarray(coeffs, dtype=dtype)
148+
coeffs = (
149+
coeffs_asarray.copy()
150+
if copy and np.may_share_memory(coeffs, coeffs_asarray)
151+
else coeffs_asarray
152+
)
148153

149154
if ignore_pauli_phase:
150155
# 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,5 +1,5 @@
11
rustworkx>=0.13.0
2-
numpy>=1.17,<2
2+
numpy>=1.17,<3
33
ply>=3.10
44
psutil>=5
55
scipy>=1.5
@@ -8,4 +8,4 @@ dill>=0.3
88
python-dateutil>=2.8.0
99
stevedore>=3.0.0
1010
typing-extensions; python_version<'3.11'
11-
symengine>=0.11; platform_machine == 'x86_64' or platform_machine == 'aarch64' or platform_machine == 'ppc64le' or platform_machine == 'amd64' or platform_machine == 'arm64'
11+
symengine>=0.11; platform_machine == 'x86_64' or platform_machine == 'aarch64' or platform_machine == 'ppc64le' or platform_machine == 'amd64' or platform_machine == 'arm64'

0 commit comments

Comments
 (0)