Skip to content

qndk.simulator

Steps a partitioned circuit through the network against finite resources and produces a timed event log. This is where contention becomes latency.

The three schedulers

All take (partition_result, network) and return a SimulationResult.

Class Model Use when
DQCScheduler Event-driven, with entanglement lookahead, a Bell-pair inventory, and explicit path reservation Default. The only one supporting the fault-tolerant path.
StaticScheduler Pre-computed schedule Quick comparisons; what the batch scheduler adapter uses
DynamicScheduler Dynamic, without the full DQC resource model Between the two
from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.simulator.dqc import DQCScheduler
from qndk.simulator.static import StaticScheduler
from qndk.simulator.dynamic import DynamicScheduler

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=8))
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))
partition = KernighanLinPartitioner(circuit, network).partition()

for cls in (DQCScheduler, StaticScheduler, DynamicScheduler):
    result = cls(partition).run_simulation()
    print(f"{cls.__name__:18s} name={result.sim_type:8s} "
          f"epr={result.epr_cost():3d} "
          f"finish={result.final_timestep:.4f} ms")

What DQCScheduler actually does

Worth understanding, because the numbers it produces are a consequence of these mechanisms.

Lookahead: prefetching entanglement

Generating a Bell pair is the long pole in a distributed circuit — far slower than a local gate. A scheduler that discovers it needs one at the moment it needs it stalls. So DQCScheduler looks ahead: it walks two DAG layers past the current execution frontier, finds every two-qubit gate out there that will cross a QPU boundary, works out which QPU pairs are about to need entanglement, and starts generating early.

It is on by default. Turn it off to compare against naive on-demand generation:

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.simulator.dqc import DQCScheduler

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=8))
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))
partition = KernighanLinPartitioner(circuit, network).partition()

for pregen in (False, True):
    scheduler = DQCScheduler(partition)
    scheduler.use_epr_lookahead_pregen = pregen
    scheduler.num_lookahead_layers = 2
    result = scheduler.run_simulation()
    print(f"lookahead={str(pregen):5s} finish={result.final_timestep:.4f} ms")

EPR banks: Bell pairs on a shelf

Pre-generated pairs need somewhere to live. The scheduler keeps a queue per QPU pair, and every entry carries two timestamps: when it becomes usable, and when it decoheres. A gate takes a live pair off the shelf and falls back to on-demand generation only when the shelf is empty.

Pairs that expire unused are logged as real events rather than vanishing — a Bell pair that decohered before anything used it still consumed communication qubits at both endpoints, a Bell-state measurement at every switch on its path, and link bandwidth for its whole generation window.

The restraint is the interesting part. Stockpiling is not free: a held pair pins a comm qubit at both endpoints and resources along its route, so hoarding for one busy QPU pair starves every other pair sharing those QPUs. The rule is deliberately conservative — restocking fires only when that pair's shelf is completely empty, at most one in flight at a time.

Path reservation

The scheduler reserves an actual path for every remote gate: comm qubits at the endpoints, a Bell-state measurement at each switch, bandwidth on each link. Contention is modelled rather than assumed away, which is what lets the reported latency feed honestly into the memory term of the error budget.

Watching it run

plot_dag_with_timesteps() gives an interactive figure with a slider over execution steps: the circuit DAG on the left, the network on the right, and the active gate plus the entanglement path it is using highlighted at each step.

Distributed QFT executing step by step across a four-QPU Clos network

Follow one highlight sweep and a single cross-QPU gate lights up a whole path — a QPU, up through two switch tiers, back down to another QPU. Every switch on that path is holding a Bell-state measurement for the duration, which is exactly the contention the scheduler is reserving against.

For somewhere a figure cannot be interactive — a slide, a README — the same sweep renders to a GIF:

# fragment: writes a file and needs Graphviz
result.save_dag_timestep_animation("execution.gif", fps=2.5, scale=0.85)

It drives the very same slider, so the frames are what you would see dragging it yourself rather than a second implementation that could disagree.

SimulationResult

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.simulator.dqc import DQCScheduler

circuit = QiskitCircuit("qft", num_qubits=8)
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))
partition = KernighanLinPartitioner(circuit, network).partition()
result = DQCScheduler(partition).run_simulation()

print("gate counts  :", result.gate_counts())
print("epr cost     :", result.epr_cost())
print("finish time  :", f"{result.final_timestep:.4f} ms")
print("idle time    :", f"{result.compute_idle_time():.4f} ms")
print("switch seq   :", result.switch_sequence()[:4], "...")
print("devices/step :", len(result.devices_per_timestep()), "timesteps")
Member What it gives you
gate_counts() num_local, num_remote, num_magic
epr_cost() Entangled pairs consumed
final_timestep Makespan, in milliseconds
compute_idle_time() Total qubit idle time — the input to the memory error term
bsm_usage_per_step() Bell-state measurements spent per switch per timestep
execution_log_summary_table() A text table of every event type
devices_per_timestep() Which devices were busy when
total_operation_dag() The DAG with simulation events folded in

The event log

Every scheduling decision is a logged event, which is what makes a run auditable rather than a black box:

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.simulator.dqc import DQCScheduler

circuit = QiskitCircuit("qft", num_qubits=6)
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=3, connectivity="all2all"))
partition = KernighanLinPartitioner(circuit, network).partition()
result = DQCScheduler(partition).run_simulation()

from collections import Counter
kinds = Counter(e["event_type"] for e in result.execution_log)
for kind, n in sorted(kinds.items()):
    print(f"  {kind:22s} {n}")

Event types you will see include local-exec, non-local-exec, epr-pre-gen, epr-consumed, and — on the fault-tolerant path — magic-state events. An epr-expired event means a pair decohered unused, which is pure waste worth investigating.

BSM contention over time

The clearest view of whether the network is the bottleneck:

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.kl import KernighanLinPartitioner
from qndk.simulator.dqc import DQCScheduler

circuit = QiskitCircuit("qft", num_qubits=8)
network = build_clos(ports_per_switch=4, qpus_per_rack=2,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))
partition = KernighanLinPartitioner(circuit, network).partition()
result = DQCScheduler(partition).run_simulation()

rows = result.bsm_usage_per_step()
print(f"{len(rows)} timesteps with BSM activity")
for row in rows[:3]:
    print(" ", row)

Gate timing

LocalGateTiming configures how long local operations take. Defaults are in milliseconds.

from qndk.simulator.dqc import LocalGateTiming

timing = LocalGateTiming(
    single_qubit_ms=0.01,
    two_qubit_ms=0.1,
    remote_ms=1.0,
    swap_ms=None,          # None -> derived from two_qubit_ms
)
print(f"1q={timing.single_qubit_ms} 2q={timing.two_qubit_ms} "
      f"remote={timing.remote_ms}")

gate_durations_ms takes a per-gate-name override dict when one gate is unusually expensive on your hardware.

Plotting

# fragment: opens windows; needs Graphviz
result.plot()                          # timeline overview
result.plot_gantt()                    # per-device Gantt chart
result.plot_bsms_used_per_timestep()   # switch contention
result.plot_latency_distribution()     # Monte Carlo latency spread
result.plot_dag_with_timesteps()       # the DAG, annotated with when

Writing your own

Subclass Simulator and implement run_simulation() plus name(). You get the partition result and network as self.pr and self.network. Building a SimulationResult and calling add_event() as you go is what gives you all the plotting and metrics for free.

Next

A SimulationResult goes to analysis for metrics, or to tler() in quantum_error_correction for a logical error rate.