Skip to content

qndk.transpile

Turns a circuit into the representations the distributed stages actually work on. Everything downstream reads graphs, not gate lists, and this is where those graphs come from.

TranspiledCircuit is the one type that matters.

What it does

Three things, in order:

  1. Normalizes the gate set. Runs the circuit through Qiskit's transpiler so later stages see a predictable basis rather than whatever high-level gates you wrote.
  2. Builds a DAG. Converts the Qiskit DAGCircuit into a NetworkX DiGraph with node attributes the simulator and partitioner can annotate — which is what lets the SDK inject synthetic nodes for teleportation events and entanglement generation.
  3. Builds the qubit interaction graph. An undirected graph whose nodes are circuit qubits and whose edge weights count two-qubit gates between them. This is the graph partitioners actually minimize a cut on.
from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=6))

dag = circuit.networkx_dag()
interactions = circuit.qubit_interaction_graph()

print(f"dag          : {dag.number_of_nodes()} nodes, "
      f"{dag.number_of_edges()} edges")
print(f"interactions : {interactions.number_of_nodes()} qubits, "
      f"{interactions.number_of_edges()} interacting pairs")

# edge weight = how many two-qubit gates act on that pair
heaviest = sorted(interactions.edges(data=True),
                  key=lambda e: -e[2].get("weight", 1))[:3]
for u, v, data in heaviest:
    print(f"  {u} <-> {v}: weight {data.get('weight')}")

That interaction graph is the whole reason partitioning works. Qubits that interact often want to be on the same QPU; the edge weights are what says which pairs those are.

Construction options

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit

base = QiskitCircuit("qft", num_qubits=6)

for level in (0, 1, 3):
    tc = TranspiledCircuit(base, optimization_level=level)
    qc = tc.circuit
    print(f"optimization_level={level}: depth={qc.depth():4d} "
          f"size={qc.size():4d}")
Parameter Default Effect
optimization_level 0 Passed to Qiskit's transpiler. 0 preserves your circuit's structure, which keeps the interaction graph faithful to what you wrote.
decompose True Break composite gates down so the DAG has real operations in it
basis_gates None Restrict the gate set; None leaves Qiskit's default

Leaving optimization_level=0 is deliberate as a default: optimizing the circuit changes its interaction graph, and therefore changes the partitioning problem. If you want to study the effect of optimization on EPR cost, that is a valid experiment — just be explicit about it.

Checking capacity before you commit

Partitioning a circuit too large for the network raises. This asks first:

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

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=10))

for comp in (2, 4):
    network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                         qpu_spec=QPUSpec(comp_qubits=comp,
                                          connectivity="all2all"))
    fits, needed = circuit.enough_physical_qubits(network)
    print(f"comp_qubits={comp}: fits={fits} "
          f"needs={needed} has={network.total_comp_qubits()}")

Reuse across partitioners

Transpiling is not free, and the result is immutable enough to share. Build it once when comparing strategies:

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=8))
network = build_clos(ports_per_switch=4, qpus_per_rack=1,
                     qpu_spec=QPUSpec(comp_qubits=4, connectivity="all2all"))

for cls in (KernighanLinPartitioner, WindowBasedCircuitPartitioner):
    partition = cls(circuit, network).partition()
    print(f"{cls.__name__:32s} epr={partition.total_epr_count}")

Use .copy() if a stage needs to mutate one without disturbing the other, and .dag_copy() for a throwaway copy of just the DAG.

Qubit identifiers

Qubits are referred to by string ids throughout the SDK, not by Qiskit Qubit objects, because they have to survive serialization and cross process boundaries. global_qubit_id() and global_clbit_id() produce them.

from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
from qndk.transpile.ids import global_qubit_id

circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=4))
ids = [global_qubit_id(q) for q in circuit.circuit.qubits]
print("qubit ids:", ids)

Those same strings are the keys in a PartitionResult's qubit mapping, so the ids are what ties the circuit to the network.

Injecting simulation results back into the DAG

After a run, the event log can be folded back into a copy of the DAG so entanglement generation and teleportation appear as real nodes. This is what the DAG-with-timesteps plot draws.

# fragment: needs a completed SimulationResult
from qndk.transpile.transpile import (
    inject_simulation_results_to_networkx_dag,
    inject_teleportation_events_to_networkx_dag)

graph = circuit.dag_copy()
graph = inject_teleportation_events_to_networkx_dag(
    graph, partition.partition_events)
graph = inject_simulation_results_to_networkx_dag(
    graph, result.execution_log)

Plotting

# fragment: opens windows; needs Graphviz
circuit.plot_dag()                        # the annotated DAG
circuit.plot_unpartitioned_interactions() # the interaction graph
circuit.draw_qiskit_dag()                 # Qiskit's own view

The ML transpiler

An experimental learned alternative to the gate-normalization step also ships, outside the default path. It is not part of the documented API yet; ask via Support if you want to try it.

Next

The transpiled circuit goes to partition, which needs a network to split it across.