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

Make RouteCQC errorout on intermediate measurements on 3+ qubits #6307

Merged
merged 28 commits into from
Nov 10, 2023

Conversation

ghost
Copy link

@ghost ghost commented Oct 2, 2023

router mishandling mid-circuit measurements #issue6293

Description of the issue

cirq.RouteCQC commutes gates in undefined ways due to intermediate measurements with 3 or more qubits not being handled correctly.

This PR aims to fix this issue by:

  1. Raising an error and provide a descriptive message on what's occurred

Next Steps

  1. Break intermediate measurements on 3+ qubits into single qubit measurements if -and only if- the result isn't stored (i.e. key is None)
  2. Check if it's possible to create a ClassicallyControlledOperation controlled by the result of a multiqubit measurement
  • if it's possible then we stop here until there is a need to support this case because then we will know how this should be handled.
  • if it's not possible then break all intermediate multiqubit measurements into single measurements even those that have a key (because they are not using it anyway) while logging a warning to the user that the key is being dropped.

@ghost ghost requested review from vtomole, cduck and a team as code owners October 2, 2023 15:34
@ghost ghost requested a review from dabacon October 2, 2023 15:34
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@CirqBot CirqBot added the size: M 50< lines changed <250 label Oct 2, 2023
@NoureldinYosri
Copy link
Collaborator

thanks for helping with this issue. please check https://github.com/quantumlib/Cirq/blob/master/docs/dev/development.md#continuous-integration-and-local-testing for CI instructions. currently your PR will fail format and pylint tests and maybe others.

In the describtion section of your PR please explain what your change does. also the title should be informative.


As it stands this PR doesn't fix the problem. There are two ways to go about this

  1. Modify the algorithm so that it handles intermidiate measurements correctly.
  2. treat intermediate measurements has seperators and split the original circuits into multiple smaller circuits and then combine the results.

The first harder conceptually, the second is more of a coding excersize. I think we prefer the first option unless the routing algorithm https://arxiv.org/pdf/1902.08091.pdf can't support this case.

@ghost
Copy link
Author

ghost commented Oct 2, 2023

Thanks for the help, will review the routing algorthm to see if it supports intermidiate measurements case. I should be able to implment the second implemention just to get a quick solution out but will also look for example of handling intermidiate measurements correctly in the source code and on online.

@ghost ghost changed the title Shef4/issue6293 added intermediate measurement circuit breaks for router mishandling mid-circuit measurements Oct 5, 2023
@ghost ghost changed the title added intermediate measurement circuit breaks for router mishandling mid-circuit measurements added circuit breaks to fixRouteCQC mishandling intermediate measurements Oct 5, 2023
@ghost ghost changed the title added circuit breaks to fixRouteCQC mishandling intermediate measurements added circuit breaks to fix RouteCQC mishandling intermediate measurements Oct 5, 2023
@NoureldinYosri
Copy link
Collaborator

I think, it's better to not support intermediate multiqubit measurements. I'm not aware of any usage of intermdiate multiqubit measurements that can't be done with single qubit measurements.

@ghost ghost changed the title added circuit breaks to fix RouteCQC mishandling intermediate measurements added error catch to prevent RouteCQC mishandling intermediate measurements on multiple qubits Oct 30, 2023
@NoureldinYosri
Copy link
Collaborator

this seems like a lot of changes for a simple check. The check is just

if any(protocols.num_qubits(op) > 2 and protocols.is_measurement(op) for op in itertools.chain(*circuit.moments[:-1])):
   # There is at least one non-terminal measurement on 3+ qubits

@ghost
Copy link
Author

ghost commented Oct 30, 2023

Yes, apologies, I pulled the changes from upstream so that my branch would be up-to-date before doing the commit. It added changes I wasn't expecting.

I did 2 separate commits Errorout for mid-circuit measurements on 3+ qubits with just my changes and Merge commit 'e81919ef8e48ae3efa7f866d6d3e98bd480e7c10' into shef4/is… to fix the divergence.

Was able to revert to the intended commit. The change should only be class, unit test, and cleaned-up example notebook files.

Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

almost there. a few more changes. after you are done please run

./check/format-incremental --apply

and commit its effect

@NoureldinYosri NoureldinYosri changed the title added error catch to prevent RouteCQC mishandling intermediate measurements on multiple qubits Make RouteCQC errorout on intermediate measurements on 3+ qubits Nov 6, 2023
Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

a few more things.

& please clear you changes to the notebook

@ghost
Copy link
Author

ghost commented Nov 6, 2023

Added the changes you recommended. Cleaning up the notebook was done in removed debug code commit. Will try and replace the debug code with an empty cell in the meantime just in case.

@NoureldinYosri
Copy link
Collaborator

@shef4 check the changed files then
6JdM949Gttprvpb

@ghost
Copy link
Author

ghost commented Nov 6, 2023

I see what the changes are, will update now.

@ghost
Copy link
Author

ghost commented Nov 6, 2023

Just updated the notebook. Changed files made it way easier, thanks!

@NoureldinYosri
Copy link
Collaborator

@shef4 please fix failing CI checks and update the branch to head

@ghost
Copy link
Author

ghost commented Nov 6, 2023

currently testing change locally

@ghost
Copy link
Author

ghost commented Nov 6, 2023

I ran CI and everything is passing.
Got a weird bug with code coverage saying my test cases need test cases to cover them but I think that was a mistake

Copy link

codecov bot commented Nov 6, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (aa312bc) 97.83% compared to head (59d8e21) 97.83%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6307   +/-   ##
=======================================
  Coverage   97.83%   97.83%           
=======================================
  Files        1108     1108           
  Lines       96352    96372   +20     
=======================================
+ Hits        94270    94290   +20     
  Misses       2082     2082           
Files Coverage Δ
...ore/cirq/transformers/routing/route_circuit_cqc.py 99.22% <100.00%> (+0.01%) ⬆️
...irq/transformers/routing/route_circuit_cqc_test.py 100.00% <100.00%> (ø)

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

@NoureldinYosri NoureldinYosri merged commit 11ae0bd into quantumlib:master Nov 10, 2023
@ghost ghost deleted the shef4/issue6293 branch November 10, 2023 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants