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

Improved handling for Identities on no wires #5570

Merged
merged 11 commits into from
Apr 25, 2024
Merged

Improved handling for Identities on no wires #5570

merged 11 commits into from
Apr 25, 2024

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Apr 24, 2024

Context:

While we have added the ability to have qml.I() on no wires to better reflect the wire-independent nature of the identity and create operations again from a simplified form, we don't necessarily have extensive support and coverage for this new style of observable yet.

Description of the Change:

This PR extends support for qml.I() for devices.qubit measurement pathways measure and measure_with_samples. It also allows us to calculate the eigenvalues for composite ops that contain an operator on no wires. And finally, it adds support for observables with no wires to qml.pauli.group_observables.

Throughout, I assume an observable with no wires commutes with everything, as qml.I does in fact commute with everything.

Benefits:

Possible Drawbacks:

Related GitHub Issues:

[sc-61509] Fixes #5532

@Qottmann
Copy link
Contributor

Awesome! Seems to fix my issues with qnodes that compute expectation values with CompositeOps that contain I() instances 👌

@qml.qnode(qml.device("default.qubit"))
def qnode():
    return qml.expval(I() + Z(0))

qnode()

@albi3ro albi3ro marked this pull request as ready for review April 24, 2024 18:21
Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

Awesome, thanks so much @albi3ro

Just missing one integration test with a qnode perhaps? Otherwise happy to approve!

@albi3ro albi3ro requested a review from Qottmann April 24, 2024 19:05
Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

One minor point about straight I() not being allowed to be measured

pennylane/utils.py Show resolved Hide resolved
pennylane/measurements/expval.py Outdated Show resolved Hide resolved
Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

Great PR, thanks so much for the fix @albi3ro ! 🥂

My main concern of Hamiltonians/Sums containing I() being able to be handled is solved with this 👍

Would be good to also be able to directly measure I() as well, but for the moment this will do!

pennylane/pauli/grouping/group_observables.py Outdated Show resolved Hide resolved
@trbromley trbromley added this to the v0.36 milestone Apr 25, 2024
Copy link

codecov bot commented Apr 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.68%. Comparing base (9151477) to head (8247bc9).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5570      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         410      411       +1     
  Lines       38278    38030     -248     
==========================================
- Hits        38161    37912     -249     
- Misses        117      118       +1     

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

@albi3ro albi3ro enabled auto-merge (squash) April 25, 2024 15:27
@albi3ro albi3ro added the merge-ready ✔️ All tests pass and the PR is ready to be merged. label Apr 25, 2024
@albi3ro albi3ro merged commit 0dd1617 into master Apr 25, 2024
39 checks passed
@albi3ro albi3ro deleted the no-wires-I-fixes branch April 25, 2024 15:56
astralcai added a commit that referenced this pull request Apr 26, 2024
**Context:**
In the latest run of legacy opmath tests:
https://github.com/PennyLaneAI/pennylane/actions/runs/8837734136/job/24267397281,
a newly added device test from
#5570 is failing because
`expval` of `Hamiltonian` containing `Identity` with no wires is not
supported.

**Description of the Change:**
Update the test case to explicitly use `qml.sum` instead of the `+`
operator.

**Benefits:**
Fixes failure in legacy opmath tests.

**Possible Drawbacks:**
`Hamiltonian` with `I()` is still not supported.
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.

[BUG] Cannot run qnode with sum that contains I() (Identity with empty wires)
5 participants