-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
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 ofbind
becomes independent oflen(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