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_cz_depth_line_mr to Rust #12949

Merged
merged 22 commits into from
Aug 20, 2024

Conversation

jlofti
Copy link
Contributor

@jlofti jlofti commented Aug 13, 2024

Summary

This PR is aims to advance #12212 by porting #12229 specifically.

Details and comments

This PR ports the synth_cz_depth_line_mr function to Rust. Notes:

  • I added the port in the lnn.rs file similar to how pmh.rs is currently implemented.
  • Following the advice of using gate sequences as a return type then building circuits from CircuitData.
  • Made a little bit of optimizations by removing an inner j and patcnt loop in synth_cz_depth_line_mr
  • Broke out shared logic into lnn::_append_phase_gate
  • Added _append_cx_stage1/2 in the lnn.rs file, but can break them out if we want them in seperate file more similar to Python

Please let me know any changes to make and I'll take care of it

Thanks!

fixes #12229

Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Aug 13, 2024
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

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

@CLAassistant
Copy link

CLAassistant commented Aug 13, 2024

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

coveralls commented Aug 15, 2024

Pull Request Test Coverage Report for Build 10465567689

Details

  • 160 of 161 (99.38%) changed or added relevant lines in 7 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.04%) to 89.589%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/synthesis/linear_phase/cz_depth_lnn.rs 101 102 99.02%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
crates/qasm2/src/lex.rs 4 91.98%
Totals Coverage Status
Change from base Build 10459812488: 0.04%
Covered Lines: 67644
Relevant Lines: 75505

💛 - Coveralls

@ShellyGarion ShellyGarion added synthesis Rust This PR or issue is related to Rust code in the repository labels Aug 15, 2024
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.

@jlofti - thank you very much for your contribution to Qiskit!
I have a few preliminary comments, about the code structure and performance.

First, could you please transfer your code to a new directory called:
crates/accelerate/src/synthesis/linear_phase
(as CZ circuits are not linear).

Note that there is another algorithm to synthesize linear circuits for LNN (see also #12228)

In addition, could you perhaps compare the performance of the new rust and original python code for CZ circuits of different sizes ?

crates/accelerate/src/synthesis/linear/mod.rs Outdated Show resolved Hide resolved
crates/accelerate/src/synthesis/linear/lnn.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @jlofti! Could you maybe run a small benchmark comparing the runtime of the old Python code and the new Rust implementation? It can be something a simple as taking a random binary matrix (rather large, maybe 1000x1000?) and running both codes from the Python interface 🙂

crates/accelerate/src/synthesis/linear/lnn.rs Outdated Show resolved Hide resolved
crates/accelerate/src/synthesis/linear/lnn.rs Outdated Show resolved Hide resolved
crates/accelerate/src/synthesis/linear/lnn.rs Outdated Show resolved Hide resolved
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
@jlofti
Copy link
Contributor Author

jlofti commented Aug 15, 2024

Hi @ShellyGarion @Cryoris ,

Thanks for all the feedback! Made all the changes, hopefully everything will come back good in CI/CD. My machine was having trouble but for a binary matrix of 350x350 I get the results:

Rust: 308s
Python: 300s

Please let me know if everything looks good or if more changes should be made!

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.

Thanks @jlofti for handling our comments.

  • Usually, the rust code should be much faster than python. Could you perhaps check why this is not the case here?

  • Could you please update the current reverse permutation code to use rust code?

  • Could you please add release notes stating that the CZ synthesis code and reverse permutation code for LNN were ported to rust (you can see examples in previous PRs) ?

@alexanderivrii
Copy link
Contributor

alexanderivrii commented Aug 16, 2024

Hi @jlofti, when you compile the rust code, do you compile it in the release mode?

python setup.py build_rust --inplace --release

Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
@jlofti
Copy link
Contributor Author

jlofti commented Aug 16, 2024

Thanks @jlofti for handling our comments.

* Usually, the rust code should be much faster than python. Could you perhaps check why this is not the case here?

* Could you please update the current [reverse permutation code](https://github.com/Qiskit/qiskit/blob/main/qiskit/synthesis/permutation/permutation_reverse_lnn.py) to use rust code?

* Could you please add release notes stating that the CZ synthesis code and reverse permutation code for LNN were ported to rust (you can see examples in previous PRs) ?

Hi @jlofti, when you compile the rust code, do you compile it in the release mode?

python setup.py build_rust --inplace --release

Thats exactly what happened lol. Redid the benchmarking and Rust takes about 10s. So seeing around 30x speedup. Also added release notes and changed synth_permutation_reverse_lnn_kms to use Rust implementation

Copy link
Contributor

@alexanderivrii alexanderivrii left a comment

Choose a reason for hiding this comment

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

The PR is in a great shape. I had a minor nitpick about promoting comments to docstrings and some suggestions for the release notes, but other than that it looks ready. The 30x improvement in performance is nice :).

jlofti and others added 2 commits August 18, 2024 18:35
…41948112a.yaml

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>
Signed-off-by: jlofti <65878716+jlofti@users.noreply.github.com>
@jlofti
Copy link
Contributor Author

jlofti commented Aug 18, 2024

The PR is in a great shape. I had a minor nitpick about promoting comments to docstrings and some suggestions for the release notes, but other than that it looks ready. The 30x improvement in performance is nice :).

Thanks! Promoted the comments to docstrings and added the ones you requested 👍

Co-authored-by: Julien Gacon <gaconju@gmail.com>
@jlofti
Copy link
Contributor Author

jlofti commented Aug 19, 2024

@ShellyGarion @Cryoris @alexanderivrii

Hi all, was looking for guidance on how to best deduce whats happening with the mac python 3.12 unit tests failing now? from the commit release notes and simplified comments to promoted to docstring and added new docstrings no code changes were made (just comments). Scratching my head a little trying to track down why these tests are failing now in the qft tests. Looks numpy related?

@ShellyGarion
Copy link
Member

@jlofti - it seems that the failing tests are not related to your PR. Qiskit team in handling it.

@jlofti
Copy link
Contributor Author

jlofti commented Aug 19, 2024

@jlofti - it seems that the failing tests are not related to your PR. Qiskit team in handling it.

Ok cool thanks! Just wanted to double check. Took care of all comments so standing by

@ShellyGarion
Copy link
Member

The CI failures should be fixed after #12979 will be merged

Copy link
Contributor

@alexanderivrii alexanderivrii left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for addressing our comments.

@alexanderivrii alexanderivrii added this pull request to the merge queue Aug 20, 2024
Merged via the queue into Qiskit:main with commit a11e76c Aug 20, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo Rust This PR or issue is related to Rust code in the repository synthesis
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Port synth_cz_depth_line_mr to Rust
7 participants