Skip to content

Fix bug in circuit.insert#7823

Merged
pavoljuhas merged 5 commits into
quantumlib:mainfrom
codrut3:issue-7611-c
May 18, 2026
Merged

Fix bug in circuit.insert#7823
pavoljuhas merged 5 commits into
quantumlib:mainfrom
codrut3:issue-7611-c

Conversation

@codrut3

@codrut3 codrut3 commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Change _group_into_moment_compatible to take into account measurement and control keys. Otherwise two incompatible operations can be put in the same moment.
I also changed _can_add_op_at to look at measurement and control keys.
I added unit tests that show the issue.

Change _group_into_moment_compatible to take into account measurement
and control keys.
@codrut3 codrut3 requested review from a team and vtomole as code owners January 3, 2026 10:28
@codrut3 codrut3 requested a review from mpharrigan January 3, 2026 10:28
@github-actions github-actions Bot added the size: M 50< lines changed <250 label Jan 3, 2026
@codecov

codecov Bot commented Jan 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.63%. Comparing base (3164943) to head (c4e0106).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7823      +/-   ##
==========================================
- Coverage   99.63%   99.63%   -0.01%     
==========================================
  Files        1110     1110              
  Lines      100072   100136      +64     
==========================================
+ Hits        99710    99773      +63     
- Misses        362      363       +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.

@daxfohl

daxfohl commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

Allowing multiple measurements to the same key in the same moment was a requirement from the HW team. Unless that has changed, this would be breaking.

@codrut3

codrut3 commented Jan 5, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for pointing this out @daxfohl ! Doesn't this mean this condition here is also wrong:

not op_measurement_keys.isdisjoint(moment_measurement_keys)

@daxfohl

daxfohl commented Jan 6, 2026

Copy link
Copy Markdown
Collaborator

Maybe. Though it can't be removed completely, because then it could fall through to earlier moments and break commutativity. (There's a lot of https://xkcd.com/1172/ in the circuit construction code).

@pavoljuhas

Copy link
Copy Markdown
Collaborator

cirq-cync - @codrut3 - please create an issue for this PR, e.g., to request that measurement and control keys cannot overlap in the same moment. This would be potentially a breaking change so request feedback from Ilya Drozdov and Christopher Wood for the issue to comment if the current behavior is needed in some situations.

@codrut3

codrut3 commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

Thank you! I created #7829

@github-actions

Copy link
Copy Markdown

This pull request has been automatically labeled as stale because 90 days have passed without activity. If no further activity occurs and the status/stale label is not removed by a maintainer within 60 days, this pull request will be closed. If you want to keep the pull request open, leave a comment here and the status will be updated automatically.

If you have questions or feedback about this process, please open a new issue in this repository to let us know. You can also send email to the maintainers at quantum-oss-maintainers@google.com.

@github-actions github-actions Bot added the status/stale Closed due to inactivity for an extended period of time label Apr 12, 2026
@mhucka mhucka removed the status/stale Closed due to inactivity for an extended period of time label Apr 15, 2026
@mhucka mhucka added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque priority/after-1.7 Leave for after the Cirq 1.7 release labels Apr 29, 2026
@pavoljuhas

Copy link
Copy Markdown
Collaborator

@codrut3 - would you be available to update this along the last comments in #7829 ?

@codrut3

codrut3 commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

@pavoljuhas Thank you for checking! I updated the PR by allowing operations with the same measurement key in the same moment, in the context of the methods changed by this PR (_can_add_op_at, _group_into_moment_compatible).

However, there is older code in circuit.py where one checks that measurement keys are disjoint: for example in _PlacementCache, or here. Should I remove these checks to be consistent everywhere?

@pavoljuhas

Copy link
Copy Markdown
Collaborator

Re #7823 (comment)

Allowing multiple measurements to the same key in the same moment was a requirement from the HW team.

There is a distinction in Cirq between MeasurementGate acting on 2 qubits with common key, Moment(cirq.measure(q0, q1, key="k")) and 2 single-qubit measurement gates, i.e., Moment(cirq.measure(q0, key="k"), cirq.measure(q1, key="k")). In the first case cirq simulation allows to recover results for both qubits, in the second case the result for q0 seems overwritten by q1.

Do we need to allow that case or can HW team do with the first scenario?

@pavoljuhas

pavoljuhas commented May 13, 2026

Copy link
Copy Markdown
Collaborator

cynq discussion - let us do a check for overlapping measurement keys in Circuit.insert, but first verify if it breaks anything in the internal code. Circuits should use cirq.measure(q0, q1, key="k") to setup multi-qubit measurement with the same key.

  • @pavoljuhas - confirm this does not break tests in the internal code.

Disallow one-qubit measurement operations with overlapping keys.
These should be replaced with `cirq.measure(q0, q1, key="k")`.

Keep the new tests, but adjust them for expected behavior.

This partially reverts commit 0ca9121.
Skip potentially expensive lookup of measurement and control
keys from a Moment when inserted operation has no such keys.

No change in code function.
@pavoljuhas

Copy link
Copy Markdown
Collaborator

I have restored the comparison with measurement keys in 42c13d6 and ran unit tests (those which use cirq) for the internal code at I9765f3a55b8fa0e9e08bf72ce918fb46606af871. There were no new failures or noticeable difference w/r to using main-branch Cirq.

Once this is merged and out in a dev-release, I will run a full internal test suite on the CI to be sure this is not breaking anything.

@pavoljuhas pavoljuhas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I restored the check of measurement-keys overlap in 42c13d6 and also added a micro-optimization in c4e0106 to save on unnecessary lookup of measurement and control keys in Moment-s.

@codrut3 - PTAL and confirm if this looks OK from your side.

PS: Thank you for careful and patient work on this!

@pavoljuhas pavoljuhas changed the title Fix bug in circuit.insert. Fix bug in circuit.insert May 14, 2026
@pavoljuhas pavoljuhas added this pull request to the merge queue May 18, 2026
Merged via the queue into quantumlib:main with commit be76e86 May 18, 2026
44 checks passed
@pavoljuhas pavoljuhas added BREAKING CHANGE For pull requests that are important to mention in release notes. and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque priority/after-1.7 Leave for after the Cirq 1.7 release labels May 18, 2026
@pavoljuhas

Copy link
Copy Markdown
Collaborator

Confirming the internal CI passed successfully with this change at q-cl/150609 (not submitted, test only).

Adding BREAKING CHANGE label for an unlikely case this changes someone's circuits built with Circuit.insert.

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.

4 participants