import std.fmt
import std.quantum as q
import std.stabilizer as st
const CLOCK_MONOTONIC: i32 = 1
struct Timespec {
seconds: i64
nanos: i64
}
extern fn clock_gettime(clock: i32, ts: *Timespec) -> i32
fn monotonic_ns() -> i64 {
var ts = Timespec { seconds: 0, nanos: 0 }
assert(unsafe { clock_gettime(CLOCK_MONOTONIC, &ts) } == 0, "CLOCK_MONOTONIC")
ts.seconds * 1000000000 + ts.nanos
}
fn elapsed_ms(start: i64) -> f64 { (monotonic_ns() - start) as f64 / 1000000.0 }
fn enumerate() {
let one = st.enumerate_cliffords(1)
let orders = one.order_histogram.unwrap()
assert(one.projective == 24 && one.symplectic == 6 && orders == [1, 9, 8, 6],
"one-qubit enumeration")
fmt.println("Cl_1 / U(1): {} elements, orders [1: {}, 2: {}, 3: {}, 4: {}]",
(one.projective, orders[0], orders[1], orders[2], orders[3]))
let two = st.enumerate_cliffords(2)
assert(two.projective == 11520 && two.symplectic == 720, "two-qubit enumeration")
fmt.println("Cl_2 / U(1): {} symplectic matrices (|S_6|), {} projective Cliffords",
(two.symplectic, two.projective))
for n in 1..3 {
let counts = if n == 1 { one } else { two }
assert(st.symplectic_order(n).unwrap() == counts.symplectic as u128 &&
st.clifford_order(n).unwrap() == counts.projective as u128, "closed-form group orders")
}
}
fn identities() {
let i = st.Tableau.new(1, 0)
var t = i.clone()
for _ in 0..3 {
t.s(0)
t.h(0)
}
assert(t.clifford_eq(i), "(H S)^3 = I")
for _ in 0..4 { t.s(0) }
assert(t.clifford_eq(i), "S^4 = I")
var x = i.clone()
var y = i.clone()
var z = i.clone()
x.x(0)
y.y(0)
z.z(0)
t.h(0)
t.x(0)
t.h(0)
assert(t.clifford_eq(z), "H X H = Z")
t = i.clone()
t.h(0)
t.z(0)
t.h(0)
assert(t.clifford_eq(x), "H Z H = X")
t = i.clone()
t.sdg(0)
t.x(0)
t.s(0)
assert(t.clifford_eq(y), "S X S† = Y")
let i = st.Tableau.new(2, 0)
var a = i.clone()
var b = i.clone()
a.swap(0, 1)
b.cnot(0, 1)
b.cnot(1, 0)
b.cnot(0, 1)
assert(a.clifford_eq(b), "SWAP = CNOT(0,1) CNOT(1,0) CNOT(0,1)")
a = i.clone()
b = i.clone()
a.cz(0, 1)
b.h(1)
b.cnot(0, 1)
b.h(1)
assert(a.clifford_eq(b), "CZ = H(1) CNOT(0,1) H(1)")
println("Verified seven Clifford identities by exact tableau equality")
}
fn bell() {
var cpu = q.CpuSim.new(17)
inout c = &cpu.circuit()
var a = q.alloc_zero(&c)
var b = q.alloc_zero(&c)
q.h(&c, &a)
q.cnot(&c, a, &b)
var t = st.Tableau.new(2, 17)
t.apply_circuit(c).unwrap()
cpu.execute()
assert(t.measure_expectation_z(0) == st.ZExpectation.Random &&
abs(cpu.probability_one(0) - 0.5) < 1e-12, "Bell probability")
let expected = t.measure(0)
let first = q.measure(&cpu, move a)
let second = q.measure(&cpu, move b)
assert(first == expected && second == expected && t.measure(1) == expected,
"seeded Bell collapse agrees")
println("Bell Circuit replay agrees with CpuSim before and after measurement")
}
fn wire_word(wire: i64, word: i64) -> u64 {
if wire >= 0 && wire / 64 == word { 1 as u64 << (wire % 64) } else { 0 }
}
fn cluster() {
let n = 1024
let start = monotonic_ns()
var t = st.Tableau.new(n, 0)
for i in 0..n { t.h(i) }
for i in 1..n { t.cz(i - 1, i) }
let prepare = elapsed_ms(start)
let check = monotonic_ns()
for i in 0..n {
let row = t.stabilizer(i)
assert(!row.is_negative(), "cluster generator sign")
for j in 0..row.x_words().len {
let x = wire_word(i, j)
var z = wire_word(i - 1, j)
if i + 1 < n { z |= wire_word(i + 1, j) }
assert(row.x_words()[j] == x && row.z_words()[j] == z, "local cluster generator")
}
}
fmt.println(
"1,024-qubit linear cluster: prepared in {:.3} ms, verified 1,024 checks in {:.3} ms",
(prepare, elapsed_ms(check))
)
}
fn ghz() {
let n = 2048
let start = monotonic_ns()
var t = st.Tableau.new(n, 0)
t.h(0)
for i in 1..n { t.cnot(0, i) }
let prepare = elapsed_ms(start)
let check = monotonic_ns()
assert(t.measure_expectation_z(0) == st.ZExpectation.Random, "GHZ branches have equal weight")
let one = t.measure(0)
for i in 1..n { assert(t.measure(i) == one, "all GHZ measurements agree") }
fmt.println("2,048-qubit GHZ: prepared in {:.3} ms, measured all wires as {} in {:.3} ms",
(prepare, one as i64, elapsed_ms(check)))
}
fn main() {
enumerate()
identities()
bell()
cluster()
ghz()
}
View on GitHub