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
separate DensityMatrixMP into its own measurement process #4558
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #4558 +/- ##
=======================================
Coverage 99.65% 99.65%
=======================================
Files 376 376
Lines 33104 33108 +4
=======================================
+ Hits 32991 32995 +4
Misses 113 113
☔ View full report in Codecov by Sentry. |
albi3ro
reviewed
Aug 31, 2023
mudit2812
approved these changes
Sep 1, 2023
rmoyard
approved these changes
Sep 1, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it is a nice one! Just one comment
mudit2812
pushed a commit
that referenced
this pull request
Sep 8, 2023
**Context:** Today, `qml.state()` returns a `StateMP` with no wires set, while `qml.density_matrix()` returns a `StateMP` with the wires of the subsystem being traced out. The new device implementation would benefit greatly from being able to assign wires to a state measurement, and also it just makes sense for these to be separate things. **Description of the Change:** Adds a new `DensityMatrixMP` that inherits from `StateMP` (not the parent `StateMeasurement` so that `isinstance(mp, StateMP)` still returns True for density matrix measurements). I literally copy-pasted the `StateMP` class, then deleted any density-matrix-related things from the `StateMP` class, and deleted and state-related things from the `DensityMatrixMP` class. **Benefits:** - Density matrix is its own measurement process! - We can assign wires to a `StateMP` without it thinking it represents a density matrix. **Possible Drawbacks:** This can also be viewed as a benefit (see above, no code changes outside module needed), but `isinstance(my_density_mp, StateMP)` returns true. If you want to know that your measurement _is_ a state measurement, you can do `type(mp) is StateMP`. **UPDATE for reviewers**: Responding to a comment, I've changed `StateMP` to not accept `wires` in its init, so now `DensityMatrixMP` directly calls `StateMeasurement.__init__(self, wires=wires, id=id)` to skip its parent (StateMP) which would now fail if given wires. I call it directly because this is the most pythonic way to call a grandparent's method (the other option being `super(StateMP, self).__init__` but I think `StateMeasurement` is a more reliable and responsible choice than "whoever StateMP's parent is"). Plus, I'll be removing that change when StateMP supports wires.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context:
Today,
qml.state()
returns aStateMP
with no wires set, whileqml.density_matrix()
returns aStateMP
with the wires of the subsystem being traced out. The new device implementation would benefit greatly from being able to assign wires to a state measurement, and also it just makes sense for these to be separate things.Description of the Change:
Adds a new
DensityMatrixMP
that inherits fromStateMP
(not the parentStateMeasurement
so thatisinstance(mp, StateMP)
still returns True for density matrix measurements).I literally copy-pasted the
StateMP
class, then deleted any density-matrix-related things from theStateMP
class, and deleted and state-related things from theDensityMatrixMP
class.Benefits:
StateMP
without it thinking it represents a density matrix.Possible Drawbacks:
This can also be viewed as a benefit (see above, no code changes outside module needed), but
isinstance(my_density_mp, StateMP)
returns true. If you want to know that your measurement is a state measurement, you can dotype(mp) is StateMP
.UPDATE for reviewers:
Responding to a comment, I've changed
StateMP
to not acceptwires
in its init, so nowDensityMatrixMP
directly callsStateMeasurement.__init__(self, wires=wires, id=id)
to skip its parent (StateMP) which would now fail if given wires. I call it directly because this is the most pythonic way to call a grandparent's method (the other option beingsuper(StateMP, self).__init__
but I thinkStateMeasurement
is a more reliable and responsible choice than "whoever StateMP's parent is"). Plus, I'll be removing that change when StateMP supports wires.