Preserve cached values in Circuit more granularly#7649
Preserve cached values in Circuit more granularly#7649daxfohl wants to merge 15 commits intoquantumlib:mainfrom
Conversation
|
Okay, I lean toward not making the change in At some point, it would be possible to add a parameter to allow each use case to choose whether to create the cache or not, but that would be a separate PR. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7649 +/- ##
==========================================
- Coverage 99.38% 99.37% -0.01%
==========================================
Files 1090 1090
Lines 98248 98317 +69
==========================================
+ Hits 97643 97706 +63
- Misses 605 611 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ts, update tests accordingly.
|
Updated PR to undo the change to |
|
This pull request has been automatically labeled as stale because 90 days have passed without comments or other activity. If no further activity occurs and the If you have questions or feedback about this process, we welcome your input. You can open a new issue to let us know (please also reference this issue there, for continuity), or reach out to the project maintainers at quantum-oss-maintainers@google.com. |
This PR improves the resilience of the cached member fields in Circuit. Most importantly,
_placement_cache, which is used to speed upCircuit.append()from O(N) to O(1), is made to be resilient and consistent acrossinsertand batch inserts,addandmuloperations, andcopy. Prior to this PR, performing any of those actions on a circuit would delete the cache, andappendwould fall back to O(N) behavior from that point on.Summary of changes:
_PlacementCachehas new methodsinsert_moment(index, count)andput(index, mop), that update the key indices to account for new moments or operations.Circuithas new corresponding_insert_moments(...)and_put_ops(...)methods to modify the circuit and update the cache atomically, and logic strewn aroundCircuithas been updated to use those methods when possible.Circuit.copy()now preserves all cache values._placement_cacheare now immutable types, socopydoesn't have to make copies.Moment.with_operations(*ops)gave a 15% speedup in the common case oflen(ops)==1.Overall, the performance of
appendandinsertindividually are unaffected by this change; measured time is equal, but the performance when mixing inserts and appends is improved from O(N) to O(1). It also cleans up the code a bit IMO, with less need to placeself._mutated()everywhere.Fixes #7464