Skip to content

Conversation

@maffoo
Copy link
Contributor

@maffoo maffoo commented Mar 18, 2025

When freezing or unfreezing circuits, we have a list or tuple of Moment objects so we can call the _from_moments classmethod on the relevant target type which avoids the isinstance typechecks that are needed when calling from_moments as we do currently.

See below for a microbenchmark of this change. On main, freezing an example circuit with 10_001 moments takes something like 550us, while on this branch the same freeze operation takes about 40us (note that we have to copy the circuit before freezing because we cache the result of freeze if a circuit has not been mutated).

With main:

In [1]: import cirq; q = cirq.q(0); c = cirq.Circuit([cirq.X(q)] * 10_000, cirq.M(q))

In [2]: %timeit c.copy()
25.6 μs ± 135 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [3]: %timeit c.copy().freeze()
575 μs ± 821 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

With this branch:

In [1]: import cirq; q = cirq.q(0); c = cirq.Circuit([cirq.X(q)] * 10_000, cirq.M(q))

In [2]: %timeit c.copy()
26 μs ± 137 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [3]: %timeit c.copy().freeze()
66.2 μs ± 1.78 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

@maffoo maffoo requested review from a team and vtomole as code owners March 18, 2025 15:48
@maffoo maffoo requested a review from senecameeks March 18, 2025 15:48
@codecov
Copy link

codecov bot commented Mar 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.13%. Comparing base (ce31720) to head (b8e2378).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7150   +/-   ##
=======================================
  Coverage   98.13%   98.13%           
=======================================
  Files        1093     1093           
  Lines       95579    95579           
=======================================
+ Hits        93795    93797    +2     
+ Misses       1784     1782    -2     

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

Thank you!

@maffoo maffoo added this pull request to the merge queue Mar 18, 2025
Merged via the queue into main with commit 840bb2b Mar 18, 2025
43 checks passed
@maffoo maffoo deleted the u/maffoo/circuit-freeze branch March 18, 2025 18:22
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants