Skip to content
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

Perf optimization for building circuits using only appends #6882

Merged
merged 33 commits into from
Jan 23, 2025

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Dec 26, 2024

Added a helper class _PlacementCache that maintains all the indices needed for fast near-constant-time op placement during append, speeding up a worst-case quadratic perf behavior when constructing circuits via repeated append calls. Any non-append modification of the circuit simply nulls out the placer, and subsequent modifications just fall back to the (existing, slow) placement index search from then on.

Updated transformer_primitives.apply_map_func to also use this class, since much of the code was duplicated.

There's probably room to support additional insert strategies, or to allow for manual recreation of a nulled-out circuit placer, etc., to increase the applicability of this. But for now, append is the most common way to create circuits, and this PR handles that case.

As mentioned in the test, this change fixes the placement perf, which is most egregious for long narrow circuits. (For short, wide circuits, the cost of recreating the (immutable) Moment for each new op added to it dominates, which this PR does not address.)

Also note that this change has no effect on performance of constructing circuits by passing in a list of ops into the Circuit constructor, which is already fast for both long and wide circuits. The only reason this PR is needed is because many of our tutorials demonstrate using repeated append calls for constructing circuits rather than passing the ops into the Circuit constructor, and it appears that many users do the same.

@daxfohl daxfohl requested review from vtomole and a team as code owners December 26, 2024 01:14
@CirqBot CirqBot added the size: M 50< lines changed <250 label Dec 26, 2024
@daxfohl daxfohl force-pushed the circuit-append-perf branch from 2160169 to c03dc02 Compare December 29, 2024 22:54
@daxfohl daxfohl changed the title Perf optimization for circuit construction using only appends Perf optimization for building circuits using only appends Dec 30, 2024
Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.88%. Comparing base (4206cb1) to head (98eb6dc).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6882      +/-   ##
==========================================
- Coverage   97.88%   97.88%   -0.01%     
==========================================
  Files        1085     1085              
  Lines       94962    94983      +21     
==========================================
+ Hits        92952    92972      +20     
- Misses       2010     2011       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mhucka mhucka left a comment

Choose a reason for hiding this comment

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

LGTM.

cirq-core/cirq/circuits/circuit_test.py Outdated Show resolved Hide resolved
@daxfohl daxfohl marked this pull request as draft January 19, 2025 04:06
@daxfohl daxfohl marked this pull request as ready for review January 19, 2025 22:08
@daxfohl daxfohl enabled auto-merge January 23, 2025 02:08
@daxfohl daxfohl added this pull request to the merge queue Jan 23, 2025
Merged via the queue into quantumlib:main with commit 48aadd0 Jan 23, 2025
37 checks passed
@daxfohl daxfohl deleted the circuit-append-perf branch January 23, 2025 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants