Spaces:
Running
Running
File size: 6,419 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 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 | /**
* qec_lineage.test.ts — runtime parity with Lutar/QEC/*.lean modules.
*
* Tests cover Hamming (1950), Shor (1995), CSS (1996), Kitaev (1997/2003).
*/
import assert from 'node:assert/strict';
import {
hammingDist,
hammingWeight,
hammingDistByte,
minDistance,
shorEncode,
shorMajorityPayload,
classicalToCSS,
cssConsistent,
vertexParity,
singleSiteError,
allErrors,
noErrors,
PhysicalReceipt,
Site,
VertexCheck,
} from './qec_lineage';
function run(name: string, fn: () => void) {
try {
fn();
console.log(` ok ${name}`);
} catch (e) {
console.error(` FAIL ${name}`);
console.error(e);
process.exitCode = 1;
}
}
console.log('QEC lineage — runtime parity with Lutar.QEC');
// ─── Hamming ───────────────────────────────────────────────────────────
run('Hamming: equal bit arrays have distance 0', () => {
assert.equal(hammingDist([true, false, true], [true, false, true]), 0);
});
run('Hamming: 000 vs 111 distance 3', () => {
assert.equal(hammingDist([false, false, false], [true, true, true]), 3);
});
run('Hamming: 1010 vs 0101 distance 4', () => {
assert.equal(hammingDist([true, false, true, false], [false, true, false, true]), 4);
});
run('Hamming: weight of all-zero is 0', () => {
assert.equal(hammingWeight([false, false, false]), 0);
});
run('Hamming: weight of all-one len 4 is 4', () => {
assert.equal(hammingWeight([true, true, true, true]), 4);
});
run('Hamming: weight of 1010 is 2', () => {
assert.equal(hammingWeight([true, false, true, false]), 2);
});
run('Hamming: length mismatch throws', () => {
assert.throws(() => hammingDist([true], [true, false]));
});
run('Hamming byte: 0x00 vs 0xFF distance 8', () => {
assert.equal(hammingDistByte(0x00, 0xff), 8);
});
run('Hamming byte: 0xAA vs 0x55 distance 8', () => {
assert.equal(hammingDistByte(0xaa, 0x55), 8);
});
run('Hamming byte: 0x42 vs 0x42 distance 0', () => {
assert.equal(hammingDistByte(0x42, 0x42), 0);
});
run('Hamming code minimum distance', () => {
// Hamming [7,4,3] partial code: include 4 codewords spanning the space.
const cw = [
[false, false, false, false, false, false, false],
[true, true, false, true, false, false, false],
[false, true, true, false, true, false, false],
[true, false, true, true, true, false, false],
];
const d = minDistance(cw);
assert.ok(d >= 1);
});
// ─── Shor ──────────────────────────────────────────────────────────────
run('Shor: encode yields 9 receipts', () => {
const logical: PhysicalReceipt = { payload: 0x42, lineage: 0xa5 };
const bundle = shorEncode(logical);
assert.equal(bundle.length, 9);
});
run('Shor: clean bundle decodes to original payload', () => {
const logical: PhysicalReceipt = { payload: 0x42, lineage: 0xa5 };
const bundle = shorEncode(logical);
assert.equal(shorMajorityPayload(bundle), 0x42);
});
run('Shor: single-fault bundle still decodes correctly (majority)', () => {
const logical: PhysicalReceipt = { payload: 0x42, lineage: 0xa5 };
const bundle = shorEncode(logical);
bundle[5] = { payload: 0xff, lineage: 0xff };
assert.equal(shorMajorityPayload(bundle), 0x42);
});
run('Shor: 4-fault bundle still decodes correctly (5 majority)', () => {
const logical: PhysicalReceipt = { payload: 0x42, lineage: 0xa5 };
const bundle = shorEncode(logical);
for (let i = 0; i < 4; i += 1) bundle[i] = { payload: 0xff, lineage: 0xff };
assert.equal(shorMajorityPayload(bundle), 0x42);
});
run('Shor: 5-fault bundle flips majority', () => {
const logical: PhysicalReceipt = { payload: 0x42, lineage: 0xa5 };
const bundle = shorEncode(logical);
for (let i = 0; i < 5; i += 1) bundle[i] = { payload: 0xff, lineage: 0xff };
assert.equal(shorMajorityPayload(bundle), 0xff);
});
// ─── CSS ───────────────────────────────────────────────────────────────
run('CSS: 0x00 -> (0x00, 0xFF)', () => {
const p = classicalToCSS(0x00);
assert.equal(p.xParity, 0x00);
assert.equal(p.zParity, 0xff);
});
run('CSS: 0xFF -> (0xFF, 0x00)', () => {
const p = classicalToCSS(0xff);
assert.equal(p.xParity, 0xff);
assert.equal(p.zParity, 0x00);
});
run('CSS: 0x55 -> (0x55, 0xAA), consistent', () => {
const p = classicalToCSS(0x55);
assert.equal(p.xParity, 0x55);
assert.equal(p.zParity, 0xaa);
assert.ok(cssConsistent(p));
});
run('CSS: inconsistent pair detected', () => {
assert.ok(!cssConsistent({ xParity: 0x00, zParity: 0x00 }));
});
run('CSS: injective on classical codewords', () => {
const all = new Set<string>();
for (let i = 0; i < 256; i += 1) {
const p = classicalToCSS(i);
all.add(`${p.xParity},${p.zParity}`);
}
assert.equal(all.size, 256);
});
// ─── Kitaev ────────────────────────────────────────────────────────────
const v0: VertexCheck = {
n: { agent: 0, slice: 0 },
s: { agent: 0, slice: 1 },
e: { agent: 1, slice: 0 },
w: { agent: 0, slice: 2 },
};
run('Kitaev: no errors -> parity 0', () => {
assert.equal(vertexParity(noErrors(), v0), false);
});
run('Kitaev: single-site error at n -> parity 1', () => {
assert.equal(vertexParity(singleSiteError(v0.n), v0), true);
});
run('Kitaev: single-site error at s -> parity 1', () => {
assert.equal(vertexParity(singleSiteError(v0.s), v0), true);
});
run('Kitaev: single-site error at e -> parity 1', () => {
assert.equal(vertexParity(singleSiteError(v0.e), v0), true);
});
run('Kitaev: single-site error at w -> parity 1', () => {
assert.equal(vertexParity(singleSiteError(v0.w), v0), true);
});
run('Kitaev: all errors -> parity 0 (4 errors cancel)', () => {
assert.equal(vertexParity(allErrors(), v0), false);
});
run('Kitaev: error at off-lattice site -> parity 0', () => {
const offSite: Site = { agent: 99, slice: 99 };
assert.equal(vertexParity(singleSiteError(offSite), v0), false);
});
console.log(process.exitCode === 1 ? '\nFAIL' : '\nall green (24 tests)');
|