question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circuit parameters do not respect insertion order

See original GitHub issue

Environment

  • Qiskit Terra version: 0.19.2
  • Python version: 3.7.7
  • Operating system: OSX

What is happening?

Parameters are not implemented on a parametrized circuit in the order defined by a user, but alphabetically. The circuit diagram shows the insertion order, but binding parameters is not done in the same order.

How can we reproduce the issue?

In the following example, I want to assign value [1, 2, 3] to [theta, gamma, lam]. The circuit diagram shows the order I defined, .i.e. theta, gamma , lam but the values are assigned in the alphabetical order rather than the insertion order, therefore, the parameters get wrong values.

param_keys = Parameter('theta'), Parameter('gamma'), Parameter('lam') 

ansatz = QuantumCircuit(1)
ansatz.u(*(param_keys), 0)
print(ansatz)

param_vals = np.array([1, 2, 3])
qc = ansatz.assign_parameters(param_vals)
print(qc)
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
q: ─ U(theta,gamma,lam) β”œ
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
q: ─ U(3,1,2) β”œ
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

What should happen?

The list of the parameters should be bound in the order that is given by a user.

Any suggestions?

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jakelishmancommented, Apr 1, 2022

Thanks for the explanation Julien, and I’m glad this is all ok. I’ll close the issue now, but feel free to open a new one if there are bugs in the handling.

1reaction
Cryoriscommented, Apr 1, 2022

We decided against sorting by insertion because then the order in which you call the gate, a seemingly random choice, would have an effect on how the array of values will be bound. That would’ve likely been a source of bugs in particular in the applications and implementation-wise it would be more effort since it would require book-keeping of the parameter order when ever we transpile or modify the circuit. Sorting by name is a global order which doesn’t have these drawbacks πŸ™‚

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generic Key/Value pair collection in that preserves insertion ...
I'm looking for something like a Dictionary<K,V> however with a guarantee that it preserves insertion order. Since Dictionary is a hashtable, I do...
Read more >
qiskit.circuit.QuantumCircuit
The snippet below shows that insertion order of parameters does not matter. >>> from qiskit.circuit import QuantumCircuit, Parameter >>> a, b,Β ...
Read more >
RF engineering basic concepts: S-parameters
Abstract. The concept of describing RF circuits in terms of waves is discussed and the. S-matrix and related matrices are defined.
Read more >
Understanding and Interpreting Standard-Logic Data Sheets
This means that any current that flows out of the respective terminal is considered to be a negative quantity.
Read more >
SPICE Circuit Components
N1 and N2 are the two element nodes. VALUE is the resistance (in ohms) and may be positive or negative but not zero....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found