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

Move utility functions _inverse_pattern and _get_ordered_swap to Rust #12327

Merged
merged 14 commits into from
Jun 10, 2024

Conversation

jpacold
Copy link
Contributor

@jpacold jpacold commented May 2, 2024

Summary

This PR starts addressing #12225. I moved the implementations of two functions from qiskit/synthesis/permutation/permutation_utils.py to Rust.

Details and comments

  • Created permutation.rs

    • I added some checks to ensure that inputs are actually permutations. Otherwise, _get_ordered_swap uses the same logic as it did in Python.
    • Performance is better than Python, but sensitive to input type (more notes below)
  • Added a module qiskit._accelerate.permutation and a submodule qiskit._accelerate.permutation.permutation_utils (following the structure of the existing Python modules). The wrapped functions are in the permutation_utils submodule.

  • Replaced the function bodies in permutation_utils.py with calls to the Rust functions.

  • Added tests.

Performance

As expected, this implementation of _get_ordered_swap is faster than the Python version. However, the input type is expected to be an array of int64 and there is a performance cost for converting from any other type. On my machine:

Input type Speedup over Python
Numpy array (int64) ~15x
Numpy array (int32, uint32, or uint64) ~4x
Python list ~7x

For now, I don't think this matters much, because the bottleneck in synth_permutation_basic is constructing the circuit, but it is something that might need to be optimized later on.

@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 2, 2024
@CLAassistant
Copy link

CLAassistant commented May 2, 2024

CLA assistant check
All committers have signed the CLA.

@mtreinish mtreinish added performance synthesis Rust This PR or issue is related to Rust code in the repository labels May 2, 2024
@mtreinish mtreinish added this to the 1.2.0 milestone May 2, 2024
@coveralls
Copy link

coveralls commented May 2, 2024

Pull Request Test Coverage Report for Build 9411521330

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

  • 78 of 80 (97.5%) changed or added relevant lines in 4 files are covered.
  • 139 unchanged lines in 11 files lost coverage.
  • Overall coverage decreased (-0.02%) to 89.577%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/permutation.rs 75 77 97.4%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
crates/qasm2/src/lex.rs 3 93.13%
qiskit/compiler/transpiler.py 4 92.52%
qiskit/circuit/parameterexpression.py 5 96.51%
qiskit/qasm2/parse.py 6 96.7%
crates/qasm2/src/parse.rs 12 96.69%
qiskit/transpiler/preset_passmanagers/init.py 13 90.3%
crates/circuit/src/circuit_instruction.rs 18 89.09%
crates/circuit/src/circuit_data.rs 24 93.98%
qiskit/visualization/circuit/text.py 24 95.23%
Totals Coverage Status
Change from base Build 9291000954: -0.02%
Covered Lines: 62474
Relevant Lines: 69743

💛 - Coveralls

@jpacold jpacold marked this pull request as ready for review May 7, 2024 04:00
@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 the following people are requested to review this:

@raynelfss raynelfss self-assigned this May 22, 2024
@raynelfss
Copy link
Contributor

Is this still a work in progress? @jpacold if it's not, it would be helpful to remove the [WIP] from the title as it seems to me this has been completed.

@jpacold jpacold changed the title [WIP] Move utility functions _inverse_pattern and _get_ordered_swap to Rust Move utility functions _inverse_pattern and _get_ordered_swap to Rust May 24, 2024
@jpacold
Copy link
Contributor Author

jpacold commented May 24, 2024

@raynelfss Done, thanks for pointing that out.

raynelfss
raynelfss previously approved these changes May 30, 2024
Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

LGTM, just a small correction on the docstrings for your rust functions. Thank you for working on this!

crates/accelerate/src/permutation.rs Outdated Show resolved Hide resolved
crates/accelerate/src/permutation.rs Outdated Show resolved Hide resolved
jpacold and others added 2 commits May 29, 2024 21:09
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.

Just on comment otherwise LGTM as well 👍🏻

crates/accelerate/src/permutation.rs 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.

LGTM thanks for the updates! If @ShellyGarion and @raynelfss are good with it I think we can merge 👍🏻

Edit: both are good with the changes 🙂

@Cryoris Cryoris added this pull request to the merge queue Jun 10, 2024
Merged via the queue into Qiskit:main with commit 1956220 Jun 10, 2024
15 checks passed
Cryoris added a commit to Cryoris/qiskit-terra that referenced this pull request Jun 19, 2024
also port circuit construction to rust and add a reno
github-merge-queue bot pushed a commit that referenced this pull request Jun 24, 2024
* Follow up on #12327

also port circuit construction to rust and add a reno

* move _get_ordered_swap to Rust only

* drop redundant Ok(expect())

* proper synthesis structure
@ElePT ElePT added the Changelog: None Do not include in changelog label Jul 31, 2024
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
…Qiskit#12327)

* Move utility functions _inverse_pattern and _get_ordered_swap to Rust

* fix formatting and pylint issues

* Changed input type to `PyArrayLike1<i64, AllowTypeChange>`

* Refactor `permutation.rs`, clean up imports, fix coverage error

* fix docstring for `_inverse_pattern`

Co-authored-by: Raynel Sanchez <[email protected]>

* fix docstring for `_get_ordered_swap`

Co-authored-by: Raynel Sanchez <[email protected]>

* remove pymodule nesting

* remove explicit `AllowTypeChange`

* Move input validation out of `_inverse_pattern` and `_get_ordered_swap`

---------

Co-authored-by: Raynel Sanchez <[email protected]>
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
* Follow up on Qiskit#12327

also port circuit construction to rust and add a reno

* move _get_ordered_swap to Rust only

* drop redundant Ok(expect())

* proper synthesis structure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo performance 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.

9 participants