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

Support joint measurement statistics for mid-circuit measurements #4888

Merged
merged 32 commits into from
Dec 13, 2023

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Nov 27, 2023

Context:
Adding support for advanced statistics for mid-circuit measurements. Users can now collect statistics on multiple measurement values. This can be done in two ways:

  1. Using arithmetic operators to collect measurement values into a composite value. Eg: m0 * m1 - 2 * m3 or m1 > m0 or ~m0 & m1, etc. Available for qml.expval, qml.var, qml.sample, qml.counts.
  2. Putting values in a list. This will give wire-like results. Eg: [m0, m1, m2]. Available for qml.sample, qml.probs, qml.counts.

Composite vs single MeasurementValues:
Composite MeasurementValues refers to MeasurementValues created by applying arithmetic/boolean operators to MeasurementValues as shown in the first point above. A single MeasurementValue is the output of qml.measure. Applying arithmetic/boolean operators to MeasurementValues also returns MeasurementValues.

Description of the Change:

  • Add __repr__ to MeasurementValue and update MeasurementProcess.__repr__ appropriately.
  • Update MeasurementProcess.eigvals to return outcomes of composed MeasurementValues when in use. If a list of MeasurementValues is given, the eigenvalues are just range(2**num_wires).
  • Update QubitDevice to use the correct eigenvalues for composite MeasurementValues, and treat a list of MeasurementValues as wires.
  • Update qml.expval and qml.var to disallow using lists of MeasurementValues, and allow composite MeasurementValue`s.
  • Update qml.sample and qml.counts to allow both lists of MeasurementValues as well as composite MeasurementValues.
  • Update qml.defer_measurements to disallow using qml.probs with composite MeasurementValues. This is done because the wire order of the composite value is not deterministic as the sorting is determined by the ID of the mid-circuit measurements, which are generated randomly. There isn't really a way to go around this at the moment, so I just blocked users from being able to do it. Besides, the results should be the same as when they use a list of MeasurementValues.

TODO: Add docs.

Benefits:

  • Advanced mid-circuit statistics are now supported! And in multiple ways 😄

Possible Drawbacks:

Related GitHub Issues:

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.

Copy link

codecov bot commented Nov 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f435f87) 99.66% compared to head (16f5fac) 99.65%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4888      +/-   ##
==========================================
- Coverage   99.66%   99.65%   -0.02%     
==========================================
  Files         388      388              
  Lines       35379    35158     -221     
==========================================
- Hits        35262    35038     -224     
- Misses        117      120       +3     

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

@mudit2812
Copy link
Contributor Author

[sc-48303]

@mudit2812 mudit2812 marked this pull request as ready for review December 7, 2023 15:52
@mudit2812 mudit2812 requested review from a team and removed request for a team December 7, 2023 16:22
@mudit2812 mudit2812 requested review from a team and trbromley December 7, 2023 20:47
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
@trbromley trbromley mentioned this pull request Dec 7, 2023
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, super comprehensive testing! got a few questions first but very nice 😎

@mudit2812 mudit2812 requested a review from timmysilv December 11, 2023 18:42
@albi3ro albi3ro self-requested a review December 11, 2023 19:35
@mudit2812 mudit2812 requested a review from albi3ro December 11, 2023 22:31
@mudit2812
Copy link
Contributor Author

mudit2812 commented Dec 12, 2023

Smol breaking change: qml.counts with a single MeasurementValue used to return strings for the keys, but now it returns integers. This is because of potentially having to process measurement values if unary operators are applied to it (eg. ~m0). I'm not very worried about this though since mid-circuit measurement support for qml.counts has only been out for 1 release. Thanks @timmysilv for catching this.

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.

🎉

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.

Impressive amount of work and an exciting new feature!

@mudit2812 mudit2812 enabled auto-merge (squash) December 13, 2023 20:17
@mudit2812 mudit2812 merged commit 687ed22 into master Dec 13, 2023
@mudit2812 mudit2812 deleted the mcm-measurements branch December 13, 2023 20:32
mudit2812 added a commit that referenced this pull request Dec 15, 2023
)

**Context:**
Adding support for advanced statistics for mid-circuit measurements.
Users can now collect statistics on multiple measurement values. This
can be done in two ways:
1. Using arithmetic operators to collect measurement values into a
composite value. Eg: `m0 * m1 - 2 * m3` or `m1 > m0` or `~m0 & m1`, etc.
Available for `qml.expval`, `qml.var`, `qml.sample`, `qml.counts`.
2. Putting values in a list. This will give wire-like results. Eg: `[m0,
m1, m2]`. Available for `qml.sample`, `qml.probs`, `qml.counts`.

**Composite vs single `MeasurementValue`s**:
Composite `MeasurementValue`s refers to `MeasurementValue`s created by
applying arithmetic/boolean operators to `MeasurementValue`s as shown in
the first point above. A single `MeasurementValue` is the output of
`qml.measure`. Applying arithmetic/boolean operators to
`MeasurementValue`s also returns `MeasurementValue`s.

**Description of the Change:**
* Add `__repr__` to `MeasurementValue` and update
`MeasurementProcess.__repr__` appropriately.
* Update `MeasurementProcess.eigvals` to return outcomes of composed
`MeasurementValue`s when in use. If a list of `MeasurementValue`s is
given, the eigenvalues are just `range(2**num_wires)`.
* Update `QubitDevice` to use the correct eigenvalues for composite
`MeasurementValue`s, and treat a list of `MeasurementValue`s as wires.
* Update `qml.expval` and `qml.var` to disallow using lists of
`MeasurementValue`s, and allow composite MeasurementValue`s.
* Update `qml.sample` and `qml.counts` to allow both lists of
`MeasurementValue`s as well as composite `MeasurementValue`s.
* Update `qml.defer_measurements` to disallow using `qml.probs` with
composite `MeasurementValue`s. This is done because the wire order of
the composite value is not deterministic as the sorting is determined by
the ID of the mid-circuit measurements, which are generated randomly.
There isn't really a way to go around this at the moment, so I just
blocked users from being able to do it. Besides, the results should be
the same as when they use a list of `MeasurementValue`s.

**TODO**: Add docs.

**Benefits:**
* Advanced mid-circuit statistics are now supported! And in multiple
ways 😄

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
mudit2812 added a commit that referenced this pull request Jan 19, 2024
)

**Context:**
Adding support for advanced statistics for mid-circuit measurements.
Users can now collect statistics on multiple measurement values. This
can be done in two ways:
1. Using arithmetic operators to collect measurement values into a
composite value. Eg: `m0 * m1 - 2 * m3` or `m1 > m0` or `~m0 & m1`, etc.
Available for `qml.expval`, `qml.var`, `qml.sample`, `qml.counts`.
2. Putting values in a list. This will give wire-like results. Eg: `[m0,
m1, m2]`. Available for `qml.sample`, `qml.probs`, `qml.counts`.

**Composite vs single `MeasurementValue`s**:
Composite `MeasurementValue`s refers to `MeasurementValue`s created by
applying arithmetic/boolean operators to `MeasurementValue`s as shown in
the first point above. A single `MeasurementValue` is the output of
`qml.measure`. Applying arithmetic/boolean operators to
`MeasurementValue`s also returns `MeasurementValue`s.

**Description of the Change:**
* Add `__repr__` to `MeasurementValue` and update
`MeasurementProcess.__repr__` appropriately.
* Update `MeasurementProcess.eigvals` to return outcomes of composed
`MeasurementValue`s when in use. If a list of `MeasurementValue`s is
given, the eigenvalues are just `range(2**num_wires)`.
* Update `QubitDevice` to use the correct eigenvalues for composite
`MeasurementValue`s, and treat a list of `MeasurementValue`s as wires.
* Update `qml.expval` and `qml.var` to disallow using lists of
`MeasurementValue`s, and allow composite MeasurementValue`s.
* Update `qml.sample` and `qml.counts` to allow both lists of
`MeasurementValue`s as well as composite `MeasurementValue`s.
* Update `qml.defer_measurements` to disallow using `qml.probs` with
composite `MeasurementValue`s. This is done because the wire order of
the composite value is not deterministic as the sorting is determined by
the ID of the mid-circuit measurements, which are generated randomly.
There isn't really a way to go around this at the moment, so I just
blocked users from being able to do it. Besides, the results should be
the same as when they use a list of `MeasurementValue`s.

**TODO**: Add docs.

**Benefits:**
* Advanced mid-circuit statistics are now supported! And in multiple
ways 😄

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
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.

4 participants