open-standards/wai/wai-web/src/quantum_compile.rs
Transaction Science 8eb90cf901
Some checks failed
WAI conformance / cargo-class corpora · windows (push) Successful in 1m13s
WAI conformance / cross-arch byte-equality · windows (push) Successful in 22s
WAI conformance / cargo-class corpora · linux-arm64 (push) Successful in 51s
WAI conformance / cross-arch byte-equality · linux-arm64 (push) Successful in 19s
WAI conformance / cargo-class corpora · linux (push) Successful in 29s
WAI conformance / cross-arch byte-equality · linux (push) Successful in 15s
WAI conformance / cargo-class corpora · macos (push) Has been cancelled
WAI conformance / cross-arch byte-equality · macos (push) Has been cancelled
wai: circuit compilation + equivalence proof — wai.quantum.compile
Compilation correctness as a signed certificate: a deterministic coupling-graph
router + a byte-exact Clifford stabilizer-tableau equivalence check.

- quantum_compile.rs: Clifford tableau (Aaronson–Gottesman H/S/CX/SWAP updates on
  the 2n generator images); a coupling graph (line/ring/edges) + BFS routing that
  inserts SWAPs so every two-qubit gate is local; equivalence = routed ++ (swaps
  restoring identity) ≡ source, all through the tableau sim. random_clifford +
  compile_and_seal.
- quantum_ops.rs: CompileReceipt binds (source hash, compiled hash + final map,
  coupling hash) → (equivalent, swaps, work, joules); merkle + Ed25519, joule
  ceiling in verify(). equivalent is in the receipt (a compiler can re-run the
  same deterministic check).
- wai-web: CompileDemo binding (route+prove + tamper-rejection).

Honest boundary: exact for Clifford circuits (the standard tableau-checker scope),
covering routing/mapping correctness. Tests green (routing preserves equivalence
over 40 random circuits, tamper is caught, HH=I, S^4=I, SWAP=3·CX).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-08-26 11:55:54 -04:00

150 lines
4.9 KiB
Rust

//! Circuit compilation + equivalence proof in the browser (`wai.quantum.compile`).
//! The SAME deterministic Clifford router + stabilizer-tableau equivalence check a
//! native sink runs, compiled to wasm32: route a random Clifford circuit onto a
//! coupling graph, prove the routed circuit is equivalent to the source, and seal
//! a signed CompileReceipt — then watch the same check reject a tampered circuit.
use ed25519_dalek::SigningKey;
use wai::quantum_compile::{
circuit_bytes, compile_and_seal, routed_bytes, verify_routing, Coupling, Gate, random_clifford,
};
use wai::quantum_ops::{content_hash, CompileReceipt, GrantRef};
use wasm_bindgen::prelude::*;
fn signer() -> SigningKey {
SigningKey::from_bytes(&[0x51; 32])
}
fn hex(b: &[u8]) -> String {
b.iter().map(|x| format!("{x:02x}")).collect()
}
fn coupling_of(kind: &str, n: u32) -> Coupling {
match kind {
"ring" => Coupling::ring(n),
_ => Coupling::line(n),
}
}
fn count_2q(gates: &[Gate]) -> u32 {
gates.iter().filter(|g| matches!(g, Gate::Cx(_, _) | Gate::Swap(_, _))).count() as u32
}
fn nonlocal_2q(gates: &[Gate], c: &Coupling) -> u32 {
gates
.iter()
.filter(|g| match g {
Gate::Cx(a, b) | Gate::Swap(a, b) => !c_coupled(c, *a, *b),
_ => false,
})
.count() as u32
}
// coupling.coupled is private; reconstruct via edges for the count.
fn c_coupled(c: &Coupling, a: u32, b: u32) -> bool {
edges_flat(c).chunks(2).any(|e| (e[0] == a && e[1] == b) || (e[0] == b && e[1] == a))
}
fn edges_flat(c: &Coupling) -> Vec<u32> {
// recover undirected edges from the coupling's canonical bytes
let bytes = c.bytes();
// header "wai:qc-coupling\x01" (16) + n (4), then u32 pairs
let mut out = Vec::new();
let start = 16 + 4;
let mut i = start;
while i + 8 <= bytes.len() {
let a = u32::from_le_bytes([bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]);
let b = u32::from_le_bytes([bytes[i + 4], bytes[i + 5], bytes[i + 6], bytes[i + 7]]);
out.push(a);
out.push(b);
i += 8;
}
out
}
#[wasm_bindgen]
pub struct CompileDemo {
n: u32,
kind: String,
source: Vec<Gate>,
routed: Vec<Gate>,
final_map: Vec<u32>,
swaps: u32,
equivalent: bool,
work: u64,
tamper_equiv: bool,
receipt: CompileReceipt,
coupling: Coupling,
src_nonlocal: u32,
}
#[wasm_bindgen]
impl CompileDemo {
/// Route a random `depth`-gate Clifford circuit on `n` qubits onto a `line` or
/// `ring` coupling, prove equivalence, and also check that a tampered routing
/// (one gate dropped) is correctly rejected.
#[wasm_bindgen(constructor)]
pub fn new(n: u32, depth: u32, seed: f64, kind: String) -> CompileDemo {
let coupling = coupling_of(&kind, n);
let source = random_clifford(n, depth, seed as u64);
let src_nonlocal = nonlocal_2q(&source, &coupling);
let (r, equivalent, receipt) = compile_and_seal(
&signer(), "did:key:demo-lab", &source, &coupling, 300_000,
GrantRef::unbounded("quantum.compile"),
);
let work = verify_routing(&source, &r, n).1;
// tamper: drop the last routed gate → should NOT verify
let mut tampered = r.clone();
tampered.routed.pop();
let (tamper_equiv, _) = verify_routing(&source, &tampered, n);
CompileDemo {
n, kind, source, routed: r.routed.clone(), final_map: r.final_map.clone(),
swaps: r.swaps, equivalent, work,
tamper_equiv, receipt, coupling, src_nonlocal,
}
}
pub fn n(&self) -> u32 {
self.n
}
pub fn coupling_kind(&self) -> String {
self.kind.clone()
}
/// Coupling edges, flattened `[a0,b0,a1,b1,…]`.
pub fn edges(&self) -> Vec<u32> {
edges_flat(&self.coupling)
}
pub fn source_gates(&self) -> u32 {
self.source.len() as u32
}
pub fn source_2q(&self) -> u32 {
count_2q(&self.source)
}
/// Source two-qubit gates that are NOT on a coupled pair (need routing).
pub fn source_nonlocal(&self) -> u32 {
self.src_nonlocal
}
pub fn routed_gates(&self) -> u32 {
self.routed.len() as u32
}
pub fn swaps(&self) -> u32 {
self.swaps
}
pub fn equivalent(&self) -> bool {
self.equivalent
}
pub fn tamper_equivalent(&self) -> bool {
self.tamper_equiv
}
pub fn work(&self) -> u32 {
self.work as u32
}
pub fn final_map(&self) -> Vec<u32> {
self.final_map.clone()
}
pub fn source_hash_hex(&self) -> String {
hex(&content_hash(&circuit_bytes(&self.source, self.n)))
}
pub fn target_hash_hex(&self) -> String {
hex(&content_hash(&routed_bytes(&self.routed, self.n, &self.final_map)))
}
pub fn receipt_verifies(&self) -> bool {
self.receipt.verify()
}
pub fn receipt_json(&self) -> String {
self.receipt.to_json()
}
}