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:

- 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>
  • Loading branch information
jakelishman and levbishop committed Apr 16, 2024
1 parent 7591490 commit 80c1780
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
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 @@ -165,8 +165,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 @@ -144,7 +144,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,5 +1,5 @@
rustworkx>=0.13.0
numpy>=1.17,<2
numpy>=1.17,<3
ply>=3.10
psutil>=5
scipy>=1.5
Expand All @@ -8,4 +8,4 @@ dill>=0.3
python-dateutil>=2.8.0
stevedore>=3.0.0
typing-extensions; python_version<'3.11'
symengine>=0.11; platform_machine == 'x86_64' or platform_machine == 'aarch64' or platform_machine == 'ppc64le' or platform_machine == 'amd64' or platform_machine == 'arm64'
symengine>=0.11; platform_machine == 'x86_64' or platform_machine == 'aarch64' or platform_machine == 'ppc64le' or platform_machine == 'amd64' or platform_machine == 'arm64'

0 comments on commit 80c1780

Please sign in to comment.