gridsynth

import std.fmt
import std.gridsynth as gs

extern fn log2(x: f64) -> f64

fn main() {
    let words = [
        [gs.Gate.W(3), gs.Gate.H, gs.Gate.T, gs.Gate.H, gs.Gate.S, gs.Gate.Tdg],
        [gs.Gate.T, gs.Gate.X, gs.Gate.H, gs.Gate.S, gs.Gate.H, gs.Gate.W(7)],
        [gs.Gate.Y, gs.Gate.Z, gs.Gate.Sdg, gs.Gate.H, gs.Gate.T, gs.Gate.H],
    ]
    var worst = 0.0
    var checked = 0
    for word in words {
        let target = gs.gate_matrix(word)
        let gates = gs.synthesize_exact(target).unwrap()
        assert(gs.verify_exact_matrix(gates.as_slice(), target), "exact matrix equality")
        let error = gs.operator_distance(gs.cpu_matrix(gates.as_slice()), target.to_matrix())
        assert(error < 1e-15, "exact synthesis agrees with CpuSim")
        worst = max(worst, error)
        checked += 1
    }
    fmt.println(
        "Exact single-qubit synthesis | {} exact matrices | worst CpuSim error {}",
        (checked, worst)
    )
    println("")
    println("Angle     | eps  |  k |  T | T / log2(1/eps) | Gates | CpuSim operator error")
    let pi = 3.141592653589793
    for (label, theta) in [("pi/8", pi / 8.0), ("pi/128", pi / 128.0), ("1.0", 1.0)] {
        for (digits, eps) in [(3, 1e-3), (4, 1e-4), (5, 1e-5), (6, 1e-6), (7, 1e-7), (8, 1e-8)] {
            let result = gs.synthesize_rz(theta, eps).unwrap()
            let gates = result.gates.as_slice()
            let count = gs.t_count(gates)
            let ratio = count as f64 / (unsafe { log2(1.0 / eps) })
            assert(gs.verify_exact_matrix(gates, result.target), "exact rotation matrix")
            assert(result.error <= eps && result.cpu_error <= eps, "rotation accuracy")
            fmt.println("{:<9} | 1e-{} | {:>2} | {:>2} | {:>15.3} | {:>5} | {}",
                (label, digits, result.target.sde(), count, ratio, gates.len, result.cpu_error))
        }
    }
}

View on GitHub

Esc