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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

jpacold
Copy link

@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 9311713986

Details

  • 78 of 78 (100.0%) changed or added relevant lines in 4 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.03%) to 89.625%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 93.13%
Totals Coverage Status
Change from base Build 9291000954: 0.03%
Covered Lines: 62399
Relevant Lines: 69622

💛 - 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
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
Co-authored-by: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com>
Co-authored-by: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com>
}

#[pymodule]
pub fn permutation_utils(m: &Bound<PyModule>) -> PyResult<()> {
Copy link
Member

Choose a reason for hiding this comment

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

why do you need two [pymodule]s here? I see that in other cases only one was used.

Copy link
Author

Choose a reason for hiding this comment

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

It's not really necessary. I copied the structure from the Python synthesis module:

qiskit
    |-- synthesis
        |-- permutation
            | -- permutation_full
            | -- permutation_lnn
            | -- permutation_reverse_lnn
            | -- permutation_utils

since I was thinking the idea is to eventually move all of it to Rust.

Copy link
Member

Choose a reason for hiding this comment

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

you are right that the idea is to eventually move all of the synthesis code to rust,
however, it seems that currently the structure is flat, see e.g.

sys.modules["qiskit._accelerate.two_qubit_decompose"] = qiskit._accelerate.two_qubit_decompose

sys.modules["qiskit._accelerate.euler_one_qubit_decomposer"] = (

Copy link
Author

Choose a reason for hiding this comment

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

Got it, I made another commit to flatten this as well.

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 performance Rust This PR or issue is related to Rust code in the repository synthesis
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

7 participants