Skip to content

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

@yjt98765

Description

@yjt98765

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedcommunity contributions welcome. For filters like http://github-help-wanted.com/

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions