Skip to content
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

Pennylane is compatible with numpy 2.0 #6061

Merged
merged 51 commits into from
Sep 16, 2024
Merged

Conversation

EmilianoG-byte
Copy link
Contributor

@EmilianoG-byte EmilianoG-byte commented Aug 1, 2024

Context:

We want to make Pennylane compatible with Numpy 2.0. After several discussions, we decided to test NumPy 2.0 on the CI by default in every PR (testing both Python versions would have been to slow). Some jobs still downgrade automatically to Numpy 1.x, since some interfaces (such as Tensorflow) still do not support NumPy 2.0.

Description of the Change:

We can distinguish the changes into 3 main categories:

Changes to workflows

  • None in the final version

Changes to requirements and setup files

  • Unpin the Numpy version in setup.py (now we also allow Numpy 2.0).
  • Update requirements-ci.txt to include Scipy 1.13 (this adds support for Numpy 2.0).

Changes to the source code

  • Change np.NaN to np.nan.
  • Use legacy printing representation in tests, contrary to the new numpy representation of scalars, e.g. np.float64(3.0) rather than just 3.0.
  • Update probabilities warning to be case insensitive and check for a partial match, since this warning was changed in Numpy 2.0.
  • Check the datatype of np.exp from the Global phase only for Numpy 1.x, since this gets promoted to complex128 in Numpy 2.x. https://numpy.org/neps/nep-0050-scalar-promotion.html#schema-of-the-new-proposed-promotion-rules.

Benefits:

Make Pennylane compatible with Numpy 2.0.

Possible Drawbacks:

  • We need to create a separate workflow to keep testing PennyLane with NumPy 1.x, since we still want to maintain compatibility with previous NumPy versions. This will be done in a separate PR.

  • We are not testing Numpy 2.x for the interfaces that implicitly require Numpy 1.x. These currently seem to be tensorflow and openfermionpyscf (notice that tensorflow is required in some code sections like qcut). In particular, openfermionpyscf causes an error:

AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead.

in the qchem tests. The attribute np.string_ is not used in the PL source code, so it is a problem with the package itself.

[sc-61399]
[sc-66548]

Copy link
Contributor

github-actions bot commented Aug 1, 2024

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@EmilianoG-byte EmilianoG-byte marked this pull request as ready for review August 27, 2024 15:04
Copy link

codecov bot commented Aug 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.58%. Comparing base (d0344b0) to head (591671d).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6061      +/-   ##
==========================================
- Coverage   99.59%   99.58%   -0.01%     
==========================================
  Files         443      443              
  Lines       42255    42255              
==========================================
- Hits        42082    42078       -4     
- Misses        173      177       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR only fixing tests?

.github/workflows/install_deps/action.yml Outdated Show resolved Hide resolved
pennylane/numpy/random.py Outdated Show resolved Hide resolved
@EmilianoG-byte EmilianoG-byte changed the title np.sign is compatible with PennyLane Pennylane is compatible with numpy 2.0 Aug 28, 2024
Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @EmilianoG-byte for all of the work
A few questions from my side:

.github/workflows/install_deps/action.yml Outdated Show resolved Hide resolved
.github/workflows/install_deps/action.yml Show resolved Hide resolved
.github/workflows/install_deps/action.yml Show resolved Hide resolved
.github/workflows/install_deps/action.yml Outdated Show resolved Hide resolved
.github/workflows/install_deps/action.yml Outdated Show resolved Hide resolved
requirements-ci.txt Outdated Show resolved Hide resolved
requirements-ci.txt Show resolved Hide resolved
requirements-ci.txt Outdated Show resolved Hide resolved
tests/data/attributes/operator/test_operator.py Outdated Show resolved Hide resolved
tests/devices/test_default_qubit_legacy.py Show resolved Hide resolved
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I left a few comments and suggestions.

.github/workflows/install_deps/action.yml Show resolved Hide resolved
.github/workflows/install_deps/action.yml Outdated Show resolved Hide resolved
.github/workflows/interface-unit-tests.yml Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
tests/data/attributes/test_dict.py Outdated Show resolved Hide resolved
tests/devices/test_default_qubit_legacy.py Show resolved Hide resolved
@PietropaoloFrisoni PietropaoloFrisoni added the ci:run-full-test-suite Run the full test-suite on the pull request label Sep 12, 2024
@PietropaoloFrisoni
Copy link
Contributor

Update: now, Codecov project complains since it detects a -0.07% coverage compared to the commit associated with this PR. As specified in the PR itself, Code cov failures are in default.qubit.legacy and QubitDevice, so can be ignored.

@PietropaoloFrisoni PietropaoloFrisoni removed the ci:run-full-test-suite Run the full test-suite on the pull request label Sep 13, 2024
@PietropaoloFrisoni PietropaoloFrisoni merged commit 94f067a into master Sep 16, 2024
36 checks passed
@PietropaoloFrisoni PietropaoloFrisoni deleted the compatible-np-2.0 branch September 16, 2024 13:08
mudit2812 pushed a commit that referenced this pull request Sep 16, 2024
**Context:**

We want to make Pennylane compatible with Numpy 2.0. After several
discussions, we decided to test NumPy 2.0 on the CI by default in every
PR (testing both Python versions would have been to slow). Some jobs
still downgrade automatically to Numpy 1.x, since some interfaces (such
as Tensorflow) still do not support NumPy 2.0.

**Description of the Change:**

We can distinguish the changes into 3 main categories:

*Changes to workflows*

- None in the final version

*Changes to requirements and setup files*

- Unpin the Numpy version in `setup.py` (now we also allow Numpy 2.0).
- Update `requirements-ci.txt` to include Scipy 1.13 (this adds support
for Numpy 2.0).
- Pin Numpy in `requirements-ci.txt` to 2.0. 

*Changes to the source code*
- Change `np.NaN` to `np.nan`.
- Use legacy printing representation in tests, contrary to the new numpy
representation of scalars, e.g. np.float64(3.0) rather than just 3.0.
- Update probabilities warning to be case insensitive and check for a
partial match, since this warning was changed in Numpy 2.0.
- Check the datatype of np.exp from the Global phase only for Numpy 1.x,
since this gets promoted to complex128 in Numpy 2.x.
https://numpy.org/neps/nep-0050-scalar-promotion.html#schema-of-the-new-proposed-promotion-rules.

**Benefits:**

Make Pennylane compatible with Numpy 2.0.

**Possible Drawbacks:**

- We need to create a separate workflow to keep testing PennyLane with
NumPy 1.x, since we still want to maintain compatibility with previous
NumPy versions. This will be done in a separate PR.

- We are not testing Numpy 2.x for the interfaces that implicitly
require Numpy 1.x. These currently seem to be `tensorflow` and
`openfermionpyscf` (notice that `tensorflow` is required in some code
sections like qcut). In particular, `openfermionpyscf` causes an error:
```
AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead.
``` 
in the qchem tests. The attribute `np.string_` is not used in the PL
source code, so it is a problem with the package itself.

[sc-61399]
[sc-66548]

---------

Co-authored-by: PietropaoloFrisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropfrisoni@gmail.com>
mudit2812 pushed a commit that referenced this pull request Sep 16, 2024
**Context:**

We want to make Pennylane compatible with Numpy 2.0. After several
discussions, we decided to test NumPy 2.0 on the CI by default in every
PR (testing both Python versions would have been to slow). Some jobs
still downgrade automatically to Numpy 1.x, since some interfaces (such
as Tensorflow) still do not support NumPy 2.0.

**Description of the Change:**

We can distinguish the changes into 3 main categories:

*Changes to workflows*

- None in the final version

*Changes to requirements and setup files*

- Unpin the Numpy version in `setup.py` (now we also allow Numpy 2.0).
- Update `requirements-ci.txt` to include Scipy 1.13 (this adds support
for Numpy 2.0).
- Pin Numpy in `requirements-ci.txt` to 2.0. 

*Changes to the source code*
- Change `np.NaN` to `np.nan`.
- Use legacy printing representation in tests, contrary to the new numpy
representation of scalars, e.g. np.float64(3.0) rather than just 3.0.
- Update probabilities warning to be case insensitive and check for a
partial match, since this warning was changed in Numpy 2.0.
- Check the datatype of np.exp from the Global phase only for Numpy 1.x,
since this gets promoted to complex128 in Numpy 2.x.
https://numpy.org/neps/nep-0050-scalar-promotion.html#schema-of-the-new-proposed-promotion-rules.

**Benefits:**

Make Pennylane compatible with Numpy 2.0.

**Possible Drawbacks:**

- We need to create a separate workflow to keep testing PennyLane with
NumPy 1.x, since we still want to maintain compatibility with previous
NumPy versions. This will be done in a separate PR.

- We are not testing Numpy 2.x for the interfaces that implicitly
require Numpy 1.x. These currently seem to be `tensorflow` and
`openfermionpyscf` (notice that `tensorflow` is required in some code
sections like qcut). In particular, `openfermionpyscf` causes an error:
```
AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead.
``` 
in the qchem tests. The attribute `np.string_` is not used in the PL
source code, so it is a problem with the package itself.

[sc-61399]
[sc-66548]

---------

Co-authored-by: PietropaoloFrisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropfrisoni@gmail.com>
PietropaoloFrisoni added a commit that referenced this pull request Sep 17, 2024
**Context:** As part of the effort to make PL compatible with Numpy 2.0
(see #6061), we need to upgrade JAX to 0.4.26+ since such a version
introduced the support for Numpy 2.0. We opted for JAX 0.4.28 since it
is the same version used by Catalyst.

**Description of the Change:** As above.

**Benefits:** PL is compatible with Numpy 2.0 and Jax 0.4.28.

**Possible Drawbacks:** 

- From JAX 0.4.27, in `jax.jit`, passing invalid static_argnums or
static_argnames now leads to an error rather than a warning. In PL, this
breaks every test where we set `shots` in the `QNode` call with
`static_argnames=["shots"]`. At this stage, we decided to mark such
tests with `pytest.xfail` to allow the upgrade.

**Related GitHub Issues:** None.

**Related Shortcut Stories**: [sc-61389]

---------

Co-authored-by: dwierichs <david.wierichs@xanadu.ai>
mudit2812 pushed a commit that referenced this pull request Sep 18, 2024
**Context:**

We want to make Pennylane compatible with Numpy 2.0. After several
discussions, we decided to test NumPy 2.0 on the CI by default in every
PR (testing both Python versions would have been to slow). Some jobs
still downgrade automatically to Numpy 1.x, since some interfaces (such
as Tensorflow) still do not support NumPy 2.0.

**Description of the Change:**

We can distinguish the changes into 3 main categories:

*Changes to workflows*

- None in the final version

*Changes to requirements and setup files*

- Unpin the Numpy version in `setup.py` (now we also allow Numpy 2.0).
- Update `requirements-ci.txt` to include Scipy 1.13 (this adds support
for Numpy 2.0).
- Pin Numpy in `requirements-ci.txt` to 2.0. 

*Changes to the source code*
- Change `np.NaN` to `np.nan`.
- Use legacy printing representation in tests, contrary to the new numpy
representation of scalars, e.g. np.float64(3.0) rather than just 3.0.
- Update probabilities warning to be case insensitive and check for a
partial match, since this warning was changed in Numpy 2.0.
- Check the datatype of np.exp from the Global phase only for Numpy 1.x,
since this gets promoted to complex128 in Numpy 2.x.
https://numpy.org/neps/nep-0050-scalar-promotion.html#schema-of-the-new-proposed-promotion-rules.

**Benefits:**

Make Pennylane compatible with Numpy 2.0.

**Possible Drawbacks:**

- We need to create a separate workflow to keep testing PennyLane with
NumPy 1.x, since we still want to maintain compatibility with previous
NumPy versions. This will be done in a separate PR.

- We are not testing Numpy 2.x for the interfaces that implicitly
require Numpy 1.x. These currently seem to be `tensorflow` and
`openfermionpyscf` (notice that `tensorflow` is required in some code
sections like qcut). In particular, `openfermionpyscf` causes an error:
```
AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead.
``` 
in the qchem tests. The attribute `np.string_` is not used in the PL
source code, so it is a problem with the package itself.

[sc-61399]
[sc-66548]

---------

Co-authored-by: PietropaoloFrisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropfrisoni@gmail.com>
mudit2812 pushed a commit that referenced this pull request Sep 18, 2024
**Context:** As part of the effort to make PL compatible with Numpy 2.0
(see #6061), we need to upgrade JAX to 0.4.26+ since such a version
introduced the support for Numpy 2.0. We opted for JAX 0.4.28 since it
is the same version used by Catalyst.

**Description of the Change:** As above.

**Benefits:** PL is compatible with Numpy 2.0 and Jax 0.4.28.

**Possible Drawbacks:** 

- From JAX 0.4.27, in `jax.jit`, passing invalid static_argnums or
static_argnames now leads to an error rather than a warning. In PL, this
breaks every test where we set `shots` in the `QNode` call with
`static_argnames=["shots"]`. At this stage, we decided to mark such
tests with `pytest.xfail` to allow the upgrade.

**Related GitHub Issues:** None.

**Related Shortcut Stories**: [sc-61389]

---------

Co-authored-by: dwierichs <david.wierichs@xanadu.ai>
mehrdad2m added a commit to PennyLaneAI/catalyst that referenced this pull request Sep 19, 2024
**Context:** 
After [Pennylane support numpy
2.0](PennyLaneAI/pennylane#6061), catalyst needs
to add this support.

**Description of the Change:**
1. Bump pennylane version to include the numpy 2.0 support
2. Since programs built using numpy 1.x would not readily work with
numpy 2.0, we upgraded reuirements.txt to install numpy2.0. The reason
is that wrapper.cpp uses the numpy C-API that should be built using
numpy 2.x.
3. pyblind11 dependency was upgraded to 2.12.0 which adds support to
numpy 2.0
4. scipy dependency includes 1.13 which adds support for numpy 2.0
5. removed optax dependancy from requirements (it was a TODO and
unrelated to numpy but would cause CI delay)

**Benefits:**
Adds support for numpy 2.0

**Related GitHub Issues:**

[sc-61401]
PietropaoloFrisoni added a commit that referenced this pull request Sep 20, 2024
**Context:** In #6061, we started using by default NumPy 2 (instead of
NumPy 1) on the CI to test every PR targeting the master branch. Some
jobs (for example, those requiring `tensorflow`) still automatically
downgrade to NumPy 1.26 since not all interfaces are supported at the
current stage.

This PR aims to create another workflow to test some jobs with NumPy
1.26 (those that don't automatically downgrade to NumPy 1 during the
day). Such a workflow should run at night to ensure PennyLane is still
compatible with NumPy 1.26.

**We'll keep this workflow only temporarily** (probably a quarter, but
this still has to be established). After that, we'll get rid of it.

Finally, It has been verified (see
[here](https://github.com/PennyLaneAI/pennylane/actions/runs/10927439038))
that the tests introduced with this PR are green. This has been verified
a second time (see
[here](https://github.com/PennyLaneAI/pennylane/actions/runs/10946347640)
before merging this PR into master).

**Description of the Change:** As above.

**Benefits:** We ensure that PennyLane is still compatible with NumPy
1.26.

**Possible Drawbacks:** 

- This issue was discovered accidentally (and potentially fixed) during
this PR. The `TestSinglePrecision` class in the `test_jax_jit_qnode.py`
file contains tests that change the JAX precision with
`jax.config.update("jax_enable_x64", False)`. When running the jax tests
using multiple workers, we can face race conditions if some workers
change jax configuration, because other workers in other test classes
downgrade to a single precision causing numerous failures. To avoid
this, we add
[--dist=loadscope](https://pytest-xdist.readthedocs.io/en/latest/distribution.html)
as additional pytest arg for jax tests, grouping tests by module for
test functions and by class for test methods.

- The new workflow is it is to a large extent a (very simplified) copy
of `interface-unit-tests.yml`. This is against the principle of avoiding
code duplication whenever possible. Still, this is just a temporary
workflow that lasts for a few months at most (and runs only at night),
and it may be convenient to disentangle it as much as possible from the
usual test interface.

**Related GitHub Issues:** None.

**Related Shortcut Stories:** [sc-73744]
mudit2812 pushed a commit that referenced this pull request Sep 23, 2024
**Context:**

We want to make Pennylane compatible with Numpy 2.0. After several
discussions, we decided to test NumPy 2.0 on the CI by default in every
PR (testing both Python versions would have been to slow). Some jobs
still downgrade automatically to Numpy 1.x, since some interfaces (such
as Tensorflow) still do not support NumPy 2.0.

**Description of the Change:**

We can distinguish the changes into 3 main categories:

*Changes to workflows*

- None in the final version

*Changes to requirements and setup files*

- Unpin the Numpy version in `setup.py` (now we also allow Numpy 2.0).
- Update `requirements-ci.txt` to include Scipy 1.13 (this adds support
for Numpy 2.0).
- Pin Numpy in `requirements-ci.txt` to 2.0. 

*Changes to the source code*
- Change `np.NaN` to `np.nan`.
- Use legacy printing representation in tests, contrary to the new numpy
representation of scalars, e.g. np.float64(3.0) rather than just 3.0.
- Update probabilities warning to be case insensitive and check for a
partial match, since this warning was changed in Numpy 2.0.
- Check the datatype of np.exp from the Global phase only for Numpy 1.x,
since this gets promoted to complex128 in Numpy 2.x.
https://numpy.org/neps/nep-0050-scalar-promotion.html#schema-of-the-new-proposed-promotion-rules.

**Benefits:**

Make Pennylane compatible with Numpy 2.0.

**Possible Drawbacks:**

- We need to create a separate workflow to keep testing PennyLane with
NumPy 1.x, since we still want to maintain compatibility with previous
NumPy versions. This will be done in a separate PR.

- We are not testing Numpy 2.x for the interfaces that implicitly
require Numpy 1.x. These currently seem to be `tensorflow` and
`openfermionpyscf` (notice that `tensorflow` is required in some code
sections like qcut). In particular, `openfermionpyscf` causes an error:
```
AttributeError: np.string_ was removed in the NumPy 2.0 release. Use np.bytes_ instead.
``` 
in the qchem tests. The attribute `np.string_` is not used in the PL
source code, so it is a problem with the package itself.

[sc-61399]
[sc-66548]

---------

Co-authored-by: PietropaoloFrisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Pietropaolo Frisoni <pietropfrisoni@gmail.com>
mudit2812 pushed a commit that referenced this pull request Sep 23, 2024
**Context:** As part of the effort to make PL compatible with Numpy 2.0
(see #6061), we need to upgrade JAX to 0.4.26+ since such a version
introduced the support for Numpy 2.0. We opted for JAX 0.4.28 since it
is the same version used by Catalyst.

**Description of the Change:** As above.

**Benefits:** PL is compatible with Numpy 2.0 and Jax 0.4.28.

**Possible Drawbacks:** 

- From JAX 0.4.27, in `jax.jit`, passing invalid static_argnums or
static_argnames now leads to an error rather than a warning. In PL, this
breaks every test where we set `shots` in the `QNode` call with
`static_argnames=["shots"]`. At this stage, we decided to mark such
tests with `pytest.xfail` to allow the upgrade.

**Related GitHub Issues:** None.

**Related Shortcut Stories**: [sc-61389]

---------

Co-authored-by: dwierichs <david.wierichs@xanadu.ai>
mudit2812 pushed a commit that referenced this pull request Sep 23, 2024
**Context:** In #6061, we started using by default NumPy 2 (instead of
NumPy 1) on the CI to test every PR targeting the master branch. Some
jobs (for example, those requiring `tensorflow`) still automatically
downgrade to NumPy 1.26 since not all interfaces are supported at the
current stage.

This PR aims to create another workflow to test some jobs with NumPy
1.26 (those that don't automatically downgrade to NumPy 1 during the
day). Such a workflow should run at night to ensure PennyLane is still
compatible with NumPy 1.26.

**We'll keep this workflow only temporarily** (probably a quarter, but
this still has to be established). After that, we'll get rid of it.

Finally, It has been verified (see
[here](https://github.com/PennyLaneAI/pennylane/actions/runs/10927439038))
that the tests introduced with this PR are green. This has been verified
a second time (see
[here](https://github.com/PennyLaneAI/pennylane/actions/runs/10946347640)
before merging this PR into master).

**Description of the Change:** As above.

**Benefits:** We ensure that PennyLane is still compatible with NumPy
1.26.

**Possible Drawbacks:**

- This issue was discovered accidentally (and potentially fixed) during
this PR. The `TestSinglePrecision` class in the `test_jax_jit_qnode.py`
file contains tests that change the JAX precision with
`jax.config.update("jax_enable_x64", False)`. When running the jax tests
using multiple workers, we can face race conditions if some workers
change jax configuration, because other workers in other test classes
downgrade to a single precision causing numerous failures. To avoid
this, we add
[--dist=loadscope](https://pytest-xdist.readthedocs.io/en/latest/distribution.html)
as additional pytest arg for jax tests, grouping tests by module for
test functions and by class for test methods.

- The new workflow is it is to a large extent a (very simplified) copy
of `interface-unit-tests.yml`. This is against the principle of avoiding
code duplication whenever possible. Still, this is just a temporary
workflow that lasts for a few months at most (and runs only at night),
and it may be convenient to disentangle it as much as possible from the
usual test interface.

**Related GitHub Issues:** None.

**Related Shortcut Stories:** [sc-73744]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants