-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update sources for compatibility with NumPy 2 #6706
Comments
Starting some notes. A find-grep through the Cirq codebase reveals slightly over 500 files containing find . ! -path '*/.git/*' \
! -path '*/__pycache__/*' \
! -path '*/.*_cache/*' \
! -name '*.pyc' \
-type f -print0 \
| xargs -0 -e egrep -n "import numpy|from numpy" Doing a find-grep to search for instances of " In addition to the NumPy migration guide, there is also a separate document with NumPy 2.0-specific advice. NumPy version 2.1 has some additional changes since the 2.0 release, so we need to look at those too: NumPy 2.1.0 Release Notes. Past related issues and PRs: |
The primarily goal here is to fix all numpy API calls that were removed or changed in 2.0, Finally, we should run check/pytest and verify if there are any numpy-related deprecation warnings. We should perhaps also add ruff to our CI checks following https://numpy.org/doc/stable/numpy_2_0_migration_guide.html#ruff-plugin, to prevent numpy-1 APIs creeping back in. That should however go to a separate PR. PS: I have quickly glanced over 2.1.0 release notes and there does not seem much relevant for our code. |
(Continuing notes.) Incredibly enough, ruff check --verbose --select NPY201 --fix . does not find anything to change. (This was so surprising that I tried a number of variations to make sure ruff was actually checking things!) The Python requirements file that installs NumPy is
Temporarily moving them out of the way (and removing references to them in a couple of places) then allows installation to proceed and |
If we temporarily ignore the modules mentioned above, the Numpy 2 has a configuration option to control that behavior (https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html#numpy-set-printoptions), and setting We need to change the rendering code for circuits so that it prints these np data values as strings (without setting a global configuration parameter). |
One of the changes in NumPy 2 is to type promotion. The table at https://numpy.org/neps/nep-0050-scalar-promotion.html#t6 summarizes the changes. Particularly notable for Cirq is cases involving It would not be appropriate to change measurement values to be larger than Additional info: |
A conflict exists in the versions of NumPy needed by second-level dependencies. Quimb requires Numba, and while Quimb itself is compatible with NumPy 2.1, if you have NumPy 2.1 installed, a pip install of Numba 0.60 (the latest version) results in pip uninstalling Numpy 2.1 and installing 2.0.2 instead. Trying to force NumPy 2.1 seems to result in pip installation errors involving llvm. (The Numba sources don't seem to require NumPy 2.0 per se, so I think what's happening is Numba requires something else, and that leads to conflicts.) Now, Cirq tests pass even with NumPy 2.0.x, so one option is to accept NumPy > 2.0 and < 2.1 for now, request Numba to support NumPy 2.1, and update our requirements files later. This would also align with the internal Google codebase, because the upcoming internal transition to NumPy 2 is going to use version 2.0.1. |
All tests pass locally for me now, except for cirq-rigetti. Unfortunately, PyQuil (used by cirq-rigetti) limits the version of NumPy to < 2.0, so it is impossible to satisfy the requirement for importing it for cirq-rigetti. This cirq-rigetti issue is also the reason the CI checks are currently failing for this PR. |
Note cirq-rigetti is excluded, because it is not yet NumPy-2 compatible. The test is only run on Ubuntu for the sake of speed and simplicity. Related to quantumlib#6706
Description of the issue
Let us get our sources compatible with recently released NumPy 2
Ref: https://numpy.org/doc/stable/numpy_2_0_migration_guide.html
Cirq version
1.5.0.dev at d94c457
The text was updated successfully, but these errors were encountered: