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.05k stars 2.33k forks source link

Circuits with if_test fail when running on compatible hardware #10494

Closed joeharrisuk closed 1 year ago

joeharrisuk commented 1 year ago

Environment

What is happening?

Basic circuits containing an if_test statement incur a TypeError upon execution on ibm_sherbrooke and other backends with dynamic circuit support. The specific error message is: TypeError: Type is not JSON serializable: QuantumCircuit

I mention ibm_sherbrooke since it was used in the recent IBM Quantum Challenge where if_test circuits were run extensively.

How can we reproduce the issue?

Run the following (with your own provider inserted):

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit_ibm_provider import IBMProvider
provider = IBMProvider(name=your_provider_name)
sherbrooke = provider.get_backend('ibm_sherbrooke')

qr = QuantumRegister(1)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
(q0,) = qr
b0, b1 = cr

qc.h(q0)
qc.measure(q0, b0)
with qc.if_test((b0, 1)):
    qc.x(q0)
qc.measure(q0, b1)

job = sherbrooke.run(qc, shots=1024)

Once the job has run on the device, job.status() returns <JobStatus.ERROR: 'job incurred error'> and job.error_message() returns 'TypeError: Type is not JSON serializable: QuantumCircuit'.

Using, for example, the aer_simulator_statevector backend instead, the job executes successfully.

What should happen?

The job should execute successfully.

Any suggestions?

No response

jakelishman commented 1 year ago

If this is still occurring after you've updated your version of qiskit_ibm_provider (and even if not), it would potentially be a bug on their side, not Terra's - the error message should have been raised by that package. I suspect the most likely is that your version of qiskit_ibm_provider was a bit old, and you still needed to pass dynamic=True to backend.run if you wanted to run a dynamic circuit.

Either way, I'm going to close this here because the problem isn't in Terra. You might want to ask in Qiskit Slack or at the GitHub page for qiskit-ibm-provider, but this stuff does work in general.

Fwiw, I was able to run your code verbatim (except for providing a name for the provider) with qiskit_ibm_provider==0.6.2 on Sherbooke.