qndk.analysis¶
Collects metrics across a run and turns a set of runs into dataframes and
comparison plots. Passive by design: recording never modifies simulation
state, so adding an Analyzer to a script cannot change its results.
The model¶
Analyzer is a recorder with one method per pipeline stage. You call the
record_* methods as the run proceeds, then query.
from qndk.analysis.analyzer import Analyzer
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
analyzer = Analyzer(run_id="demo")
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()
result = DQCScheduler(partition).run_simulation()
analyzer.record_circuit(circuit, name="qft-8")
analyzer.record_transpile(circuit)
analyzer.record_network(network)
analyzer.record_topology(network, topology_type="clos", topology_id="4x1")
analyzer.record_partition("kernighan-lin", partition)
snapshot = analyzer.to_dict()
print("top-level keys:", sorted(snapshot))
# the per-stage metrics live under "record"; "runs" and "jobs" hold the
# cross-run rows added by record_run_global / record_run_jobs
record = snapshot["record"]
print("recorded stages:", sorted(k for k, v in record.items() if v))
print("circuit :", record["circuit"])
print("transpile :", record["transpile"])
print("partitions :", sorted(record["partitions"]))
to_dict() has two halves worth keeping straight: record is this run's
per-stage detail, while runs and jobs are the tabular rows that
get_runs_df() and get_jobs_df() read.
| Method | Records |
|---|---|
record_circuit(obj, name=...) |
Logical qubit count and circuit identity |
record_transpile(obj) |
Depth, size, width, gate counts |
record_topology(obj, ...) |
QPU, switch and physical qubit counts |
record_network(obj) |
A network summary |
record_partition(algorithm, result) |
EPR cost, per-partition costs, optionally the full mapping |
record_schedule(algorithm, obj, ...) |
Makespan, wastage, latency stats |
record_run_global(run_key, ...) |
One row per run, for cross-run comparison |
record_run_jobs(run_key, rows) |
Per-job rows within a run |
Comparing runs¶
record_run_global is the one that matters for experiments: each call adds
a row keyed by run_key, and the whole set comes back as a dataframe.
from qndk.analysis.analyzer import Analyzer
from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
from qndk.quantum_network import QPUSpec, build_clos, build_fattree
from qndk.partition.kl import KernighanLinPartitioner
from qndk.partition.wbcp import WindowBasedCircuitPartitioner
from qndk.simulator.dqc import DQCScheduler
analyzer = Analyzer(run_id="topology-x-partitioner")
qpu_spec = QPUSpec(comp_qubits=4, connectivity="all2all")
topologies = {"clos": build_clos(ports_per_switch=4, qpus_per_rack=1,
qpu_spec=qpu_spec),
"fattree": build_fattree(num_qpus=4, qpu_spec=qpu_spec)}
partitioners = {"kl": KernighanLinPartitioner,
"wbcp": WindowBasedCircuitPartitioner}
for topo_name, network in topologies.items():
for algo_name, cls in partitioners.items():
circuit = TranspiledCircuit(QiskitCircuit("qft", num_qubits=8))
partition = cls(circuit, network).partition()
result = DQCScheduler(partition).run_simulation()
analyzer.record_run_global(
run_key=f"{topo_name}-{algo_name}",
topology_type=topo_name,
topology_id=f"{len(network.qpu_ids())}qpu",
algorithm=algo_name,
makespan=result.final_timestep,
avg_latency_over_jobs=result.final_timestep,
switch_events_sum=len(result.switch_sequence()),
extras={"epr_cost": result.epr_cost()},
)
df = analyzer.get_runs_df()
print(df[["run_key", "topology_type", "algorithm", "makespan"]]
.to_string(index=False))
Then narrow or aggregate:
# fragment: continues the run above
print(analyzer.find_run_keys(topology_type="clos"))
print(analyzer.summarize_runs(by=("topology_type", "algorithm"),
metrics=("makespan",), agg="mean"))
Persisting¶
from qndk.analysis.analyzer import Analyzer
from qndk.circuit.qiskit_circuits import QiskitCircuit
from qndk.transpile.transpile import TranspiledCircuit
analyzer = Analyzer(run_id="saved-run")
analyzer.record_circuit(TranspiledCircuit(QiskitCircuit("qft",
num_qubits=4)),
name="qft-4")
path = analyzer.save_json("metrics.json")
print("wrote:", path)
Analyzer(run_id=..., base_dir="runs") controls where. Values that do not
serialize cleanly are sanitized rather than raising, so a save cannot lose
a run's other metrics because one field was awkward.
Plots¶
All take a run_key or a dataframe, and all come off the Analyzer:
| Method | Shows |
|---|---|
plot_makespan_by_algo_topo() |
Makespan across topologies and algorithms |
plot_wastage_by_algo_topo() |
Wastage, same axes |
plot_avg_latency_by_algo_topo() |
Average latency, same axes |
plot_switch_events_stacked_by_algo_topo() |
NIR and TEL switch events, stacked |
plot_latency_per_job(run_key) |
Per-circuit latency within one run |
plot_circuit_timeline(run_key) |
When each circuit ran |
plot_levels_utilization(run_key) |
Qubit utilization over time |
plot_levels_utilization_smooth(run_key) |
The same, smoothed |
plot_levels_wastage_interactive(run_key) |
Interactive per-level wastage (Plotly) |
plot_switch_events_per_job_stacked(run_key) |
Switch events per circuit |
# fragment: opens windows
analyzer.plot_makespan_by_algo_topo()
analyzer.plot_wastage_by_algo_topo()
analyzer.plot_levels_utilization(run_key="clos-kl")
The utilization and wastage plots want data from batchscheduler — they read the per-qubit timeline a batch run produces.
Dataframes¶
| Method | Returns |
|---|---|
get_runs_df(filters=...) |
One row per run |
get_jobs_df(filters=...) |
One row per job across all runs |
get_job_rows(job_id, run_key=...) |
Rows for one job |
job_specifics(job_id, ...) |
One job's detail, optionally as a dataframe |
summarize_runs(by=..., metrics=..., agg=...) |
Grouped aggregate |
find_run_keys(**filters) |
Run keys matching a filter |
These are ordinary pandas dataframes, so anything pandas can do — pivot, merge, export — works from here.
Next¶
compile is the other terminal stage: rather than metrics, it produces a circuit you can actually execute.