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.28k stars 2.37k forks source link

Serialization fails for controlled unitary gate #10802

Closed AzizNgoueya closed 1 year ago

AzizNgoueya commented 1 year ago

Describe the bug I get the error: AttributeError: 'NoneType' object has no attribute 'clbits and it seems that the Qpy to QASM conversion failed: unable to parse quantum circuit for a circuit containing a controlled unitary gate.

Steps to reproduce

from qiskit.extensions import UnitaryGate
from qiskit.quantum_info import random_unitary, SparsePauliOp
from qiskit import QuantumCircuit, QuantumRegister
from qiskit_ibm_runtime import QiskitRuntimeService, Options, Estimator, Session

UU = random_unitary(4, seed=12345)
qr = QuantumRegister(3)
qc = QuantumCircuit(qr)
qc.h(0)
qc.append(UnitaryGate(UU).control(1), qr)

service = QiskitRuntimeService()
backend = service.get_backend('ibmq_qasm_simulator')

options = Options(optimization_level=2, resilience_level=0)
estimator = Estimator(backend=backend, options=options)

XXexp = estimator.run(qc, observables=SparsePauliOp('IIX')).result().values[0]

Expected behavior It should work

Suggested solutions

Additional Information

Transpiling before works transpile(qc, basis_gates = backend.configuration().basis_gates)

kt474 commented 1 year ago

@jakelishman is this a bug with qpy? If so, can it be transferred to qiskit (terra)

jakelishman commented 1 year ago

Yep, looks like it to me. I've no write access on this to transfer, but @mtreinish could.

ElePT commented 1 year ago

@AzizNgoueya As a quick patch until the bug is found/fixed, you can try transpiling the circuit locally before sending it to the estimator:

from qiskit import transpile
qc = transpile(qc, basis_gates=['sx', 'x', 'rz', 'cx'])
ElePT commented 1 year ago

I can take a look at this bug :)