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

CU gate does not get its parameter assigned #10131

Closed
woodsp-ibm opened this issue May 19, 2023 · 3 comments · Fixed by #11032
Closed

CU gate does not get its parameter assigned #10131

woodsp-ibm opened this issue May 19, 2023 · 3 comments · Fixed by #11032
Labels
bug Something isn't working

Comments

@woodsp-ibm
Copy link
Member

woodsp-ibm commented May 19, 2023

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.
@woodsp-ibm woodsp-ibm added the bug Something isn't working label May 19, 2023
@wycp
Copy link

wycp commented May 22, 2023

It seems that CUGate params getter retruns a temporary list
https://github.com/Qiskit/qiskit-terra/blob/bedecbdce563f4e83acc11c7ec5ca6878a36a4b7/qiskit/circuit/library/standard_gates/u.py#L286
once the list is indexed and assigned
https://github.com/Qiskit/qiskit-terra/blob/bedecbdce563f4e83acc11c7ec5ca6878a36a4b7/qiskit/circuit/quantumcircuit.py#L2842
temporary list is updated, but not CUGate fields.
Let's return some object with custom indexed asignmets implementation instead of temporary list...
I would be happy to continue fixing the issue as my first qiskit contribution.

@Cryoris
Copy link
Contributor

Cryoris commented May 22, 2023

I think this should get fixed by #9118, which fixes the parameter assignment issues with the CU gate 🙂

@wycp
Copy link

wycp commented May 22, 2023

Indeed both issues have the same root cause, thank you for pointing it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants