-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 looped QuantumCircuit.assign_parameters
#13337
Fix performance regression in looped QuantumCircuit.assign_parameters
#13337
Conversation
When calling `assign_parameters` on a heavily parametric circuit with the unusual access pattern of binding off a single parameter at a time, Qiskit 1.2 had a severe performance regression compared to Qiskit 1.1 stemming from Qiskitgh-12794. The calls to `unsorted_parameters` on each iteration were creating a new `set`, which could be huge if the number of parameters in the circuit was large. In Qiskit 1.1 and before, that object was a direct view onto the underlying `ParameterTable` (assuming the input circuit did not have a parametric global phase), so was free to construct.
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 11630513281Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks mostly good to me, but it might be nice if you could add a bit more explanation around create_mapping_view
in the form of a code comment(s). Personally, I'm not used to seeing nonlocal
and I had to look up how it works / its lifetime implications.
x = 0
def f():
# This is a syntax error because there's no enclosing scopes containing `f`.
# (The global scope isn't "enclosing" in Python terminology).
nonlocal x
def g():
x = 1
def f():
# This is fine, and gets the `x = 1`
nonlocal x
class A:
x = 2
def f(self):
# Also a syntax error, because class bodies don't introduce enclosing scopes.
nonlocal x Unless you feel particularly strongly, I'd rather not add comments on how |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless you feel particularly strongly, I'd rather not add comments on how nonlocal works - it's not a particularly common keyword, but we do use it occasionally for things just like this, and it's one of the reasons the keyword exists.
I'm in agreement with this, and I like the way you've described what's going on here in terms of "business logic" (I'd personally never argue to add commentary intended to teach the reader the specifics of a programming language heh).
Thanks for these changes, it'll be a lot more understandable now what's going on here to someone unfamiliar with this module.
Summary
When calling
assign_parameters
on a heavily parametric circuit with the unusual access pattern of binding off a single parameter at a time, Qiskit 1.2 had a severe performance regression compared to Qiskit 1.1 stemming from gh-12794. The calls tounsorted_parameters
on each iteration were creating a newset
, which could be huge if the number of parameters in the circuit was large. In Qiskit 1.1 and before, that object was a direct view onto the underlyingParameterTable
(assuming the input circuit did not have a parametric global phase), so was free to construct.Details and comments
For example, using a circuit like
and the not-so-great calling convention
Qiskit 1.1.2 took about 60ms, Qiskit 1.2.4 took about 1.1s, and this commit takes about 50ms. This commit is dominated by the Python-space calls dealing with
Parameter
objects.This is probably stable for backport, but since the immediate problem the regression was causing was mitigated by other means, and since it substantially touches a core method, I'd propose leaving this til 1.3.