Documentation

CslibFrontier.Computability.Circuit.Boolean.ApproximateCount

Correcting overlapping approximate counts #

This module proves the arithmetic correction step in the proof of Lemma 1 of [Ser25b], §3, pp. 12–13, and correctness of an explicit sequential reconstruction from independently rounded high parts. Each supplied approximation is either the true high part or its successor. Comparing two overlap bits determines whether to borrow from the previous high part or replace the new low block by the largest digit.

The arithmetic is stated for any positive radix; the paper uses radix 2 ^ s, with s ≥ 2. The reconstruction computes from the supplied approximations alone: it does not inspect the true value appearing in the correctness theorem. These are steps toward the paper's approximate counting construction, not its parallel per-block depth bound. The remaining construction and depth estimates are tracked in issue #7708.

Main definitions #

Main results #

References #

One low radix digit and a borrow to apply to the already reconstructed high part.

  • digit : ℕ

    The low digit to append.

  • borrow : Bool

    Whether the high part must first be decreased by one.

Instances For

    Append a corrected low digit to a high part, using natural subtraction for the borrow.

    The arithmetic rule in Sergeev's proof of Lemma 1: compare the residues modulo four of high and estimate / radix. Borrow when the former is one ahead; replace the low digit by radix - 1 when the latter is one ahead.

    A native De Morgan formula testing whether the two-bit word (a₁,a₀) is the successor modulo four of (b₁,b₀). It computes the borrow bit of the overlap correction.

    @[simp]
    theorem Cslib.Circuits.Boolean.ApproximateCount.eval_borrowFormula {n : ℕ} (a₀ a₁ b₀ b₁ : Fin n) (x : BitString n) :
    (borrowFormula a₀ a₁ b₀ b₁).eval interpretation x = decide ((x a₀).toNat + 2 * (x a₁).toNat = ((x b₀).toNat + 2 * (x b₁).toNat + 1) % 4)

    Boolean semantics of the overlap comparison. The word order is low bit first.

    @[simp]
    theorem Cslib.Circuits.Boolean.ApproximateCount.size_borrowFormula {n : ℕ} (a₀ a₁ b₀ b₁ : Fin n) :
    (borrowFormula a₀ a₁ b₀ b₁).size = 22

    The correction-bit formula has twenty-two De Morgan operation nodes.

    theorem Cslib.Circuits.Boolean.ApproximateCount.depth_borrowFormula_le {n : ℕ} (a₀ a₁ b₀ b₁ : Fin n) :
    (borrowFormula a₀ a₁ b₀ b₁).depth ≤ 8

    The correction-bit formula has depth at most eight, independently of the block width.

    theorem Cslib.Circuits.Boolean.ApproximateCount.eval_borrowFormula_overlap (radix high estimate : ℕ) :
    (borrowFormula 0 1 2 3).eval interpretation ![high.testBit 0, high.testBit 1, (estimate / radix).testBit 0, (estimate / radix).testBit 1] = (correctBlock radix high estimate).borrow

    Feeding the actual two-bit overlaps to the native formula computes the borrow selected by correctBlock; this links the Boolean realization to the arithmetic correction.

    theorem Cslib.Circuits.Boolean.ApproximateCount.digit_correctBlock_lt {radix : ℕ} (hr : 0 < radix) (high estimate : ℕ) :
    (correctBlock radix high estimate).digit < radix

    The corrected low digit is a radix digit, even without the approximation invariant.

    theorem Cslib.Circuits.Boolean.ApproximateCount.extend_correctBlock_of_rounding {radix value high estimate : ℕ} (hr : 0 < radix) (hhigh : high = value / radix ∨ high = value / radix + 1) (hestimate : estimate = value ∨ estimate = value + 1) :
    (correctBlock radix high estimate).extend radix high = value ∨ (correctBlock radix high estimate).extend radix high = value + 1

    An arithmetic step in Sergeev's approximate-count construction, not its depth bound. If high approximates value / radix and estimate approximates value, both by rounding up by at most one, the explicitly corrected extension still equals value or value + 1. No approximation or correction is selected by the conclusion.

    Reconstruct the low value from levels + 1 rounded high-part estimates. estimate i approximates the value divided by radix ^ i; the highest estimate initializes the recursion. The algorithm has no access to the true value.

    theorem Cslib.Circuits.Boolean.ApproximateCount.reconstruct_of_rounding {radix : ℕ} (hr : 0 < radix) (levels value : ℕ) (estimate : ℕ → ℕ) (hestimate : ∀ i ≤ levels, estimate i = value / radix ^ i ∨ estimate i = value / radix ^ i + 1) :
    reconstruct radix levels estimate = value ∨ reconstruct radix levels estimate = value + 1

    Sequential correctness of the block reconstruction, an arithmetic step toward Sergeev's Lemma 1. This does not assert that the sequential reconstruction has the paper's parallel depth. Every approximation used by the algorithm is constrained, including the initial high part.