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

Clifford+T decomposition for RZ gates #4678

Closed
wants to merge 24 commits into from
Closed

Conversation

timmysilv
Copy link
Contributor

Context:
I implemented this paper in python. I just "copy-pasted" the author's implementation of it in Haskell. I kept function names and variable names as consistent as possible with the original to make it easier to cross-reference code in the future if bugs pop up. Some notes:

  • Haskell variables can be named with apostrophes like a'. I named those a_ instead to be accepted in python
  • I didn't add docstrings everywhere because so many of these functions are just little helper functions
  • The most important thing to know is the entrypoint, theta_to_gates in synthesis.py. Example below.

The algorithm is effectively three parts.

  1. given a theta and epsilon, find a u value in the $D[\omega]$ ring to be the top-left element of the matrix
  2. solve a Diophantine equation to find t, the off-diagonal elements in the matrix
  3. decompose the matrix into Clifford+T gates using the multi-qubit synthesis algorithm introduced in this paper

Files:

  • gridpoints.py: solver for the first part of the algorithm. Its code creates a generator of possible solutions, which will be verified later. There are many, so the generator is key.
  • diophantine.py: solver for the second part of the algorithm. It raises a DiophantineError if it got a bad result.
  • newsynth.py: entrypoint for the first two parts of the algorithm. It gets the generator from gridpoints.py and checks each candidate with code in diophantine.py until it finds one that works
  • rings.py: this file holds the different Ring type definitions. This part is most different from the Haskell. I only implemented the functions/dunders needed to make the code work, and I raise ValueErrors otherwise. This should help with bug reporting if it ever happens.
  • shapes.py: some shapes/objects that were useful to define
  • synthesis.py: the third part of the algorithm. This part is a little overly sophisticated, because it works (mostly) for multi-qubit gates, even though I only needed an algorithm for single-qubit gates. I opted for this one because it was already implemented in Haskell, simple as that. It works : )

Description of the Change:
Introduce the gridsynth Clifford+T decomposition algorithm! It takes a theta value (for an RZ rotation) and an epsilon value (tends to work down to 1e-8 from what I've seen, but maybe we can improve it).

>>> theta_to_gates(1.2345, 1e-3)
'THTHTHTHTHTHTSHTSHTSHTSHTSHTSHTHTSHTSHTSHTSHTSHTHTSHTSHTSHTHTHTHTSHTHXXTSZX'

Benefits:
We can do Clifford+T decompositions!

Possible Drawbacks:

  • There was a lot of recursion being used in the original code, because it works in Haskell. I copied directly until code was working. I don't think performance should be a problem, so I will leave it, but it would be a good future improvement to un-recursion this code now that it's in python
  • It only works with fairly low epsilon (error) bounds, and will often fail (infinite recursion, or negative determinants being square-rooted). The former can be improved with the above, and the latter can be potentially improved with the fact that we know the square root of the determinant of that big Ellipse matrix to be $2\epsilon^{-3}$. I tried locally and it caused other issues, but it's correct and should probably work, and will maybe improve the support.
  • Testing will be tough to please codecov...

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Oct 16, 2023

Codecov Report

Attention: 128 lines in your changes are missing coverage. Please review.

Comparison is base (e68a56f) 99.64% compared to head (c755168) 99.28%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4678      +/-   ##
==========================================
- Coverage   99.64%   99.28%   -0.36%     
==========================================
  Files         376      383       +7     
  Lines       33810    34995    +1185     
==========================================
+ Hits        33689    34746    +1057     
- Misses        121      249     +128     
Files Coverage Δ
...nsforms/decompositions/clifford_plus_t/__init__.py 100.00% <100.00%> (ø)
...transforms/decompositions/clifford_plus_t/rings.py 100.00% <100.00%> (ø)
...forms/decompositions/clifford_plus_t/gridpoints.py 98.90% <98.90%> (ø)
...nsforms/decompositions/clifford_plus_t/newsynth.py 97.26% <97.26%> (ø)
...ransforms/decompositions/clifford_plus_t/shapes.py 95.77% <95.77%> (ø)
...sforms/decompositions/clifford_plus_t/synthesis.py 91.34% <91.34%> (ø)
...orms/decompositions/clifford_plus_t/diophantine.py 64.05% <64.05%> (ø)

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

@trbromley trbromley added the do not merge ⚠️ Do not merge the pull request until this label is removed label Oct 16, 2023
@mlxd mlxd closed this Oct 20, 2023
@mlxd mlxd deleted the clifford-t-decomp branch October 20, 2023 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge ⚠️ Do not merge the pull request until this label is removed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants