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

Optimize Clifford.__pow__ by using binary exponentiation #6581

Conversation

migueltorrescosta
Copy link
Contributor

Implements binary exponentiation as described in #6327
This change does not include the feature of exponentiation for non-integer exponents. This still needs to be discussed, since $\frac{5}{2}$ might be supported ( i.e. the square root is clear ), but other values might not. Knowing which non-integers to support needs to be discussed

@migueltorrescosta migueltorrescosta requested review from vtomole, cduck and a team as code owners May 1, 2024 00:14
@migueltorrescosta migueltorrescosta requested a review from maffoo May 1, 2024 00:14
Copy link

google-cla bot commented May 1, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@NoureldinYosri NoureldinYosri self-assigned this May 1, 2024
@NoureldinYosri NoureldinYosri self-requested a review May 1, 2024 00:15
@migueltorrescosta
Copy link
Contributor Author

@NoureldinYosri as this is my 1st contribution, I had to add the CLA. I have added it now, however I don't think I can retrigger the GitHub action to run the tests

Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

please refer to docs/dev/development.md for creating a dev environmental. personally I create a virtual env and then pip install -r dev_tools/requirements/dev.env.txt

also prefer a simpler implementation

@migueltorrescosta
Copy link
Contributor Author

personally I create a virtual env and then pip install -r dev_tools/requirements/dev.env.txt

I do the same, the issue is that my Python env defaults to the latest ( Python 3.12 ), and I think cirq's dev environment is not compatible with Python 3.12 due to the PyLaTeX dependency ( it hasn't been updated in 7 months and it breaks in Python 3.12 )

I got around it by doing pip install pytest ( in practice ignoring dev.env.txt ), and then running the small suite of tests related to this change.

Thank you for the guidance @NoureldinYosri

Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

for your convience, you can choose the python version when you create a venv (e.g. python3.11 -m ven "env_name"orconda create -n en_name python=3.11`)


to pass the format CI you can run ./check/format-incremental --apply

@@ -129,7 +129,8 @@ def apply_global_phase(self, coefficient: linear_dict.Scalar):

class CliffordTableau(StabilizerState):
"""Tableau representation of a stabilizer state
(based on Aaronson and Gottesman 2006).
Copy link
Collaborator

Choose a reason for hiding this comment

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

References:
    - [Aaronson and Gottesman](https://arxiv.org/pdf/quant-ph/0406196)

base_tableau = self.clifford_tableau.copy()
if exponent < 0:
base_tableau = base_tableau.inverse()
exponent = int(abs(exponent))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: no need to case again to int since we already checked that exponent is int

Copy link

codecov bot commented May 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.79%. Comparing base (3080d93) to head (91d5d28).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6581   +/-   ##
=======================================
  Coverage   97.79%   97.79%           
=======================================
  Files        1124     1124           
  Lines       95699    95703    +4     
=======================================
+ Hits        93589    93593    +4     
  Misses       2110     2110           

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

@@ -400,7 +400,10 @@ def _has_stabilizer_effect_(self) -> Optional[bool]:
# By definition, Clifford Gate should always return True.
return True

def __pow__(self, exponent) -> 'CliffordGate':
def __pow__(self, exponent: int) -> 'CliffordGate':
Copy link
Collaborator

Choose a reason for hiding this comment

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

to pass the type check CI make the type of exponent Union[int, float]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This did not work, as mypy complained about the type Union[int, float]. Instead using float works: ints are accepted as float. I had to include a exponent = int(exponent), after the initial assertion, to make mypy happy.

(based on Aaronson and Gottesman 2006).

References:
- [Aaronson and Gottesman](https://arxiv.org/pdf/quant-ph/0406196)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: instead of pdf make it abs

@NoureldinYosri NoureldinYosri changed the title Support binary exponentiation Optimized Clifford.__pow__ by using binary exponentiation May 2, 2024
@NoureldinYosri NoureldinYosri changed the title Optimized Clifford.__pow__ by using binary exponentiation Optimize Clifford.__pow__ by using binary exponentiation May 2, 2024
@migueltorrescosta
Copy link
Contributor Author

migueltorrescosta commented May 2, 2024

Is there a way to run the entire CI/CD locally?
I can fix the mypy issue, but running the entire CI/CD would shorten this daily loop of Make changes / submit PR / wait for approval / run CICD / see the errors / rinse and repeat 😅

@NoureldinYosri
Copy link
Collaborator

@migueltorrescosta please refer to https://github.com/quantumlib/Cirq/blob/main/docs/dev/development.md#continuous-integration-and-local-testing for running locally, note that we currently support only py3.10 & py3.11 so work in a venv/conda env with one of them

@migueltorrescosta
Copy link
Contributor Author

I have submitted the fixed mypy

@NoureldinYosri
Copy link
Collaborator

@migueltorrescosta from the changed files it doesn't look like your last commit was pushed

@migueltorrescosta
Copy link
Contributor Author

Done. Sorry for the confusion 🤦

@CirqBot CirqBot added the size: S 10< lines changed <50 label May 5, 2024
Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

@migueltorrescosta Thanks for your help with this issue

@NoureldinYosri NoureldinYosri merged commit 69e3de1 into quantumlib:main May 6, 2024
31 checks passed
jselig-rigetti pushed a commit to jselig-rigetti/Cirq that referenced this pull request May 28, 2024
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.

3 participants