import std.fmt
import std.quantum as q
import std.zx
const PI: f64 = 3.14159265358979323846
fn qft4() -> 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 e = q.alloc_zero(&c)
q.h(&c, &e)
q.cp(&c, d, PI / 2.0, &e)
q.cp(&c, b, PI / 4.0, &e)
q.cp(&c, a, PI / 8.0, &e)
q.h(&c, &d)
q.cp(&c, b, PI / 2.0, &d)
q.cp(&c, a, PI / 4.0, &d)
q.h(&c, &b)
q.cp(&c, a, PI / 2.0, &b)
q.h(&c, &a)
q.swap(&c, &a, &e)
q.swap(&c, &b, &d)
q.finish(&c, move a)
q.finish(&c, move b)
q.finish(&c, move d)
q.finish(&c, move e)
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)
}
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 toffoli_chain() -> q.Circuit {
var c = q.Circuit.new()
let a = q.alloc_zero(&c)
let b = q.alloc_zero(&c)
var scratch = q.alloc_zero(&c)
let d = q.alloc_zero(&c)
var out = q.alloc_zero(&c)
q.toffoli(&c, a, b, &scratch)
q.toffoli(&c, scratch, d, &out)
q.toffoli(&c, a, b, &scratch)
q.finish(&c, move a)
q.finish(&c, move b)
q.finish(&c, move scratch)
q.finish(&c, move d)
q.finish(&c, move out)
c
}
fn gadget_cancellation() -> q.Circuit {
var c = q.Circuit.new()
var a = q.alloc_zero(&c)
var b = q.alloc_zero(&c)
q.h(&c, &a)
q.cp(&c, a, PI / 2.0, &b)
q.s(&c, &b)
q.cp(&c, a, -PI / 2.0, &b)
q.h(&c, &a)
q.finish(&c, move a)
q.finish(&c, move b)
c
}
fn benchmark(name: str, circuit: q.Circuit) -> zx.Reduction {
var graph = zx.from_circuit(circuit).unwrap()
let report = zx.full_reduce(&graph)
let output = zx.extract_with_stats(graph).unwrap()
let error = zx.unitary_error(circuit, output.circuit)
assert(error < 1e-12, "CpuSim basis columns and complex superposition agree")
fmt.println(
"{} | {} -> {} | {} -> {} | {} -> {} | {} -> {} | {} | {} | {:.3e}",
(
name,
report.before.spiders,
report.after.spiders,
report.before.hadamards,
report.after.hadamards,
report.before.t_count,
report.after.t_count,
circuit.gates().len,
output.circuit.gates().len,
report.steps(),
output.row_additions,
error
)
)
report
}
fn main() {
println("Circuit | spiders | Hadamard edges | non-Clifford spiders | gates | rewrites | F2 row adds | CpuSim error")
benchmark("tof_3", toffoli_chain())
benchmark("QFT-4", qft4())
benchmark("2-bit Cuccaro adder", adder())
let demo = benchmark("phase-gadget cancellation", gadget_cancellation())
assert(demo.gadgets_simplified > 0 && demo.after.t_count == 0, "matching gadgets cancel")
println("")
println("Ruler-style discovery | wires | depth | terms | projective classes | candidates | retained rules")
for wires in 1..3 {
let depth = if wires == 1 { 4 } else { 3 }
let discovered = zx.discover_rules(wires, depth).unwrap()
fmt.println(" | {} | {} | {} | {} | {} | {}",
(wires, depth, discovered.enumerated, discovered.equivalence_classes,
discovered.candidates, discovered.rules.len()))
for rule in discovered.rules.as_slice() {
fmt.println(" {} -> {}", (rule.lhs.text(), rule.rhs.text()))
}
let original = if wires == 1 {
let h = zx.Gate.H(0)
zx.Word.from_gates(wires, [h, zx.Gate.Z(0), h, h, h]).unwrap()
} else {
let h = zx.Gate.H(1)
let cx = zx.Gate.Cnot(0, 1)
zx.Word.from_gates(wires, [h, cx, h, zx.Gate.H(0), zx.Gate.H(0)]).unwrap()
}
let rewrite = zx.saturate(wires, original, discovered.rules.as_slice(), 4096)
let before = zx.word_circuit(wires, original.gates().as_slice()).unwrap()
let after = zx.word_circuit(wires, rewrite.best.gates().as_slice()).unwrap()
let error = zx.unitary_error(before, after)
assert(rewrite.best.len() < original.len() && error < 1e-12, "sound saturation compression")
fmt.println(" {} -> {} ({} -> {} gates, {} terms, fixed point {}, CpuSim error {:.3e})",
(
original.text(),
rewrite.best.text(),
original.len(),
rewrite.best.len(),
rewrite.terms,
rewrite.saturated,
error
))
}
}
View on GitHub