File size: 10,470 Bytes
33e7bee
 
 
 
 
 
 
 
 
 
 
 
 
728039f
 
 
33e7bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adb5d6a
 
 
 
 
 
 
33e7bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10b136b
 
 
 
 
33e7bee
 
 
 
10b136b
 
33e7bee
 
 
 
 
adb5d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e7bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728039f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33e7bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Pre-flight lint checks for the user's pipeline graph.
//
// The "Run pipeline" button reads these and surfaces a soft banner
// above the canvas warning about common mistakes BEFORE the user
// hits Run — instead of the run itself failing 30 seconds in.
//
// Two severity tiers:
//   * `warn` — the run will probably work but the user is missing
//     something or getting a degraded path. (e.g. QuCAD will fall
//     back to a default backend).
//   * `error` — the run definitely won't produce useful output.
//     (e.g. dangling Output node, no Input upstream of an Algorithm.)
//
// Each finding can name a `node_id` identifying the offending node.
// (Carried for a future highlight affordance — the banner currently
// renders messages only; nothing consumes node_id yet.)

import type { Edge, Node } from "@xyflow/react";
import type { CircuitInfo } from "./api";
import { resolveNodeSpec, type NodeKind, type NodeSpec, type PluginNodeSpec } from "./nodeCatalog";
import type { PluginManifest } from "./api";

export type PreflightSeverity = "warn" | "error";

export type PreflightFinding = {
  severity: PreflightSeverity;
  /** Concise headline shown in the banner. */
  message: string;
  /** Optional canonical node this finding is about. */
  node_id?: string;
};

export type GraphNode = Node<{
  kind: NodeKind | string;
  /** Param values flat from the canvas node (the same shape stored
   *  under data.params on a React Flow node). Used by rules that
   *  branch on user choices (e.g. Fidelity method = sampled). */
  params?: Record<string, unknown>;
}>;

/** Build a lookup of each node's incoming edges. */
function incomingMap(edges: Edge[]): Map<string, string[]> {
  const m = new Map<string, string[]>();
  for (const e of edges) {
    const list = m.get(e.target) ?? [];
    list.push(e.source);
    m.set(e.target, list);
  }
  return m;
}

/** Build a lookup of each node's outgoing edges. */
function outgoingMap(edges: Edge[]): Map<string, string[]> {
  const m = new Map<string, string[]>();
  for (const e of edges) {
    const list = m.get(e.source) ?? [];
    list.push(e.target);
    m.set(e.source, list);
  }
  return m;
}

/** Walk upstream from `start` (inclusive) collecting nodes that match
 *  `predicate`. Used to answer "is there a backend somewhere upstream
 *  of this algorithm node?". Detects cycles defensively. */
function findUpstream(
  start: string,
  incoming: Map<string, string[]>,
  predicate: (id: string) => boolean,
): string | null {
  const seen = new Set<string>();
  const stack = [start];
  while (stack.length) {
    const id = stack.pop()!;
    if (seen.has(id)) continue;
    seen.add(id);
    if (id !== start && predicate(id)) return id;
    for (const parent of incoming.get(id) ?? []) stack.push(parent);
  }
  return null;
}

export type PreflightInput = {
  nodes: GraphNode[];
  edges: Edge[];
  circuit: CircuitInfo | null;
  plugins: PluginManifest[];
};

export function runPreflight(input: PreflightInput): PreflightFinding[] {
  const { nodes, edges, circuit, plugins } = input;
  const findings: PreflightFinding[] = [];
  const incoming = incomingMap(edges);
  const outgoing = outgoingMap(edges);

  // Per-node spec lookup. Unknown plugin kinds skip family-based
  // rules so we don't yell about user plugins we can't classify.
  const specOf = (kind: string): NodeSpec | PluginNodeSpec | undefined =>
    resolveNodeSpec(kind, plugins);
  const familyOf = (id: string): NodeSpec["family"] | null => {
    const n = nodes.find((x) => x.id === id);
    if (!n) return null;
    const s = specOf(n.data.kind);
    return s?.family ?? null;
  };

  // ---- Per-node checks ----

  let hasInput = false;
  let hasOutput = false;
  for (const n of nodes) {
    const kind = n.data.kind;
    const spec = specOf(kind);
    if (!spec) continue;

    if (spec.family === "source") hasInput = true;
    if (spec.family === "sink") hasOutput = true;

    // 1. Source node with upstream edges: someone hooked something
    //    INTO an Input block — that node's output gets discarded.
    if (spec.family === "source" && (incoming.get(n.id)?.length ?? 0) > 0) {
      findings.push({
        severity: "warn",
        message: `${spec.label} has an upstream connection — its input will be ignored.`,
        node_id: n.id,
      });
    }

    // 2. Sink node with downstream edges: dangling output past the
    //    sink. The runner just stops at the sink; let the user know.
    if (spec.family === "sink" && (outgoing.get(n.id)?.length ?? 0) > 0) {
      findings.push({
        severity: "warn",
        message: `${spec.label} has a downstream connection — nothing runs after a sink.`,
        node_id: n.id,
      });
    }

    // 3. QuCAD REQUIRES a backend upstream: the runtime handler
    //    (_handle_qucad) hard-errors without ctx["backend"] — the
    //    FakeFez soft-fallback exists only for QuBound/CompressVQC.
    //    Same truth autoConnect's warning tells ("requires a backend
    //    node upstream and will error at run time").
    if (kind === "qucad") {
      const backend = findUpstream(n.id, incoming, (pid) => familyOf(pid) === "backend");
      if (!backend) {
        findings.push({
          severity: "error",
          message: "QuCAD needs a backend node upstream — it will error at run time.",
          node_id: n.id,
        });
      }
    }

    // 3b. Fidelity in `sampled` mode needs a backend upstream because
    //     that's where the noise model + shots come from. Without one
    //     the sampled estimator falls back to noiseless AerSimulator
    //     and the result is effectively the same as `statevector` mode
    //     but slower — which defeats the user's choice.
    if (kind === "fidelity") {
      const method = n.data.params?.method;
      if (method === "sampled") {
        const backend = findUpstream(
          n.id, incoming, (pid) => familyOf(pid) === "backend",
        );
        if (!backend) {
          findings.push({
            severity: "warn",
            message:
              "Fidelity is in `sampled` mode but has no Backend block " +
              "upstream. It will fall back to a noiseless simulator — " +
              "switch to `statevector` mode or add a backend to take " +
              "advantage of sampled mode.",
            node_id: n.id,
          });
        }
      }
    }

    // 4. Algorithm + Metric + Sink nodes need SOMETHING upstream
    //    that produces a circuit. We approximate this as "upstream
    //    Input block exists".
    if (
      spec.family === "algorithm" ||
      spec.family === "metric" ||
      spec.family === "sink"
    ) {
      const upstreamSource = findUpstream(
        n.id,
        incoming,
        (pid) => familyOf(pid) === "source",
      );
      if (!upstreamSource) {
        findings.push({
          severity: "error",
          message: `${spec.label} has no Input block upstream — it has no circuit to work on.`,
          node_id: n.id,
        });
      }
    }
  }

  // ---- Graph-level checks ----

  // 5. No Input at all → nothing to run.
  if (!hasInput && nodes.length > 0) {
    findings.push({
      severity: "error",
      message: "Pipeline has no Input block. Add an `Input circuit` to feed the chain.",
    });
  }
  // 6. No Output → results aren't aggregated.
  if (!hasOutput && nodes.length > 0) {
    findings.push({
      severity: "warn",
      message: "Pipeline has no Output block. Add one to see final metrics.",
    });
  }

  // 6b. Cycle: the backend rejects cyclic graphs outright
  //     (topological_order raises ValueError before any step runs) —
  //     surface that before the round-trip. Kahn's algorithm, same as
  //     the server. Edges to/from unknown ids are ignored, matching
  //     the defensive findUpstream walk above.
  if (nodes.length > 0 && edges.length > 0) {
    const indeg = new Map<string, number>(nodes.map((n) => [n.id, 0]));
    for (const e of edges) {
      if (indeg.has(e.target) && indeg.has(e.source))
        indeg.set(e.target, (indeg.get(e.target) ?? 0) + 1);
    }
    const queue = [...indeg.entries()]
      .filter(([, d]) => d === 0)
      .map(([id]) => id);
    let visited = 0;
    while (queue.length) {
      const id = queue.shift()!;
      visited += 1;
      for (const child of outgoing.get(id) ?? []) {
        if (!indeg.has(child)) continue;
        const d = (indeg.get(child) ?? 1) - 1;
        indeg.set(child, d);
        if (d === 0) queue.push(child);
      }
    }
    if (visited < indeg.size) {
      findings.push({
        severity: "error",
        message:
          "Pipeline contains a cycle — the runner rejects cyclic graphs before executing anything.",
      });
    }
  }

  // 6c. Duplicate sources / backends (mirrors autoConnect's advisory
  //     facts about the runner): extra Inputs only add duplicate
  //     summary rows; every backend handler overwrites ctx["backend"],
  //     so with several backends the last one to run silently wins.
  const sourceCount = nodes.filter(
    (n) => specOf(n.data.kind)?.family === "source",
  ).length;
  if (sourceCount > 1) {
    findings.push({
      severity: "warn",
      message: `${sourceCount} Input blocks — extras produce duplicate summary rows.`,
    });
  }
  const backendCount = nodes.filter(
    (n) => specOf(n.data.kind)?.family === "backend",
  ).length;
  if (backendCount > 1) {
    findings.push({
      severity: "warn",
      message: `${backendCount} backend blocks — each overwrites the shared noise profile; the last one to run wins.`,
    });
  }

  // 7. QuBound out-of-range warning (LSTM trained on 5-8 qubits).
  if (circuit) {
    const usesQubound = nodes.some((n) => n.data.kind === "qubound");
    if (
      usesQubound &&
      (circuit.num_qubits < 5 || circuit.num_qubits > 8)
    ) {
      findings.push({
        severity: "warn",
        message: `QuBound was trained on 5-8 qubit circuits; yours has ${circuit.num_qubits}. The result may be less accurate.`,
      });
    }

    // 8. Qshot fallback warning.
    const usesQshot = nodes.some((n) => n.data.kind === "qshot");
    if (
      usesQshot &&
      (circuit.num_qubits < 5 || circuit.num_qubits > 8)
    ) {
      findings.push({
        severity: "warn",
        message: `Qshot's reference database is 5-8 qubits; yours has ${circuit.num_qubits}. It will fall back to a slower GNN.`,
      });
    }
  }

  return findings;
}