Skip to content

Fix nondeterminacy in Circuit.insert (simplified) #7043

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

Merged
merged 15 commits into from
Mar 11, 2025

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Feb 7, 2025

#6997 was a PR to fix all edge cases of #6986, but the behavior change was significant enough to be concerning. This PR replaces it with the minimum changes to fix only those edge cases that are outright bugs, without breaking any existing tests or causing possible behavior changes to 'oddities'.

Fixes #6711, and fixes #6986 edge cases that caused the bug due to order dependency: test_3 will now produce a result equal to that of test_4, and test_5 output will now equal that of test_6. Also fixes the bug test_2 where EARLIEST is not performed. The behaviors in test_1 and test_7 will remain as-is for now, and we can decide later whether we want to change those behaviors or not. (This PR description will close that issue; if we want to change those behaviors, open a new issue specific to them).

Implementation description from #6997 still applies:

Instead of inserting one operation at a time, it first batches up ops by moment compatibility (see docstring for _group_into_moment_compatible). This way, the insert algorithm can first check whether any op in the batch requires a new moment to be created, and if so, preemptively create that moment before placing any of the operations from that batch. This allows it to avoid the nondeterminacy caused by the one-by-one algorithm, where one op inserted at moment[k] can inadvertently get pushed forward when a later op inserts a new moment at k.

Tests added for all documented edge cases.

The diff from #6997 can be seen in commit 577f9c0

@daxfohl daxfohl requested review from vtomole and a team as code owners February 7, 2025 20:35
@daxfohl daxfohl requested a review from viathor February 7, 2025 20:35
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 7, 2025
Copy link

codecov bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.16%. Comparing base (8073d4c) to head (0d4387d).
Report is 108 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7043   +/-   ##
=======================================
  Coverage   98.16%   98.16%           
=======================================
  Files        1093     1093           
  Lines       95466    95523   +57     
=======================================
+ Hits        93711    93767   +56     
- Misses       1755     1756    +1     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

LGTM with a small suggestion.

Thank you for taking care of this!

daxfohl and others added 2 commits March 11, 2025 14:36
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
@daxfohl daxfohl requested a review from pavoljuhas March 11, 2025 22:01
@daxfohl daxfohl enabled auto-merge March 11, 2025 22:01
Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Thanks!

@daxfohl daxfohl added this pull request to the merge queue Mar 11, 2025
Merged via the queue into quantumlib:main with commit b3af2ee Mar 11, 2025
38 checks passed
@daxfohl daxfohl deleted the circuit-insert-alt branch March 11, 2025 22:59
@daxfohl daxfohl added the BREAKING CHANGE For pull requests that are important to mention in release notes. label Apr 18, 2025
@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 18, 2025

Added BREAKING label. It doesn't break any unit test, though it does change circuit construction behavior. Those changes are specified in the 2nd paragraph of the description above. The two changes involving order dependency were previously nondeterministic, and this change makes them deterministic. The other change is the example test_2 from issue #6986, which is a bug where EARLIEST was not applied when requested.

BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
* Fix nondeterminacy and a couple other issues in Circuit.insert

* code comments

* code comments

* code comments

* test1 and test7 reverted

* remove no longer relevant new tests

* Update cirq-core/cirq/circuits/circuit.py

Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>

* Update circuit.py

* Put back the last line of docstring

---------

Co-authored-by: Pavol Juhas <juhas@google.com>
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE For pull requests that are important to mention in release notes. size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Circuit.insert has odd edge cases Inconsistent behavior of Default Insert Strategy
3 participants