File size: 4,833 Bytes
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efd5605
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64aa288
 
 
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79cd0aa
 
 
 
 
 
 
 
 
 
94e47b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79cd0aa
 
 
 
 
94e47b0
 
 
efd5605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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 { Fragment } from "react";
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;
}

// Module-local since audit S3 (the export was dead — the card is the
// only consumer).
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>
      {/* ONE shared horizontal scroll container (audit backlog): the
          old per-lane overflow-x-auto gave every lane its own
          scrollbar, so lanes desynced the moment any one of them was
          scrolled — column k on q0 stopped aligning with column k on
          q1, which is exactly the correspondence a 2-qubit gate's
          ·c/·t chips rely on. A single scrolling grid moves all lanes
          in lockstep; the qN label column is sticky-left with an
          opaque panel-alt background (the SignatureCard host surface)
          so labels stay readable mid-scroll while chips pass under. */}
      <div className="mt-1.5 overflow-x-auto pb-0.5">
        <div
          className="grid w-max min-w-full items-center gap-y-1"
          style={{ gridTemplateColumns: "max-content 1fr" }}
        >
          {lanes.map(([qi, ops]) => (
            <Fragment key={qi}>
              <span className="sticky left-0 z-10 bg-surfaceAlt min-w-[24px] pr-1.5 text-right font-mono text-[9px] text-mute">
                q{qi}
              </span>
              <div className="flex items-center gap-1">
                {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>
            </Fragment>
          ))}
        </div>
      </div>
    </details>
  );
}