Skip to content

CU gate does not get its parameter assigned #10131

@woodsp-ibm

Description

@woodsp-ibm

I ran this from an editable install of current main branch, under Python 3.9 on Linux.

A problem was raised around Qsikit Machine Learning and the EstimatorQNN here https://quantumcomputing.stackexchange.com/questions/32647/qiskit-machine-learning-qnnestimator-estimator-job-failed-when-using-cu-gates The original issue was using an Estimator with the EstimatorQNN but it seems the issue is more fundamental. I included a Sampler instead of the Estimator as the net outcome is the same.

In investigating it seems the cu parameter does not get assigned the value. u works as the above author noted.

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.primitives import Sampler

p_i = Parameter("input")
p_w = Parameter("weight")
qc1 = QuantumCircuit(2)
qc1.h(0)
qc1.ry(p_i ,0)
qc1.u(p_w, 0, 0, 0)
qc1.cu(p_w, 0, 0, 0, 0, 1)
qc1.rx(p_w, 0)
qc1.measure_all()

print(qc1)

qc2 = qc1.assign_parameters([0.7, 0.9])

print(qc2)
print(qc2.parameters)


sampler = Sampler()
job = sampler.run(qc1, [0.7, 0.9])
result = job.result()

This prints where the parameterized circuit is shown first and then that after the assign. As can be seen the weight param in the cu still appears to there though the circuit reports no parameters as seen by the empty view.

        ┌───┐┌───────────┐┌───────────────┐                   ┌────────────┐ ░ ┌─┐   
   q_0: ┤ H ├┤ Ry(input) ├┤ U(weight,0,0) ├─────────■─────────┤ Rx(weight) ├─░─┤M├───
        └───┘└───────────┘└───────────────┘┌────────┴────────┐└────────────┘ ░ └╥┘┌─┐
   q_1: ───────────────────────────────────┤ U(weight,0,0,0) ├───────────────░──╫─┤M├
                                           └─────────────────┘               ░  ║ └╥┘
meas: 2/════════════════════════════════════════════════════════════════════════╩══╩═

                                                                                0  1 
        ┌───┐┌─────────┐┌────────────┐                   ┌─────────┐ ░ ┌─┐   
   q_0: ┤ H ├┤ Ry(0.7) ├┤ U(0.9,0,0) ├─────────■─────────┤ Rx(0.9) ├─░─┤M├───
        └───┘└─────────┘└────────────┘┌────────┴────────┐└─────────┘ ░ └╥┘┌─┐
   q_1: ──────────────────────────────┤ U(weight,0,0,0) ├────────────░──╫─┤M├
                                      └─────────────────┘            ░  ║ └╥┘
meas: 2/════════════════════════════════════════════════════════════════╩══╩═
                                                                        0  1 
ParameterView([])

The sampler taking the original circuit and parameter values fails to run and raises an error

TypeError: ParameterExpression with unbound parameters ({Parameter(weight)}) cannot be cast to a float.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions