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

Create and diagonalize mid-circuit measurements in conditionals #7037

Open
wants to merge 31 commits into
base: move_validation_to_cond
Choose a base branch
from

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Mar 4, 2025

Context:

  • Currently, we can't correctly execute mid-circuit measurements inside Conditionals, so we need to get rid of those before we get to execution.
  • The wrapper created in qml.cond doesn't return anything (so its not suitable for conditionally applying functions that return a MeasurementValue), and would need different handling for measurement functions than for an Operator or qfunc. We've decided to continue limiting it Operator and qfunc, and make a separate function just for measurements. It will be restricted in its inputs and outputs to make sure that what it returns is up-to-date with our current ability to process conditional MCMs, and it will be part of the ftqc module.

Description of the Change:

  • We add cond_meas, with a call signature condition, true_fn, false_fn, to match qml.cond. Limitation on inputs will be informed by our limitations on what we can process for execution, and will currently be:

    • both true_fn and false_fn are required, and should create a MidMeasureMP (likely a parametrized one from the ftqc module) and return a MeasurementValue
    • the measurements on both branches must be applied to the same wire, and they must have the same reset and postselect attributes. Effectively this means only the measurement basis is conditional.
  • We update the diagonalize_mcms transform to assume any Conditional containing a mid-circuit measurement is accompanied by its corresponding false_fn Conditional on the tape, and to diagonalize pairs of Conditionals together. The transform "factors out" the measurement as a Z-basis measurement, so it's no longer conditional, and applies conditional diagonalizing gates.

Benefits:

We can add a measurement to a circuit whose measurement basis is conditional on a previous measurement outcome, and retrieve the measurement value.

Possible Drawbacks:
It's restrictive, and it's limited to devices where we want to diagonalize the MCMs - but for now, it does what we need it to do.

[sc-84499]

@lillian542 lillian542 changed the title Handle mid-circuit measurements in conditionals [WIP] Handle mid-circuit measurements in conditionals Mar 4, 2025
@lillian542 lillian542 changed the base branch from master to validate_mcm_in_cond March 4, 2025 22:26
@lillian542 lillian542 changed the base branch from validate_mcm_in_cond to master March 4, 2025 22:26
)
assert diagonalizing_gates[0].meas_val.processing_fn == measurement.meas_val.processing_fn

def test_diagonalize_mcm_cond_two_outcomes(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's currently only support for conditional having two measurement outcomes, so this test and the previous test became the same test

@lillian542 lillian542 changed the title [WIP] Handle mid-circuit measurements in conditionals [WIP] Create and diagonalize mid-circuit measurements in conditionals Mar 6, 2025
@lillian542 lillian542 changed the base branch from master to move_validation_to_cond March 6, 2025 22:24
@lillian542 lillian542 marked this pull request as ready for review March 7, 2025 23:13
Copy link
Contributor

github-actions bot commented Mar 7, 2025

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.

@lillian542 lillian542 changed the title [WIP] Create and diagonalize mid-circuit measurements in conditionals Create and diagonalize mid-circuit measurements in conditionals Mar 7, 2025
Copy link

codecov bot commented Mar 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (move_validation_to_cond@c23b383). Learn more about missing BASE report.

Additional details and impacted files
@@                    Coverage Diff                     @@
##             move_validation_to_cond    #7037   +/-   ##
==========================================================
  Coverage                           ?   99.60%           
==========================================================
  Files                              ?      489           
  Lines                              ?    47005           
  Branches                           ?        0           
==========================================================
  Hits                               ?    46820           
  Misses                             ?      185           
  Partials                           ?        0           

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant