Skip to content

qndk.partition

Decides which circuit qubit runs on which QPU. This is the single most consequential choice in the pipeline, because it determines how many gates have to cross the network — and every crossing costs an entangled pair.

The problem

Take the circuit's qubit interaction graph, whose edge weights count two-qubit gates between pairs of qubits. Cut it into groups that fit the QPUs. Every edge you cut becomes remote gates.

So minimizing EPR cost is minimizing a weighted graph cut, subject to capacity constraints. That is the problem all three partitioners solve, by different means.

The three strategies

Class Approach Use when
KernighanLinPartitioner Recursive Kernighan-Lin bisection with restarts Default. Pure Python, always available.
MetisPartitioner METIS multilevel partitioning via pymetis Large interaction graphs where KL gets slow. Not on Windows.
WindowBasedCircuitPartitioner Splits the circuit into windows and re-partitions each The interaction pattern shifts as the circuit runs.
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.partition.wbcp import WindowBasedCircuitPartitioner

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=10))
network = build_clos(ports_per_switch=4, qpus_per_rack=2,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))

for cls in (KernighanLinPartitioner, WindowBasedCircuitPartitioner):
    result = cls(circuit, network).partition()
    groups = result.circuit_qubit_groups()
    print(f"{cls.__name__:32s} epr={result.total_epr_count:3d} "
          f"groups={[len(g) for g in groups]}")

All three share one constructor shape — (circuit, network, seed=...) — and all return a PartitionResult. Swapping strategies is a one-word change.

Windowed partitioning

A single static mapping assumes the circuit's interaction pattern is constant, and it usually is not. WindowBasedCircuitPartitioner splits the circuit into consecutive windows and partitions each one separately, paying state-teleportation cost to move qubits between windows in exchange for cheaper gates within them.

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.wbcp import WindowBasedCircuitPartitioner

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=10))
network = build_clos(ports_per_switch=4, qpus_per_rack=2,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))

for window_size in (None, 4, 8):
    result = WindowBasedCircuitPartitioner(
        circuit, network, window_size=window_size).partition()
    tracker = result.qubit_tracker
    print(f"window_size={str(window_size):4s} epr={result.total_epr_count:3d} "
          f"dynamic={tracker.is_dynamic()}")

window_size=None lets the algorithm choose. A tracker reporting is_dynamic() means the qubit-to-QPU mapping changes during execution; is_static() means one mapping holds throughout.

PartitionResult

What every partitioner returns, and what the simulator consumes.

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

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"))
result = KernighanLinPartitioner(circuit, network).partition()

print("epr cost        :", result.total_epr_count)
print("qubit groups    :", result.circuit_qubit_groups())
print("edge weights    :", result.calculate_partition_edge_weights())
print("partition events:", len(result.partition_events or []))
print("network is same :", result.network is network)

# which network qubit each circuit qubit landed on, for the first DAG node
mapping = result.qubit_tracker.last_circuit_to_network()
for circuit_qubit, network_qubit in list(mapping.items())[:4]:
    print(f"  {circuit_qubit} -> {network_qubit}")
Member What it gives you
total_epr_count Entangled pairs the split implies
circuit_qubit_groups() Which circuit qubits share a QPU
calculate_partition_edge_weights() Cut weight between each partition pair
qubit_tracker The full qubit-to-network mapping, per window
partition_events Teleportation and gate-packing decisions
network The network it was built against — the same object you passed in

QubitPartitionTracker

Holds the mapping from DAG node to window to qubit assignment. You rarely build one directly, but reading it is how you find out what the partitioner decided.

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.wbcp import WindowBasedCircuitPartitioner

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"))
tracker = WindowBasedCircuitPartitioner(circuit, network).partition().qubit_tracker

print("empty   :", tracker.is_empty())
print("static  :", tracker.is_static())
print("dynamic :", tracker.is_dynamic())
print("teleports:", len(tracker.teleportation_events()))

Partition events

Three kinds, recorded as PartitionEvent objects tied to DAG node ids:

event_type Meaning
gate-pack Several remote gates batched onto one entangled pair
gate-teleport The operation moves to where the data is
state-teleport The qubit moves to where the operation will run

The distinction matters because they cost differently: teleporting a state is a one-off that then makes many gates local, while packing amortizes one pair across several gates.

A manual partition

Sometimes you want an explicit assignment rather than an algorithm's — to reproduce a paper's layout, or as a baseline.

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

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

# two circuit qubits per QPU, chosen by hand. Ids follow the
# global_qubit_id() convention: q_0, q_1, ... for a single register.
groups = [["q_0", "q_1"], ["q_2", "q_3"]]
result = new_partition_result(qc, network, manual_qubit_groups=groups)
sim = DQCScheduler(result).run_simulation()
print("manual layout epr cost:", sim.epr_cost())

Qubit names follow the global_qubit_id() convention from transpile — q_0, q_1, and so on for a single register. Getting them wrong raises a KeyError naming the id it could not find, rather than silently partitioning something else.

Writing your own

Subclass Partitioner and implement _partition() — note the underscore. The public partition() is a template method on the base class: it calls your _partition(), validates what came back, and wraps it in a PartitionResult with the network and circuit already attached. So you return the three things only you can know, and inherit the bookkeeping.

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.quantum_network import build_clos, QPUSpec
from qndk.partition.partition import (Partitioner, comp_qubits_by_qpu_desc,
                                      qubit_tracker_from_groups)


class RoundRobinPartitioner(Partitioner):
    """Deal circuit qubits to QPUs in order. A deliberately bad baseline."""

    def _partition(self):
        # -> (QubitPartitionTracker, total_epr_count, [PartitionEvent])
        qpus = comp_qubits_by_qpu_desc(self.network)
        groups = [[] for _ in qpus]
        for i in range(len(self.circuit.circuit.qubits)):
            groups[i % len(qpus)].append(f"q_{i}")
        tracker, epr = qubit_tracker_from_groups(
            self.circuit, self.network, groups)
        return tracker, epr, []


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"))
result = RoundRobinPartitioner(circuit, network).partition()
print("round-robin epr:", result.total_epr_count)
print("groups         :", result.circuit_qubit_groups())

_partition() must return exactly that triple; returning anything else raises a ValueError naming the expected shape rather than failing later inside the simulator.

The same shape works for any layout strategy you want to try.

Determinism

Partitioners seed their own RNG with a fixed default (seed=20250101), so the same circuit on the same network always produces the same layout. That is what makes A/B comparisons readable — change one hardware parameter and the difference you see is that parameter, not layout noise.

Pass a different seed to sample other layouts:

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

circuit = QiskitCircuit("qft", num_qubits=10)
network = build_clos(ports_per_switch=4, qpus_per_rack=2,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))
for seed in (20250101, 7, 99):
    result = KernighanLinPartitioner(circuit, network, seed=seed).partition()
    print(f"seed={seed:<9d} epr={result.total_epr_count}")

ILP refinement

PartitionResult.remap_qubits() re-places partitions onto racks with an ILP, trading solve time for a better placement. See Extras.

Plotting

# fragment: opens windows; needs Graphviz
result.plot()             # the partitioned interaction graph
result.plot_network()     # the split drawn on the topology

Next

A PartitionResult goes to the simulator, or to compile if you want a runnable circuit rather than metrics.