Exact single-qubit synthesis and Z rotations
Import std.gridsynth to synthesize one-qubit Clifford+T circuits. Exact
synthesis takes a unitary over ℤ[1/√2, i] and returns a sequence with minimum
T-count, including any global phase needed for matrix equality.
synthesize_rz(theta, eps) approximates the rotation
diag(exp(-i theta/2), exp(i theta/2)) in operator norm, with angles in radians.
import std.gridsynth as gs
fn main() {
let target = gs.gate_matrix([gs.Gate.H, gs.Gate.T, gs.Gate.H])
let gates = gs.synthesize_exact(target).unwrap()
assert(gs.t_count(gates.as_slice()) == 1, "minimum T-count")
assert(gs.verify_exact_matrix(gates.as_slice(), target), "exact unitary")
let rz = gs.synthesize_rz(1.0, 1e-8).unwrap()
let synthesized = rz.gates.as_slice()
assert(gs.verify_exact_matrix(synthesized, rz.target), "exact approximation matrix")
assert(rz.error <= 1e-8 && rz.cpu_error <= 1e-8, "rotation accuracy")
let circuit = gs.to_circuit(synthesized)
assert(circuit.wire_count() == 1, "one-qubit circuit")
}
Exact arithmetic and decomposition
ZSqrt2.new(a, b) represents a + b√2. Its bullet() changes the sign
of √2, norm() gives a² − 2b², and comparisons use integer arithmetic.
scale_lambda(n) multiplies by (1 + √2)^n for either sign of n.
ZOmega.new(a, b, c, d) represents aω³ + bω² + cω + d, with
ω = exp(iπ/4). It has independent adj() and bullet() conjugations.
norm_squared() returns the quadratic integer u u† and norm()
returns its integer norm. Both rings provide div_rem, gcd and
div_sqrt2. The Euclidean quotient rounds each coefficient of the
field quotient to a nearest integer. GCDs are defined up to units.
DOmega.new(u, k) represents u / √2^k and cancels common factors of √2.
ExactMatrix.from_entries(entries, k) validates four row-major numerators
with a shared denominator. ExactMatrix.from_column(u, v, k, l) validates
u u† + v v† = 2^k and uses the determinant ω^l to construct the second
column. sde() returns the least denominator exponent. The matrix
constructors and exact synthesizer accept exponents through 60. Low-level
ring operations check that intermediate integer coefficients fit in
i128, including in release builds.
Synthesis uses the Giles–Selinger parity rules on the Bloch matrix.
The position of the zero row among the residues modulo √2 determines
whether to remove T, HT or SHT. Each step decreases the Bloch
denominator exponent by one. That exponent is the exact minimum T-count.
The 2×2 unitary is tracked in parallel so the 24 projective Clifford
representatives and eight powers of W = ωI recover the exact remainder.
Kliuchnikov, Maslov and Mosca ·
Giles and Selinger
Gate lists are in execution order. gate_matrix multiplies the exact
matrices in that order, and verify_exact_matrix checks equality including
phase. to_circuit records the gates on one std.quantum.Circuit wire. A W gate records a
global phase as a scalar unitary on that wire.
cpu_matrix executes the recording from both basis inputs to recover its
two columns. operator_distance returns the largest singular value of
the difference of two matrices.
Z rotations
Ross–Selinger search takes the part of the unit disk within the supporting
line Re(exp(i theta/2) u) >= 1 − eps²/2. Its second embedding must also
lie in the unit disk. An enclosing ellipse and the conjugate disk are
reduced with the Ross–Selinger grid operators. The search enumerates
bounding-box lattice points in increasing denominator order, using powers
of 1 + √2 to balance each one-dimensional grid. It tests membership in
both disks exactly. Two-component floating-point arithmetic preserves the
thin cap and the translated intervals during enumeration.
Ross and Selinger, Sections 4–5 and Appendix A
solve_norm(xi) solves t†t = xi for a doubly nonnegative ZSqrt2.
It removes powers of √2, then factors the integer norm by trial division,
deterministic 64-bit Miller–Rabin and bounded Pollard rho. Primes congruent
to 1, 3 and 5 modulo 8 admit lifts via modular square roots and cyclotomic
GCDs. Each real prime above a prime congruent to 7 modulo 8 needs even
valuation. An even power of 1 + √2 corrects the residual unit, followed
by an exact check of the norm equation. After removing powers of √2, the
absolute integer norm must be at most 2^60. The input coefficients must
have magnitude at most 2^61.
Ross and Selinger, Section 6 and Appendix C
For each candidate u / √2^k, the norm solver supplies the other entry
from xi = 2^k − u†u. These two entries form a determinant-one unitary.
The result reports error using the exact norm
slack and the target angle, and cpu_error measured independently in
operator norm using both simulated columns. Both errors must fit the
requested tolerance with the synthesized global phase. The search accepts
finite tolerances of 1e-8 or greater, with denominators through 52 and a
bounded number of grid points. It can skip candidates whose norm could
not be factored within the budget, and returns SearchExhausted when its
search bounds are reached. The exact decomposition of each
selected unitary is T-optimal. Approximate T-counts typically scale as
3 log₂(1/eps) under the distribution assumptions in Ross and Selinger.
Ross and Selinger, Sections 7–8
Run jihoo run examples/gridsynth.jh for exact round-trips and the rotation
table for π/8, π/128 and 1.0 radians from 1e-3 through 1e-8. At
1e-8 the three runs use 80 T gates each and their measured operator errors
range from 6.3e-9 to 8.0e-9.