Skip to content

QPy roundtripping discards physical qubit information on circuit #10112

@taalexander

Description

@taalexander

Environment

  • Qiskit Terra version: main
  • Python version: 3.9
  • Operating system: OSX

What is happening?

When roundtripping a transpiled circuit for a backend, and then dumping to OpenQASM 3 the circuit is no longer on physical qubits when emitted to OpenQASM 3. This impacts the backend when circuit merging is not applied.

How can we reproduce the issue?

from qiskit import qasm3, transpile, QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import qpy

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(1, 1)
qc.x(0)

qc_transpiled = transpile(qc, backend)

with open('transpiled.qpy', 'wb') as fd:
    qpy.dump(qc_transpiled, fd)

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

print(qc_qasm3 := qasm3.dumps(qc_transpiled, disable_constants=True, experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1))

Emits

OPENQASM 3;
include "stdgates.inc";
bit[1] c;
qubit[7] _all_qubits;
let q = _all_qubits[0:6];
x q[0];

What should happen?

Commenting out the QPy roundtripping

from qiskit import qasm3, transpile, QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import qpy

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(1, 1)
qc.x(0)

qc_transpiled = transpile(qc, backend)

#with open('transpiled.qpy', 'wb') as fd:
#    qpy.dump(qc_transpiled, fd)

#with open('transpiled.qpy', 'rb') as fd:
#    qc_transpiled = qpy.load(fd)[0]

print(qc_qasm3 := qasm3.dumps(qc_transpiled, disable_constants=True, experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1))

Gives

OPENQASM 3;
include "stdgates.inc";
bit[1] c;
x $0;

Any suggestions?

This may be fixed by setting the layout on roundtripping

from qiskit import qasm3, transpile, QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import qpy
from qiskit.transpiler import Layout

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(1, 1)
qc.x(0)

qc_transpiled = transpile(qc, backend)

with open('transpiled.qpy', 'wb') as fd:
    qpy.dump(qc_transpiled, fd)

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


qc_transpiled._layout = Layout({qubit: idx for idx, qubit in enumerate(qc_transpiled.qubits)})

print(qc_qasm3 := qasm3.dumps(qc_transpiled, disable_constants=True, experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1))

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions