Canvas becomes data-aware: shapes, headline metrics, perf tint, pre-flight
Browse filesAfter a Run, the canvas itself shows what's happening at every node
and every edge — instead of just being a connection diagram with the
real numbers banished to a sidebar.
circuit_shape on every StepResult (backend)
StepResult schema gains a CircuitShape sidecar field
({num_qubits, depth, size, num_parameters}). workflow_service
snapshots ctx["circuit"] AFTER every successful handler and attaches
it to the step. Built-in handlers and plugin steps both populate;
ctx-less steps (source plugins with no upstream, errored steps)
leave it as None.
Edge data-flow labels (frontend)
styledEdges memo in FlowCanvas reads the SOURCE node's circuit_shape
and attaches a tiny monospace label to the React Flow edge:
"5q · d8 · 17g". Falls back to no label when the source step doesn't
have a shape (no run yet, or source errored). Drop-target highlight
during drag-to-insert composes on top.
Per-node headline metric + duration chip + tone (frontend)
New lib/headlineMetric.ts table picks the most representative scalar
per node kind, formatted for the tile:
- QuCAD → "Pruned 23%"
- QuBound → "Error bound ≤ 0.142"
- CompressVQC → "Folded 17→14"
- Qshot → "Shots 8192"
- Fidelity → "Fidelity 0.95" (color-toned by value)
- Output → "Final fidelity 0.95"
- plugins → first numeric in summary/scalars
New QNode.RunResultStrip renders the headline + a duration chip
color-coded by speed bucket (fast / medium / slow / very-slow) +
the existing cached-from-prefix indicator. Tooltip on the duration
chip explains the bucket ("Took over 1 minute — likely the
pipeline's bottleneck"). Errored steps get a red strip with the
backend's message instead of the headline.
Pre-flight pipeline lint (frontend)
New lib/preflightChecks.ts runs a cheap O(nodes+edges) lint over
the current graph + loaded circuit. Findings split into warn / error
severities:
- error: Algorithm/Metric/Sink with no upstream Input
- error: no Input block at all
- warn: no Output block
- warn: Source has upstream connection (input gets discarded)
- warn: Sink has downstream connection (nothing runs after a sink)
- warn: QuCAD without backend upstream (will use default FakeFez)
- warn: QuBound on circuit outside 5-8 qubit training range
- warn: Qshot on circuit outside 5-8 qubit reference range
FlowCanvas memos the findings against {nodes, edges, circuit,
plugins} so the banner updates live as the user edits the graph.
The banner sits between the canvas toolbar and the React Flow
viewport, collapsed by default (one-line "N issues" chip), expands
on click into a per-finding bullet list. Run button is NOT disabled
— users can still attempt a run with known warnings; this is a
research tool, not a CI gate.
7-case logic test on the preflight rules covers each branch
(clean baseline, missing input, qucad±backend, source upstream,
sink downstream, qubound out-of-range). All pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- backend/app/schemas.py +19 -0
- backend/app/services/workflow_service.py +40 -3
- frontend/src/components/FlowCanvas.tsx +125 -16
- frontend/src/components/QNode.tsx +101 -0
- frontend/src/lib/api.ts +13 -0
- frontend/src/lib/headlineMetric.ts +152 -0
- frontend/src/lib/preflightChecks.ts +213 -0
|
@@ -74,6 +74,19 @@ class RunRequest(BaseModel):
|
|
| 74 |
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
class StepResult(BaseModel):
|
| 78 |
"""One stage of the pipeline's output (rendered as a panel in the UI)."""
|
| 79 |
|
|
@@ -92,6 +105,12 @@ class StepResult(BaseModel):
|
|
| 92 |
# PluginFigures component renders inline below the step's summary
|
| 93 |
# table. None means "no figures" (the common case for built-ins).
|
| 94 |
figures: list[dict[str, Any]] | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
class RunResponse(BaseModel):
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
+
class CircuitShape(BaseModel):
|
| 78 |
+
"""Post-step circuit shape, attached to each StepResult so the
|
| 79 |
+
frontend can render data-flow labels on the edges leaving this
|
| 80 |
+
node (e.g. "5q · d8 · 17 gates"). None means "this step didn't
|
| 81 |
+
touch the circuit" (most metric / sink blocks) and the frontend
|
| 82 |
+
falls back to the upstream shape."""
|
| 83 |
+
|
| 84 |
+
num_qubits: int
|
| 85 |
+
depth: int
|
| 86 |
+
size: int # total gate count
|
| 87 |
+
num_parameters: int
|
| 88 |
+
|
| 89 |
+
|
| 90 |
class StepResult(BaseModel):
|
| 91 |
"""One stage of the pipeline's output (rendered as a panel in the UI)."""
|
| 92 |
|
|
|
|
| 105 |
# PluginFigures component renders inline below the step's summary
|
| 106 |
# table. None means "no figures" (the common case for built-ins).
|
| 107 |
figures: list[dict[str, Any]] | None = None
|
| 108 |
+
# Snapshot of the circuit's shape AFTER this step ran. Used by
|
| 109 |
+
# the canvas to render data-flow labels on outgoing edges. None
|
| 110 |
+
# for steps that ran in a context with no circuit (e.g. a source
|
| 111 |
+
# plugin executed with no upstream) or that errored before the
|
| 112 |
+
# circuit was computable.
|
| 113 |
+
circuit_shape: CircuitShape | None = None
|
| 114 |
|
| 115 |
|
| 116 |
class RunResponse(BaseModel):
|
|
@@ -96,6 +96,7 @@ def _make_step(
|
|
| 96 |
started_at: float | None = None,
|
| 97 |
label: str | None = None,
|
| 98 |
figures: list[dict] | None = None,
|
|
|
|
| 99 |
) -> StepResult:
|
| 100 |
t0 = started_at if started_at is not None else _now()
|
| 101 |
return StepResult(
|
|
@@ -108,9 +109,22 @@ def _make_step(
|
|
| 108 |
summary=summary or {},
|
| 109 |
message=message,
|
| 110 |
figures=figures,
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
def _default_label(node_type: str) -> str:
|
| 115 |
return {
|
| 116 |
"input_circuit": "Input circuit",
|
|
@@ -660,6 +674,7 @@ def run_pipeline(
|
|
| 660 |
"depth": circuit.depth(),
|
| 661 |
"num_parameters": circuit.num_parameters,
|
| 662 |
},
|
|
|
|
| 663 |
)
|
| 664 |
)
|
| 665 |
continue
|
|
@@ -667,7 +682,15 @@ def run_pipeline(
|
|
| 667 |
handler = _HANDLERS.get(node.type)
|
| 668 |
if handler is not None:
|
| 669 |
try:
|
| 670 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
except Exception:
|
| 672 |
# Log the real traceback server-side; the user-facing
|
| 673 |
# message stays generic so library exception text
|
|
@@ -688,8 +711,13 @@ def run_pipeline(
|
|
| 688 |
|
| 689 |
# Not a built-in — try a user plugin if we have a user_id.
|
| 690 |
if user_id:
|
| 691 |
-
|
| 692 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
break
|
| 694 |
continue
|
| 695 |
|
|
@@ -768,6 +796,7 @@ def run_pipeline_stream(
|
|
| 768 |
"depth": circuit.depth(),
|
| 769 |
"num_parameters": circuit.num_parameters,
|
| 770 |
},
|
|
|
|
| 771 |
)
|
| 772 |
_cache_put(prefix_key, result, ctx)
|
| 773 |
yield result
|
|
@@ -777,6 +806,10 @@ def run_pipeline_stream(
|
|
| 777 |
if handler is not None:
|
| 778 |
try:
|
| 779 |
result = handler(node, ctx, settings)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
_cache_put(prefix_key, result, ctx)
|
| 781 |
yield result
|
| 782 |
except Exception:
|
|
@@ -800,6 +833,10 @@ def run_pipeline_stream(
|
|
| 800 |
# Not a built-in — plugin dispatch path.
|
| 801 |
if user_id:
|
| 802 |
result = _handle_plugin(node, ctx, settings, user_id=user_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
# Only cache successful plugin runs so a transient crash
|
| 804 |
# doesn't get pinned to the prefix.
|
| 805 |
if result.status == "ok":
|
|
|
|
| 96 |
started_at: float | None = None,
|
| 97 |
label: str | None = None,
|
| 98 |
figures: list[dict] | None = None,
|
| 99 |
+
circuit_shape: dict | None = None,
|
| 100 |
) -> StepResult:
|
| 101 |
t0 = started_at if started_at is not None else _now()
|
| 102 |
return StepResult(
|
|
|
|
| 109 |
summary=summary or {},
|
| 110 |
message=message,
|
| 111 |
figures=figures,
|
| 112 |
+
circuit_shape=circuit_shape, # type: ignore[arg-type]
|
| 113 |
)
|
| 114 |
|
| 115 |
|
| 116 |
+
def _shape_of(qc: "QuantumCircuit") -> dict:
|
| 117 |
+
"""Extract the small (qubits, depth, size, num_parameters) shape
|
| 118 |
+
tuple from a QuantumCircuit, JSON-ready for the StepResult. Used
|
| 119 |
+
by the run-loop to attach a per-step snapshot for edge labels."""
|
| 120 |
+
return {
|
| 121 |
+
"num_qubits": qc.num_qubits,
|
| 122 |
+
"depth": qc.depth(),
|
| 123 |
+
"size": qc.size(),
|
| 124 |
+
"num_parameters": qc.num_parameters,
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
|
| 128 |
def _default_label(node_type: str) -> str:
|
| 129 |
return {
|
| 130 |
"input_circuit": "Input circuit",
|
|
|
|
| 674 |
"depth": circuit.depth(),
|
| 675 |
"num_parameters": circuit.num_parameters,
|
| 676 |
},
|
| 677 |
+
circuit_shape=_shape_of(circuit),
|
| 678 |
)
|
| 679 |
)
|
| 680 |
continue
|
|
|
|
| 682 |
handler = _HANDLERS.get(node.type)
|
| 683 |
if handler is not None:
|
| 684 |
try:
|
| 685 |
+
step = handler(node, ctx, settings)
|
| 686 |
+
# Snapshot the circuit's current shape onto the step.
|
| 687 |
+
# Handlers don't bother setting this themselves —
|
| 688 |
+
# they just mutate ctx, and we read it here.
|
| 689 |
+
if step.status == "ok" and "circuit" in ctx:
|
| 690 |
+
step = step.model_copy(
|
| 691 |
+
update={"circuit_shape": _shape_of(ctx["circuit"])},
|
| 692 |
+
)
|
| 693 |
+
steps.append(step)
|
| 694 |
except Exception:
|
| 695 |
# Log the real traceback server-side; the user-facing
|
| 696 |
# message stays generic so library exception text
|
|
|
|
| 711 |
|
| 712 |
# Not a built-in — try a user plugin if we have a user_id.
|
| 713 |
if user_id:
|
| 714 |
+
step = _handle_plugin(node, ctx, settings, user_id=user_id)
|
| 715 |
+
if step.status == "ok" and "circuit" in ctx:
|
| 716 |
+
step = step.model_copy(
|
| 717 |
+
update={"circuit_shape": _shape_of(ctx["circuit"])},
|
| 718 |
+
)
|
| 719 |
+
steps.append(step)
|
| 720 |
+
if step.status == "error":
|
| 721 |
break
|
| 722 |
continue
|
| 723 |
|
|
|
|
| 796 |
"depth": circuit.depth(),
|
| 797 |
"num_parameters": circuit.num_parameters,
|
| 798 |
},
|
| 799 |
+
circuit_shape=_shape_of(circuit),
|
| 800 |
)
|
| 801 |
_cache_put(prefix_key, result, ctx)
|
| 802 |
yield result
|
|
|
|
| 806 |
if handler is not None:
|
| 807 |
try:
|
| 808 |
result = handler(node, ctx, settings)
|
| 809 |
+
if result.status == "ok" and "circuit" in ctx:
|
| 810 |
+
result = result.model_copy(
|
| 811 |
+
update={"circuit_shape": _shape_of(ctx["circuit"])},
|
| 812 |
+
)
|
| 813 |
_cache_put(prefix_key, result, ctx)
|
| 814 |
yield result
|
| 815 |
except Exception:
|
|
|
|
| 833 |
# Not a built-in — plugin dispatch path.
|
| 834 |
if user_id:
|
| 835 |
result = _handle_plugin(node, ctx, settings, user_id=user_id)
|
| 836 |
+
if result.status == "ok" and "circuit" in ctx:
|
| 837 |
+
result = result.model_copy(
|
| 838 |
+
update={"circuit_shape": _shape_of(ctx["circuit"])},
|
| 839 |
+
)
|
| 840 |
# Only cache successful plugin runs so a transient crash
|
| 841 |
# doesn't get pinned to the prefix.
|
| 842 |
if result.status == "ok":
|
|
@@ -46,6 +46,7 @@ import { useApp } from "../lib/store";
|
|
| 46 |
import { api } from "../lib/api";
|
| 47 |
import { getUserId } from "../lib/userId";
|
| 48 |
import { usePrefersReducedMotion } from "../lib/useMediaQuery";
|
|
|
|
| 49 |
import {
|
| 50 |
buildSharePayload,
|
| 51 |
buildShareUrl,
|
|
@@ -137,6 +138,10 @@ export function FlowCanvas() {
|
|
| 137 |
const sampleKey = useApp((s) => s.sampleKey);
|
| 138 |
const setRun = useApp((s) => s.setRun);
|
| 139 |
const running = useApp((s) => s.running);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
const setRunning = useApp((s) => s.setRunning);
|
| 141 |
const useLiveIbm = useApp((s) => s.useLiveIbm);
|
| 142 |
const pendingBlockKinds = useApp((s) => s.pendingBlockKinds);
|
|
@@ -406,23 +411,77 @@ export function FlowCanvas() {
|
|
| 406 |
|
| 407 |
/** Style override for the targeted edge — animated dashed accent so
|
| 408 |
* it's visually obvious you're about to splice into it. */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
const styledEdges = useMemo(() => {
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
// ---- Touch drag bridge (mobile drag-to-insert) ---------------------
|
| 428 |
//
|
|
@@ -778,6 +837,7 @@ export function FlowCanvas() {
|
|
| 778 |
</button>
|
| 779 |
</div>
|
| 780 |
</div>
|
|
|
|
| 781 |
<div
|
| 782 |
ref={wrapperRef}
|
| 783 |
// qf-canvas-wrapper applies touch-action: none so iOS Safari
|
|
@@ -872,6 +932,55 @@ export function FlowCanvas() {
|
|
| 872 |
/** Floating chip following the finger during a touch drag. Shows the
|
| 873 |
* block's family color + initials so the user sees what they're
|
| 874 |
* carrying. */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
function TouchDragPreview({
|
| 876 |
kind,
|
| 877 |
x,
|
|
|
|
| 46 |
import { api } from "../lib/api";
|
| 47 |
import { getUserId } from "../lib/userId";
|
| 48 |
import { usePrefersReducedMotion } from "../lib/useMediaQuery";
|
| 49 |
+
import { runPreflight, type PreflightFinding } from "../lib/preflightChecks";
|
| 50 |
import {
|
| 51 |
buildSharePayload,
|
| 52 |
buildShareUrl,
|
|
|
|
| 138 |
const sampleKey = useApp((s) => s.sampleKey);
|
| 139 |
const setRun = useApp((s) => s.setRun);
|
| 140 |
const running = useApp((s) => s.running);
|
| 141 |
+
// Subscribe to the latest run so edge labels can pull per-step
|
| 142 |
+
// circuit shape (see styledEdges memo below). Cheap re-render
|
| 143 |
+
// when the user clicks Run.
|
| 144 |
+
const run = useApp((s) => s.run);
|
| 145 |
const setRunning = useApp((s) => s.setRunning);
|
| 146 |
const useLiveIbm = useApp((s) => s.useLiveIbm);
|
| 147 |
const pendingBlockKinds = useApp((s) => s.pendingBlockKinds);
|
|
|
|
| 411 |
|
| 412 |
/** Style override for the targeted edge — animated dashed accent so
|
| 413 |
* it's visually obvious you're about to splice into it. */
|
| 414 |
+
// Build a node_id → StepResult lookup so we can quickly answer
|
| 415 |
+
// "what came out of this node's source?" when attaching edge
|
| 416 |
+
// labels. Cheap to recompute; the run only changes after a run.
|
| 417 |
+
const stepByNodeId = useMemo(() => {
|
| 418 |
+
const map = new Map<string, NonNullable<typeof run>["steps"][number]>();
|
| 419 |
+
if (run) for (const s of run.steps) map.set(s.node_id, s);
|
| 420 |
+
return map;
|
| 421 |
+
}, [run]);
|
| 422 |
+
|
| 423 |
+
// Pre-flight lint pass: rerun whenever the graph or the loaded
|
| 424 |
+
// circuit changes. Cheap (O(nodes+edges)) and surfaces the findings
|
| 425 |
+
// as a soft banner above the canvas.
|
| 426 |
+
const preflightPlugins = useApp((s) => s.plugins);
|
| 427 |
+
const preflight: PreflightFinding[] = useMemo(
|
| 428 |
+
() =>
|
| 429 |
+
runPreflight({
|
| 430 |
+
nodes: nodes.map((n) => ({ ...n, data: { kind: n.data.kind } })),
|
| 431 |
+
edges,
|
| 432 |
+
circuit,
|
| 433 |
+
plugins: preflightPlugins,
|
| 434 |
+
}),
|
| 435 |
+
[nodes, edges, circuit, preflightPlugins],
|
| 436 |
+
);
|
| 437 |
+
|
| 438 |
const styledEdges = useMemo(() => {
|
| 439 |
+
return edges.map((edge) => {
|
| 440 |
+
const isTarget = edge.id === dropTargetEdgeId;
|
| 441 |
+
// Data-flow label: when a successful run exists, each edge
|
| 442 |
+
// shows the shape of what's flowing from source to target
|
| 443 |
+
// (qubits · depth · gates). Reads from the source node's step.
|
| 444 |
+
const srcStep = stepByNodeId.get(edge.source);
|
| 445 |
+
const shape = srcStep?.circuit_shape ?? null;
|
| 446 |
+
const label = shape
|
| 447 |
+
? `${shape.num_qubits}q · d${shape.depth} · ${shape.size}g`
|
| 448 |
+
: undefined;
|
| 449 |
+
let next = edge;
|
| 450 |
+
if (label) {
|
| 451 |
+
next = {
|
| 452 |
+
...next,
|
| 453 |
+
label,
|
| 454 |
+
labelBgPadding: [4, 2] as [number, number],
|
| 455 |
+
labelBgBorderRadius: 4,
|
| 456 |
+
labelStyle: {
|
| 457 |
+
fill: "rgb(var(--color-ink))",
|
| 458 |
+
fontSize: 9,
|
| 459 |
+
fontFamily: "monospace",
|
| 460 |
+
fontWeight: 500,
|
| 461 |
+
},
|
| 462 |
+
labelBgStyle: {
|
| 463 |
+
fill: "rgb(var(--color-surface))",
|
| 464 |
+
stroke: "rgb(var(--color-edge))",
|
| 465 |
+
strokeWidth: 0.5,
|
| 466 |
+
fillOpacity: 0.95,
|
| 467 |
+
},
|
| 468 |
+
};
|
| 469 |
+
}
|
| 470 |
+
if (isTarget) {
|
| 471 |
+
next = {
|
| 472 |
+
...next,
|
| 473 |
+
style: {
|
| 474 |
+
...(next.style ?? {}),
|
| 475 |
+
stroke: "rgb(var(--color-accent2))",
|
| 476 |
+
strokeWidth: 3,
|
| 477 |
+
strokeDasharray: "8 4",
|
| 478 |
+
},
|
| 479 |
+
animated: true,
|
| 480 |
+
};
|
| 481 |
+
}
|
| 482 |
+
return next;
|
| 483 |
+
});
|
| 484 |
+
}, [edges, dropTargetEdgeId, stepByNodeId]);
|
| 485 |
|
| 486 |
// ---- Touch drag bridge (mobile drag-to-insert) ---------------------
|
| 487 |
//
|
|
|
|
| 837 |
</button>
|
| 838 |
</div>
|
| 839 |
</div>
|
| 840 |
+
{preflight.length > 0 && <PreflightBanner findings={preflight} />}
|
| 841 |
<div
|
| 842 |
ref={wrapperRef}
|
| 843 |
// qf-canvas-wrapper applies touch-action: none so iOS Safari
|
|
|
|
| 932 |
/** Floating chip following the finger during a touch drag. Shows the
|
| 933 |
* block's family color + initials so the user sees what they're
|
| 934 |
* carrying. */
|
| 935 |
+
/** Soft banner above the canvas summarising pre-flight findings.
|
| 936 |
+
* Renders as a collapsed-by-default single line "N issues" chip;
|
| 937 |
+
* expands on click into a per-finding list. The Run button stays
|
| 938 |
+
* enabled (we don't block on warnings); errors get a danger-tone
|
| 939 |
+
* treatment but still don't disable Run since the user might want
|
| 940 |
+
* to try anyway. */
|
| 941 |
+
function PreflightBanner({ findings }: { findings: PreflightFinding[] }) {
|
| 942 |
+
const [open, setOpen] = useState(false);
|
| 943 |
+
const errors = findings.filter((f) => f.severity === "error");
|
| 944 |
+
const warns = findings.filter((f) => f.severity === "warn");
|
| 945 |
+
const dominantTone = errors.length > 0 ? "danger" : "warn";
|
| 946 |
+
const toneClass =
|
| 947 |
+
dominantTone === "danger"
|
| 948 |
+
? "border-danger/40 text-danger bg-danger/5"
|
| 949 |
+
: "border-warn/40 text-warn bg-warn/5";
|
| 950 |
+
const headline = errors.length
|
| 951 |
+
? `${errors.length} issue${errors.length === 1 ? "" : "s"} likely to break the run`
|
| 952 |
+
: `${warns.length} pipeline note${warns.length === 1 ? "" : "s"}`;
|
| 953 |
+
return (
|
| 954 |
+
<div className={`shrink-0 border-b ${toneClass}`}>
|
| 955 |
+
<button
|
| 956 |
+
type="button"
|
| 957 |
+
onClick={() => setOpen((v) => !v)}
|
| 958 |
+
aria-expanded={open}
|
| 959 |
+
className="w-full flex items-center gap-2 px-3 py-1.5 text-[11px]"
|
| 960 |
+
>
|
| 961 |
+
<AlertTriangle className="w-3.5 h-3.5" />
|
| 962 |
+
<span className="font-medium">{headline}</span>
|
| 963 |
+
<span className="text-mute/80">— click to {open ? "hide" : "see details"}</span>
|
| 964 |
+
</button>
|
| 965 |
+
{open && (
|
| 966 |
+
<ul className="px-3 pb-2 space-y-1 text-[11px]">
|
| 967 |
+
{findings.map((f, i) => (
|
| 968 |
+
<li key={i} className="flex items-start gap-1.5">
|
| 969 |
+
<span
|
| 970 |
+
className={`mt-0.5 inline-block w-1.5 h-1.5 rounded-full shrink-0 ${
|
| 971 |
+
f.severity === "error" ? "bg-danger" : "bg-warn"
|
| 972 |
+
}`}
|
| 973 |
+
aria-hidden
|
| 974 |
+
/>
|
| 975 |
+
<span>{f.message}</span>
|
| 976 |
+
</li>
|
| 977 |
+
))}
|
| 978 |
+
</ul>
|
| 979 |
+
)}
|
| 980 |
+
</div>
|
| 981 |
+
);
|
| 982 |
+
}
|
| 983 |
+
|
| 984 |
function TouchDragPreview({
|
| 985 |
kind,
|
| 986 |
x,
|
|
@@ -17,6 +17,7 @@ import { NodeParamEditor } from "./NodeParamEditor";
|
|
| 17 |
import { TipIcon } from "./TipIcon";
|
| 18 |
import { useApp } from "../lib/store";
|
| 19 |
import { FAMILY_HINTS } from "../lib/familyHints";
|
|
|
|
| 20 |
|
| 21 |
export interface QNodeData extends Record<string, unknown> {
|
| 22 |
kind: NodeKind;
|
|
@@ -46,6 +47,15 @@ export function QNode({ id, data, selected }: NodeProps) {
|
|
| 46 |
const circuit = useApp((s) => s.circuit);
|
| 47 |
const sampleKey = useApp((s) => s.sampleKey);
|
| 48 |
const plugins = useApp((s) => s.plugins);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
// Spec resolution — built-in OR user plugin. Hook order requires
|
| 50 |
// this stay above any early return.
|
| 51 |
const spec = resolveNodeSpec(d.kind, plugins);
|
|
@@ -273,6 +283,21 @@ export function QNode({ id, data, selected }: NodeProps) {
|
|
| 273 |
</div>
|
| 274 |
)
|
| 275 |
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
{hasInput && (
|
| 277 |
<Handle type="target" position={Position.Left} isConnectable={true} />
|
| 278 |
)}
|
|
@@ -283,6 +308,82 @@ export function QNode({ id, data, selected }: NodeProps) {
|
|
| 283 |
);
|
| 284 |
}
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
/** Build a one-line "param₁ · param₂ · …" summary for the collapsed
|
| 287 |
* state of the param editor. Selects use the raw value (short keys
|
| 288 |
* like "pittsburgh_1") rather than the long display label so the
|
|
|
|
| 17 |
import { TipIcon } from "./TipIcon";
|
| 18 |
import { useApp } from "../lib/store";
|
| 19 |
import { FAMILY_HINTS } from "../lib/familyHints";
|
| 20 |
+
import { durationTone, headlineFor } from "../lib/headlineMetric";
|
| 21 |
|
| 22 |
export interface QNodeData extends Record<string, unknown> {
|
| 23 |
kind: NodeKind;
|
|
|
|
| 47 |
const circuit = useApp((s) => s.circuit);
|
| 48 |
const sampleKey = useApp((s) => s.sampleKey);
|
| 49 |
const plugins = useApp((s) => s.plugins);
|
| 50 |
+
// After-run state: subscribe to the latest run so each node can
|
| 51 |
+
// surface its own headline metric + duration badge. Cheap because
|
| 52 |
+
// run only changes once per Run click.
|
| 53 |
+
const run = useApp((s) => s.run);
|
| 54 |
+
const step = run?.steps.find((s) => s.node_id === id);
|
| 55 |
+
const stepDurationS =
|
| 56 |
+
step && step.status === "ok"
|
| 57 |
+
? step.finished_at - step.started_at
|
| 58 |
+
: null;
|
| 59 |
// Spec resolution — built-in OR user plugin. Hook order requires
|
| 60 |
// this stay above any early return.
|
| 61 |
const spec = resolveNodeSpec(d.kind, plugins);
|
|
|
|
| 283 |
</div>
|
| 284 |
)
|
| 285 |
)}
|
| 286 |
+
{step && step.status === "ok" && (
|
| 287 |
+
<RunResultStrip
|
| 288 |
+
kind={d.kind}
|
| 289 |
+
step={step}
|
| 290 |
+
durationS={stepDurationS}
|
| 291 |
+
/>
|
| 292 |
+
)}
|
| 293 |
+
{step && step.status === "error" && (
|
| 294 |
+
<div className="mt-2 pt-2 border-t border-danger/30 text-[10px] text-danger flex items-start gap-1">
|
| 295 |
+
<AlertTriangle className="w-3 h-3 mt-0.5 shrink-0" />
|
| 296 |
+
<span className="truncate" title={step.message ?? ""}>
|
| 297 |
+
{step.message ?? "Failed"}
|
| 298 |
+
</span>
|
| 299 |
+
</div>
|
| 300 |
+
)}
|
| 301 |
{hasInput && (
|
| 302 |
<Handle type="target" position={Position.Left} isConnectable={true} />
|
| 303 |
)}
|
|
|
|
| 308 |
);
|
| 309 |
}
|
| 310 |
|
| 311 |
+
/** After-run strip at the bottom of the node tile: headline metric +
|
| 312 |
+
* duration chip + cache indicator. None of these are interactive,
|
| 313 |
+
* but they make the canvas itself a glanceable dashboard once a run
|
| 314 |
+
* has happened. */
|
| 315 |
+
function RunResultStrip({
|
| 316 |
+
kind,
|
| 317 |
+
step,
|
| 318 |
+
durationS,
|
| 319 |
+
}: {
|
| 320 |
+
kind: NodeKind;
|
| 321 |
+
step: import("../lib/api").StepResult;
|
| 322 |
+
durationS: number | null;
|
| 323 |
+
}) {
|
| 324 |
+
const headline = headlineFor(kind, step);
|
| 325 |
+
const tone = durationS !== null ? durationTone(durationS) : null;
|
| 326 |
+
const durBadgeClass = {
|
| 327 |
+
fast: "border-edge/60 text-mute/80",
|
| 328 |
+
medium: "border-accent/40 text-accent/90",
|
| 329 |
+
slow: "border-warn/50 text-warn",
|
| 330 |
+
"very-slow": "border-danger/50 text-danger",
|
| 331 |
+
}[tone ?? "fast"];
|
| 332 |
+
const durFormatted =
|
| 333 |
+
durationS === null
|
| 334 |
+
? ""
|
| 335 |
+
: durationS < 1
|
| 336 |
+
? `${Math.round(durationS * 1000)}ms`
|
| 337 |
+
: `${durationS.toFixed(durationS < 10 ? 2 : 1)}s`;
|
| 338 |
+
return (
|
| 339 |
+
<div className="mt-2 pt-1.5 border-t border-edge/40 flex items-baseline justify-between gap-2">
|
| 340 |
+
{headline ? (
|
| 341 |
+
<div className="min-w-0 flex-1">
|
| 342 |
+
<div className="text-[9px] uppercase tracking-wider text-mute/70 truncate">
|
| 343 |
+
{headline.label}
|
| 344 |
+
</div>
|
| 345 |
+
<div
|
| 346 |
+
className={`text-[12px] font-mono font-semibold leading-tight truncate ${
|
| 347 |
+
headline.tone ?? "text-ink"
|
| 348 |
+
}`}
|
| 349 |
+
title={`${headline.label}: ${headline.value}`}
|
| 350 |
+
>
|
| 351 |
+
{headline.value}
|
| 352 |
+
</div>
|
| 353 |
+
</div>
|
| 354 |
+
) : (
|
| 355 |
+
<div className="flex-1 text-[10px] text-ok/80">✓ ran</div>
|
| 356 |
+
)}
|
| 357 |
+
<div className="flex items-center gap-1 shrink-0">
|
| 358 |
+
{step.from_step_cache && (
|
| 359 |
+
<span
|
| 360 |
+
className="text-[9px] px-1 py-0.5 rounded border border-accent/30 text-accent"
|
| 361 |
+
title="Served from per-node cache (pipeline prefix unchanged)"
|
| 362 |
+
>
|
| 363 |
+
cached
|
| 364 |
+
</span>
|
| 365 |
+
)}
|
| 366 |
+
{durationS !== null && (
|
| 367 |
+
<span
|
| 368 |
+
className={`text-[9px] px-1 py-0.5 rounded border font-mono ${durBadgeClass}`}
|
| 369 |
+
title={
|
| 370 |
+
tone === "very-slow"
|
| 371 |
+
? "Took over 1 minute — likely the pipeline's bottleneck"
|
| 372 |
+
: tone === "slow"
|
| 373 |
+
? "Took over 10 seconds"
|
| 374 |
+
: tone === "medium"
|
| 375 |
+
? "Took 1-10 seconds"
|
| 376 |
+
: "Sub-second"
|
| 377 |
+
}
|
| 378 |
+
>
|
| 379 |
+
{durFormatted}
|
| 380 |
+
</span>
|
| 381 |
+
)}
|
| 382 |
+
</div>
|
| 383 |
+
</div>
|
| 384 |
+
);
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
/** Build a one-line "param₁ · param₂ · …" summary for the collapsed
|
| 388 |
* state of the param editor. Selects use the raw value (short keys
|
| 389 |
* like "pittsburgh_1") rather than the long display label so the
|
|
@@ -49,6 +49,17 @@ export interface CircuitInfo {
|
|
| 49 |
|
| 50 |
export type StepStatus = "ok" | "skipped" | "error";
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
export interface StepResult {
|
| 53 |
node_id: string;
|
| 54 |
node_type: string;
|
|
@@ -66,6 +77,8 @@ export interface StepResult {
|
|
| 66 |
* null/absent when the step is a built-in block or a plugin that
|
| 67 |
* doesn't emit figures. */
|
| 68 |
figures?: Array<Record<string, unknown>> | null;
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
export interface RunResponse {
|
|
|
|
| 49 |
|
| 50 |
export type StepStatus = "ok" | "skipped" | "error";
|
| 51 |
|
| 52 |
+
/** Post-step circuit shape, attached by the backend after each
|
| 53 |
+
* successful step. Drives the data-flow labels rendered on edges
|
| 54 |
+
* leaving this node. Null when the step ran without a circuit in
|
| 55 |
+
* context (e.g. a source plugin) or errored before computing one. */
|
| 56 |
+
export interface CircuitShape {
|
| 57 |
+
num_qubits: number;
|
| 58 |
+
depth: number;
|
| 59 |
+
size: number;
|
| 60 |
+
num_parameters: number;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
export interface StepResult {
|
| 64 |
node_id: string;
|
| 65 |
node_type: string;
|
|
|
|
| 77 |
* null/absent when the step is a built-in block or a plugin that
|
| 78 |
* doesn't emit figures. */
|
| 79 |
figures?: Array<Record<string, unknown>> | null;
|
| 80 |
+
/** Shape of the circuit AFTER this step ran. */
|
| 81 |
+
circuit_shape?: CircuitShape | null;
|
| 82 |
}
|
| 83 |
|
| 84 |
export interface RunResponse {
|
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Per-node-kind logic for "what's the one headline number to show on
|
| 2 |
+
// the canvas tile after a successful run?".
|
| 3 |
+
//
|
| 4 |
+
// The result card in the right pane shows the full summary table;
|
| 5 |
+
// this helper picks the single most representative scalar so the
|
| 6 |
+
// canvas itself becomes a glanceable dashboard. Returning null means
|
| 7 |
+
// "no headline — render nothing", which is the fallback for kinds
|
| 8 |
+
// whose summary doesn't expose a clean scalar.
|
| 9 |
+
//
|
| 10 |
+
// Kept as a per-kind table rather than baked into a server-side
|
| 11 |
+
// "primary_metric" field because the canvas display logic (number
|
| 12 |
+
// format, unit suffix, accent tone) is presentational and belongs
|
| 13 |
+
// near the rendering.
|
| 14 |
+
|
| 15 |
+
import type { StepResult } from "./api";
|
| 16 |
+
import type { NodeKind } from "./nodeCatalog";
|
| 17 |
+
|
| 18 |
+
export type Headline = {
|
| 19 |
+
/** Short label shown above the number. */
|
| 20 |
+
label: string;
|
| 21 |
+
/** Pre-formatted value string. */
|
| 22 |
+
value: string;
|
| 23 |
+
/** Optional accent class for the number (Tailwind, e.g. text-ok). */
|
| 24 |
+
tone?: string;
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
/** Format a number with up to 4 significant digits, dropping trailing
|
| 28 |
+
* zeros. NaN/null returns "—". */
|
| 29 |
+
function fmt(n: unknown, digits = 4): string {
|
| 30 |
+
if (typeof n !== "number" || !Number.isFinite(n)) return "—";
|
| 31 |
+
if (Number.isInteger(n)) return String(n);
|
| 32 |
+
return n
|
| 33 |
+
.toPrecision(digits)
|
| 34 |
+
.replace(/\.?0+(?=e|$)/, "")
|
| 35 |
+
.replace(/(\.\d*?)0+(?=$)/, "$1");
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
function pct(n: unknown): string {
|
| 39 |
+
if (typeof n !== "number" || !Number.isFinite(n)) return "—";
|
| 40 |
+
return `${(n * 100).toFixed(1)}%`;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function asNumber(v: unknown): number | null {
|
| 44 |
+
return typeof v === "number" && Number.isFinite(v) ? v : null;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
/** Per-kind dispatcher. Returns null when the kind has no
|
| 48 |
+
* meaningful headline (e.g. fake_backend just configures the next
|
| 49 |
+
* step), so the caller can skip rendering the strip entirely. */
|
| 50 |
+
export function headlineFor(
|
| 51 |
+
kind: NodeKind | string,
|
| 52 |
+
step: StepResult,
|
| 53 |
+
): Headline | null {
|
| 54 |
+
const s = step.summary;
|
| 55 |
+
switch (kind) {
|
| 56 |
+
case "input_circuit":
|
| 57 |
+
return null; // shape already shown via the circuit info block
|
| 58 |
+
case "fake_backend":
|
| 59 |
+
case "ibm_backend":
|
| 60 |
+
return null; // no scalar output to surface
|
| 61 |
+
case "qucad": {
|
| 62 |
+
// The QuCAD card surfaces "sparsity" / "params_pruned".
|
| 63 |
+
const sp = asNumber(s.sparsity ?? s.pruned_fraction);
|
| 64 |
+
if (sp !== null) return { label: "Pruned", value: pct(sp), tone: "text-ok" };
|
| 65 |
+
const kept = asNumber(s.params_kept);
|
| 66 |
+
const total = asNumber(s.params_total);
|
| 67 |
+
if (kept !== null && total !== null && total > 0) {
|
| 68 |
+
return {
|
| 69 |
+
label: "Kept",
|
| 70 |
+
value: `${kept}/${total}`,
|
| 71 |
+
tone: "text-ink",
|
| 72 |
+
};
|
| 73 |
+
}
|
| 74 |
+
return null;
|
| 75 |
+
}
|
| 76 |
+
case "qubound": {
|
| 77 |
+
const eb = asNumber(s.predicted_error_bound ?? s.qubound_value);
|
| 78 |
+
if (eb !== null) {
|
| 79 |
+
return { label: "Error bound", value: `≤ ${fmt(eb)}`, tone: "text-accent" };
|
| 80 |
+
}
|
| 81 |
+
return null;
|
| 82 |
+
}
|
| 83 |
+
case "compvqc": {
|
| 84 |
+
const folded = asNumber(s.rotations_folded ?? s.compressed);
|
| 85 |
+
const original = asNumber(s.rotations_original);
|
| 86 |
+
if (folded !== null && original !== null && original > 0) {
|
| 87 |
+
return {
|
| 88 |
+
label: "Folded",
|
| 89 |
+
value: `${original}→${original - folded}`,
|
| 90 |
+
tone: "text-ok",
|
| 91 |
+
};
|
| 92 |
+
}
|
| 93 |
+
return null;
|
| 94 |
+
}
|
| 95 |
+
case "qshot": {
|
| 96 |
+
const shots = asNumber(s.recommended_shots ?? s.shots);
|
| 97 |
+
if (shots !== null) {
|
| 98 |
+
return { label: "Shots", value: fmt(shots), tone: "text-accent" };
|
| 99 |
+
}
|
| 100 |
+
return null;
|
| 101 |
+
}
|
| 102 |
+
case "fidelity": {
|
| 103 |
+
const f = asNumber(s.fidelity);
|
| 104 |
+
if (f !== null) {
|
| 105 |
+
return {
|
| 106 |
+
label: "Fidelity",
|
| 107 |
+
value: fmt(f),
|
| 108 |
+
tone: f >= 0.9 ? "text-ok" : f >= 0.7 ? "text-warn" : "text-danger",
|
| 109 |
+
};
|
| 110 |
+
}
|
| 111 |
+
return null;
|
| 112 |
+
}
|
| 113 |
+
case "output": {
|
| 114 |
+
const f = asNumber(s.fidelity);
|
| 115 |
+
if (f !== null) {
|
| 116 |
+
return { label: "Final fidelity", value: fmt(f), tone: "text-ok" };
|
| 117 |
+
}
|
| 118 |
+
return null;
|
| 119 |
+
}
|
| 120 |
+
default: {
|
| 121 |
+
// User plugin: prefer "scalars" they wrote, then look for any
|
| 122 |
+
// single numeric in summary.
|
| 123 |
+
const summaryDict = s as Record<string, unknown>;
|
| 124 |
+
for (const k of ["fidelity", "score", "metric", "value"]) {
|
| 125 |
+
const v = asNumber(summaryDict[k]);
|
| 126 |
+
if (v !== null) return { label: k, value: fmt(v), tone: "text-accent" };
|
| 127 |
+
}
|
| 128 |
+
// First scalar in summary.
|
| 129 |
+
for (const [k, v] of Object.entries(summaryDict)) {
|
| 130 |
+
const n = asNumber(v);
|
| 131 |
+
if (n !== null) return { label: k, value: fmt(n), tone: "text-mute" };
|
| 132 |
+
}
|
| 133 |
+
return null;
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
/** Classify run duration into a tint tone for the node card border /
|
| 139 |
+
* background. The thresholds are picked from typical run profiles:
|
| 140 |
+
* built-in QuCAD / CompressVQC are sub-second; QuBound LSTM training
|
| 141 |
+
* and Qshot HDBSCAN warmup land in the 30s-3min range; anything past
|
| 142 |
+
* that is a stand-out outlier worth flagging. */
|
| 143 |
+
export function durationTone(duration_s: number):
|
| 144 |
+
| "fast"
|
| 145 |
+
| "medium"
|
| 146 |
+
| "slow"
|
| 147 |
+
| "very-slow" {
|
| 148 |
+
if (duration_s < 1) return "fast";
|
| 149 |
+
if (duration_s < 10) return "medium";
|
| 150 |
+
if (duration_s < 60) return "slow";
|
| 151 |
+
return "very-slow";
|
| 152 |
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Pre-flight lint checks for the user's pipeline graph.
|
| 2 |
+
//
|
| 3 |
+
// The "Run pipeline" button reads these and surfaces a soft banner
|
| 4 |
+
// above the canvas warning about common mistakes BEFORE the user
|
| 5 |
+
// hits Run — instead of the run itself failing 30 seconds in.
|
| 6 |
+
//
|
| 7 |
+
// Two severity tiers:
|
| 8 |
+
// * `warn` — the run will probably work but the user is missing
|
| 9 |
+
// something or getting a degraded path. (e.g. QuCAD will fall
|
| 10 |
+
// back to a default backend).
|
| 11 |
+
// * `error` — the run definitely won't produce useful output.
|
| 12 |
+
// (e.g. dangling Output node, no Input upstream of an Algorithm.)
|
| 13 |
+
//
|
| 14 |
+
// Each finding can name a `node_id` so the canvas can highlight the
|
| 15 |
+
// offending node when the banner is hovered or clicked.
|
| 16 |
+
|
| 17 |
+
import type { Edge, Node } from "@xyflow/react";
|
| 18 |
+
import type { CircuitInfo } from "./api";
|
| 19 |
+
import { resolveNodeSpec, type NodeKind, type NodeSpec, type PluginNodeSpec } from "./nodeCatalog";
|
| 20 |
+
import type { PluginManifest } from "./api";
|
| 21 |
+
|
| 22 |
+
export type PreflightSeverity = "warn" | "error";
|
| 23 |
+
|
| 24 |
+
export type PreflightFinding = {
|
| 25 |
+
severity: PreflightSeverity;
|
| 26 |
+
/** Concise headline shown in the banner. */
|
| 27 |
+
message: string;
|
| 28 |
+
/** Optional canonical node this finding is about. */
|
| 29 |
+
node_id?: string;
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
export type GraphNode = Node<{ kind: NodeKind | string }>;
|
| 33 |
+
|
| 34 |
+
/** Build a lookup of each node's incoming edges. */
|
| 35 |
+
function incomingMap(edges: Edge[]): Map<string, string[]> {
|
| 36 |
+
const m = new Map<string, string[]>();
|
| 37 |
+
for (const e of edges) {
|
| 38 |
+
const list = m.get(e.target) ?? [];
|
| 39 |
+
list.push(e.source);
|
| 40 |
+
m.set(e.target, list);
|
| 41 |
+
}
|
| 42 |
+
return m;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
/** Build a lookup of each node's outgoing edges. */
|
| 46 |
+
function outgoingMap(edges: Edge[]): Map<string, string[]> {
|
| 47 |
+
const m = new Map<string, string[]>();
|
| 48 |
+
for (const e of edges) {
|
| 49 |
+
const list = m.get(e.source) ?? [];
|
| 50 |
+
list.push(e.target);
|
| 51 |
+
m.set(e.source, list);
|
| 52 |
+
}
|
| 53 |
+
return m;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/** Walk upstream from `start` (inclusive) collecting nodes that match
|
| 57 |
+
* `predicate`. Used to answer "is there a backend somewhere upstream
|
| 58 |
+
* of this algorithm node?". Detects cycles defensively. */
|
| 59 |
+
function findUpstream(
|
| 60 |
+
start: string,
|
| 61 |
+
incoming: Map<string, string[]>,
|
| 62 |
+
predicate: (id: string) => boolean,
|
| 63 |
+
): string | null {
|
| 64 |
+
const seen = new Set<string>();
|
| 65 |
+
const stack = [start];
|
| 66 |
+
while (stack.length) {
|
| 67 |
+
const id = stack.pop()!;
|
| 68 |
+
if (seen.has(id)) continue;
|
| 69 |
+
seen.add(id);
|
| 70 |
+
if (id !== start && predicate(id)) return id;
|
| 71 |
+
for (const parent of incoming.get(id) ?? []) stack.push(parent);
|
| 72 |
+
}
|
| 73 |
+
return null;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
export type PreflightInput = {
|
| 77 |
+
nodes: GraphNode[];
|
| 78 |
+
edges: Edge[];
|
| 79 |
+
circuit: CircuitInfo | null;
|
| 80 |
+
plugins: PluginManifest[];
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
export function runPreflight(input: PreflightInput): PreflightFinding[] {
|
| 84 |
+
const { nodes, edges, circuit, plugins } = input;
|
| 85 |
+
const findings: PreflightFinding[] = [];
|
| 86 |
+
const incoming = incomingMap(edges);
|
| 87 |
+
const outgoing = outgoingMap(edges);
|
| 88 |
+
|
| 89 |
+
// Per-node spec lookup. Unknown plugin kinds skip family-based
|
| 90 |
+
// rules so we don't yell about user plugins we can't classify.
|
| 91 |
+
const specOf = (kind: string): NodeSpec | PluginNodeSpec | undefined =>
|
| 92 |
+
resolveNodeSpec(kind, plugins);
|
| 93 |
+
const familyOf = (id: string): NodeSpec["family"] | null => {
|
| 94 |
+
const n = nodes.find((x) => x.id === id);
|
| 95 |
+
if (!n) return null;
|
| 96 |
+
const s = specOf(n.data.kind);
|
| 97 |
+
return s?.family ?? null;
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
// ---- Per-node checks ----
|
| 101 |
+
|
| 102 |
+
let hasInput = false;
|
| 103 |
+
let hasOutput = false;
|
| 104 |
+
for (const n of nodes) {
|
| 105 |
+
const kind = n.data.kind;
|
| 106 |
+
const spec = specOf(kind);
|
| 107 |
+
if (!spec) continue;
|
| 108 |
+
|
| 109 |
+
if (spec.family === "source") hasInput = true;
|
| 110 |
+
if (spec.family === "sink") hasOutput = true;
|
| 111 |
+
|
| 112 |
+
// 1. Source node with upstream edges: someone hooked something
|
| 113 |
+
// INTO an Input block — that node's output gets discarded.
|
| 114 |
+
if (spec.family === "source" && (incoming.get(n.id)?.length ?? 0) > 0) {
|
| 115 |
+
findings.push({
|
| 116 |
+
severity: "warn",
|
| 117 |
+
message: `${spec.label} has an upstream connection — its input will be ignored.`,
|
| 118 |
+
node_id: n.id,
|
| 119 |
+
});
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// 2. Sink node with downstream edges: dangling output past the
|
| 123 |
+
// sink. The runner just stops at the sink; let the user know.
|
| 124 |
+
if (spec.family === "sink" && (outgoing.get(n.id)?.length ?? 0) > 0) {
|
| 125 |
+
findings.push({
|
| 126 |
+
severity: "warn",
|
| 127 |
+
message: `${spec.label} has a downstream connection — nothing runs after a sink.`,
|
| 128 |
+
node_id: n.id,
|
| 129 |
+
});
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
// 3. QuCAD needs a backend upstream (it uses a noise model).
|
| 133 |
+
// Without one, the runner falls back to a default Fake — fine,
|
| 134 |
+
// but worth flagging.
|
| 135 |
+
if (kind === "qucad") {
|
| 136 |
+
const backend = findUpstream(n.id, incoming, (pid) => familyOf(pid) === "backend");
|
| 137 |
+
if (!backend) {
|
| 138 |
+
findings.push({
|
| 139 |
+
severity: "warn",
|
| 140 |
+
message: "QuCAD has no backend upstream; it will use the default FakeFez.",
|
| 141 |
+
node_id: n.id,
|
| 142 |
+
});
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// 4. Algorithm + Metric + Sink nodes need SOMETHING upstream
|
| 147 |
+
// that produces a circuit. We approximate this as "upstream
|
| 148 |
+
// Input block exists".
|
| 149 |
+
if (
|
| 150 |
+
spec.family === "algorithm" ||
|
| 151 |
+
spec.family === "metric" ||
|
| 152 |
+
spec.family === "sink"
|
| 153 |
+
) {
|
| 154 |
+
const upstreamSource = findUpstream(
|
| 155 |
+
n.id,
|
| 156 |
+
incoming,
|
| 157 |
+
(pid) => familyOf(pid) === "source",
|
| 158 |
+
);
|
| 159 |
+
if (!upstreamSource) {
|
| 160 |
+
findings.push({
|
| 161 |
+
severity: "error",
|
| 162 |
+
message: `${spec.label} has no Input block upstream — it has no circuit to work on.`,
|
| 163 |
+
node_id: n.id,
|
| 164 |
+
});
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
// ---- Graph-level checks ----
|
| 170 |
+
|
| 171 |
+
// 5. No Input at all → nothing to run.
|
| 172 |
+
if (!hasInput && nodes.length > 0) {
|
| 173 |
+
findings.push({
|
| 174 |
+
severity: "error",
|
| 175 |
+
message: "Pipeline has no Input block. Add an `Input circuit` to feed the chain.",
|
| 176 |
+
});
|
| 177 |
+
}
|
| 178 |
+
// 6. No Output → results aren't aggregated.
|
| 179 |
+
if (!hasOutput && nodes.length > 0) {
|
| 180 |
+
findings.push({
|
| 181 |
+
severity: "warn",
|
| 182 |
+
message: "Pipeline has no Output block. Add one to see final metrics.",
|
| 183 |
+
});
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// 7. QuBound out-of-range warning (LSTM trained on 5-8 qubits).
|
| 187 |
+
if (circuit) {
|
| 188 |
+
const usesQubound = nodes.some((n) => n.data.kind === "qubound");
|
| 189 |
+
if (
|
| 190 |
+
usesQubound &&
|
| 191 |
+
(circuit.num_qubits < 5 || circuit.num_qubits > 8)
|
| 192 |
+
) {
|
| 193 |
+
findings.push({
|
| 194 |
+
severity: "warn",
|
| 195 |
+
message: `QuBound was trained on 5-8 qubit circuits; yours has ${circuit.num_qubits}. The result may be less accurate.`,
|
| 196 |
+
});
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// 8. Qshot fallback warning.
|
| 200 |
+
const usesQshot = nodes.some((n) => n.data.kind === "qshot");
|
| 201 |
+
if (
|
| 202 |
+
usesQshot &&
|
| 203 |
+
(circuit.num_qubits < 5 || circuit.num_qubits > 8)
|
| 204 |
+
) {
|
| 205 |
+
findings.push({
|
| 206 |
+
severity: "warn",
|
| 207 |
+
message: `Qshot's reference database is 5-8 qubits; yours has ${circuit.num_qubits}. It will fall back to a slower GNN.`,
|
| 208 |
+
});
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
return findings;
|
| 213 |
+
}
|