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

separate DensityMatrixMP into its own measurement process #4558

Merged
merged 6 commits into from Sep 1, 2023

Conversation

timmysilv
Copy link
Contributor

@timmysilv timmysilv commented Aug 30, 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.

@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (929e7b8) 99.65% compared to head (cef6da4) 99.65%.

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           
Files Changed Coverage Δ
pennylane/measurements/__init__.py 100.00% <100.00%> (ø)
pennylane/measurements/state.py 100.00% <100.00%> (ø)

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

@timmysilv timmysilv marked this pull request as ready for review August 31, 2023 18:05
@timmysilv timmysilv requested a review from a team August 31, 2023 18:10
Copy link
Contributor

@rmoyard rmoyard left a 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

pennylane/measurements/state.py Show resolved Hide resolved
@timmysilv timmysilv enabled auto-merge (squash) September 1, 2023 15:36
@timmysilv timmysilv merged commit 0843183 into master Sep 1, 2023
39 checks passed
@timmysilv timmysilv deleted the split-state-and-dm branch September 1, 2023 16:15
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants