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

Fully port BarrierBeforeFinalMeasurements to rust #13220

Merged
merged 3 commits into from
Sep 26, 2024

Conversation

mtreinish
Copy link
Member

Summary

This commit migrates the BarrierBeforeFinalMeasurements transpiler pass to operate fully in Rust. The full path of the transpiler pass now never leaves Rust until it has finished modifying the DAGCircuit. The one exception is when the label is not set then we still call MergeAdjacentBarriers in the python code of the pass.

This is the first step in the improvement of the performance of this pass. We can easily leverage multhreading to potentially parallelize the analysis portion of this pass that searches for the final operations and returns the set of indices. But this is blocked on #13219 which prevents us from accessing the PackedInstructions stored in the DAGCircuit in a multithreaded context.

This commit also fixes an issue related to shared references in the disjoint_utils module around barrier labels. The combine_barriers() function was incorrectly mutating the label by reference which wouldn't persist in the DAG, and this was causing failures after the barrier was originally generated in Rust with this pass now.

Details and comments

Fixes #12253

This commit migrates the BarrierBeforeFinalMeasurements transpiler pass
to operate fully in Rust. The full path of the transpiler pass now never
leaves Rust until it has finished modifying the DAGCircuit. The one
exception is when the `label` is not set then we still call
`MergeAdjacentBarriers` in the python code of the pass.

This is the first step in the improvement of the performance of this
pass. We can easily leverage multhreading to potentially parallelize the
analysis portion of this pass that searches for the final operations and
returns the set of indices. But this is blocked on Qiskit#13219 which prevents
us from accessing the PackedInstructions stored in the DAGCircuit in a
multithreaded context.

This commit also fixes an issue related to shared references in
the disjoint_utils module around barrier labels. The combine_barriers()
function was incorrectly mutating the label by reference which wouldn't
persist in the DAG, and this was causing failures after the barrier was
originally generated in Rust with this pass now.

Fixes Qiskit#12253
@mtreinish mtreinish added performance Rust This PR or issue is related to Rust code in the repository mod: transpiler Issues and PRs related to Transpiler labels Sep 24, 2024
@mtreinish mtreinish added this to the 1.3.0 milestone Sep 24, 2024
@mtreinish mtreinish requested a review from a team as a code owner September 24, 2024 20:32
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@mtreinish
Copy link
Member Author

I ran the dedicated asv microbenchmarks on this and it yielded:

Benchmarks that have improved:

| Change   | Before [1344cddb] <stash~1>   | After [f62a6d79] <port-barrier-before-final-measure>   |   Ratio | Benchmark (Parameter)                                                  |
|----------|-------------------------------|--------------------------------------------------------|---------|------------------------------------------------------------------------|
| -        | 17.1±0.1ms                    | 12.3±0.1ms                                             |    0.72 | passes.PassBenchmarks.time_barrier_before_final_measurements(14, 1024) |
| -        | 7.42±0.03ms                   | 5.12±0.1ms                                             |    0.69 | passes.PassBenchmarks.time_barrier_before_final_measurements(5, 1024)  |
| -        | 29.7±0.2ms                    | 8.93±0.2ms                                             |    0.3  | passes.PassBenchmarks.time_barrier_before_final_measurements(20, 1024) |

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

@coveralls
Copy link

coveralls commented Sep 24, 2024

Pull Request Test Coverage Report for Build 11022176994

Details

  • 90 of 93 (96.77%) changed or added relevant lines in 6 files are covered.
  • 22 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.07%) to 88.832%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/layout/disjoint_utils.py 12 13 92.31%
crates/accelerate/src/barrier_before_final_measurement.rs 73 75 97.33%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 91.73%
crates/circuit/src/dag_circuit.rs 17 87.84%
Totals Coverage Status
Change from base Build 11019425622: 0.07%
Covered Lines: 73565
Relevant Lines: 82814

💛 - Coveralls

Copy link
Contributor

@ElePT ElePT 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 taking over this pass. It looks pretty straightforward, I only had one small question.

Comment on lines +80 to +83
#[cfg(feature = "cache_pygates")]
instruction: new_barrier.clone().unbind(),
#[cfg(not(feature = "cache_pygates"))]
instruction: new_barrier.unbind(),
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the difference between doing this or simply omitting the #[cfg(feature = "cache_pygates")] comment and setting the instruction argument to new_barrier.unbind()?

Copy link
Member Author

Choose a reason for hiding this comment

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

When cache_pygates is necessary we need to clone new_barrier because it's used in the PyInstruction here and also again in the pycache for the PackedInstruction that we insert into the dag. I used the cfg macros here to conditionally compile this so we only clone if the cache_pygates feature is enabled and otherwise skip the clone() where it's not needed. This would work fine if I removed the macros and just had it always do new_barrier.clone().unbind() regardless of whether the feature was enabled, but it was just me micro-optimizing a little as the clone() and Drop for new_barrier as a Bound<PyAny> is just python ref counting and very cheap, but I figured I could just save the overhead of that here.

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Looks good!

@ElePT ElePT added this pull request to the merge queue Sep 26, 2024
Merged via the queue into Qiskit:main with commit a0c3026 Sep 26, 2024
15 checks passed
cameron-d28 pushed a commit to cameron-d28/qiskit that referenced this pull request Sep 26, 2024
* Fully port BarrierBeforeFinalMeasurements to rust

This commit migrates the BarrierBeforeFinalMeasurements transpiler pass
to operate fully in Rust. The full path of the transpiler pass now never
leaves Rust until it has finished modifying the DAGCircuit. The one
exception is when the `label` is not set then we still call
`MergeAdjacentBarriers` in the python code of the pass.

This is the first step in the improvement of the performance of this
pass. We can easily leverage multhreading to potentially parallelize the
analysis portion of this pass that searches for the final operations and
returns the set of indices. But this is blocked on Qiskit#13219 which prevents
us from accessing the PackedInstructions stored in the DAGCircuit in a
multithreaded context.

This commit also fixes an issue related to shared references in
the disjoint_utils module around barrier labels. The combine_barriers()
function was incorrectly mutating the label by reference which wouldn't
persist in the DAG, and this was causing failures after the barrier was
originally generated in Rust with this pass now.

Fixes Qiskit#12253

* Remove unused imports
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Oct 9, 2024
* Fully port BarrierBeforeFinalMeasurements to rust

This commit migrates the BarrierBeforeFinalMeasurements transpiler pass
to operate fully in Rust. The full path of the transpiler pass now never
leaves Rust until it has finished modifying the DAGCircuit. The one
exception is when the `label` is not set then we still call
`MergeAdjacentBarriers` in the python code of the pass.

This is the first step in the improvement of the performance of this
pass. We can easily leverage multhreading to potentially parallelize the
analysis portion of this pass that searches for the final operations and
returns the set of indices. But this is blocked on Qiskit#13219 which prevents
us from accessing the PackedInstructions stored in the DAGCircuit in a
multithreaded context.

This commit also fixes an issue related to shared references in
the disjoint_utils module around barrier labels. The combine_barriers()
function was incorrectly mutating the label by reference which wouldn't
persist in the DAG, and this was causing failures after the barrier was
originally generated in Rust with this pass now.

Fixes Qiskit#12253

* Remove unused imports
@ElePT ElePT added the Changelog: None Do not include in changelog label Nov 6, 2024
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 mod: transpiler Issues and PRs related to Transpiler 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 BarrierBeforeFinalMeasurements to Rust
4 participants