qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Unable to assign new parameters to controlled-U gates

Open dalin27 opened this issue 3 years ago • 1 comments

Environment

  • Qiskit Terra version: 0.21.0
  • Python version: 3.9.2
  • Operating system: Windows

What is happening?

The assign_parameters method of the QuantumCircuit class does not seem to work when the parameters to be substituted are part of a controlled-U gate.

How can we reproduce the issue?

Input:

from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector

qc = QuantumCircuit(2)
p = ParameterVector('p', 4)
qc.cu(*p, 0, 1)
qc.draw()

Output:

q_0: ────────────■─────────────
     ┌───────────┴────────────┐
q_1: ┤ U(p[0],p[1],p[2],p[3]) ├
     └────────────────────────┘

At this point, everything is fine. Here is the issue:

qc.assign_parameters(dict(zip(p, [1,2,3,4]))).draw()

The output remains unchanged even after assigning numeric values [1,2,3,4]:

q_0: ────────────■─────────────
     ┌───────────┴────────────┐
q_1: ┤ U(p[0],p[1],p[2],p[3]) ├
     └────────────────────────┘

Assigning new parameter objects also does not work. For other gates, like the U gate, it seems to work just fine:

qc = QuantumCircuit(2)
p = ParameterVector('p', 3)
qc.u(*p, 0)
qc.assign_parameters(dict(zip(p, [1,2,3]))).draw()
     ┌──────────┐
q_0: ┤ U(1,2,3) ├
     └──────────┘
q_1: ────────────

What should happen?

Should be clear from above.

Any suggestions?

No response

dalin27 avatar Sep 09 '22 17:09 dalin27

I will provide a (hopefully) quick update.

The method assign_parameters receives a dictionary, and within that method, each pair (key, value) in the dictionary is treated separately by the method _assign_parameters (i.e. the parameter objects are assigned to their value one at a time). Here are the first few lines of the method _assign_parameters:

https://github.com/Qiskit/qiskit-terra/blob/3ae23373e05819e6051a1af1ab3e93582185c27e/qiskit/circuit/quantumcircuit.py#L2784-L2794

I think the problem mentioned in my initial post might arise from the line 2792, and more specifically by the params setter in the Instruction class. Here is the link:

https://github.com/Qiskit/qiskit-terra/blob/3ae23373e05819e6051a1af1ab3e93582185c27e/qiskit/circuit/instruction.py#L214

Indeed, if I put the following print statement print(instr.params) between the lines 2792 and 2793 in quantumcircuit.py, here is the output of the code from my initial post:

image

From the screenshot above, we clearly see that in the case of the U gate (i.e. In [3]), each call to the method _assign_parameters (there are three calls so there are three print statements) result in the substitution of one parameter object to its associated numerical value. On the other hand, this is clearly not the case for the controlled-U gate (i.e. In [2]); while the four parameter objects are listed at each call of _assign_parameters, none are substituted. That is why I suspect the params setter of the Instruction class to be the cause of the issue. The question is then, why would it work for one gate and not the other?

Someone more familiar than me with setters might be able to spot the issue (or maybe the issue is somewhere else?).

dalin27 avatar Sep 16 '22 18:09 dalin27

#7487 should handle this, though that PR needs some more work as we saw some performance hits on it... 🙂

Cryoris avatar Oct 07 '22 07:10 Cryoris

Hi! Is this bug still a thing? I'm having exactly the same issue, even though i'm pretty sure i've tried with the latest qiskit version.

Should i try to walk arround it, or am i missing some update or something? Thanks! :)

efemarchese avatar Dec 16 '22 12:12 efemarchese

This should have been fixed by #11032. Please feel free to re-open if that's not the case.

jakelishman avatar Oct 20 '23 16:10 jakelishman