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

Fix transforms that error with non-commuting measurements #5424

Merged
merged 33 commits into from
May 7, 2024

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Mar 20, 2024

Context:

tape.expand will error out if the tape has any non-commuting measurements present. But multiple transforms that don't care about measurements at all use it to decompose to a target gate set. Even when expand_measurements=False, tape.expand requires measurements to be commuting, or composite observables. And this is a core behavior of tape.expand that people might rely on. It's not as much a bug as a problematic feature. AKA load-bearing bug.

Fortunately, we now have qml.devices.preprocess.decompose, which is a transform that is capable of decomposing to a target gateset. While yes, this transform is in the devices module, so using it is introducing a cross-module dependency in a direction that we do not want, it gets the job done. I would call this a sign that we should potentially consider moving it into the transforms module and making it's import path more accessible.

Description of the Change:

Use decompose instead of tape.expand in a variety of transforms.

Benefits:

These transforms now allow non-commuting measurements through.

Possible Drawbacks:

We still have other transforms still relying on tape.expand. But we can investigate those another day.

Introducing a cross-module dependency in a direction we don't want a dependency. This could be fixed by moving decompose to the transforms module, but that is a problem for another day.

Related GitHub Issues:

Fixes #5401 Fixes #5316 [sc-58086] [sc-59118]

@trbromley trbromley modified the milestone: v0.36 Apr 19, 2024
mudit2812 and others added 5 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>
albi3ro and others added 4 commits April 30, 2024 11:38
- [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.
@albi3ro albi3ro changed the base branch from master to v0.36.0-rc0 April 30, 2024 19:20
Copy link

codecov bot commented Apr 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.68%. Comparing base (7e51d2b) to head (5c64d10).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5424      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         412      412              
  Lines       38641    38385     -256     
==========================================
- Hits        38523    38264     -259     
- Misses        118      121       +3     

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

@albi3ro albi3ro changed the base branch from v0.36.0-rc0 to master May 2, 2024 18:55
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.

Had some picky questions and a few typo/copy-paste-related suggestions.

pennylane/devices/preprocess.py Outdated Show resolved Hide resolved
pennylane/devices/preprocess.py Outdated Show resolved Hide resolved
pennylane/devices/preprocess.py Outdated Show resolved Hide resolved
pennylane/devices/preprocess.py Show resolved Hide resolved
pennylane/gradients/finite_difference.py Outdated Show resolved Hide resolved
pennylane/gradients/finite_difference.py Show resolved Hide resolved
pennylane/gradients/parameter_shift.py Show resolved Hide resolved
pennylane/ops/op_math/pow.py Outdated Show resolved Hide resolved
tests/gradients/finite_diff/test_finite_difference.py Outdated Show resolved Hide resolved
tests/transforms/test_transpile.py Show resolved Hide resolved
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Copy link
Contributor

@lillian542 lillian542 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 to me!

pennylane/transforms/compile.py Show resolved Hide resolved
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.

Nice upgrade using existing code @albi3ro, thanks!

I like @lillian542's idea to modify the error message, otherwise good to go for me!

@albi3ro albi3ro enabled auto-merge (squash) May 7, 2024 15:47
@albi3ro albi3ro merged commit cfcef86 into master May 7, 2024
38 checks passed
@albi3ro albi3ro deleted the non-commuting-expanding-fixes branch May 7, 2024 16:14
mudit2812 added a commit that referenced this pull request May 8, 2024
**Context:**

`tape.expand` will error out if the tape has any non-commuting
measurements present. But multiple transforms that don't care about
measurements at all use it to decompose to a target gate set. Even when
`expand_measurements=False`, `tape.expand` requires measurements to be
commuting, or composite observables. And this is a core behavior of
`tape.expand` that people might rely on. It's not as much a bug as a
problematic feature. AKA load-bearing bug.

Fortunately, we now have `qml.devices.preprocess.decompose`, which is a
transform that is capable of decomposing to a target gateset. While yes,
this transform is in the `devices` module, so using it is introducing a
cross-module dependency in a direction that we do not want, it gets the
job done. I would call this a sign that we should potentially consider
moving it into the `transforms` module and making it's import path more
accessible.

**Description of the Change:**

Use `decompose` instead of `tape.expand` in a variety of transforms.

**Benefits:**

These transforms now allow non-commuting measurements through.

**Possible Drawbacks:**

We still have other transforms still relying on `tape.expand`. But we
can investigate those another day.

Introducing a cross-module dependency in a direction we don't want a
dependency. This could be fixed by moving `decompose` to the transforms
module, but that is a problem for another day.

**Related GitHub Issues:**

Fixes #5401 Fixes #5316 [sc-58086]  [sc-59118]

---------

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: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
dwierichs added a commit that referenced this pull request May 14, 2024
**Context:**
The first example in the docs of `qml.transforms.insert` was reported to
not be working
[here](https://discuss.pennylane.ai/t/qml-device-error-with-qiskit-aer/4556/9).
Other examples also do not work.

**Description of the Change:**
This PR changes the docs examples to work.

It also replicates the change in #5424 to use
`devices.preprocess.decompose` instead of `tape.expand`, adding support
for non-commuting measurements (which is unrelated to the transform
anyways), and removing the need for a custom error to be raised.

**Benefits:**
Docs examples work.
Non-commuting measurements support.

**Possible Drawbacks:**
Similar to #5424, this introduces cross-dependency to the devices
module.

**Related GitHub Issues:**
Shiro-Raven pushed a commit that referenced this pull request May 15, 2024
**Context:**
The first example in the docs of `qml.transforms.insert` was reported to
not be working
[here](https://discuss.pennylane.ai/t/qml-device-error-with-qiskit-aer/4556/9).
Other examples also do not work.

**Description of the Change:**
This PR changes the docs examples to work.

It also replicates the change in #5424 to use
`devices.preprocess.decompose` instead of `tape.expand`, adding support
for non-commuting measurements (which is unrelated to the transform
anyways), and removing the need for a custom error to be raised.

**Benefits:**
Docs examples work.
Non-commuting measurements support.

**Possible Drawbacks:**
Similar to #5424, this introduces cross-dependency to the devices
module.

**Related GitHub Issues:**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants