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 looped QuantumCircuit.assign_parameters #13337

Merged

Conversation

jakelishman
Copy link
Member

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

Details and comments

For example, using a circuit like

from qiskit.circuit import QuantumCircuit, Parameter

def make(num_qubits, num_reps):
    initials = [Parameter(str(i)) for i in range(num_qubits * num_reps)]
    qc = QuantumCircuit(num_qubits)
    ps = iter(initials)
    for _ in range(num_reps):
        for q in qc.qubits:
            qc.rz(next(ps), q)
    return qc

qc = make(127, 30)

and the not-so-great calling convention

for p in list(qc.parameters):
    qc.assign_parameters({p: Parameter(p.name + "x")}, inplace=True)

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.

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.
@jakelishman jakelishman added performance Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Oct 16, 2024
@jakelishman jakelishman added this to the 1.3.0 milestone Oct 16, 2024
@jakelishman jakelishman requested a review from a team as a code owner October 16, 2024 20:15
@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 16, 2024

Pull Request Test Coverage Report for Build 11630513281

Details

  • 16 of 17 (94.12%) changed or added relevant lines in 2 files are covered.
  • 17 unchanged lines in 6 files lost coverage.
  • Overall coverage decreased (-0.02%) to 88.718%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 13 14 92.86%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 92.09%
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
crates/qasm2/src/expr.rs 1 94.02%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 58.05%
crates/qasm2/src/lex.rs 6 91.48%
qiskit/synthesis/two_qubit/xx_decompose/decomposer.py 6 90.77%
Totals Coverage Status
Change from base Build 11617024653: -0.02%
Covered Lines: 76356
Relevant Lines: 86066

💛 - Coveralls

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.

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.

qiskit/circuit/quantumcircuit.py Outdated Show resolved Hide resolved
@jakelishman
Copy link
Member Author

jakelishman commented Nov 1, 2024

nonlocal is needed because Python has implicit variable declarations on assignment. If there's no assignment, you do regular scoped lookups. Since we assign to mapping_view in the function, that would imply that we were creating a new variable (doesn't matter that we read it before we wrote to it - that just results in an UnboundLocalError). nonlocal suppresses the implicit-declaration behaviour and instead makes the scoped lookup work as expected, though with the minor restriction that the binding must resolve at compile time to a variable in a nonlocal scope:

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

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.

@jakelishman

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.

@kevinhartman kevinhartman added this pull request to the merge queue Nov 1, 2024
Merged via the queue into Qiskit:main with commit 5b1a358 Nov 1, 2024
17 checks passed
@jakelishman jakelishman deleted the assign-parameters-perf-regression branch November 1, 2024 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants