https://github.com/cqcl/grid_synthesis
Implementing Ross/Selinger's z-rotation synthesis (1403.2975v3) in Rust for real-time profiling purposes.
Science Score: 36.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
✓Academic publication links
Links to: arxiv.org -
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.1%) to scientific vocabulary
Repository
Implementing Ross/Selinger's z-rotation synthesis (1403.2975v3) in Rust for real-time profiling purposes.
Basic Info
- Host: GitHub
- Owner: CQCL
- Language: Rust
- Default Branch: main
- Size: 3.99 MB
Statistics
- Stars: 2
- Watchers: 5
- Forks: 1
- Open Issues: 1
- Releases: 0
Metadata Files
README.md
grid_synthesis
Implementing Ross/Selinger's z-rotation synthesis (1403.2975v3) in Rust for real-time profiling purposes.
WARNING: Nihar is an amateur coder. You have been warned.
Organization
src contains the codes.
src/structs contains the rings
src/tests contain some tests
src/algorithms contain the algorithms
Running
Do cargo test -- --nocapture in the repository for debugging, testing.
Do cargo run in the repository for running the hashtable generating algorithm.
(Note: this requires a file named data/gates_with_small_t_count.dat to exist;
you should create this file before running cargo run for the first time.)
Run the following for some nice output about exact_synth.
cargo test exact_synth_tests::testing_exact_synth_rapidly_with_long_sequences -- --nocapture
Run the following to see sum output about inexact_synth. The following test will often fail,
and is a good way to explore the current problems with the code.
cargo test inexact_synth_tests::random_inexact_synth_test -- --nocapure
Run the following to test code coverage. It will generate an html report in \target that
you can browse happily.
cargo llvm-cov --html
Testing
Tests that fail will probably do so due to one of the following - Floating point weirdness. - Integer overflows
Plan of implementation
Section 7.3 of [1] outlines the main algorithm.
- [X] Write an exact synthesis library (to do Step 3 as in the outline)
- [X] Figure out prime factorization (implement or include)
- [O] Debug exact synthesis
- [X] Works for single H gate
- [X] Works for single T gate
- [X] Works for HT gate!
- [X] Works for really long gate sequences!!!
- [o] Debug inexact synthesis
- [X] Fix the erroroneous prime factorization in the KMMring
- [X] Write tests.
- [ ] Works upto $\varepsilon \simeq 10^-3$.
- [ ] Generalize to gates like $X{\pi/2},Y{\pi/2},Z_{\pi/2}$.
Some notes and overall verdict
I will make a verbose description of the state of affairs.
What works in the code
- Gate synthesis!
- Various number theoretic rings that are of interest for this project.
- Exact gate synthesis. Given a long chain of "H" and "T" gates, it can do some number theory and give out a shorter one. This performs better with larger strings than with smaller strings
- Hash table reading, writing. This could possibly be repurposed to do brute-force gate searches.
- Lenstra–Lenstra–Lovász algorithm for 4d dimensional lattice basis reduction.
What doesn't work and why
- The
Inttype use insrc/structs/rings/mod.rsis ai128(which is still better than ani64used initially). But at some point, if I have $x=a+b\omega+c\omega^2+d\omega^3 \in \mathbb{Z}[\omega]$, where $\omega = e^{\tfrac{1}{4}i\pi}$ and if I want to compute $N(x)$, this would involve taking fourth powers of $a,b,c,d$. This is actually a very routine computation that is needed to calculategcdin this ring and in the cases where it needs to be done, $a,b,c,d$ might already be coming from sums of squares of integers.i128is the largest integer type Rust allows and this means that working with integers bigger than 65536 could lead to overflows. Now there is aBigIntcrate that could be called, but this does not implement thatCopytrait, which is because aBigIntcannot have memory allocated during compile. Not usingCopymakes doing arithmetics much harder and usingBigIntwould produce very ugly code with stuff likerandom_integer+Int::one()instead ofrandom_integer+1. The fallout is that we are not able to get gate synthesis for values below $\varepsilon < 10^-3$.
Proposed changes that I did not get time for
- Replace the
i128insrc/structs/rings/mod.rswithBigIntfromnum::BigInt. Remove borrowing theCopytrait in some structs as prompted by the compiler. Take care of the >1000 bugs that it throws thereafter. An alternate way could be to write anIntstruct that works likei512or something. - Once the integer overflow problem is pushed away, we would run into problems with prime factorization. The Ross-Selinger paper throws away lattice points for which prime factorization is too hard. Right now, this isn't a problem but when it happens, one of the two fixes are possible:
- Do as they do. Throw away primes for which factorization takes longer than average.
- Make a parallel process for each lattice point at the point where factorization has to occur. When at least one point leads to a succesful gate find, stop adding new processes. The point of doing this is, we get a "second to optimal" $T$-count quickly and then some more optimal "T"-counts after the processes finish. I did not get time to implement this, but I don't think it should take more than a week or two to do this.
- Almost all the overflow errors happen in
structs/rings/zomega.rsat line 224. That's because we're taking norms which involves taking fourth powers. There might be a sleek way to do the same operation without this? - The Ross-Selinger paper suggests that once you get find a gate $U$ that is close to the $e^{i\theta Z}$, running
exact_synthfor both $U$ and $T^{-1} U T$ and take the one with the smaller $T$ count. This has only effects upto global phase and perhaps pytket will already do this optimization if needed. But writing this is pending since the final description of gate sequences is not clear (is it an array? Binary sequence? String?). - Apply the Rust warnings. I have turned off the warnings in the
main.rs,*/mod.rsfiles because they were annoying. They can be fixed easily for someone inclined. I rancargo fixandcargo fix --clippya few times but I couldn't get them all. It mostly contains useless stuff like unused imports, redundant parantheses and uppercase/lowercase variable names.
Formula for zomega.rs that could be more efficiently implemented to fix integer overflows
What we need is to edit to output the following in line 217 of zomega.rs to make it accomodate larger integers than i128. Here is the formula.
Suppose $\text{self} = x0 + x1\omega + x2 \omega^2 + x3 \omega^3$ and $\text{other} = y0 + y1\omega + y2 \omega^2 + y3 \omega^3$. What we want is to calculate the integers:
```
z3 = (((x3y0 - x0y1 - x1y2 - x2y3)*y0 + (x0*y0 + x1*y1 + x2*y2 + x3*y3)y1 - (x1y0 + x2y1 + x3y2 - x0y_3)y2 + (x2y0 + x3y1 - x0y2 - x1y3)*y3)y0 - ((x2y0 + x3y1 - x0y2 - x1y_3)y0 - (x3y0 - x0y1 - x1y2 - x2y3)*y1 - (x0*y0 + x1*y1 + x2*y2 + x3*y3)y2 + (x1y0 + x2y1 + x3y2 - x0y_3)y3)*y1 + ((x1*y0 + x2*y1 + x3*y2 - x0*y3)y0 - (x2y0 + x3y1 - x0y2 - x1y_3)y1 + (x3y0 - x0y1 - x1y2 - x2y3)*y2 + (x0*y0 + x1*y1 + x2*y2 + x3*y3)y_3)y2 - ((x0y0 + x1y1 + x2y2 + x3y3)*y0 - (x1*y0 + x2*y1 + x3*y2 - x0*y3)y1 + (x2y0 + x3y1 - x0y2 - x1y_3)y2 - (x3y0 - x0y1 - x1y2 - x2y3)*y3)*y_3) ;
z2 = (((x2y0 + x3y1 - x0y2 - x1y3)*y0 - (x3*y0 - x0*y1 - x1*y2 - x2*y3)y1 - (x0y0 + x1y1 + x2y2 + x3y_3)y2 + (x1y0 + x2y1 + x3y2 - x0y3)*y3)y0 - ((x1y0 + x2y1 + x3y2 - x0y_3)y0 - (x2y0 + x3y1 - x0y2 - x1y3)*y1 + (x3*y0 - x0*y1 - x1*y2 - x2*y3)y2 + (x0y0 + x1y1 + x2y2 + x3y_3)y3)*y1 + ((x0*y0 + x1*y1 + x2*y2 + x3*y3)y0 - (x1y0 + x2y1 + x3y2 - x0y_3)y1 + (x2y0 + x3y1 - x0y2 - x1y3)*y2 - (x3*y0 - x0*y1 - x1*y2 - x2*y3)y_3)y2 + ((x3y0 - x0y1 - x1y2 - x2y3)*y0 + (x0*y0 + x1*y1 + x2*y2 + x3*y3)y1 - (x1y0 + x2y1 + x3y2 - x0y_3)y2 + (x2y0 + x3y1 - x0y2 - x1y3)*y3)*y_3) ;
z1 = (((x1y0 + x2y1 + x3y2 - x0y3)*y0 - (x2*y0 + x3*y1 - x0*y2 - x1*y3)y1 + (x3y0 - x0y1 - x1y2 - x2y_3)y2 + (x0y0 + x1y1 + x2y2 + x3y3)*y3)y0 - ((x0y0 + x1y1 + x2y2 + x3y_3)y0 - (x1y0 + x2y1 + x3y2 - x0y3)*y1 + (x2*y0 + x3*y1 - x0*y2 - x1*y3)y2 - (x3y0 - x0y1 - x1y2 - x2y_3)y3)*y1 - ((x3*y0 - x0*y1 - x1*y2 - x2*y3)y0 + (x0y0 + x1y1 + x2y2 + x3y_3)y1 - (x1y0 + x2y1 + x3y2 - x0y3)*y2 + (x2*y0 + x3*y1 - x0*y2 - x1*y3)y_3)y2 + ((x2y0 + x3y1 - x0y2 - x1y3)*y0 - (x3*y0 - x0*y1 - x1*y2 - x2*y3)y1 - (x0y0 + x1y1 + x2y2 + x3y_3)y2 + (x1y0 + x2y1 + x3y2 - x0y3)*y3)*y_3) ;
z0 = ((x0y0 + x1y1 + x2y2 + x3y3)*y0 - (x1*y0 + x2*y1 + x3*y2 - x0*y3)y1 + (x2y0 + x3y1 - x0y2 - x1y_3)y2 - (x3y0 - x0y1 - x1y2 - x2y3)*y3)y0 + ((x3y0 - x0y1 - x1y2 - x2y_3)y0 + (x0y0 + x1y1 + x2y2 + x3y3)*y1 - (x1*y0 + x2*y1 + x3*y2 - x0*y3)y2 + (x2y0 + x3y1 - x0y2 - x1y_3)y3)*y1 - ((x2*y0 + x3*y1 - x0*y2 - x1*y3)y0 - (x3y0 - x0y1 - x1y2 - x2y_3)y1 - (x0y0 + x1y1 + x2y2 + x3y3)*y2 + (x1*y0 + x2*y1 + x3*y2 - x0*y3)y_3)y2 + ((x1y0 + x2y1 + x3y2 - x0y3)*y0 - (x2*y0 + x3*y1 - x0*y2 - x1*y3)y1 + (x3y0 - x0y1 - x1y2 - x2y_3)y2 + (x0y0 + x1y1 + x2y2 + x3y3)*y3)*y_3 ;
n0 = ((x0^2 + x1^2 + x2^2 + x3^2)*x0 - (x0*x1 + x1*x2 - x0*x3 + x2*x3)x1 + (x0x1 + x1x2 - x0x3 + x2x_3)x3)*x0 - ((x0*x1 + x1*x2 - x0*x3 + x2*x3)x0 - (x0^2 + x1^2 + x2^2 + x_3^2)x1 + (x0x1 + x1x2 - x0x3 + x2x3)*x2)x1 - ((x0x1 + x1x2 - x0x3 + x2x_3)x1 - (x0^2 + x1^2 + x2^2 + x3^2)*x2 + (x0*x1 + x1*x2 - x0*x3 + x2*x3)x_3)x2 + ((x0x1 + x1x2 - x0x3 + x2x3)*x0 - (x0*x1 + x1*x2 - x0*x3 + x2*x3)x2 + (x0^2 + x1^2 + x2^2 + x_3^2)x3)*x3 ; ``` and then we want to return $\lfloor \frac{z0}{n0} \rceil + \lfloor \frac{z1}{n1} \rceil \omega + \lfloor \frac{z1}{n0} \rceil \omega^2 + \lfloor \frac{z2}{n0} \rceil \omega^3$ where $\lfloor\bullet \rceil$ means the nearest integer to $\bullet$.
Feel free to write to me at for any discussions, or just start an issue on this repo.
References
- [1] Ross Selinger paper.
- [2] Original Haskell code
- [3] Seyon's haunted code
- [4] Golden gates paper
Owner
- Name: Cambridge Quantum
- Login: CQCL
- Kind: organization
- Location: Cambridge, UK
- Website: http://www.cambridgequantum.com
- Repositories: 48
- Profile: https://github.com/CQCL
Quantum Software and Technologies
GitHub Events
Total
- Watch event: 1
- Fork event: 1
Last Year
- Watch event: 1
- Fork event: 1
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Nihar | n****a@q****m | 108 |
| Breakfastisready | s****y@t****m | 30 |
| Ben Criger | b****r@g****m | 11 |
| nihargargava | n****a@g****m | 2 |
| Alec Edgington | 5****c | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 0
- Total pull requests: 2
- Average time to close issues: N/A
- Average time to close pull requests: 3 days
- Total issue authors: 0
- Total pull request authors: 2
- Average comments per issue: 0
- Average comments per pull request: 0.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
- ss2165 (1)
- cqc-alec (1)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- approx 0.5.1
- autocfg 1.1.0
- bytemuck 1.12.3
- cfg-if 1.0.0
- either 1.8.0
- getrandom 0.2.8
- itertools 0.10.5
- libc 0.2.137
- matrixmultiply 0.3.2
- nalgebra 0.31.4
- nalgebra-macros 0.1.0
- num 0.4.0
- num-bigint 0.4.3
- num-complex 0.4.2
- num-integer 0.1.45
- num-iter 0.1.43
- num-rational 0.4.1
- num-traits 0.2.15
- paste 1.0.9
- ppv-lite86 0.2.17
- prime_factorization 1.0.3
- proc-macro2 1.0.47
- quote 1.0.21
- rand 0.8.5
- rand_chacha 0.3.1
- rand_core 0.6.4
- rawpointer 0.2.1
- safe_arch 0.6.0
- simba 0.7.3
- syn 1.0.103
- typenum 1.15.0
- unicode-ident 1.0.5
- wasi 0.11.0+wasi-snapshot-preview1
- wide 0.7.5