Skip to content

qndk.compile

The other terminal stage. Where the simulator produces metrics, this produces an actual runnable circuit — the partitioned computation lowered into explicit operations, including the entanglement generation that cross-QPU gates depend on.

What it produces

new_exec_plan(partition_result) returns an ExecutionPlan: a Qiskit QuantumCircuit in which every remote gate has been expanded into its real implementation, plus the op counts that expansion produced.

import json

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.compile.distributed import new_exec_plan

circuit = QiskitCircuit("simple", num_qubits=4)
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=2, connectivity="all2all"))

partition = KernighanLinPartitioner(circuit, network).partition()
plan = new_exec_plan(partition)

blob = json.loads(plan.to_json())
for key, value in sorted(blob["stats"].items()):
    print(f"  {key:18s} {value}")
print(f"  {'qasm3 bytes':18s} {len(blob['qasm3'])}")
Op count Meaning
entanglements Bell pairs the compiled circuit generates explicitly
two_qubit_gates After expansion
one_qubit_gates After expansion
measurements Including the mid-circuit measurements teleportation needs
unique_op_types Distinct operation names present

The two lowerings

A cross-QPU gate can be implemented two ways, and the compiler inserts whichever the partitioner chose:

Gate teleportation — the operation moves to where the data is. Consumes one Bell pair, a pair of mid-circuit measurements, and classically conditioned corrections. Cheaper when a qubit is needed remotely once.

State teleportation — the qubit itself moves. Also one Bell pair, but afterwards the qubit is local, so subsequent gates on it are free. Cheaper when many gates follow.

Both appear in the compiled circuit as real Qiskit operations, which is what makes the output executable rather than schematic. EntanglementGate is the custom gate marking where a Bell pair is created.

Executing it

ExecutionPlan.simulate() hands the circuit to Qiskit Aer:

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.compile.distributed import new_exec_plan

circuit = QiskitCircuit("simple", num_qubits=3)
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=2, connectivity="all2all"))
plan = new_exec_plan(KernighanLinPartitioner(circuit, network).partition())

counts = plan.simulate(shots=128)
print("measured outcomes:", counts)

This one really does simulate the state

Unlike the rest of the SDK, this computes amplitudes, so it scales exponentially in qubit count. Keep circuits small — a handful of qubits. Everything else in the pipeline models execution and has no such limit.

simulate_circuit() is the same thing for a circuit that has not been through the distributed compiler, which is useful as a correctness baseline:

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.compile.distributed import simulate_circuit

circuit = QiskitCircuit("simple", num_qubits=3)
print("undistributed:", simulate_circuit(circuit, shots=128))

Comparing the two is how you check that distributing a circuit did not change what it computes.

Serializing

to_json() returns a JSON string with the QASM3 of the compiled circuit plus its op counts — the handoff format for something outside Python.

import json

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.compile.distributed import new_exec_plan

circuit = QiskitCircuit("simple", num_qubits=3)
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=2, connectivity="all2all"))
plan = new_exec_plan(KernighanLinPartitioner(circuit, network).partition())

blob = json.loads(plan.to_json())
print("keys:", sorted(blob))
print("first QASM lines:")
for line in blob["qasm3"].splitlines()[:6]:
    print("   ", line)

Round-robin resource assignment

RoundRobinPool maps QPU ids to resources and hands them out in rotation. It is how the compiler picks which communication qubit to use for each entanglement without pinning everything to the first one:

from qndk.compile.distributed import RoundRobinPool

pool = RoundRobinPool()
for name in ("comm-a", "comm-b"):
    pool.add("qpu-0", name)

print("all for qpu-0:", pool.all_for_qpu("qpu-0"))
print("next:", pool.next("qpu-0"))
print("next:", pool.next("qpu-0"))
print("next:", pool.next("qpu-0"), "(wrapped around)")

Plotting

# fragment: opens windows; needs Graphviz
plan.draw_qiskit_dag()   # the compiled circuit's DAG

Next

Nothing — this is a leaf. For metrics instead of a circuit, see simulator; for the remaining packages, see Extras.