Skip to content

Conversation

dstrain115
Copy link
Collaborator

  • Noticed some inefficiencies in the creation of WaitGate where it is doing unnecessary object boxing.
  • This removes (slow) comparisons between Duration objects.

This improves creation of WaitGate objects (e.g. from cirq.wait(nanos=10)) from about 27 micros to about 16 micros. While not a huge effect, this could have measurable savings for certain T1 and similar experiments that involve creation of lots of wait gates.

- Noticed some inefficiencies in the creation of WaitGate where it
is doing unnecessary object boxing.
- This removes (slow) comparisons between Duration objects.

This improves creation of WaitGate objects (e.g. from
cirq.wait(nanos=10)) from about 27 micros to about 16 micros.
While not a huge effect, this could have measurable savings for
certain T1 and similar experiments that involve creation of
lots of wait gates.
@dstrain115 dstrain115 requested review from a team and vtomole as code owners September 25, 2025 19:26
@dstrain115 dstrain115 requested a review from dabacon September 25, 2025 19:26
@github-actions github-actions bot added the size: S 10< lines changed <50 label Sep 25, 2025
duration if isinstance(duration, value.Duration) else value.Duration(duration)
)
if not self.duration._is_parameterized_() and self.duration.total_picos() < 0:
# if not protocols.is_parameterized(self.duration) and self.duration.total_picos() < 0:
Copy link
Collaborator

@pavoljuhas pavoljuhas Sep 25, 2025

Choose a reason for hiding this comment

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

nit - remove comment-disabled code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done


def _is_parameterized_(self) -> bool:
return protocols.is_parameterized(self._time_vals)
return any(isinstance(val, sympy.Expr) for val in self._time_vals)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit - maybe sympy.Basic as below?

if isinstance(val, sympy.Basic):
return True

Copy link

codecov bot commented Sep 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.36%. Comparing base (c8d8d59) to head (ee34a74).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7671      +/-   ##
==========================================
- Coverage   99.37%   99.36%   -0.01%     
==========================================
  Files        1082     1082              
  Lines       96708    96711       +3     
==========================================
  Hits        96101    96101              
- Misses        607      610       +3     

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

self._duration = (
duration if isinstance(duration, value.Duration) else value.Duration(duration)
)
if not self.duration._is_parameterized_() and self.duration.total_picos() < 0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

On my Debian box the time saving is ~300ns from 2.2 to 1.9µs.

If the overall time save of this PR is ~11µs is it worth to call a dunder method of different class rather than standard protocols.is_parameterized?

May be we could have a similar time save by reordering the checks in the is_parameterized call.

Copy link
Collaborator Author

@dstrain115 dstrain115 Sep 26, 2025

Choose a reason for hiding this comment

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

How about this? I changed it to not use is_parameterized at all. I just call total_picos and verify it's not a sympy expr before I verify it's greater than 0:

        total_picos = self.duration.total_picos()
        if not isinstance(total_picos, sympy.Basic) and total_picos < 0:
            raise ValueError('duration < 0')

From my calculation, this is even faster and avoids the private method call.

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.

LGTM, but please consider if it is necessary to use the protected Duration._is_parameterized_ instead of the protocol function.

@dstrain115 dstrain115 added this pull request to the merge queue Sep 26, 2025
Merged via the queue into quantumlib:main with commit 92b0206 Sep 26, 2025
35 checks passed
@dstrain115 dstrain115 deleted the optimize_wait_gate branch September 26, 2025 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants