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

Port synth_clifford_greedy to Rust #12601

Merged
merged 47 commits into from
Jul 3, 2024

Conversation

alexanderivrii
Copy link
Contributor

@alexanderivrii alexanderivrii commented Jun 18, 2024

Summary

Addresses #12235.

This PR ports the method synth_clifford_greedy for synthesizing Clifford operators to Rust. In the process the algorithm was additionally improved by (1) ignoring all (expensive!) clifford phase computations till the very end, thus working on the "symplectic matrix" part of the tableau, (2) completely avoiding (expensive!) clifford evolve and clifford adjoint operations by pre-composing rather than post-composing the current Clifford with elementary Cliffords (corresponding to CX, SWAP, S, and H gates), (3) almost completely avoiding copying clifford objects, (4) fixing an inefficiency in cost computations.

Runtime improvements:
On my laptop, the old python code takes about 400 seconds to synthesize 50 random 50-qubit Cliffords, and the new code takes 0.25 seconds to do the same. So it's at least 1000x improvement in performance.

Quality improvements:
After fixing an inaccuracy in cost computations, the number of SWAPs introduced by the new algorithm is smaller than before; the totals for the 50 random cliffords from before are:

total_swap_old = 1688, total_cx_old = 63179
total_swap_new = 418, total_cx_new = 63283

@alexanderivrii alexanderivrii requested review from ShellyGarion and a team as code owners June 18, 2024 11:18
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @kevinhartman
  • @mtreinish

@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9564063418

Details

  • 27 of 625 (4.32%) changed or added relevant lines in 6 files are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.8%) to 89.0%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/operations.rs 0 8 0.0%
crates/accelerate/src/linear_matrix.rs 8 127 6.3%
crates/accelerate/src/clifford.rs 5 476 1.05%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 2 92.62%
Totals Coverage Status
Change from base Build 9562946226: -0.8%
Covered Lines: 63419
Relevant Lines: 71257

💛 - Coveralls

@ShellyGarion
Copy link
Member

ShellyGarion commented Jun 18, 2024

it will be better to merge #12456 and #12598 and #12560 before this PR

@coveralls
Copy link

coveralls commented Jun 18, 2024

Pull Request Test Coverage Report for Build 9567144979

Details

  • 27 of 620 (4.35%) changed or added relevant lines in 6 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.7%) to 89.005%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/operations.rs 0 8 0.0%
crates/accelerate/src/linear_matrix.rs 8 127 6.3%
crates/accelerate/src/clifford.rs 5 471 1.06%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 3 92.37%
Totals Coverage Status
Change from base Build 9562946226: -0.7%
Covered Lines: 63418
Relevant Lines: 71252

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9577770854

Details

  • 27 of 597 (4.52%) changed or added relevant lines in 5 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.7%) to 89.049%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/linear_matrix.rs 8 124 6.45%
crates/accelerate/src/clifford.rs 5 459 1.09%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 3 92.88%
Totals Coverage Status
Change from base Build 9571576575: -0.7%
Covered Lines: 63572
Relevant Lines: 71390

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 24, 2024

Pull Request Test Coverage Report for Build 9641284102

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 490 of 592 (82.77%) changed or added relevant lines in 5 files are covered.
  • 723 unchanged lines in 53 files lost coverage.
  • Overall coverage decreased (-0.06%) to 89.696%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/linear_matrix.rs 75 124 60.48%
crates/accelerate/src/clifford.rs 406 459 88.45%
Files with Coverage Reduction New Missed Lines %
qiskit/converters/circuit_to_gate.py 1 97.44%
crates/qasm2/src/expr.rs 1 94.02%
qiskit/compiler/assembler.py 1 96.13%
qiskit/converters/circuit_to_instruction.py 1 98.21%
qiskit/assembler/assemble_schedules.py 1 98.35%
qiskit/qasm2/export.py 1 98.5%
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/pulse/configuration.py 2 96.08%
qiskit/circuit/library/hamiltonian_gate.py 2 83.33%
qiskit/circuit/library/standard_gates/xx_plus_yy.py 2 95.74%
Totals Coverage Status
Change from base Build 9571576575: -0.06%
Covered Lines: 63972
Relevant Lines: 71321

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 29, 2024

Pull Request Test Coverage Report for Build 9722822853

Details

  • 412 of 462 (89.18%) changed or added relevant lines in 5 files are covered.
  • 19 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.04%) to 89.735%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/clifford/mod.rs 15 16 93.75%
crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs 389 438 88.81%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 4 92.88%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 9718390567: -0.04%
Covered Lines: 64306
Relevant Lines: 71662

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 29, 2024

Pull Request Test Coverage Report for Build 9725700405

Details

  • 414 of 463 (89.42%) changed or added relevant lines in 5 files are covered.
  • 31 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.05%) to 89.72%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs 393 442 88.91%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 4 92.88%
crates/qasm2/src/parse.rs 24 96.23%
Totals Coverage Status
Change from base Build 9718390567: -0.05%
Covered Lines: 64296
Relevant Lines: 71663

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 29, 2024

Pull Request Test Coverage Report for Build 9725842348

Details

  • 414 of 463 (89.42%) changed or added relevant lines in 5 files are covered.
  • 27 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.06%) to 89.713%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs 393 442 88.91%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 6 91.86%
crates/qasm2/src/parse.rs 18 96.23%
Totals Coverage Status
Change from base Build 9718390567: -0.06%
Covered Lines: 64291
Relevant Lines: 71663

💛 - Coveralls

@alexanderivrii
Copy link
Contributor Author

alexanderivrii commented Jun 30, 2024

This is ready for review (@ShellyGarion, @Cryoris). All suggestions to make the code even faster or more "rusty" are highly welcome.

Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

The code looks great! My main comment is that we should think a little bit on the API, since it will be used for other Clifford synthesis methods as well as to port the Clifford class to rust later (#12367)

struct Clifford {
/// Number of qubits.
num_qubits: usize,
/// Matrix with dimensions (2 * num_qubits) x (2 * num_qubits + 1).
Copy link
Member

Choose a reason for hiding this comment

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

what's the reason to have the phases as the last line of the matrix and not as a separate vector (unlike the current Clifford class)? will it be useful later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, this is an interesting suggestion, which has its pros and cons. The current form is consistent with how we store a Clifford tableau in Python, but we can change it of course. It is also easier to perform row operations directly on the (2n) x (2n+1) matrix.

@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9741012360

Details

  • 414 of 463 (89.42%) changed or added relevant lines in 6 files are covered.
  • 13 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.03%) to 89.742%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs 256 263 97.34%
crates/accelerate/src/synthesis/clifford/utils.rs 137 179 76.54%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 4 92.62%
crates/qasm2/src/parse.rs 6 97.61%
Totals Coverage Status
Change from base Build 9718390567: -0.03%
Covered Lines: 64312
Relevant Lines: 71663

💛 - Coveralls

Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

LGTM except a minor comment.
The implementation and correctness of the algorithm look good to me.
I would be happy if someone could also look into the rust implementation aspects.

@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9741620005

Details

  • 414 of 463 (89.42%) changed or added relevant lines in 6 files are covered.
  • 8 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.03%) to 89.748%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs 256 263 97.34%
crates/accelerate/src/synthesis/clifford/utils.rs 137 179 76.54%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 5 92.11%
Totals Coverage Status
Change from base Build 9718390567: -0.03%
Covered Lines: 64316
Relevant Lines: 71663

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9743540137

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 414 of 463 (89.42%) changed or added relevant lines in 6 files are covered.
  • 153 unchanged lines in 9 files lost coverage.
  • Overall coverage decreased (-0.02%) to 89.752%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs 256 263 97.34%
crates/accelerate/src/synthesis/clifford/utils.rs 137 179 76.54%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/standard_gates/rxx.py 1 97.56%
qiskit/circuit/library/standard_gates/rzx.py 1 97.56%
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/circuit/library/standard_gates/ryy.py 2 95.12%
qiskit/circuit/library/standard_gates/rzz.py 2 94.74%
crates/qasm2/src/lex.rs 8 91.35%
crates/qasm2/src/parse.rs 12 97.15%
qiskit/circuit/quantumcircuit.py 21 95.5%
crates/circuit/src/operations.rs 105 76.04%
Totals Coverage Status
Change from base Build 9718390567: -0.02%
Covered Lines: 64452
Relevant Lines: 71811

💛 - Coveralls

@ShellyGarion ShellyGarion added this pull request to the merge queue Jul 3, 2024
Merged via the queue into Qiskit:main with commit 0f585bd Jul 3, 2024
15 checks passed
@ElePT ElePT added the Changelog: New Feature Include in the "Added" section of the changelog label Jul 30, 2024
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
* starting to experiment

* porting code

* messy code porting

* printing statements to enable debugging

* fixes

* fixing phase

* removing some of the printing statements

* fixing inaccuracy for cost computation

* Moving some of the functionality to SymplecticMatrix class

* reducing the number of warnings

* formatting

* replacing expensive adjoint and compose operations for symplectic matrices by significantly cheaper in-place prepend operations

* resolving merge conflicts

* cleanup

* code cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* using fast lookup

* cleanup

* clippy

* including params in gate_seq to avoid mapping

* removing unnecessary inner function

* cleanup

* renaming

* changes on the python side

* reno

* adding error handling

* improved error handling

* removing redundant Ok(Some(...))

* using random_clifford in tests

* reorganizing clifford code

* fixes

* formatting

* improved error handling

* do not panic

* formatting

* Applying refactoring suggestions d/utils.rs from code review

* release notes update

* adding comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog performance Rust This PR or issue is related to Rust code in the repository synthesis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants