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.15k stars 2.35k forks source link

KeyError from dumping qpy after Qasm3 conversion with coupling map #11559

Open ljkitt opened 9 months ago

ljkitt commented 9 months ago

Environment

What is happening?

Creating a circuit and transpiling it with a coupling map or using a backend, then having a roundtrip conversion with Qasm3, and finally converting to Qpy, results in a KeyError, for example:

Traceback (most recent call last):
  Cell In[21], line 21
    qpy.dump(qc, fd)
  File /opt/conda/lib/python3.10/site-packages/qiskit/utils/deprecation.py:182 in wrapper
    return func(*args, **kwargs)
  File /opt/conda/lib/python3.10/site-packages/qiskit/qpy/interface.py:168 in dump
    writer(file_obj, program, metadata_serializer=metadata_serializer)
  File /opt/conda/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py:994 in write_circuit
    _write_layout(file_obj, circuit)
  File /opt/conda/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py:805 in _write_layout
    qubit = layout_mapping[i]
KeyError: 0

How can we reproduce the issue?

Run the following:

import qiskit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.library.standard_gates import *
qr = QuantumRegister(1, name='qr')
cr = ClassicalRegister(1, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')
qc.measure(qr, cr)

from qiskit.providers.fake_provider import FakeGuadalupeV2
from qiskit import transpile
backend = FakeGuadalupeV2()
qc = transpile(qc, backend=backend, basis_gates=None, optimization_level=2, coupling_map=None)

from qiskit import qasm3
qc = qasm3.loads(qasm3.dumps(qc))

from qiskit import qpy
with open('tmp.qpy', 'wb') as fd:
    qpy.dump(qc, fd)

What should happen?

The program should be able to dump qpy from the circuit without error.

Any suggestions?

No response

Waliur003 commented 8 months ago

@ljkitt I Think You have Misunderstanding to use Qpy's Load and Dump with with qasm3 load and dump thing and that's why your code shows error. QPY and Qasm3 are different components of Qiskit.

Here is your Modified Code

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.library.standard_gates import *
qr = QuantumRegister(1, name='qr')
cr = ClassicalRegister(1, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')
qc.measure(qr, cr)

from qiskit.providers.fake_provider import FakeGuadalupeV2
from qiskit import transpile
backend = FakeGuadalupeV2()
qc = transpile(qc, backend=backend, basis_gates=None, optimization_level=2, coupling_map=None)

from qiskit.qasm3 import dumps
dumps(qc)

from qiskit import qpy
with open('tmp.qpy', 'wb') as fd:
    qpy.dump(qc, fd)

with open('tmp.qpy', 'rb') as fd:
    new_qc = qpy.load(fd)[0]

Here is the documentation of QPY And Qasm3 https://docs.quantum.ibm.com/api/qiskit/qpy https://docs.quantum.ibm.com/build/interoperate-qiskit-qasm3

I Hope it helps

I Hope It Helps