Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.04k stars 2.32k forks source link

There is something wrong when crx gate is controlled in parameterized quantum circuit. #9514

Closed chenchenchen321 closed 11 months ago

chenchenchen321 commented 1 year ago

Environment

What is happening?

Running the circuit will result in an error when a CRX gate is controlled in the parameterized circuit.

How can we reproduce the issue?

Input:

beta = Parameter('θ')
circ = QuantumCircuit(3)
circ.crx(beta, 2, 0)
c1 = circ.control(1)
c1.measure_all()
backend = Aer.get_backend('aer_simulator')
c = transpile(c1, backend)
circuits=c.bind_parameters({c.parameters[0]: 2})
job = backend.run(circuits)
counts = job.result().get_counts()
print(counts)

Output: Simulation failed and returned the following error message: ERROR: Failed to load qobj: Unable to cast Python instance to C++ type (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)

But if we use cry gate, the result is correct: Input:

beta = Parameter('θ')
circ = QuantumCircuit(3)
circ.cry(beta, 2, 0)
c1 = circ.control(1)
c1.measure_all()
backend = Aer.get_backend('aer_simulator')
c = transpile(c1, backend)
circuits=c.bind_parameters({c.parameters[0]: 2})
job = backend.run(circuits)
counts = job.result().get_counts()
print(counts)

Output: {'0000': 1024}

If we replace c1 = circ.control(1) with c1 = circ.control(2), the result is also correct:

Input: beta = Parameter('θ') circ = QuantumCircuit(3) circ.cry(beta, 2, 0) c1 = circ.control(2) c1.measure_all() backend = Aer.get_backend('aer_simulator') c = transpile(c1, backend) circuits=c.bind_parameters({c.parameters[0]: 2}) job = backend.run(circuits) counts = job.result().get_counts() print(counts)

Output: {'0000': 1024}

What should happen?

Expected answer {'0000': 1024}

Any suggestions?

No response

chenchenchen321 commented 1 year ago

The same error when mcrx gate and mcry gate are used in PQC.

Cryoris commented 1 year ago

This seems to be caused by #8723, as the transpiled circuit contains CUGates where the parameter is still unbound. As a workaround until that is fixed you could use the RXGate with two controls:

from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.circuit.library import RXGate
from qiskit import Aer, transpile

beta = Parameter('θ')
gate = RXGate(beta).control(2)
circ = QuantumCircuit(3)
circ.append(gate, [0, 1, 2])
circ.measure_all()

backend = Aer.get_backend('aer_simulator')
bound = circ.bind_parameters([2])
tqc = transpile(bound, backend)
job = backend.run(tqc)
counts = job.result().get_counts()
print(counts)
jakelishman commented 11 months ago

This should have been fixed by #11032. Feel free to re-open if that's not the case.