import { useMemo, useState } from "react"; import { X as XIcon } from "lucide-react"; import { gloss } from "../../lib/glossary"; import { applyH1, applyX1, applyZ1, measureMany, mulberry32, probs, zero1, type State1, } from "../../lib/quantumToy"; import { TipIcon } from "../TipIcon"; import { CircleState } from "./CircleState"; import { Tally } from "./Tally"; /** * Step 2 — "Gates steer the lean." One horizontal wire, a shelf with * X, H and Z chips, up to four removable slots. The state runs * left→right through the slots on every edit (exact sim — * lib/quantumToy.ts), the CircleState at the wire's end shows the * resulting state live (the full circle, because Z can push the * needle to the left half — the semicircular dial could not show * that), and the step-1 histogram under it re-tallies a persistent * seeded ×200 auto-measure of the current end state. * * The dynamic caption's boldest claim — H twice is EXACTLY back where * you started — only holds because the toy sim carries real * amplitudes with signs (interference), not probabilities. * * REACHABLE-SET TRUTH (asserted in scripts/check_quantum_toy.test.ts): * from |0⟩, words in X/H/Z generate a dihedral orbit of 8 SIGNED * amplitude pairs at 45° multiples of the amplitude circle — but a * global sign is unphysical, so the pairs collapse two-by-two into * exactly FOUR physical states: |0⟩, |+⟩, |1⟩, |−⟩, the circle's * compass points. Leans stay {0%, 50%, 100%}; what Z adds is not a * new lean but the left half of the circle (|−⟩), invisible to the * tally, visible to H. The caption's case analysis below is * exhaustive over those four states, not heuristic. */ type GateKind = "X" | "H" | "Z"; const MAX_SLOTS = 4; const SHOTS = 200; const TALLY_SEED = 42; // fixed → the histogram is a pure function of the circuit const near = (a: number, b: number) => Math.abs(a - b) < 1e-9; function describe(gates: GateKind[], states: State1[]): string { if (gates.length === 0) return "An empty wire changes nothing: the qubit stays at 0, with certainty."; const prev = probs(states[states.length - 2])[1]; const now = probs(states[states.length - 1])[1]; const last = gates[gates.length - 1]; if (last === "X") return near(now, 0.5) ? // X genuinely fixes BOTH fifty-fifty points here: X|+⟩ = |+⟩ and // X|−⟩ = −|−⟩, and an overall sign is not a different state. "X swaps 0-ness and 1-ness — but a fifty-fifty state is its own swap, so the needle stays put." : `X flips: the qubit now reads ${now > 0.5 ? "1" : "0"}, with certainty.`; if (last === "Z") return near(now, 0.5) ? "Z flips a hidden sign: the needle swings across the circle, yet the tally will not budge." : "On a definite 0 or 1, Z changes nothing at all — its sign flip only matters mid-lean."; // last === "H"; a lean of exactly 0 or 1 before it means a fresh split if (near(prev, 0) || near(prev, 1)) return "H splits a definite value into a fifty-fifty lean — the needle moves to the side of the circle."; const before = gates[gates.length - 2]; if (before === "H") return "H twice: exactly back where it started — thanks to a hidden direction you'll meet in a moment."; if (before === "Z") return `H turns the hidden sign into something visible: a definite ${now > 0.5 ? "1" : "0"}, with certainty.`; return `The two mixes cancelled — a definite ${now > 0.5 ? "1" : "0"} again. Nothing here was random.`; } export function Step2Gates() { const [gates, setGates] = useState(["H"]); // preloaded example const states = useMemo(() => { const acc: State1[] = [zero1()]; for (const g of gates) { const cur = acc[acc.length - 1]; acc.push(g === "X" ? applyX1(cur) : g === "H" ? applyH1(cur) : applyZ1(cur)); } return acc; }, [gates]); const end = states[states.length - 1]; const tallies = useMemo( () => ({ "0": 0, "1": 0, ...measureMany(end, SHOTS, mulberry32(TALLY_SEED)) }), [end], ); const add = (g: GateKind) => setGates((gs) => (gs.length >= MAX_SLOTS ? gs : [...gs, g])); const removeAt = (i: number) => setGates((gs) => gs.filter((_, j) => j !== i)); return (
{/* gate shelf */}
gate shelf {gates.length >= MAX_SLOTS && ( wire full — remove one to add )}
{/* the wire: start chip → 4 slots → end dial */}
0 {Array.from({ length: MAX_SLOTS }).map((_, i) => gates[i] != null ? ( {gates[i]} ) : ( ), )}
from 0, every X/H/Z recipe lands on one of these four compass points — never in between
{/* dynamic caption — what the LAST edit did */}

{describe(gates, states)}

{/* step-1's histogram, fed by a persistent seeded auto-measure */}
if you measured now — 200 shots{" "}
); }