Skip to content

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Feb 8, 2025

Fixes #1657

Updates everywhere _mixture_ is called, to ensure gates can be handled. Changes BitFlip to be a mixture of X and I gates as an example.

Also updates apply_mixture to take advantage of this. Currently apply_mixture calls mixture() and passes the results to apply_unitary one-by-one. But since mixture() converts the gates now returned by _mixture_ to unitaries, that prevents it from being able to use the more efficient _apply_unitary_ strategies that the gate itself may provide. This PR changes apply_mixture to use the values returned by _mixture_ directly instead, allowing for the optimizations of those gates have them.

Some of the code could be simplified if we did #7039 (plus allowing unitary(val: <raw np unitary>) => val), but that's purely aesthetics. (Side note: the three different protocols are each implemented with slightly different styles; it might be good to standardize on one).

Note: since the mixture protocol now converts non-numpy return values to unitaries, the existing unit tests that used strings 'a' and 'b' no longer work, and had to be changed to proper unitaries. That shouldn't be a problem in any real-world use.

@daxfohl daxfohl requested review from a team and vtomole as code owners February 8, 2025 19:05
@daxfohl daxfohl requested a review from fdmalone February 8, 2025 19:05
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 8, 2025
@daxfohl daxfohl changed the title Mixture Allow '_mixture_' to return gates instead of just raw unitary matrices Feb 8, 2025
Copy link

codecov bot commented Feb 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.65%. Comparing base (4436b85) to head (08d9837).
Report is 39 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7048      +/-   ##
==========================================
- Coverage   98.66%   98.65%   -0.01%     
==========================================
  Files        1106     1106              
  Lines       95914    95869      -45     
==========================================
- Hits        94630    94581      -49     
- Misses       1284     1288       +4     

☔ 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.

Looks great, thank you!

Please see comments before merging. (Non essential feel free to skip them)

@pavoljuhas pavoljuhas added the priority/before-1.5 Finish before the Cirq 1.5 release label Apr 5, 2025
@daxfohl daxfohl enabled auto-merge April 5, 2025 04:40
@daxfohl daxfohl added this pull request to the merge queue Apr 5, 2025
Merged via the queue into quantumlib:main with commit 02941bf Apr 5, 2025
38 checks passed
@daxfohl daxfohl deleted the mixture branch April 5, 2025 05:08
@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

Labeling as BREAKING since unit tests had to change. However the unit test were artificial, returning strings from the mixture implementation. I can't think of any real-world thing this would break.

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. priority/before-1.5 Finish before the Cirq 1.5 release size: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_mixture_ should be able to return List[Tuple[float, Operation]] instead of List[Tuple[float, matrix]]

3 participants