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

Refactor _evaluate_gradient for param_shift #5666

Merged
merged 35 commits into from
May 24, 2024

Conversation

dwierichs
Copy link
Contributor

@dwierichs dwierichs commented May 7, 2024

Context:
param_shift uses the internal method _evaluate_gradient, which mostly consists of logic to take the contraction of tape execution results with the parameter-shift rule coefficients and map it over tuple axes. It also needs to respect batching of execution results if broadcast=True is used in param_shift.

Description of the Change:
This PR cleans up _evaluate_gradient, extends it to multi-measurement and shot vector scenarios when broadcasting is used, and recycles helper methods from gradient_transform.py to reduce the code.
We also add unit tests for this method, allowing to reduce integration test count in the future.

Benefits:
Prepare _evaluate_gradient for multi-measurement and shot vector support with broadcast=True.
Improve testing and code quality.

Possible Drawbacks:

Related GitHub Issues:
prepares a bug fix for #5598

[sc-62283]

mudit2812 and others added 22 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
Copy link

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (85ff62b) to head (9304392).
Report is 258 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5666      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         416      416              
  Lines       39055    38757     -298     
==========================================
- Hits        38931    38632     -299     
- Misses        124      125       +1     

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

@dwierichs dwierichs added the review-ready 👌 PRs which are ready for review by someone from the core team. label May 13, 2024
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.

Very thorough with all the scenarios, LGTM!

@albi3ro albi3ro self-requested a review May 16, 2024 17:36
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

A little difficult to keep track of what things should be what shapes where, but I think that's just a byproduct of our choice of result specification.

At the end of the day, tests pass and all individual chunks make sense.

@dwierichs dwierichs enabled auto-merge (squash) May 22, 2024 21:34
@dwierichs dwierichs added merge-ready ✔️ All tests pass and the PR is ready to be merged. and removed review-ready 👌 PRs which are ready for review by someone from the core team. labels May 23, 2024
@dwierichs dwierichs merged commit 58668d7 into master May 24, 2024
38 checks passed
@dwierichs dwierichs deleted the refactor-evaluate_gradient branch May 24, 2024 10:59
dwierichs added a commit that referenced this pull request Jun 14, 2024
…oadcast=True)` (#5667)

**Context:**
Shot vectors and multiple measurements are currently explicitly not
supported when using `param_shift` with the `broadcast=True` option.

In #5666 , a crucial subroutine `_evaluat_gradient` is being prepared to
allow for support of these scenarios.

**Description of the Change:**
Allow for shot vectors and/or multiple measurements in `param_shift(...,
broadcast=true)`.
Due to the changes in #5666 including unit tests for the relevant
subroutines, this PR mostly is concerned with integration tests.

**Benefits:**
Feature parity with `broadcast=False`, allowing faster gradient
evaluation.

**Possible Drawbacks:**

**Related GitHub Issues:**
fixes #5598 

[sc-62283]

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-ready ✔️ All tests pass and the PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants