Skip to content

Addressing performance bottlenecks in ParameterExpression.bind #14471

@SamFerracin

Description

@SamFerracin

Environment

  • Qiskit version: 2.0.0

What is happening?

Summary

Let us consider a parameter expression expr and a dictionary parameter_values: dict[Parameter, float] with M key, value pairs. Consider the following code to bind the expression:

expression.bind(parameter_values)

As it turns out, this line takes time that grows with len(M). As far as I can tell, this is because qiskit applies some checks to all of the parameters in parameter_values . Even if it turns out that expression only needs one of them, all the parameters are checked and then only one of them is used.

Why this needs fixing

Sometimes, it is useful to maintain a log of parameters outside of a circuit (e.g., in a parameter table) and bind these parameters when needed agains a parameter_values dict. In this case, the QuantumCircuit.assign_parameters method (which does some tricks to speed things up) is not available, and users take a hit in performance when they bind.

Some suggestions on how to fix this

  • Provide an option for users so that they can choose to check only the "relevant" parameter values (i.e., those present in expression), so that the runtime of bind becomes independent of len(M).
  • Review the checks and remove those that are not needed.

How can we reproduce the issue?

from qiskit.circuit import Parameter

N: int = ...

parameter_values = {Parameter(f"th_{i}"): 1 for i in range(N)}
parameter_values[param := Parameter("my_param")] = 1

%timeit param.bind(parameter_values, allow_unknown_parameters=True)

On my laptop, with N=1 bind takes ~2.5 μs, but with N=10**5 it takes 17.8 ms.

What should happen?

param.bind should take a time that is not dependent on N

Any suggestions?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions