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

Add QNode config for mid-circuit measurement options #5679

Merged
merged 59 commits into from
Jun 4, 2024

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented May 10, 2024

Context:
This PR adds qnode arguments to configure mid-circuit measurement behaviour

Description of the Change:

  • Added postselect_mode and mcm_method kwargs to qnode.
    • postselect_mode is a string, and setting it to "fill-shots" will return all samples regardless of validity. Setting it to "hw-like" will scale shots with postselection. Note that "hw-like" with jax will replace invalid samples with INTEGER_MIN_VAL with mcm_method="one-shot". An error will be raised with defer_measurements with `postselect_mode="hw-like"`` and jax jit.
    • mcm_method is a string and can be either "deferred" or "one-shot"
  • Update qml.devices.preprocess.mid_circuit_measurements to accommodate mcm configuration options when deciding which transform to use.
  • Update QNode._execution_component to use the qml.devices.preprocess.mid_circuit_measurements transform for old API devices.
  • Update ExecutionConfig to include an mcm_config and update QNode and qml.execute to set this config.
  • Added new section with details about the kwargs to the measurements intro doc.

Note:
When using jax-jit, postselect_mode="hw-like" will add dummy values to the samples with dynamic_one_shot and these won't be used for MPs other than qml.sample. However, with defer_measurements, an error will be raised. This is a limitation of the current implementation of defer_measurements, and I've documented it as such

Benefits:
Users can easily configure how to apply and process mid-circuit measurements.

Possible Drawbacks:
More kwargs to QNode and qml.execute.

Related GitHub Issues:

@mudit2812 mudit2812 assigned mudit2812 and unassigned mudit2812 May 10, 2024
@mudit2812
Copy link
Contributor Author

[sc-62105] [sc-62103]

@mudit2812 mudit2812 marked this pull request as ready for review May 14, 2024 18:14
@mudit2812 mudit2812 marked this pull request as draft May 14, 2024 18:14
@mudit2812 mudit2812 marked this pull request as ready for review May 22, 2024 23:35
Copy link

codecov bot commented May 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (c14ce7b) to head (2e3e3a0).
Report is 256 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5679      +/-   ##
==========================================
- Coverage   99.67%   99.67%   -0.01%     
==========================================
  Files         414      414              
  Lines       39316    39067     -249     
==========================================
- Hits        39188    38939     -249     
  Misses        128      128              

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

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Nice job @mudit2812 !
I may still be confused about a few aspects of the config. Most of the documentation is really clear, there were just a few points where I was not sure I understood what the configuration will end up doing.
I left a number of comments, some of which are very subjective and definitely non-blocking!

doc/introduction/measurements.rst Outdated Show resolved Hide resolved
doc/introduction/measurements.rst Outdated Show resolved Hide resolved
doc/introduction/measurements.rst Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
tests/transforms/test_defer_measurements.py Show resolved Hide resolved
tests/transforms/test_dynamic_one_shot.py Outdated Show resolved Hide resolved
pennylane/devices/qubit/apply_operation.py Show resolved Hide resolved
pennylane/devices/qubit/simulate.py Outdated Show resolved Hide resolved
doc/introduction/measurements.rst Outdated Show resolved Hide resolved
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thanks for all the explanations and the nice update to behaviour with JITting.
Nice work! 🎉

pennylane/workflow/execution.py Show resolved Hide resolved
Copy link
Contributor

@vincentmr vincentmr left a comment

Choose a reason for hiding this comment

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

Very nice upgrade, well done @mudit2812 . I'm just sad I'll have to update #5180 for the n-th time accordingly :p

pennylane/devices/default_qubit.py Show resolved Hide resolved
@mudit2812 mudit2812 requested a review from vincentmr June 3, 2024 21:06
Copy link
Contributor

@vincentmr vincentmr left a comment

Choose a reason for hiding this comment

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

Nice work, happy to reapprove @mudit2812 :)

pennylane/devices/default_qubit.py Show resolved Hide resolved
pennylane/devices/execution_config.py Show resolved Hide resolved
pennylane/devices/qubit/simulate.py Show resolved Hide resolved
pennylane/devices/qubit/simulate.py Show resolved Hide resolved
pennylane/transforms/dynamic_one_shot.py Outdated Show resolved Hide resolved
pennylane/workflow/qnode.py Show resolved Hide resolved
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.

Approved from the docs perspective, just leaving one suggestion.

doc/introduction/measurements.rst Outdated Show resolved Hide resolved
doc/introduction/measurements.rst Show resolved Hide resolved
doc/introduction/measurements.rst Outdated Show resolved Hide resolved
@mudit2812 mudit2812 requested a review from albi3ro June 4, 2024 19:33
@mudit2812 mudit2812 merged commit 4ff59f7 into master Jun 4, 2024
40 checks passed
@mudit2812 mudit2812 deleted the postselect-choice branch June 4, 2024 20:48
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.

7 participants