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

Improve OpenQASM 3 exporter performance for hardware-like circuits #13343

Merged
merged 2 commits into from
Nov 1, 2024

Conversation

jakelishman
Copy link
Member

Summary

This reorganises the variable lookups and internal AST generation slightly to improve the OpenQASM 3 exporter performance for the common case of circuits with many singly parametric 1q gates, parametrised by a single Parameter expression. This is quite common in high-throughput cases, where the circuit is being used as a parametric template for a backend compiler.

Details and comments

Similar to #13342, this was a couple of very easy tweaks I could make within Python space to slightly improve the runtime performance of the OQ3 importer. It's still pretty slow for what it does, but there's little point focussing on its performance while it's still pure Python, and the majority of our data models are moving to Rust.

This reorganises the variable lookups and internal AST generation
slightly to improve the OpenQASM 3 exporter performance for the common
case of circuits with many singly parametric 1q gates, parametrised by a
single `Parameter` expression.  This is quite common in high-throughput
cases, where the circuit is being used as a parametric template for
a backend compiler.
@jakelishman jakelishman added performance mod: qasm3 Related to OpenQASM 3 import or export labels Oct 17, 2024
@jakelishman jakelishman requested a review from a team as a code owner October 17, 2024 17:09
@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 17, 2024

Pull Request Test Coverage Report for Build 11633949120

Details

  • 81 of 83 (97.59%) changed or added relevant lines in 2 files are covered.
  • 19 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.01%) to 88.735%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qasm3/ast.py 63 65 96.92%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 92.09%
crates/qasm2/src/lex.rs 6 92.48%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 11632855301: 0.01%
Covered Lines: 76436
Relevant Lines: 86140

💛 - Coveralls

@jakelishman jakelishman added this to the 1.3.0 milestone Oct 17, 2024
Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

Looks mostly good, just a few minor comments.

Thanks for doing this :)

@@ -1145,9 +1145,11 @@ def _rebind_scoped_parameters(self, expression):
# missing, pending a new system in Terra to replace it (2022-03-07).
if not isinstance(expression, ParameterExpression):
return expression
if isinstance(expression, Parameter):
return self.symbols.get_variable(expression).string
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a different behavior here? I've not looked too much at ParameterExpression, but is it odd for this to return a string rather than an Expression? Perhaps I'm missing something 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

You're not missing anything, it's just abuse of the complete absence of handling for ParameterExpression in the exporter - we literally just call str and hope for the best.

The reason not to construct a ParameterExpression is because the case of a standalone Parameter is very common on the performance critical path, and ParameterExpression (and Parameter) are both very expensive to construct.

qiskit/qasm3/exporter.py Show resolved Hide resolved
Copy link
Contributor

@kevinhartman kevinhartman 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.

Thanks for the comments / clarifications!

@kevinhartman kevinhartman added this pull request to the merge queue Nov 1, 2024
Merged via the queue into Qiskit:main with commit 8b21013 Nov 1, 2024
17 checks passed
@jakelishman jakelishman deleted the qasm3/tweak-performance branch November 1, 2024 20:10
@ElePT ElePT added the Changelog: None Do not include in changelog label Nov 6, 2024
joshuasn added a commit to joshuasn/qiskit that referenced this pull request Dec 18, 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: qasm3 Related to OpenQASM 3 import or export performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants