Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

Add measurement_keys property

Open daxfohl opened this issue 3 months ago • 1 comments

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

daxfohl avatar Sep 29 '25 13:09 daxfohl

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 99.36%. Comparing base (ad37bb7) to head (1321cc9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7674      +/-   ##
==========================================
- Coverage   99.38%   99.36%   -0.02%     
==========================================
  Files        1090     1090              
  Lines       98248    98262      +14     
==========================================
- Hits        97643    97641       -2     
- Misses        605      621      +16     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Sep 29 '25 13:09 codecov[bot]