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

[Oxidize BasisTranslator]: Move the rest of the BasisTranslator to Rust. #13237

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

Conversation

raynelfss
Copy link
Contributor

Fixes #12246

This is the final act of the efforts to move the BasisTranslator transpiler pass into Rust. With many of the parts of this pass already living in Rust, the following commits attempt to bring in the final changes to allow complete operation of this pass in the Rust space (with of course some interaction with Python.)

Summary

This is the final act of the efforts to move the BasisTranslator transpiler pass into Rust. With many of the parts of this pass already living in Rust, the following commits attempt to bring in the final changes to allow complete operation of this pass in the Rust space (with of course some interaction with Python.)

Details and comments

The way this works is by keeping the original BasisTranslator python class, and have it store the rust-space counterpart (now called CoreBasisTranslator) which will perform all of the operations leveraging the existent Rust API's available for the Target (#12292), EquivalenceLibrary(#12585) and the BasisTranslator methods basis_search (#12811) and compose_transforms(#13137).

All of the inner methods will have private visibility and will not be accessible to Python as they're intended to be internal by design.

By removing the extra layers of conversion we should be seeing a considerable speed-up, alongside all of the other incremental improvements we have made.

Changes:

  • Add the pyo3 class/struct BasisTranslator that will contain all of the main data used by the transpiler pass to perform its operation.
  • Convert the target_basis into a set manually from python before sending it into the Rust space.
  • Remove the exposure of basis_search and compose_transforms to python.
  • Change basis_search so that it accepts references to HashSet instances instead of accepting a HashSet<&str> instance.
  • Change inner method's visibility for basis_search and compose_transform modules in rust.
  • Expose the exception imports from Target to the accelerate crate.
  • Expose DAGCircuit::copy_empty_like to the rest of the crates.
  • Remove all of the unused imports in the Python-side BasisTranslator.

Blockers:

Once these changes have merged this branch will be properly rebased.

raynelfss and others added 11 commits September 26, 2024 09:29
- Add rust counterpart for `basis_search`.
- Consolidated the `BasisSearchVisitor` into the function due to differences in rust behavior.
- Due to the nature of `hashbrown` we must use owned Strings instead of `&str`.
- Remove import of `random` in `basis_translator`.
Fixes Port `BasisTranslator` to Rust Qiskit#12246

This is the final act of the efforts to move the `BasisTranslator` transpiler pass into Rust. With many of the parts of this pass already living in Rust, the following commits attempt to bring in the final changes to allow complete operation of this pass in the Rust space (with of course some interaction with Python.)

Methodology:
The way this works is by keeping the original `BasisTranslator` python class, and have it store the rust-space counterpart (now called `CoreBasisTranslator`) which will perform all of the operations leveraging the existent Rust API's available for the `Target` (Qiskit#12292), `EquivalenceLibrary`(Qiskit#12585) and the `BasisTranslator` methods `basis_search` (Qiskit#12811) and `compose_transforms`(Qiskit#13137).

All of the inner methods will have private visibility and will not be accessible to `Python` as they're intended to be internal by design.

By removing the extra layers of conversion we should be seeing a considerable speed-up, alongside all of the other incremental improvements we have made.

Changes:

- Add the pyo3 class/struct `BasisTranslator` that will contain allof the main data used by the transpiler pass to perform its operation.
- Convert the `target_basis` into a set manually from python before sending it into the Rust space.
- Remove the exposure of `basis_search` and `compose_transforms` to python.
- Change `basis_search` so that it accepts references to `HashSet` instances instead of accepting a `HashSet<&str>` instance.
- Change inner method's visibility for `basis_search` and `compose_transform` modules in rust.
- Expose the exception imports from `Target` to the `accelerate` crate.
- Expose `DAGCircuit::copy_empty_like` to the rest of the crates.
- Remove all of the unused imports in the Python-side `BasisTranslator`.

Blockers:
- [ ] Qiskit#12811
@raynelfss raynelfss added on hold Can not fix yet performance Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels Sep 27, 2024
@raynelfss raynelfss added this to the 1.3.0 milestone Sep 27, 2024
@raynelfss raynelfss requested a review from a team as a code owner September 27, 2024 23:27
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

Pull Request Test Coverage Report for Build 11079003500

Details

  • 780 of 898 (86.86%) changed or added relevant lines in 6 files are covered.
  • 18 unchanged lines in 6 files lost coverage.
  • Overall coverage decreased (-0.03%) to 88.837%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/basis/basis_translator/basis_search.rs 150 151 99.34%
crates/accelerate/src/basis/basis_translator/mod.rs 621 738 84.15%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/target.py 1 93.55%
crates/accelerate/src/two_qubit_decompose.rs 1 91.45%
crates/accelerate/src/target_transpiler/mod.rs 1 80.19%
crates/accelerate/src/equivalence.rs 3 79.71%
crates/circuit/src/dag_node.rs 6 79.95%
crates/qasm2/src/lex.rs 6 92.48%
Totals Coverage Status
Change from base Build 11078555137: -0.03%
Covered Lines: 74650
Relevant Lines: 84030

💛 - Coveralls

@raynelfss
Copy link
Contributor Author

raynelfss commented Sep 28, 2024

Here's what the benchmarks look like across the board. I'd like to have better ones to show so if you'd like to suggest one I should run please leave it in a comment:


basis_translator

Change Before [43feab3] After [14efd38] Ratio Benchmark (Parameter)
- 61.9±0.6ms 18.3±0.2ms 0.3 passes.MultipleBasisPassBenchmarks.time_basis_translator(5, 1024, ['u', 'cx', 'id'])
- 77.0±0.3ms 20.9±0.1ms 0.27 passes.MultipleBasisPassBenchmarks.time_basis_translator(5, 1024, ['rz', 'x', 'sx', 'cx', 'id'])
- 173±1ms 44.0±0.4ms 0.25 passes.MultipleBasisPassBenchmarks.time_basis_translator(14, 1024, ['u', 'cx', 'id'])
- 247±2ms 60.9±1ms 0.25 passes.MultipleBasisPassBenchmarks.time_basis_translator(20, 1024, ['u', 'cx', 'id'])
- 222±0.9ms 51.0±1ms 0.23 passes.MultipleBasisPassBenchmarks.time_basis_translator(14, 1024, ['rz', 'x', 'sx', 'cx', 'id'])
- 305±1ms 67.0±1ms 0.22 passes.MultipleBasisPassBenchmarks.time_basis_translator(20, 1024, ['rz', 'x', 'sx', 'cx', 'id'])
- 122±1ms 25.1±0.2ms 0.21 passes.MultipleBasisPassBenchmarks.time_basis_translator(5, 1024, ['rx', 'ry', 'rz', 'r', 'rxx', 'id'])
- 374±2ms 65.9±1ms 0.18 passes.MultipleBasisPassBenchmarks.time_basis_translator(14, 1024, ['rx', 'ry', 'rz', 'r', 'rxx', 'id'])
- 540±4ms 94.3±2ms 0.17 passes.MultipleBasisPassBenchmarks.time_basis_translator(20, 1024, ['rx', 'ry', 'rz', 'r', 'rxx', 'id'])

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.


utility scale

Change Before [43feab3] After [14efd38] Ratio Benchmark (Parameter)
- 137±2ms 124±0.6ms 0.9 utility_scale.UtilityScaleBenchmarks.time_bv_100('cz')
- 6.86±0.1ms 6.15±0.08ms 0.9 utility_scale.UtilityScaleBenchmarks.time_bvlike('ecr')
- 6.91±0.07ms 6.14±0.01ms 0.89 utility_scale.UtilityScaleBenchmarks.time_bvlike('cx')
- 392±2ms 345±2ms 0.88 utility_scale.UtilityScaleBenchmarks.time_qaoa('cx')
- 1.39±0.01s 1.22±0s 0.88 utility_scale.UtilityScaleBenchmarks.time_qft('ecr')
- 1.40±0.01s 1.22±0s 0.87 utility_scale.UtilityScaleBenchmarks.time_qft('cz')
- 192±0.6ms 164±1ms 0.85 utility_scale.UtilityScaleBenchmarks.time_square_heisenberg('cx')
- 585±7ms 484±3ms 0.83 utility_scale.UtilityScaleBenchmarks.time_qaoa('cz')
- 264±3ms 220±0.7ms 0.83 utility_scale.UtilityScaleBenchmarks.time_square_heisenberg('cz')
- 269±1ms 224±2ms 0.83 utility_scale.UtilityScaleBenchmarks.time_square_heisenberg('ecr')
- 569±3ms 468±2ms 0.82 utility_scale.UtilityScaleBenchmarks.time_qaoa('ecr')
- 1.31±0.01s 1.04±0.01s 0.79 utility_scale.UtilityScaleBenchmarks.time_qv('cx')
- 1.78±0.01s 1.39±0s 0.78 utility_scale.UtilityScaleBenchmarks.time_qv('cz')
- 1.70±0.01s 1.33±0.01s 0.78 utility_scale.UtilityScaleBenchmarks.time_qv('ecr')

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.


randomized benchmarking

Change Before [43feab3] <oxidize-basis-translator~1^2> After [14efd38] Ratio Benchmark (Parameter)
- 9.25±0s 4.36±0.02s 0.47 randomized_benchmarking.RandomizedBenchmarkingBenchmark.time_ibmq_backend_transpile([0, 1])
- 9.29±0.02s 4.38±0.01s 0.47 randomized_benchmarking.RandomizedBenchmarkingBenchmark.time_ibmq_backend_transpile_single_thread([0, 1])
- 4.54±0.01s 1.68±0.01s 0.37 randomized_benchmarking.RandomizedBenchmarkingBenchmark.time_ibmq_backend_transpile([0])
- 4.54±0.02s 1.68±0s 0.37 randomized_benchmarking.RandomizedBenchmarkingBenchmark.time_ibmq_backend_transpile_single_thread([0])

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

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 on hold Can not fix yet performance Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port BasisTranslator to Rust
3 participants