import std.fmt
import std.quantum as q
import std.tcount as tc
extern fn sqrt(x: f64) -> f64
fn identity_15() -> q.Circuit {
var poly = tc.PhasePoly.new(4)
for parity in 1..16 { poly.add_phase(parity as u64, 1) }
poly.to_circuit()
}
fn ccz() -> q.Circuit {
var c = q.Circuit.new()
var a = q.alloc_zero(&c)
var b = q.alloc_zero(&c)
var t = q.alloc_zero(&c)
let z = q.Matrix { u00: q.Complex.new(1.0, 0.0), u01: q.Complex.new(0.0, 0.0),
u10: q.Complex.new(0.0, 0.0), u11: q.Complex.new(-1.0, 0.0) }
q.controlled(&c, [q.control(a, true), q.control(b, true)], z, &t)
q.finish(&c, move a)
q.finish(&c, move b)
q.finish(&c, move t)
c
}
// The published T-par benchmarks. H Z(controlled twice) H is a Toffoli
// on the same target. Keep that primitive visible before phase folding.
// https://github.com/meamy/t-par/tree/master/Benchmarks
fn mod5_4() -> q.Circuit {
var c = q.Circuit.new()
var b = q.alloc_zero(&c)
var d = q.alloc_zero(&c)
var e = q.alloc_zero(&c)
var f = q.alloc_zero(&c)
var a = q.alloc_zero(&c)
q.x(&c, &a)
q.toffoli(&c, b, f, &a)
q.toffoli(&c, e, f, &a)
q.cnot(&c, f, &a)
q.toffoli(&c, d, e, &a)
q.cnot(&c, e, &a)
q.toffoli(&c, b, d, &a)
q.cnot(&c, d, &a)
q.cnot(&c, b, &a)
q.finish(&c, move b)
q.finish(&c, move d)
q.finish(&c, move e)
q.finish(&c, move f)
q.finish(&c, move a)
c
}
fn barenco_tof_3() -> q.Circuit {
var c = q.Circuit.new()
var a = q.alloc_zero(&c)
var b = q.alloc_zero(&c)
var d = q.alloc_zero(&c)
var scratch = q.alloc_zero(&c)
var target = q.alloc_zero(&c)
q.toffoli(&c, d, scratch, &target)
q.toffoli(&c, a, b, &scratch)
q.toffoli(&c, d, scratch, &target)
q.toffoli(&c, a, b, &scratch)
q.finish(&c, move a)
q.finish(&c, move b)
q.finish(&c, move d)
q.finish(&c, move scratch)
q.finish(&c, move target)
c
}
fn tof_3() -> q.Circuit {
var c = q.Circuit.new()
var a = q.alloc_zero(&c)
var b = q.alloc_zero(&c)
var d = q.alloc_zero(&c)
var target = q.alloc_zero(&c)
var scratch = q.alloc_zero(&c)
q.toffoli(&c, a, b, &scratch)
q.toffoli(&c, d, scratch, &target)
q.toffoli(&c, a, b, &scratch)
q.finish(&c, move a)
q.finish(&c, move b)
q.finish(&c, move d)
q.finish(&c, move target)
q.finish(&c, move scratch)
c
}
fn majority(inout c: q.Circuit, inout a: q.Qubit, inout b: q.Qubit, inout carry: q.Qubit) {
q.cnot(&c, a, &b)
q.cnot(&c, a, &carry)
q.toffoli(&c, carry, b, &a)
}
fn unmajority_add(inout c: q.Circuit, inout a: q.Qubit, inout b: q.Qubit, inout carry: q.Qubit) {
q.toffoli(&c, carry, b, &a)
q.cnot(&c, a, &carry)
q.cnot(&c, carry, &b)
}
// Two MAJ gates, a carry-out copy, then the two UMA gates in reverse order.
fn adder() -> q.Circuit {
var c = q.Circuit.new()
var a0 = q.alloc_zero(&c)
var a1 = q.alloc_zero(&c)
var b0 = q.alloc_zero(&c)
var b1 = q.alloc_zero(&c)
var cin = q.alloc_zero(&c)
var out = q.alloc_zero(&c)
majority(&c, &a0, &b0, &cin)
majority(&c, &a1, &b1, &a0)
q.cnot(&c, a1, &out)
unmajority_add(&c, &a1, &b1, &a0)
unmajority_add(&c, &a0, &b0, &cin)
q.finish(&c, move a0)
q.finish(&c, move a1)
q.finish(&c, move b0)
q.finish(&c, move b1)
q.finish(&c, move cin)
q.finish(&c, move out)
c
}
fn multiply(a: q.Complex, b: q.Complex) -> q.Complex {
q.Complex.new(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
}
fn compare(a: [q.Complex], b: [q.Complex], phase: q.Complex) -> f64 {
var worst = 0.0
for i in 0..a.len {
let aligned = multiply(a[i], phase)
let diff = q.Complex.new(aligned.re - b[i].re, aligned.im - b[i].im)
worst = max(worst, diff.abs2())
}
worst
}
fn unit_phase(a: [q.Complex], b: [q.Complex]) -> q.Complex {
var pivot = 0
for i in 1..a.len {
if a[i].abs2() > a[pivot].abs2() { pivot = i }
}
let product = multiply(q.Complex.new(a[pivot].re, -a[pivot].im), b[pivot])
let magnitude = unsafe { sqrt(product.abs2()) }
assert(magnitude > 0.0, "reference and candidate share a nonzero amplitude")
q.Complex.new(product.re / magnitude, product.im / magnitude)
}
fn evaluate(make: fn() -> q.Circuit, n: i64, check_adder: bool, check_fold: bool) -> (f64, f64) {
let size = (1 as i64) << n
var initial = Vec(q.Complex).filled(size, q.Complex.new(0.0, 0.0))
var worst = 0.0
var fold_worst = 0.0
var decoded_phase = q.Complex.new(1.0, 0.0)
var folded_phase = q.Complex.new(1.0, 0.0)
var expanded_phase = q.Complex.new(1.0, 0.0)
for basis in 0..size + 1 {
if basis < size { initial[basis] = q.Complex.new(1.0, 0.0) }
else {
let scale = 1.0 / unsafe { sqrt(size as f64) }
for i in 0..size {
initial[i] = if (i & 1) == 0 { q.Complex.new(scale, 0.0) }
else { q.Complex.new(0.0, scale) }
}
}
let native = make()
let expanded = tc.decompose(native).unwrap()
let decoded = q.CpuSim.run_from_state(tc.optimize(expanded).unwrap(), 0, initial.as_slice())
let folded = if check_fold { Some(tc.fold(expanded).unwrap()) } else { None }
let reference = q.CpuSim.run_from_state(move native, 0, initial.as_slice())
let baseline = q.CpuSim.run_from_state(move expanded, 0, initial.as_slice())
if basis == 0 {
expanded_phase = unit_phase(reference.amplitudes(), baseline.amplitudes())
decoded_phase = unit_phase(reference.amplitudes(), decoded.amplitudes())
}
assert(compare(reference.amplitudes(), baseline.amplitudes(), expanded_phase) < 1e-24,
"naive decomposition agrees with the original gates")
worst = max(worst, compare(reference.amplitudes(), decoded.amplitudes(), decoded_phase))
match move folded {
Some(circuit) => {
let folded = q.CpuSim.run_from_state(move circuit, 0, initial.as_slice())
if basis == 0 {
folded_phase = unit_phase(reference.amplitudes(), folded.amplitudes())
}
fold_worst = max(
fold_worst,
compare(reference.amplitudes(), folded.amplitudes(), folded_phase)
)
}
None => {}
}
if check_adder && basis < 32 {
let a = basis & 3
let sum = a + ((basis >> 2) & 3) + ((basis >> 4) & 1)
let expected = a | ((sum & 3) << 2) | (basis & 16) | ((sum >> 2) << 5)
assert(
abs(decoded.amplitude(expected as u64).abs2() - 1.0) < 1e-12,
"adder truth table"
)
}
if basis < size { initial[basis] = q.Complex.new(0.0, 0.0) }
}
(unsafe { sqrt(worst) }, unsafe { sqrt(fold_worst) })
}
fn benchmark(
name: str,
make: fn() -> q.Circuit,
check_adder: bool,
check_fold: bool
) -> (i64, i64) {
let native = make()
let expanded = tc.decompose(native).unwrap()
let before = tc.t_count(expanded)
let after = tc.t_count(tc.optimize(expanded).unwrap())
let (error, fold_error) = evaluate(make, native.wire_count() as i64, check_adder, check_fold)
assert(error < 1e-12 && fold_error < 1e-12, "CpuSim amplitude equivalence")
fmt.println("{} | {} -> {} | {}", (name, before, after, error))
if check_fold {
let folded = tc.t_count(tc.fold(expanded).unwrap())
fmt.println(" phase folding alone: {} -> {} | {}", (before, folded, fold_error))
}
(before, after)
}
fn main() {
println("RM(n-4, n)* | length | dimension | syndrome rank | minimum distance")
for n in 3..7 {
let code = tc.PuncturedRM.new(n - 4, n)
assert(tc.is_signature_kernel(code), "orthogonal generator and signature rows")
let distance = match code.min_dist {
Some(d) => d.to_string()
None => String.from("-")
}
fmt.println(
"{} | {} | {} | {} | {}",
(n, code.length, code.dim(), tc.syndrome_rank(n), distance)
)
}
println("Benchmark | T-count before -> after | max CpuSim amplitude error")
let ccz_counts = benchmark("CCZ", ccz, false, false)
let identity_counts = benchmark("15-T identity", identity_15, false, false)
let mod5_counts = benchmark("mod5_4", mod5_4, false, true)
let barenco_counts = benchmark("barenco_tof_3", barenco_tof_3, false, false)
let tof_counts = benchmark("tof_3", tof_3, false, false)
let adder_counts = benchmark("2-bit Cuccaro adder", adder, true, false)
assert(ccz_counts == (7, 7) && identity_counts == (15, 0), "CCZ and identity T-counts")
assert(
mod5_counts == (28, 7) &&
tc.t_count(tc.fold(tc.decompose(mod5_4()).unwrap()).unwrap()) == 8,
"mod5_4 T-counts"
)
assert(barenco_counts.0 == 28 && barenco_counts.1 < 28, "Barenco savings")
assert(tof_counts.0 == 21 && tof_counts.1 < 21, "Toffoli savings")
assert(adder_counts.0 == 28 && adder_counts.1 < 28, "adder savings")
}
View on GitHub