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.

CUGate's params getter does not comply with circuit's assign_parameters

See original GitHub issue

Environment

  • Qiskit Terra version: 0a10008
  • Python version: 3.8.10 64-bit
  • Operating system: windows 10 64-bit

What is happening?

This issue is proposed based on the discussion under #7283, especially the insight of @jakelishman.

CUGate is the controlled version of UGate, a generic single-qubit rotation gate with 3 Euler angles. Most controlled gates have the same parameters as the base gate, but CUGate has an extra one: the global phase Ξ³. To cope with this, CUGate uses a @property decorator to define a getter for the property params. This getter function creates a new array and returns it. It leads to problems that following operations may be applied to this newly created array instead of the real parameters of the CUGate object. For example, that happens in the function QuantumCircuit.assign_parameters, which is demonstrated in the following section.

The failure of assign_parameters on CUGate leads to another problem. Currently, a line is missing below line 652 in qiskit/circuit/library/standard_gates/equivalence_library.py.

q = QuantumRegister(2, "q")
theta = Parameter("theta")
phi = Parameter("phi")
lam = Parameter("lam")
cu3_to_cu = QuantumCircuit(q)
cu3_to_cu.cu(theta, phi, lam, 0, 0, 1)
# should have another line:
# _sel.add_equivalence(CU3Gate(theta, phi, lam), cu3_to_cu)

If the add_equivalence function is added, the test test_equivalence_phase would report a TypeError exception. QuantumCircuit.assign_parameters is called during the test,

How can we reproduce the issue?

from qiskit.circuit import QuantumCircuit, Parameter
ps = [Parameter("t"), Parameter("p"), Parameter("l"), Parameter("g")]
qc = QuantumCircuit(2)
qc.cu(*ps, 0, 1)
qc.cu3(*ps[:3], 0, 1)
print(qc)
print(qc.assign_parameters(dict(zip(ps, [0.1, 0.2, 0.3, 0.4])), inplace=False))

The output is

q_0: ──────■─────────────■──────
     β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
q_1: ─ U(t,p,l,g) β”œβ”€ U3(t,p,l) β”œ
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

q_0: ──────■────────────────■─────────
     β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
q_1: ─ U(t,p,l,g) β”œβ”€ U3(0.1,0.2,0.3) β”œ
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

suggesting the assignment of CUGate is failed. Note that the parameters of the U3Gate have been assigned.

What should happen?

The second circuit should be

q_0: ──────────■────────────────────■─────────
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
q_1: ─ U(0.1,0.2,0.3,0.4) β”œβ”€ U3(0.1,0.2,0.3) β”œ
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Any suggestions?

Maybe remove the Ξ³ parameter from CUGate. The global phase is a property of quantum circuits. It is not a property of other gates. Adding this parameter to CUGate makes it different from the general cases and requires special processing, e.g., here and here.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jakelishmancommented, Sep 1, 2022

Yeah, it’s the same issue - assign_parameters / bind_parameter tries to mutate indices in the CUGate.params field, but the gate has a getter that produces a new list, not the actual array. Julien at one point had a PR that added assign_parameter to Instruction instances so it could be overridden, but I don’t know what happened to that.

1reaction
jakelishmancommented, Nov 30, 2021

Perhaps calling it β€œglobal” phase is a bit misleading here (it’s the phase of the controlled gate), but yeah, the gamma parameter needs to stay for now. The main issue is [again 😦] that we don’t have a contract for what Instruction.params should contain, and how it should behave with respect to mutability. QuantumCircuit assumes that it’s a β€œtrue” attribute, and tries to write to it as if it is guaranteed to be a list, but that’s probably not something it can assume.

The β€œbug” could either be that CUGate must return a list-like object that backs all its parameters (in which case we need to do what Julien said), or other places in Terra need to stop assuming that they can mutate the object. They can write wholesale, perhaps (so they’ll trigger the setter method), but they can’t mutate. This second form is more safe, but it likely has some performance implications, because we’d need to be reconstructing several lists/tuples each time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

qiskit.circuit.QuantumCircuit.assign_parameters
Assign parameters to new parameters or values. If parameters is passed as a dictionary, the keys must be Parameter instances in the current...
Read more >
Vuex reactive mapGetters with arguments passed through
Here is a snippet from a project I have: computed: { ...mapGetters('crm', ['accountWithId']), account() { return this.accountWithId(this.
Read more >
How to spy on a property (getter or setter) with Jasmine
accessType is an optional parameter. It is the propertyName type. its value can be either 'get' or 'set' and it defaults to getΒ ......
Read more >
Getters and Setters Methods in Java - CodeGym
Something else matters: our current Cat class allows fields to be assigned absolutely insane values. As a result, the program has objectsΒ ...
Read more >
Property getters and setters - The Modern JavaScript Tutorial
For accessor properties, there is no value or writable , but instead there are get and set functions. That is, an accessor descriptor...
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