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

Allow Identity() to have no wires #5106

Merged
merged 23 commits into from Feb 1, 2024
Merged

Allow Identity() to have no wires #5106

merged 23 commits into from Feb 1, 2024

Conversation

Qottmann
Copy link
Contributor

@Qottmann Qottmann commented Jan 25, 2024

Allows Identity() to have no wires, similar to e.g. GlobalPhase

Also updating PauliWord.operation() to return qml.Identity() and PauliSentence.operation() to return qml.s_prod(0, qml.Identity()) in case of empty word and sentence, respectively.

As a temporary fix, we explicitly dont allow expvals of Identity(), but plan to fix this by updating hamiltonian_expand and sum_expand immediately after.

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.

@Qottmann
Copy link
Contributor Author

[sc-55334]

Copy link

codecov bot commented Jan 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bfd0352) 99.69% compared to head (d95f03b) 99.68%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5106      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         394      394              
  Lines       35979    35711     -268     
==========================================
- Hits        35868    35599     -269     
- Misses        111      112       +1     

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

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.

Mind adding some integration tests with "default.qubit"? We might also want to make an associated test to the lightning repo adding an integration test for support for qml.Identity().

@Qottmann
Copy link
Contributor Author

For the moment, we don't allow measuring qml.Identity() as there are some non-trivial concerns for who should handle the case (pennylane or the device). Trying to measure qml.Identity() on default qubit raises ValueError: Cannot set an empty list of wires.. However, the lightning error is cryptic and needs to be improved: ValueError: shapes (2,) and (1,) not aligned: 2 (dim 0) != 1 (dim 0)

Copy link
Contributor

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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

What will the repr look like for qml.Identity()?

@Qottmann
Copy link
Contributor Author

Qottmann commented Jan 30, 2024

Together with #5116 it would be

>>> qml.Identity()
I()

as that would also be the copy-and-paste-able version

(see changes in pennylane/ops/identity.py in #5116)

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.

This may require some follow up work, both with hamiltonian_expand, and sum_expand and unknown unknowns, but I definitely think this will help in working in working with pauli sentences.

Copy link
Contributor

@timmysilv timmysilv 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! left an optional comment, but otherwise good to go

pennylane/pauli/pauli_arithmetic.py Outdated Show resolved Hide resolved
@Qottmann Qottmann enabled auto-merge (squash) February 1, 2024 16:56
@Qottmann Qottmann merged commit 5b1abb3 into master Feb 1, 2024
35 checks passed
@Qottmann Qottmann deleted the identitynowires branch February 1, 2024 17:13
@@ -153,6 +153,9 @@
and the codecov check itself would never execute.
[(#5101)](https://github.com/PennyLaneAI/pennylane/pull/5101)

* `qml.Identity()` can be initialized without wires. Measuring it is currently not possible though.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does that include something like qml.expval(qml.PauliX(0) @ qml.Identity()) or just strictly qml.expval(qml.Identity())?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, this case is in fact not handled. Though it should also not appear accidentally since Identities are stripped from PauliWords upon initialization:

>>> pw = PauliWord({0:"X", 1:"I"})
>>> pw
X(0)
>>> pw.operation()
PauliX(wires=[0])

It could occur with still if a user specifically tries to compute qml.expval(qml.PauliX(0) @ qml.Identity()) though 😬 Luckily, a fix to handle empty-wire Identities is under way already anyway.

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.

None yet

4 participants