Phase polynomials and T-count optimization

std.tcount is an explicit import for optimizing std.quantum.Circuit recordings. The optimizer emits an executable circuit on the same wires. It counts both T and T† gates.

import std.quantum as q
import std.tcount as tc

fn main() {
    var poly = tc.PhasePoly.new(4)
    for parity in 1..16 {
        poly.add_phase(parity as u64, 1)
    }
    let original = poly.to_circuit()
    let optimized = tc.optimize(original).unwrap()
    assert(tc.t_count(original) == 15 && tc.t_count(optimized) == 0, "T-count")
    let sim = q.CpuSim.run(move optimized, 0)
    assert(sim.probability(0) == 1.0, "identity")
}

Exact phases and synthesis

On computational basis input x, a PhasePoly applies exp(i π P(x) / 4) and outputs the basis label Ax. Each row of A and each input parity is a u64 mask with wire zero in the low bit. The coefficients of P are taken modulo eight. A CNOT XORs the control row into the target row. A phase gate adds its coefficient to the parity currently held on its wire.

Use PhasePoly.new(n) for the identity, then build a block with cnot, t, tdg, s, sdg, z, cz and ccz. Use add_phase to update a parity of the block’s input wires directly. phase(x) evaluates the integer exponent modulo eight, output(x) evaluates Ax, and linear_rows() exposes the map. PhasePoly.from_circuit(c) extracts a CNOT and diagonal region. to_circuit() visits nonzero parities in Gray-code order within each target wire’s group, then synthesizes A by reversing binary Gaussian elimination.

signature() returns the degree-one, degree-two and degree-three entries of the symmetric binary signature tensor. Each entry is the parity of the number of odd-coefficient masks containing every wire in the corresponding tuple. Each vector is in lexicographic wire-tuple order. Two phase functions with the same signature differ by a diagonal Clifford phase. Synthesis restores that difference using Möbius coefficients on singleton and two-wire basis inputs. The singleton coefficients are even, the pair coefficients are multiples of four, and the matching cubic signature cancels the cubic residual.

Decoders

decode_rm_exact(poly) searches the complete syndrome coset on at most five wires. PuncturedRM.new(r, n) evaluates all degree-at-most-r Boolean monomials on the nonzero inputs, including the constant monomial for r >= 0. Generator entries are packed in u64 words. Bit j % 64 of word j / 64 is the evaluation at input j + 1. signature_matrix(n) uses the same packing for the parity checks, and is_signature_kernel(code) checks their orthogonality and the rank–dimension relation.

Wires Length Dimension of RM(n − 4, n)* Syndrome rank Minimum distance
3 7 0 7 —
4 15 1 14 15
5 31 6 25 15
6 63 22 41 15

For 0 <= r < n, puncturing keeps all generators independent, so the dimension is Σ C(n, d) over 0 <= d <= r. The minimum distance is 2^(n-r) - 1. The zero code has no nonzero word, so its distance is None. In particular the five-wire decoder tests 64 codewords, and the three-wire CCZ phase block has a minimum of seven T gates. The four-wire all-ones word removes a 15-T identity. Amy and Mosca, T-count optimization and Reed–Muller codes

decode_todd(poly) works on sparse parity masks up to the 64-wire limit of Circuit. It searches column pairs, solves the binary kernel of the linear and χ constraints, and cancels duplicates. An odd-weight kernel solution gets one extra column from the zero parity. The search restarts after each reduction and terminates when no pair admits an update. TODD is a heuristic for overall T-count. The constraints and phase correction are exact. Heyfron and Campbell, An efficient quantum compiler that reduces T count

Circuits and benchmarks

SegmentedCircuit builds or extracts successive phase blocks separated by H and X, with a toffoli builder for H–CCZ–H. Its phase_block() method returns a mutable reference to the current block. The top-level optimize(c) also commutes Hadamards through compatible gates, recognizes expanded Toffolis, and pairs relative phases across Toffolis whose control bits stay fixed. It uses exact coset decoding through five wires and TODD on larger blocks. It emits diagonal Clifford corrections directly as S, S†, Z and CZ gates. fold(c) runs the phase folding without decoding, and decompose(c) expands the input gates individually to establish a T-count baseline.

Extraction accepts the standard H, X, Y, Z, S, S†, T, T†, CNOT, CZ and Toffoli matrices, as well as CCZ recorded as a positive doubly controlled Z. Unsupported matrices and recordings with clean-release checkpoints return an error.

Run jihoo run examples/tcount.jh to measure the counts on emitted circuits. The example uses CpuSim.run_from_state to compare the naive and optimized circuits to the original on every basis input and on a complex superposition, using one global phase per comparison. It also checks the two-bit Cuccaro adder’s truth table.

Circuit Naive T-count Optimized T-count
CCZ 7 7
15-T identity 15 0
mod5_4 28 7
barenco_tof_3 28 22
tof_3 21 15
Two-bit Cuccaro adder 28 16

Phase folding alone gives eight T gates for mod5_4. The exact decoder finds a seven-T representative of the folded block. Amy and Mosca’s Table II reports 16 T gates for this benchmark. The Barenco and tof_3 counts match the Hadamard-partitioned results in Heyfron and Campbell’s Table A1. Amy and Mosca · Heyfron and Campbell · T-par benchmark circuits · Cuccaro et al.

Esc