Qiskit / qiskit-qasm3-import

Importer from OpenQASM 3 to Qiskit's QuantumCircuit
https://qiskit.github.io/qiskit-qasm3-import
Apache License 2.0
15 stars 7 forks source link

Imported transpiled circuit has different layout #28

Closed jyu00 closed 6 months ago

jyu00 commented 6 months ago

Qiskit Runtime primitives now requires ISA circuits. But running an OpenQASM3 circuit fails due to the importer not setting the qubit mapping correctly.

Code to recreate:

from qiskit.circuit.random import random_circuit
from qiskit import QuantumCircuit, qasm2, qasm3, transpile
from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService()
backend = service.backend("ibm_peekskill")

random_circ = random_circuit(5, depth=3, seed=42).decompose(reps=3)
random_circ.measure_all()
transpiled = transpile(random_circ, backend=backend)

# Export to QASM3
qasm = qasm3.dumps(random_circ).replace("\n", " ")
for line in qasm.split("\n"):
    if "ecr" in line:
        print(line)

shows the ecr gate was applied to the physical qubits:

ecr $13, $12;
ecr $13, $12;
ecr $12, $10;
ecr $11, $14;
ecr $14, $13;
ecr $14, $13;
ecr $14, $13;
ecr $14, $13;

But if I re-import that, circuit.data uses qubits 0-5:

from qiskit.qasm3 import loads as qasm3_loads

loaded_circuit = qasm3_loads(qasm)

for instruction in loaded_circuit.data:
    name = instruction.operation.name
    if name == "ecr":
        qargs = tuple(circuit.find_bit(x).index for x in instruction.qubits)
        print(f"Found instruction {name} on qubits {qargs} ")

shows

Found instruction ecr on qubits (3, 2) 
Found instruction ecr on qubits (3, 2) 
Found instruction ecr on qubits (2, 0) 
Found instruction ecr on qubits (1, 4) 
Found instruction ecr on qubits (4, 3) 
Found instruction ecr on qubits (4, 3) 
Found instruction ecr on qubits (4, 3) 
Found instruction ecr on qubits (4, 3) 
johannesgreiner commented 6 months ago

This affects API-only users sending qasm to our (Runtime) APIs. The cloud transpilation service is also affected, and returns non-ISA circuits for some circuits & users. Please do assign priority to this issue in users' interest.

jakelishman commented 6 months ago

I'll look into this. We will most likely have to add an extra keyword argument to the importer functions, because OpenQASM 3 itself simply doesn't have enough information to fully represent things - say, in the example program at the top, the circuit uses only as high as $14, but the hardware has an actual number of qubits it wants to be present in the circuit more like 127.

jakelishman commented 6 months ago

New keyword arguments (to make the full-width circuit) are new features, and they should wait until Qiskit 1.1 (May), but what we can do in the interim is fix the bug where the importer isn't making the output circuit at least as wide as the highest physical qubit used, and then it should be an easy change for IBM primitives to add on extra dummy qubits to make it full width (if they need circuits to contain them).

I think that's probably the least risky path forwards here, for the timescales.