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

Fix performance regression in UnitarySynthesis #13375

Merged
merged 3 commits into from
Oct 29, 2024

Conversation

mtreinish
Copy link
Member

Summary

This commit fixes a performance regression that was introduced in PR #13141. When the pass is looking up the preferred synthesis direction for a unitary based on the connectvity constraints the connectivity was being provided as a PyList. To look up the edge in connectivity set this meant we needed to iterate over the list and then create a set that rust could lookup if it contains an edge or it's reverse. This has significant overhead because its iterating via python and also iterating per decomposition. This commit addresses this by changing the input type to be a HashSet from Python so Pyo3 will convert a pyset directly to a HashSet once at call time and that's used by reference for lookups directly instead of needing to iterate over the list each time.

Details and comments

This commit fixes a performance regression that was introduced in
PR Qiskit#13141. When the pass is looking up the preferred synthesis direction
for a unitary based on the connectvity constraints the connectivity was
being provided as a PyList. To look up the edge in connectivity set this
meant we needed to iterate over the list and then create a set that rust
could lookup if it contains an edge or it's reverse. This has
significant overhead because its iterating via python and also iterating
per decomposition. This commit addresses this by changing the input type
to be a HashSet from Python so Pyo3 will convert a pyset directly to a
HashSet once at call time and that's used by reference for lookups
directly instead of needing to iterate over the list each time.
@mtreinish mtreinish added performance Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels Oct 28, 2024
@mtreinish mtreinish added this to the 1.3.0 milestone Oct 28, 2024
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Oct 28, 2024

Pull Request Test Coverage Report for Build 11575706080

Details

  • 31 of 32 (96.88%) changed or added relevant lines in 2 files are covered.
  • 41 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.04%) to 88.647%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 24 25 96.0%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.23%
crates/qasm2/src/parse.rs 18 96.69%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 20 58.41%
Totals Coverage Status
Change from base Build 11572090796: -0.04%
Covered Lines: 74961
Relevant Lines: 84561

💛 - Coveralls

If we're using the default plugin the execution will happen in rust
and we don't need to build the plugin data views that are defined in the
plugin interface. Profiling the benchpress test using the hamlib
hamiltonian:

ham_graph-2D-grid-nonpbc-qubitnodes_Lx-5_Ly-186_h-0.5-all-to-all

that caught this originally regression was showing an inordinate amount
of time being spent in the construction of
`_build_gate_lengths_by_qubit` and `_build_gate_errors_by_qubit` which
isn't being used because this happens internally in rust now. To
mitigate this overhead this commit migrates the sections computing these
values to the code branch that uses them and not the default plugin
branch that uses rust.
@@ -268,7 +268,7 @@ fn py_run_main_loop(
new_ids,
min_qubits,
target,
coupling_edges,
coupling_edges.clone(),
Copy link
Member Author

Choose a reason for hiding this comment

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

We can avoid this clone if we refactor the functions a bit to split the python interface out and use a reference for the recursion. Not sure how important this is because this only comes up during control flow processing and we're already slower there because of the python usage and circuit to dag conversion. Maybe something to fix for when we make a dag native control flow op.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would make sense to revisit this when we get to have rust-native control flow op.

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.

LGTM. Thanks for the catch :)

@ElePT ElePT enabled auto-merge October 29, 2024 13:12
@ElePT ElePT added this pull request to the merge queue Oct 29, 2024
Merged via the queue into Qiskit:main with commit 01642a8 Oct 29, 2024
16 checks passed
@mtreinish mtreinish deleted the fix-performance-unitary-synth-rs branch October 30, 2024 09:27
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 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.

4 participants