Spaces:
Running
Running
File size: 6,426 Bytes
518343a | 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 | /**
* qec_lineage.ts — runtime counterpart of Lutar/QEC/*.lean modules
*
* Provides:
* • Hamming distance & weight on byte arrays (Hamming 1950).
* • Shor [[9,1,3]] 9-fold receipt replication with majority decode
* (Shor 1995).
* • CSS classical→stabilizer bridge (Calderbank–Shor–Steane 1996).
* • Kitaev surface-code vertex parity check (Kitaev 1997/2003).
*
* Mirror invariants of the Lean modules; the test suite asserts each.
*
* Citations (DOIs):
* • Hamming 1950 — 10.1002/j.1538-7305.1950.tb00463.x
* • Shor 1995 — 10.1103/PhysRevA.52.R2493
* • Steane 1996 — 10.1098/rspa.1996.0136
* • Calderbank-Shor 1996 — 10.1103/PhysRevA.54.1098
* • Kitaev 2003 — 10.1016/S0003-4916(02)00018-0
* • Cover & Thomas 2006 — ISBN 978-0-471-24195-9
*
* Innovation beyond attribution: the receipt-level instantiation of
* each construction is new (no quantum-AI prior art in 1950-2003).
*/
// ──────────────────────────────────────────────────────────────────────
// Hamming foundations (1950)
// ──────────────────────────────────────────────────────────────────────
/** Hamming distance between two equal-length bit arrays. */
export function hammingDist(a: readonly boolean[], b: readonly boolean[]): number {
if (a.length !== b.length) {
throw new Error(`hammingDist: length mismatch ${a.length} vs ${b.length}`);
}
let d = 0;
for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) d += 1;
}
return d;
}
/** Hamming weight = distance from the all-zero vector. */
export function hammingWeight(a: readonly boolean[]): number {
let w = 0;
for (const bit of a) if (bit) w += 1;
return w;
}
/** Hamming distance for UInt8 bytes (XOR popcount). */
export function hammingDistByte(a: number, b: number): number {
let x = (a ^ b) & 0xff;
let count = 0;
while (x) {
count += x & 1;
x >>>= 1;
}
return count;
}
/** Minimum distance of a set of equal-length bit arrays (codewords). */
export function minDistance(codewords: ReadonlyArray<readonly boolean[]>): number {
if (codewords.length < 2) return 0;
let m = Infinity;
for (let i = 0; i < codewords.length; i += 1) {
for (let j = i + 1; j < codewords.length; j += 1) {
const d = hammingDist(codewords[i], codewords[j]);
if (d < m) m = d;
}
}
return m === Infinity ? 0 : m;
}
// ──────────────────────────────────────────────────────────────────────
// Shor [[9,1,3]] receipt code (1995)
// ──────────────────────────────────────────────────────────────────────
export interface PhysicalReceipt {
readonly payload: number; // UInt8
readonly lineage: number; // UInt8
}
/** Encode a logical receipt as a 9-fold replicated bundle. */
export function shorEncode(logical: PhysicalReceipt): PhysicalReceipt[] {
return Array.from({ length: 9 }, () => ({ ...logical }));
}
/** Majority decode the bundle by selecting the most common payload byte. */
export function shorMajorityPayload(bundle: ReadonlyArray<PhysicalReceipt>): number {
if (bundle.length === 0) return 0;
const counts = new Map<number, number>();
for (const r of bundle) {
counts.set(r.payload, (counts.get(r.payload) || 0) + 1);
}
let best = bundle[0].payload;
let bestCount = 0;
for (const [p, c] of counts) {
if (c > bestCount) {
bestCount = c;
best = p;
}
}
return best;
}
// ──────────────────────────────────────────────────────────────────────
// CSS classical → stabilizer bridge (1996)
// ──────────────────────────────────────────────────────────────────────
export interface StabilizerPair {
readonly xParity: number; // UInt8
readonly zParity: number; // UInt8
}
/** Classical 8-bit codeword to (X-parity, Z-parity) stabilizer pair. */
export function classicalToCSS(codeword: number): StabilizerPair {
return { xParity: codeword & 0xff, zParity: (codeword ^ 0xff) & 0xff };
}
/** A CSS pair is consistent when X ⊕ Z = 0xFF. */
export function cssConsistent(pair: StabilizerPair): boolean {
return (pair.xParity ^ pair.zParity) === 0xff;
}
// ──────────────────────────────────────────────────────────────────────
// Kitaev surface-code vertex check (1997/2003)
// ──────────────────────────────────────────────────────────────────────
export interface Site {
readonly agent: number;
readonly slice: number;
}
/** Equality on Site. */
function siteEq(a: Site, b: Site): boolean {
return a.agent === b.agent && a.slice === b.slice;
}
export interface VertexCheck {
readonly n: Site;
readonly s: Site;
readonly e: Site;
readonly w: Site;
}
/** Vertex parity: XOR of the 4 incident error bits. Errors are mapped
* from a `Site → boolean` function. */
export function vertexParity(
errs: (s: Site) => boolean,
v: VertexCheck,
): boolean {
return errs(v.n) !== errs(v.s) !== errs(v.e) !== errs(v.w);
}
/** Helper: an error map that flags exactly one site as corrupted. */
export function singleSiteError(target: Site): (s: Site) => boolean {
return (s: Site) => siteEq(s, target);
}
/** Helper: an error map that flags every site. */
export function allErrors(): (s: Site) => boolean {
return () => true;
}
/** Helper: an error map that flags no site. */
export function noErrors(): (s: Site) => boolean {
return () => false;
}
|