File size: 3,904 Bytes
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79cd0aa
 
 
 
 
 
 
 
 
 
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79cd0aa
 
 
 
 
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Gate-level circuit diff — per-qubit lanes, LCS-merged token order.
// Lives inside SignatureCard, collapsed behind a <details>: the
// channel strip answers "how much changed", this view answers
// "WHICH gates, on WHICH lanes" — the QuCAD-pruned / CompVQC-folded
// evidence a reader wants before believing a signature.
//
// Encoding (consistent with the delta strip's goal-anchored colors):
//   kept    — neutral dimmed chip (context, not the story)
//   removed — ok/green + strikethrough (pruning is the optimizer's
//             goal; decrease=ok everywhere in this vocabulary)
//   added   — warn/amber (growth is the thing to scrutinize)
// 2-qubit partners carry their role suffix (·c control / ·t target)
// so a cx shows up on BOTH lanes and reads correctly on each.
// Payload is capped server-side (12 qubits / 600 ops) — over the cap
// we show a one-line pointer back to the channel strip.

import type { TransformationPayload } from "./TransformationSignature";

interface GateDiffEntry {
  op: string;
  s: "kept" | "removed" | "added";
}

export interface GateDiffPayload {
  truncated: boolean;
  reason?: string;
  qubits?: Record<string, GateDiffEntry[]>;
  n_kept?: number;
  n_removed?: number;
  n_added?: number;
}

export function gateDiffOf(
  t: TransformationPayload | null | undefined,
): GateDiffPayload | null {
  const g = (t as unknown as Record<string, unknown> | null | undefined)
    ?.gate_diff;
  if (!g || typeof g !== "object") return null;
  return g as GateDiffPayload;
}

const CHIP: Record<GateDiffEntry["s"], { cls: string; title: string }> = {
  kept: {
    cls: "bg-surfaceAlt border-edge text-mute opacity-70",
    title: "unchanged by this step",
  },
  removed: {
    cls: "bg-ok/10 border-ok/40 text-ok line-through",
    title: "removed by this step",
  },
  added: {
    cls: "bg-warn/10 border-warn/40 text-warn",
    title: "added by this step",
  },
};

export default function CircuitDiff({
  t,
  defaultOpen = false,
}: {
  t: TransformationPayload;
  /** Scenario F5 boots the diff expanded (initial state only — the
   *  user can still toggle it; React never re-asserts the attribute
   *  because the vdom value doesn't change). */
  defaultOpen?: boolean;
}) {
  const g = gateDiffOf(t);
  if (!g) return null;
  if (g.truncated) {
    return (
      <div className="gate-diff text-[10px] text-mute" aria-label="gate-diff">
        circuit too large for gate-level diff (shown up to 12 qubits /
        600 gates) — see the channel strip above
      </div>
    );
  }
  const lanes = Object.entries(g.qubits ?? {}).sort(
    (a, b) => Number(a[0]) - Number(b[0]),
  );
  if (lanes.length === 0) return null;
  const nRemoved = g.n_removed ?? 0;
  const nAdded = g.n_added ?? 0;
  return (
    <details
      className="gate-diff group"
      aria-label="gate-diff"
      open={defaultOpen || undefined}
    >
      <summary className="cursor-pointer select-none text-[10px] text-mute hover:text-ink transition-colors">
        Gate-level diff · {nRemoved} removed · {nAdded} added
      </summary>
      <div className="mt-1.5 space-y-1">
        {lanes.map(([qi, ops]) => (
          <div key={qi} className="flex items-center gap-1.5 min-w-0">
            <span className="w-6 shrink-0 text-right font-mono text-[9px] text-mute">
              q{qi}
            </span>
            <div className="flex items-center gap-1 overflow-x-auto min-w-0 pb-0.5">
              {ops.map((e, i) => (
                <span
                  key={`${qi}-${i}`}
                  title={`${e.op} — ${CHIP[e.s].title}`}
                  className={`shrink-0 rounded border px-1 py-px font-mono text-[9px] leading-none whitespace-nowrap ${CHIP[e.s].cls}`}
                >
                  {e.op}
                </span>
              ))}
            </div>
          </div>
        ))}
      </div>
    </details>
  );
}