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

Documentation for QuantumCircuit.assign_parameters seems to be wrong #6533

Closed
chris-n-self opened this issue Jun 8, 2021 · 1 comment · Fixed by #6546
Closed

Documentation for QuantumCircuit.assign_parameters seems to be wrong #6533

chris-n-self opened this issue Jun 8, 2021 · 1 comment · Fixed by #6546
Labels
bug Something isn't working documentation Something is not clear or an error documentation

Comments

@chris-n-self
Copy link

Information

  • Qiskit Terra version: 0.17.4
  • Python version: 3.9.4
  • Operating system: MacOS 10.14.6

What is the current behavior?

The description of the QuantumCircuit.assign_parameters method in the docs, says that:

"If an iterable, the elements are assigned to the existing parameters in the order they were inserted. "

but this does not seem to be the behaviour of the function. Instead, if you call QuantumCircuit.parameters the parameters seem to be returned in lexical order and passing an iterable assigns elements to the existing parameters in that order. The documentation does say that the order will be specified by QuantumCircuit.parameters:

"You can call QuantumCircuit.parameters to check this order."

But the comment about this being the order the parameters were inserted is misleading.

Steps to reproduce the problem

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

circ = QuantumCircuit(4)
for idx in range(12):
    circ.ry(Parameter('p'+f'{idx}'), idx%4)
circ.draw()

print(circ.parameters)

circ2 = circ.assign_parameters([Parameter('q'+f'{idx}') for idx in range(12)])
circ2.draw()

The intended behaviour is to remap parameters so that 'p0 -> q0', 'p1 -> q1', 'p2 -> q2' etc. but because of the lexical ordering 'q2' instead replaces 'p10'.

@chris-n-self chris-n-self added the bug Something isn't working label Jun 8, 2021
@Cryoris
Copy link
Contributor

Cryoris commented Jun 9, 2021

Good catch! That's wrong, you're right. It should state

If an iterable, the elements are assigned to the existing parameters in the order of QuantumCircuit.parameters.

For you use-case, you could use a ParameterVector, then the index-order will be recognized.

from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector

p = ParameterVector('p', 12)
q = ParameterVector('q', 12)

circ = QuantumCircuit(4)
for idx in range(12):
    circ.ry(p[idx], idx%4)
circ.draw()

print(circ.parameters)

circ2 = circ.assign_parameters(q[:])
circ2.draw()

@kdk kdk added the documentation Something is not clear or an error documentation label Jun 11, 2021
@mergify mergify bot closed this as completed in #6546 Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Something is not clear or an error documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants