Skip to content

qndk.quantum_network

The datacenter model. Not a pipeline stage — the thing every stage is measured against.

You build one QuantumNetwork and hand the same object to the partitioner, the simulator and the error budget. That is deliberate: when they all read one model, the network the partitioner optimized for cannot drift from the network the simulator ran on.

The model

flowchart TB
    T["TEL switch<br/>inter-rack"] --- N1["NIR switch<br/>intra-rack"]
    T --- N2["NIR switch<br/>intra-rack"]
    N1 --- Q1["QPU<br/>comp + comm qubits"]
    N1 --- Q2["QPU"]
    N2 --- Q3["QPU"]
    N2 --- Q4["QPU<br/>magic factory"]

One NetworkX graph holding four device types:

Device Represents Key attributes
QPU One chip connectivity, its qubits (a MagicFactory QPU adds distill_rounds)
Qubit One physical qubit type (comp/comm/magic/ancilla), t1_time_ms, t2_time_ms, gate_fidelity
Switch A photonic switch subtype (nir/tel), bsm_count, reconfig_delay_ms, fidelity, loss
Link A fibre bandwidth, length_m, loss_db_km, latency_ms

Qubits are graph nodes too, not just QPU properties, which is what lets the simulator reserve an individual comm qubit and lets grid4 connectivity be a real adjacency rather than a rule.

Building one

Four topology builders, plus a dispatching form.

from qndk.quantum_network import (build_clos, build_fattree, build_bcube,
                                  build_qfly, QPUSpec)
from qndk.quantum_network.topologies import build_network

qpu = QPUSpec(comp_qubits=4, connectivity="all2all")

nets = {
    "clos":    build_clos(ports_per_switch=4, qpus_per_rack=2, qpu_spec=qpu),
    "fattree": build_fattree(num_qpus=8, qpu_spec=qpu),
    "bcube":   build_bcube(num_qpus=8, switch_layers=2,
                           ports_per_switch=4, qpu_spec=qpu),
    "qfly":    build_qfly(num_switches=4, k_ring=1, qpus_per_switch=2,
                          qpu_spec=qpu),
    # same thing, dispatched by name
    "clos-2":  build_network("clos", ports_per_switch=4, qpus_per_rack=2,
                             qpu_spec=qpu),
}

print(f"{'topology':10s} {'qpus':>5s} {'switch':>7s} {'links':>6s} "
      f"{'comp':>5s} {'comm':>5s}")
for name, net in nets.items():
    print(f"{name:10s} {len(net.qpu_ids()):5d} "
          f"{len(net.switch_ids()):7d} {len(net.link_ids()):6d} "
          f"{net.total_comp_qubits():5d} {net.total_comm_qubits():5d}")

Use build_network(topology, **kwargs) when the topology name is data — reading it from a config file, sweeping over it in a loop.

Device specs

Every builder takes one spec per kind of device: qpu_spec=QPUSpec(...), switch_spec=SwitchSpec(...), link_spec=LinkSpec(...) and magic_factory_spec=MagicFactorySpec(...). A QPU's qubit physics nests inside its spec as QPUSpec(qubit=QubitSpec(...)). Specs are the only place defaults live -- QuantumNetwork holds none -- so every value on a built device comes from the spec you passed or from that spec class's defaults. Pass only the specs you want to change.

from qndk.quantum_network import (build_clos, LinkSpec, MagicFactorySpec,
                                  QPUSpec, QubitSpec, SwitchSpec)

network = build_clos(
    ports_per_switch=4, qpus_per_rack=2,
    qpu_spec=QPUSpec(
        comp_qubits=9,
        connectivity="grid4",        # or "all2all"
        qubit=QubitSpec(t1_time_ms=1000.0, t2_time_ms=1000.0,
                        gate_fidelity=0.999),
        comm_qubit_coherence_time_ms=1000.0,
    ),
    switch_spec=SwitchSpec(
        model="generic",             # or "cisco"
        bsm_count=2,
        reconfig_delay_ms=0.01,
    ),
    link_spec=LinkSpec(bandwidth=1.0, latency_ms=0.001, loss_db_km=0.2,
                       length_m=1.0),
    magic_factory_spec=MagicFactorySpec(distill_rounds=2,
                                        distillation_level_time_ms=0.1),
    num_magic_factories=1,
)
print("built:", network.total_comp_qubits(), "compute qubits")

grid4 requires a perfect square

QPUSpec(connectivity="grid4") lays qubits on a square grid, so comp_qubits must be 4, 9, 16, 25 and so on. It raises otherwise, naming the nearest valid grid. all2all has no such constraint.

The Cisco switch model

SwitchSpec(model="cisco") derives reconfiguration delay, fidelity, purity and loss from a simulated real photonic switch of the switch's port count, instead of the generic placeholder constants. It is what turns network-level fidelity numbers from a reasonable guess into something traceable to switch physics.

from qndk.quantum_network import build_clos, QPUSpec, SwitchSpec

for model in ("generic", "cisco"):
    net = build_clos(ports_per_switch=4, qpus_per_rack=2,
                     qpu_spec=QPUSpec(comp_qubits=4),
                     switch_spec=SwitchSpec(model=model))
    sw = net.get_switch(sorted(net.switch_ids())[0])
    print(f"{model:8s} reconfig={sw.reconfig_delay_ms:.5f} ms "
          f"fidelity={sw.fidelity:.5f} purity={sw.purity:.5f} "
          f"loss={sw.loss:.4f}")

Heterogeneous fleets

qpu_specs overrides specific QPUs by index. Any index not present uses the baseline qpu_spec, so a mostly-uniform network with a few exceptions costs one dict entry per exception.

from qndk.quantum_network import build_clos, QPUSpec

old = QPUSpec(comp_qubits=4, connectivity="all2all")
new = QPUSpec(comp_qubits=9, connectivity="all2all")

net = build_clos(ports_per_switch=4, qpus_per_rack=2, qpu_spec=old,
                 qpu_specs={0: new, 3: new})
for qpu_id in sorted(net.qpu_ids()):
    print(f"  {qpu_id}: {net.get_qpu(qpu_id).num_comp_qubits} comp qubits")

For a Clos network the index maps as rack * qpus_per_rack + slot.

Switches and links stay uniform at construction. Vary them afterwards:

from qndk.quantum_network import build_clos, QPUSpec

net = build_clos(ports_per_switch=4, qpus_per_rack=2,
                 qpu_spec=QPUSpec(comp_qubits=4))

net.update_switch(sorted(net.switch_ids())[0], bsm_count=16)
net.update_link(net.link_ids()[0], length_m=5000.0)
net.update_qubit(net.all_comm_qubit_ids()[0], t1_time_ms=250.0)

sw = net.get_switch(sorted(net.switch_ids())[0])
link = net.get_link(net.link_ids()[0])
print(f"switch bsm={sw.bsm_count}  link loss={link.loss_db():.3f} dB")

Building by hand

For a shape no builder lays out, start from an empty QuantumNetwork and add devices one at a time, one spec per flavor of device. Indices are handed out automatically -- QPUs across the network, switches per level. A magic state factory is not linked to anything until you link it.

from qndk.quantum_network import (LinkSpec, MagicFactorySpec, QPUSpec,
                                  QuantumNetwork, SwitchSpec)

net = QuantumNetwork()
sw = net.add_switch(level=1, subtype="nir", num_ports=4,
                    spec=SwitchSpec(bsm_count=4))
for _ in range(2):
    qpu = net.add_qpu(QPUSpec(connectivity="all2all"))
    net.add_link(qpu.id, sw.id, LinkSpec(length_m=25_000))
factory = net.add_magic_factory(MagicFactorySpec(distill_rounds=3))
net.add_link(factory.id, sw.id)
net.validate()
print(sorted(net.qpu_ids()), net.switch_ids())

net.add_device(...) adds an already built QPU, Switch or Link as-is, for the rare case a spec cannot express.

Querying

The methods you will actually use:

from qndk.quantum_network import build_clos, QPUSpec

net = build_clos(ports_per_switch=4, qpus_per_rack=2,
                 qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))

print("qpus           :", len(net.qpu_ids()))
print("nir switches   :", len(net.nir_switch_ids()))
print("tel switches   :", len(net.tel_switch_ids()))
print("min comp/qpu   :", net.min_comp_qubits_across_qpus())
print("qubits by qpu  :", len(net.qubits_by_qpu()), "qpus mapped")

a, b = sorted(net.qpu_ids())[0], sorted(net.qpu_ids())[-1]
path = net.shortest_path(a, b)
print(f"path {a} -> {b}: {' -> '.join(path)}")
print(f"  latency  : {net.calculate_path_latency(path):.6f} ms")
print(f"  boundary : {net.path_boundary_type(path)}")

path_boundary_type() says whether a path stayed intra-rack or had to cross the TEL layer, which is the distinction that drives most of the cost difference between two placements.

Allocation

The simulator reserves qubits as it runs. The same API is available directly, which is what the batch scheduler uses:

from qndk.quantum_network import build_clos, QPUSpec

net = build_clos(ports_per_switch=4, qpus_per_rack=1,
                 qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))

qubit = net.all_comp_qubit_ids()[0]
print("available before:", net.is_qubit_available(qubit))
net.allocate_qubit(qubit, job_id="job-1")
print("available after :", net.is_qubit_available(qubit))
print("allocated total :", len(net.allocated_qubit_ids()))
net.release_qubit(qubit)
print("after release   :", net.is_qubit_available(qubit))

Serialization

Round-trips through plain JSON, which makes a topology reproducible in a bug report.

import json
from qndk.quantum_network import build_clos, QPUSpec, QuantumNetwork

net = build_clos(ports_per_switch=4, qpus_per_rack=2,
                 qpu_spec=QPUSpec(comp_qubits=4))
blob = net.to_json()
restored = QuantumNetwork.from_json(json.loads(json.dumps(blob)))
restored.validate()

print(f"original {net.total_comp_qubits()} comp qubits, "
      f"restored {restored.total_comp_qubits()}")

validate()

Call it after building a network by hand. It raises on a structurally invalid graph rather than letting the problem surface as a confusing simulator failure later.

from qndk.quantum_network import build_clos, QPUSpec

net = build_clos(ports_per_switch=4, qpus_per_rack=2,
                 qpu_spec=QPUSpec(comp_qubits=4))
net.validate()
print("network is structurally sound")

Plotting

# fragment: opens windows; needs Graphviz
network.plot()                            # devices only
network.plot(qubits=True)                 # with individual qubits
network.plot(partition_result=partition)  # colored by partition

Next

With a network and a transpiled circuit, partition decides which qubit goes where.