Skip to content

Extras

Four packages that are not part of the compile-partition-simulate path. None of them is imported unless you ask for it.


qndk.optimizers

Integer linear programming for rack-level placement, via OR-Tools.

Partitioning decides which qubits share a QPU. Placement is the next question: given those partitions and their pairwise entanglement demand, which rack should each one go in? Putting heavily communicating partitions in the same rack keeps their traffic on the cheaper NIR layer instead of crossing TEL.

RackILP solves that directly.

from qndk.optimizers.placement_ilp import RackILP

# epr[i][j] = entanglement demand between partition i and partition j
epr = [[0, 40, 5, 1],
       [40, 0, 2, 1],
       [5, 2, 0, 30],
       [1, 1, 30, 0]]
num_tor = [2, 2]          # two racks, two slots each

solver = RackILP(F_intra=0.9, F_inter=0.85, time_limit_ms=5000)
result = solver.solve(epr, num_tor)

print("status   :", result.status)
print("objective:", f"{result.objective_value:.4f}")
for partition, rack in sorted(result.part_to_rack.items()):
    print(f"  partition {partition} -> rack {rack}")

The heavily communicating pairs — 0↔1 and 2↔3 — should land together.

Parameter Meaning
F_intra Fidelity of an intra-rack entanglement
F_inter Fidelity of an inter-rack entanglement — lower, which is what creates the incentive
time_limit_ms Solver budget. ILP is exponential in the worst case; this bounds it.
num_starts Multi-start attempts
local_refine Hill-climb the ILP result afterwards
prune_percentile Drop low-demand pairs before solving, for speed

PartitionResult.remap_qubits(optimization_level="ilp") wires this into the pipeline so you do not have to build the demand matrix yourself.

Always read result.status

The answer may not have come from the ILP at all. RackILP builds a heuristic assignment first, uses it to warm-start the MIP, and keeps whichever is better — so the status tells you which path actually produced the placement:

status Meaning
OPTIMAL The MIP solved to optimality
FEASIBLE The MIP returned a valid solution but did not prove optimality — usually a time limit
HEURISTIC The MIP was not attempted
HEURISTIC-FALLBACK The MIP ran but returned no usable solution, so the heuristic result stands
HEURISTIC-EXCEPTION The MIP raised, and the heuristic result stands

A HEURISTIC-* status still gives you a usable placement — the heuristic is decent, and the example above groups the heavily communicating partitions correctly — but it is not an optimum, so do not report it as one.

The exception is swallowed

HEURISTIC-EXCEPTION means the error itself was discarded, so it cannot distinguish a missing solver from a malformed problem. If you see it, reduce the problem until it succeeds, or report it via Support. OR-Tools ships with CBC as the default; solver_name selects another.


qndk.encoders

Graph-neural-network embeddings of circuits and networks. Read-only, opt-in, and entirely outside the compile path — they exist so ML-based scheduling and partitioning research has a ready-made embedding space instead of everyone re-deriving features from scratch.

Needs PyTorch and PyTorch Geometric, which arrive with the [ml] extra.

# fragment: needs torch + torch-geometric from the [ml] extra
from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.encoders import encode_circuit, encode_network

circuit = QiskitCircuit("qft", num_qubits=6)
network = build_clos(ports_per_switch=4, qpus_per_rack=2,
                     qpu_spec=QPUSpec(comp_qubits=4))

circuit_embedding = encode_circuit(circuit)
network_embedding = encode_network(network, which="graph")
print(type(circuit_embedding), type(network_embedding))
Function Embeds
encode_circuit(circuit, device=..., return_full=...) A circuit's DAG structure
encode_network(network, device=..., which=...) A network's topology and state
preload_encoders(device=...) Loads checkpoints eagerly, so the first call in a hot loop is not the slow one

which selects the network representation: "graph", "graph_proj" or "rl" — the last shaped for reinforcement-learning use.

The trained checkpoints ship with the wheel, so no training step is needed.


qndk.http

A FastAPI service wrapping the pipeline, with WebSocket push for live visualization. Session-based: create a session, build a topology and network in it, submit jobs, run, watch.

uvicorn qndk.http.server:app --port 8080

Interactive API docs are then at /docs, and the OpenAPI schema at /openapi.json.

Routes

Method Path Purpose
POST /api/sessions Create a session
DELETE /api/sessions/{session_id} Destroy one
POST /api/sessions/{id}/topology Build a topology (Clos today)
POST /api/sessions/{id}/network Build a network over that topology
POST /api/sessions/{id}/circuits/load_from_config Load circuits from a module
POST /api/sessions/{id}/create_batch Create a batch of jobs
GET /api/sessions/{id}/get_batch_info Inspect the batch
POST /api/sessions/{id}/execute_simulation Start a run
GET /api/sessions/{id}/list_simulations List runs
GET /api/sessions/{id}/list_simulation_information/{run_id} One run's results
GET /api/sessions/{id}/list_simulation_information/{run_id}/circuits Per-circuit results
WS /ws/{session_id} Live topology and schedule updates

Session shape

The order matters — each step depends on the last:

flowchart LR
    A["POST /sessions"] --> B["POST /topology"]
    B --> C["POST /network"]
    C --> D["POST /create_batch"]
    D --> E["POST /execute_simulation"]
    E --> F["GET /list_simulation_information"]
    E -.->|live| G["WS /ws/{id}"]

Request bodies are Pydantic models, so the schema at /openapi.json is authoritative — TopologyPayload takes ports_per_switch, qpus_per_rack and qubits_per_qpu; RunRequest takes algorithm, heuristic, mapping_algorithm and step_delay.

step_delay paces the WebSocket replay so a human can watch the schedule unfold; set it to 0 for a machine consumer.

Visualization

The service renders the topology with per-job subgraph highlighting, blending colors where several jobs share a switch or link. That rendering is tied to the WebSocket protocol rather than being general-purpose plotting — for that, use the plot() methods on the SDK objects.


qndk.utils

Small shared helpers. The public one:

from qndk.utils import ordered_key

# a canonical, order-independent key for a pair of QPUs
print(ordered_key("qpu-3", "qpu-1"))
print(ordered_key("qpu-1", "qpu-3"))
print(ordered_key("qpu-1", "qpu-3") == ordered_key("qpu-3", "qpu-1"))

Entanglement between two QPUs is symmetric, so anything keyed by a QPU pair has to treat (a, b) and (b, a) as the same thing. ordered_key returns one canonical key for either order.