Skip to content

Conversation

@Vivek1106-04
Copy link

Description:
This PR addresses Issue #6327 by optimizing the __pow__ method for both CliffordGate and SingleQubitCliffordGate.

Changes proposed in this PR:

  1. CliffordGate Optimization:

    • Replaced the $O(N)$ linear loop with Binary Exponentiation (exponentiation by squaring).
    • This reduces the complexity to $O(\log N)$, significantly improving performance for large exponents.
    • Added a safety check to ensure result_tableau is initialized independently to avoid reference aliasing bugs.
  2. SingleQubitCliffordGate Optimization:

    • Integer Powers: Leveraged the group structure of single-qubit Cliffords (size 24). All integer exponents are now reduced modulo 24 before calculation.
    • Rational Powers: Added support for half-integer exponents (e.g., gate**2.5). The logic decomposes $G^{k/2}$ into $(G^{0.5})^k$ and recursively calls the optimized integer power function.
    • Added type checking to ensure invalid inputs (like strings) return NotImplemented.

Related Issue:
Fixes #6327

@Vivek1106-04 Vivek1106-04 requested review from a team and vtomole as code owners November 21, 2025 18:58
@Vivek1106-04 Vivek1106-04 requested a review from maffoo November 21, 2025 18:58
@github-actions github-actions bot added the size: M 50< lines changed <250 label Nov 21, 2025
@codecov
Copy link

codecov bot commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.57%. Comparing base (4d75b4b) to head (c127a01).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7766   +/-   ##
=======================================
  Coverage   99.57%   99.57%           
=======================================
  Files        1102     1102           
  Lines       98434    98436    +2     
=======================================
+ Hits        98014    98016    +2     
  Misses        420      420           

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

@pavoljuhas pavoljuhas self-requested a review December 10, 2025 18:36
@pavoljuhas
Copy link
Collaborator

pavoljuhas commented Dec 10, 2025

@codrut3 - can you please take a look at this? (If so, please post short comment so I can assign you as a reviewer)

@mhucka
Copy link
Contributor

mhucka commented Dec 10, 2025

Discussed in Cirq Cynq: #6919 had implemented a solution for the SingleQubitCliffordGate case. Suggestion: split this PR into 2 PRs,

  1. Modify this current PR to be just about the CliffordGate part (which is different from Improve __pow__ for SingleQubitCliffordGate (issue #6327) #6919)
  2. Do a new PR for the enhancements to the SingleQubitCliffordGate optimization case

@codrut3
Copy link
Contributor

codrut3 commented Dec 10, 2025

@codrut3 - can you please take a look at this? (If so, please post short comment so I can assign you as a reviewer)

Happy to review! Thanks Pavol!

@codrut3
Copy link
Contributor

codrut3 commented Dec 10, 2025

Hey @Vivek1106-04 ,
Looking at the existing code, both optimizations requested by #6327 are already implemented. Your PR is re-arranging if order in some cases and adding explanatory comments, but not changing the logic. In particular, the exponentiation is already done in $O(\log N)$ steps. You are also removing the base case if exponent == -1: which, although still correct, seems less efficient to me.

The only new improvement is on line 413, where you are using self.clifford_tableau.inverse() directly if the exponent is $&lt; 0$, instead of first making a copy of the tableau (the inverse() method creates a new tableau so this is safe).
Perhaps this line change can be submitted separately as a PR with an explanatory description while leaving the rest of the code unchanged?

@pavoljuhas #6327 is already solved and should be closed to avoid confusion.

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.

Improve __pow__ for SingleQubitCliffordGate and CliffordGate class

4 participants