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

Eigvals are floats #5607

Merged
merged 36 commits into from
May 17, 2024
Merged

Eigvals are floats #5607

merged 36 commits into from
May 17, 2024

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Apr 30, 2024

Context:
Most eigvals are floats, but specifically for X, Y, Z and Hadamard, they have been (1, -1). This can make trouble - for example, it caused an issue with JAX-JIT, because it doesn't know what dtype to anticipate coming from a sampled observable.

Description of the Change:
Main change here is changing eigvals to (1.0, -1.0) so that all observables have the same dtype for eigenvalues.
There is still some special handling for X, Y, Z and Hadamard, because it makes things JIT compatible for those operators, but it now pulls from the op.eigvals() property and returns floats.

Benefits:

  • Sampling of observables is more consistent in how it is processed and what dtype is returned
  • JAX-JIT with samples and an observable works now, see linked issue below

Possible Drawbacks:
If people were doing checks for int, or otherwise assuming they would get integers when returning fx. qml.sample(qml.X(0)), this will be a breaking change for them.

Related GitHub Issues:
#5369

mudit2812 and others added 10 commits April 29, 2024 16:01
As name says.

---------

Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
**Context:**
Some newly added test cases failing with legacy opmath

**Description of the Change:**
Skip these tests if new opmath is disabled:
`test_measure_identity_no_wires` in
`tests/devices/qubit/test_measure.py`
`test_identity_on_no_wires_with_other_observables` in
`tests/devices/qubit/test_sampling.py`
`test_observables_on_no_wires_coeffs` in
`tests/pauli/grouping/test_pauli_group_observables.py`
`test_multiple_hamiltonian_expansion_finite_shots` in
`tests/test_qnode_legacy.py`

**Related Shortcut Issues:**
[sc-62144]
[sc-62143]
**Context:**
1. `Hamiltonian` does not remove ops from queue if `simplify=True`
2. `TrotterProduct` does not remove `Hamiltonian`

**Description of the Change:**
Manually dequeue said ops

**Related Shortcut Issues:**
[sc-62142]

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
      test directory!

- [x] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
      render correctly by running `make docs`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `doc/releases/changelog-dev.md` file,
summarizing the
      change, and including a link back to the PR.

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      To lint modified files, simply `pip install pylint`, and then
      run `pylint pennylane/path/to/file.py`.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


------------------------------------------------------------------------------------------------------------

**Context:**
`qml.counts` does not return the same `keys` with `dynamic_one_shot` and
`defer_measurements`.

**Description of the Change:**
Cast `keys` to the type produced by `concretize`.

**Benefits:**
Consistent `keys`.

**Possible Drawbacks:**
Slight (likely unnoticeable) performance decrease since `NDArray` object
cannot be preallocated.

**Related GitHub Issues:**
#5566

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
- [x] Fix last open bug or open issue


**Context:**
Docs get out of sync over time.
In particular, the output shape of gradient transforms applied to QNodes
was changed in #4945 but not updated in the docs.

**Description of the Change:**
Update examples in docs of `qml.gradients` module.

Also updates the `qml.kernels` docs, with a few very small changes.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
2 very minor corrections to documentation added in this release
**Context:**
Fixes latest failures in the legacy opmath tests in the plugin test
matrix.

**Description of the Change:**
1. `convert_to_opmath` now dequeues the original op.
2. Skip test with `I()` when new opmath is disabled.
@lillian542 lillian542 marked this pull request as ready for review April 30, 2024 20:48
@lillian542 lillian542 changed the title Eigvals are floats [WIP] Eigvals are floats Apr 30, 2024
Copy link
Contributor

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.

Copy link

codecov bot commented May 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (354088c) to head (0b6f2df).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5607      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         415      415              
  Lines       38888    38594     -294     
==========================================
- Hits        38764    38469     -295     
- Misses        124      125       +1     

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

@lillian542 lillian542 changed the title [WIP] Eigvals are floats Eigvals are floats May 1, 2024
@dwierichs dwierichs self-requested a review May 2, 2024 14:54
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Looks great, this change makes a lot of sense! 💯
just some minor comments from my side.

doc/releases/changelog-0.36.0.md Outdated Show resolved Hide resolved
pennylane/_qubit_device.py Outdated Show resolved Hide resolved
pennylane/_qubit_device.py Show resolved Hide resolved
pennylane/_qubit_device.py Outdated Show resolved Hide resolved
pennylane/measurements/sample.py Show resolved Hide resolved
tests/interfaces/test_torch.py Show resolved Hide resolved
tests/measurements/test_sample.py Outdated Show resolved Hide resolved
tests/measurements/test_sample.py Outdated Show resolved Hide resolved
tests/tape/test_qscript.py Outdated Show resolved Hide resolved
doc/releases/changelog-0.36.0.md Outdated Show resolved Hide resolved
@lillian542 lillian542 requested a review from trbromley May 2, 2024 15:14
@lillian542 lillian542 changed the base branch from v0.36.0-rc0 to master May 2, 2024 18:46
@lillian542 lillian542 changed the title Eigvals are floats [WIP] Eigvals are floats May 2, 2024
pennylane/_qubit_device.py Outdated Show resolved Hide resolved
@lillian542 lillian542 requested a review from dwierichs May 2, 2024 19:54
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thanks @lillian542!
Approving conditioned on the (minor) change of the test docstring that references (then) removed code.

@lillian542
Copy link
Contributor Author

[sc-58742]

lillian542 and others added 3 commits May 17, 2024 11:04
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
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.

Nice work @lillian542
Just 2 small questions from my side.

Also, this shouldn't cause any issues with the other frameworks, right? If it is worth a check, can you run the Torch GPU tests too, just to verify the dtype changes are all good there?

pennylane/_qubit_device.py Show resolved Hide resolved
tests/measurements/test_sample.py Show resolved Hide resolved
@lillian542 lillian542 added the gpu label May 17, 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.

💯

@lillian542 lillian542 enabled auto-merge (squash) May 17, 2024 21:18
@lillian542 lillian542 merged commit 3543271 into master May 17, 2024
40 checks passed
@lillian542 lillian542 deleted the eigvals-are-floats branch May 17, 2024 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants