Skip to content

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Sep 29, 2025

This adds measurement_keys and control_keys properties to Circuit, Gate, Operation, and Moment, and any subclasses that require them, as an alternative to the corresponding protocols. These properties are cached for fast lookup (except on non-frozen circuits), which is critical since these methods are used in many performance-critical areas like commutativity checks in circuit construction and transformers.

The change makes circuit construction about 15% faster in most cases because of the caching and elimination of protocol overhead. Allows for better code completion and less need to store results in temp local variables since the properties are cached (see the changes in circuit.earliest_available_moment() for example), resulting in cleaner, easier to write code that is performant by default.

The code is made backwards compatible by having the base classes of Gate and Operation return the results of the protocol, but overridden in subclasses where the keys are known. It also ensures forward consistency by having the protocols check the property values by default. This approach also allows protocols to prime the object's property cache for future use.

The protocols are no longer used in cirq codebase outside of backward compatibility requirements, and could optionally be marked for deprecation for v2 in a future PR. Or we can leave them in place as alternative ways to get the keys, similar to how numpy classes frequently have member methods and package-level functions that alias them. One advantage of the deprecation route is that the base class implementations of these properties could then simply return frozenset() rather than delegating to the protocols, which would further improve performance.

Resolves #7469

@github-actions github-actions bot added the size: L 250< lines changed <1000 label Sep 29, 2025
@daxfohl daxfohl marked this pull request as ready for review September 29, 2025 13:27
@daxfohl daxfohl requested review from a team and vtomole as code owners September 29, 2025 13:27
@daxfohl daxfohl requested a review from maffoo September 29, 2025 13:27
Copy link

codecov bot commented Sep 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.36%. Comparing base (82d2cdc) to head (0c40a7f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7674      +/-   ##
==========================================
- Coverage   99.37%   99.36%   -0.02%     
==========================================
  Files        1085     1085              
  Lines       96937    96951      +14     
==========================================
+ Hits        96331    96333       +2     
- Misses        606      618      +12     

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: L 250< lines changed <1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace "structure" protocols with regular properties

1 participant