// The package as an installer leaves it: its bindings and its WebAssembly, loaded in // Node with nothing else installed. // // node wai/wai-quantum-js/test/smoke.mjs PACKAGE_DIR // // Prints one line per check and exits non-zero on the first that fails. import assert from 'node:assert/strict'; import { randomBytes } from 'node:crypto'; import { readFile } from 'node:fs/promises'; import { join, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; const dir = resolve(process.argv[2] ?? '.'); const pkg = JSON.parse(await readFile(join(dir, 'package.json'), 'utf8')); const wq = await import(pathToFileURL(join(dir, pkg.main)).href); // In Node the module is given its WebAssembly's bytes; a page lets it fetch them. await wq.default({ module_or_path: await readFile(join(dir, 'wai_quantum_bg.wasm')) }); console.log('node', process.versions.node, pkg.name, pkg.version); const BELL = 'OPENQASM 3.0; qubit[2] q; bit[2] c; h q[0]; cx q[0], q[1]; c = measure q;'; // A job run with a fresh key, and its receipt checked. const engine = new wq.QuantumEngine(randomBytes(32), 256); const signer = engine.signer_did(); assert.match(signer, /^did:key:z6Mk/, signer); const job = JSON.parse(engine.run(JSON.stringify({ qasm: BELL, shots: 1000, seed: 7 }))); assert.equal(job.status, 'done', JSON.stringify(job).slice(0, 400)); const counts = job.result.counts; assert.ok(Object.keys(counts).length > 0 && Object.keys(counts).every((k) => k === '00' || k === '11'), JSON.stringify(counts)); const v = JSON.parse(wq.engine_verify(JSON.stringify(job))); assert.equal(v.valid, true, JSON.stringify(v)); assert.equal(v.signer, signer, JSON.stringify(v)); console.log(`run and verify: ${job.method}, ${Object.values(counts).reduce((a, b) => a + b, 0)} shots, signed by this engine's key`); // A result changed after it was sealed does not verify. const forged = structuredClone(job); const k = Object.keys(forged.result.counts)[0]; forged.result.counts[k] += 1; assert.notEqual(JSON.parse(wq.engine_verify(JSON.stringify(forged))).valid, true); console.log('a forged result does not verify'); // A program read and drawn. const lint = JSON.parse(wq.engine_lint(BELL)); assert.equal(lint.ok, true, JSON.stringify(lint)); assert.equal(lint.qubits, 2, JSON.stringify(lint)); const drawn = JSON.parse(wq.engine_draw(BELL, 80)); assert.ok(typeof drawn.text === 'string' && drawn.text.trim().length > 0, JSON.stringify(drawn)); console.log(`lint and draw: ${lint.qubits} qubits, ${lint.gates} gates; drawn in ${drawn.text.trimEnd().split('\n').length} lines`); // A program the engine refuses says why, with its diagnostic code. const WHILE = 'OPENQASM 3.0; qubit[1] q; bit[1] c; c[0] = measure q[0]; while (c[0]) { x q[0]; c[0] = measure q[0]; }'; const refused = JSON.parse(engine.run(JSON.stringify({ qasm: WHILE, shots: 10 }))); assert.ok(typeof refused.error === 'string' && /^Q\d{3}$/.test(refused.code ?? ''), JSON.stringify(refused)); console.log(`a refused program says why: [${refused.code}] ${refused.meaning}`); // An energy-landscape job (wai.quantum.landscape/1): run in Node, sealed with this engine's // key, its receipt and its form checked (quantum-landscape section 5.4); a changed member // is no answer a host would seal; a program the reader refuses names its line. const PROGRAM = 'vars 4\ncost 1\nagree 0 1\nagree 0 2\nagree 1 2\noppose 2 3\nagree 3 0\nsolve restarts=2 steps=9\n'; const land = JSON.parse(engine.landscape('run', JSON.stringify({ program: PROGRAM, seed: '7' }))); assert.equal(land.result.schema, 'wai.quantum.landscape/1', JSON.stringify(land).slice(0, 400)); const lv = JSON.parse(wq.engine_verify(JSON.stringify(land))); assert.equal(lv.valid, true, JSON.stringify(lv)); assert.equal(lv.landscape.op, 'run', JSON.stringify(lv)); const graded = structuredClone(land); graded.result.member.parity = 'bit-exact'; assert.ok(JSON.parse(wq.engine_verify(JSON.stringify(graded))).reasons.includes('landscape_form')); const lref = JSON.parse(engine.landscape('run', JSON.stringify({ program: 'vars 2\noppos 0 1\nsolve\n' }))); assert.equal(lref.line, 2, JSON.stringify(lref)); console.log(`landscape: ${land.result.verdict} at energy ${land.result.energy}, its form checked; a refused program names line ${lref.line}`); // The second version (wai.quantum.landscape/2), named in the request: a constraint the first // version cannot write, run and sealed here, its answer naming the version, a sampling run's // joules stated unpriced and a run at temperature 0 priced, and its receipt held to that // version's form (quantum-landscape-2 section 5.4); the same program is none of the first's. const SECOND = 'wai.quantum.landscape/2'; const ONE_OF_THREE = 'vars 3\nexactly 1 0 1 2\nsolve restarts=2 steps=9\n'; const land2 = JSON.parse(engine.landscape('run', JSON.stringify({ program: ONE_OF_THREE, protocol: SECOND, seed: '7' }))); assert.deepEqual([land2.result.schema, land2.result.protocol, land2.result.joules.kind, land2.result.joules.pj], [SECOND, SECOND, 'unpriced', null], JSON.stringify(land2).slice(0, 400)); const lv2 = JSON.parse(wq.engine_verify(JSON.stringify(land2))); assert.ok(lv2.valid === true && lv2.landscape.problems.length === 0, JSON.stringify(lv2)); const cold2 = JSON.parse(engine.landscape('run', JSON.stringify({ program: ONE_OF_THREE, protocol: SECOND, temperature: '0' }))); assert.equal(cold2.result.joules.kind, 'priced', JSON.stringify(cold2.result.joules)); const priced2 = structuredClone(land2); priced2.result.joules.kind = 'priced'; assert.ok(JSON.parse(wq.engine_verify(JSON.stringify(priced2))).reasons.includes('landscape_form')); assert.equal(JSON.parse(engine.landscape('run', JSON.stringify({ program: ONE_OF_THREE }))).line, 2); console.log(`landscape/2: ${land2.result.verdict} at energy ${land2.result.energy}, its joules ${land2.result.joules.kind}, its form checked`); // A qudit job (wai.quantum.qudit/1): a qutrit Bell pair run in Node, sealed with this // engine's key, its receipt checked and its request run again to the same bits; a // dimension the engine holds no roots for is refused with its code. const QUTRITS = { d: 3, qudits: 2, gates: [{ gate: 'fourier', qudits: [0] }, { gate: 'csum', qudits: [0, 1] }], shots: 300, seed: 7, amplitudes: ['00'] }; const qj = JSON.parse(engine.qudit(JSON.stringify(QUTRITS))); assert.equal(qj.result.schema, 'wai.quantum.qudit/1', JSON.stringify(qj).slice(0, 400)); assert.ok(Object.keys(qj.result.counts).every((k) => ['00', '11', '22'].includes(k)), JSON.stringify(qj.result.counts)); assert.deepEqual(qj.result.amplitudes['00'], { re: 619925131, im: 0 }); assert.equal(JSON.parse(wq.engine_verify(JSON.stringify(qj))).valid, true); const qc = JSON.parse(wq.engine_qudit_check(JSON.stringify(qj))); assert.equal(qc.verified, true, JSON.stringify(qc)); const qref = JSON.parse(engine.qudit(JSON.stringify({ d: 5, qudits: 1, gates: [] }))); assert.equal(qref.code, 'J091', JSON.stringify(qref)); console.log(`qudit: a qutrit Bell pair, ${Object.keys(qj.result.counts).length} outcomes, sealed, run again to the same bits; d = 5 refused (${qref.code})`); // wasmQ envelopes (wai.quantum.wasmq/1 and /2): every example the engine carries, run in // Node on the crate's own interpreter, sealed with this engine's key, held to the // profile's form, and run again from its envelope to the same result; another envelope // is not the one the receipt binds; an envelope the profile refuses names its code. const examples = JSON.parse(wq.engine_wasmq_examples()); assert.ok(examples.length >= 5 && examples.some((e) => e.profile === 'wai.quantum.wasmq/2'), JSON.stringify(examples)); for (const e of examples) { const envelope = wq.engine_wasmq_example(e.name); assert.equal(envelope.length, e.bytes, e.name); const wj = JSON.parse(engine.wasmq(envelope)); assert.equal(wj.result.profile, e.profile, `${e.name}: ${JSON.stringify(wj).slice(0, 400)}`); const wv = JSON.parse(wq.engine_verify(JSON.stringify(wj))); assert.equal(wv.valid, true, `${e.name}: ${JSON.stringify(wv)}`); const again = JSON.parse(wq.engine_wasmq_verify(JSON.stringify(wj), envelope)); assert.ok(again.valid && again.bundle === true && again.reproduced === true, `${e.name}: ${JSON.stringify(again)}`); if (e.profile === 'wai.quantum.wasmq/2') assert.deepEqual(Object.keys(wj.result.energy.run), ['unmetered'], e.name); } const fault = JSON.parse(engine.wasmq(wq.engine_wasmq_example('fault-fuel'))); assert.equal(fault.result.fault.reason, 'fuel_exhausted', JSON.stringify(fault.result)); const elsewhere = JSON.parse(wq.engine_wasmq_verify(JSON.stringify(fault), wq.engine_wasmq_example('v2-t-toffoli'))); assert.ok(elsewhere.bundle === false && elsewhere.valid === false, JSON.stringify(elsewhere)); const wref = JSON.parse(engine.wasmq(new TextEncoder().encode('WAI1 not an envelope'))); assert.equal(wref.code, 'W013', JSON.stringify(wref)); console.log(`wasmQ: ${examples.length} envelopes run in Node, each sealed, held to its form and run again to the same result; a fault sealed; W013 refused`); // The sign-off loop: a program checked against what it must do (each case by the strongest // layer, nothing sealed), its grade sealed with this engine's key and made again by // engine_verify, and a job another key sealed attested against a trust list naming that key; // a task that would publish its answer and a key the list does not name are refused. The // engine is given 1 GiB: an intent's exact check takes a state vector with 256 MiB of room // beside it, so an engine of 256 MiB says such a case is unchecked, and why. const FAIR = { distribution: { '00': 0.5, '11': 0.5 } }; const looping = new wq.QuantumEngine(randomBytes(32), 1024); const small = JSON.parse(engine.check(JSON.stringify({ qasm: BELL, intent: FAIR }))); assert.deepEqual([small.passed, small.grade.cases[0].grade], [false, 'unchecked'], JSON.stringify(small).slice(0, 400)); const checked = JSON.parse(looping.check(JSON.stringify({ qasm: BELL, intent: FAIR }))); assert.deepEqual([checked.passed, checked.grade.cases[0].layer], [true, 'exact_simulation'], JSON.stringify(checked).slice(0, 400)); const gradedJob = looping.grade(JSON.stringify({ grade: { task: { title: 'A Bell pair', intent: FAIR }, program: BELL } })); const gv = JSON.parse(wq.engine_verify(gradedJob)); assert.ok(gv.valid === true && gv.answer.again === 'reproduced' && gv.signer === looping.signer_did(), JSON.stringify(gv)); const leaky = JSON.parse(looping.grade(JSON.stringify({ grade: { task: { intent: FAIR, reference: BELL }, program: BELL } }))); assert.ok(leaky.error.endsWith('[J001]'), JSON.stringify(leaky)); const host = new wq.QuantumEngine(randomBytes(32), 256); const hosted = host.run(JSON.stringify({ qasm: BELL, shots: 64, seed: 3 })); const listed = { format: 'wai.quantum.receipt-keys/1', keys: [{ key: JSON.parse(hosted).result.receipt.key }] }; const att = JSON.parse(looping.attest(JSON.stringify({ job: hosted, intent: { outcomes: ['00', '11'] }, trust: listed }))); assert.deepEqual([att.result.verdict.holds, att.attested.verified.valid, att.attested.held_to.trust], [true, true, true], JSON.stringify(att).slice(0, 400)); const av = JSON.parse(wq.engine_verify(JSON.stringify({ request: att.request, result: att.result }))); assert.ok(av.valid === true && av.answer.again === 'reproduced', JSON.stringify(av)); const untrusted = JSON.parse(looping.attest(JSON.stringify({ job: hosted, trust: { format: 'wai.quantum.receipt-keys/1', keys: [{ key: '00'.repeat(32) }] } }))); assert.ok(untrusted.error.includes('key_untrusted'), JSON.stringify(untrusted)); console.log(`sign-off loop: checked (${checked.grade.cases[0].layer}; unchecked in 256 MiB), graded and made again, attested against a trust list; a leaky task and an untrusted key refused`); console.log('smoke: every check passed');