Documentation

CslibFrontier.Computability.Circuit.Boolean.PopulationCount

Linear-size population counting and thresholds #

The word width at a node of a balanced addition tree is the height of that node plus one. Consequently, the existing quadratic-size adder suffices for a linear-size population counter: there are exponentially fewer nodes at increasing heights. All gates, including constants, are charged. The input is padded with zeros to a power of two, so the construction also handles an empty input.

Main definitions #

Main results #

References #

Binary addition and threshold circuits are standard; see Vollmer, Chapter 1. The concrete adders and their source attribution are in Boolean.CarryLookahead.

Add the counts of the two half blocks, retaining the final carry. A height-h tree has 2^h inputs and h+1 output bits.

The output word is the exact number of true inputs, including the full carry.

The increasing word widths cost only a constant per leaf in total.

One shared false gate extends a word with zeros.

@[simp]

Padding agrees with the standard zero extension of a bit string.

@[simp]

Zero padding charges exactly one gate.

A population count on n inputs. Padding to the next power of two uses one shared false gate, and the balanced tree returns ⌈log₂(n+1)⌉+1 bits.

The circuit computes the exact binary numeral of the input's Hamming weight.

A uniform linear gate bound, with constants and padding included.

Compare the population count with a cutoff. An impossible cutoff returns false.

@[simp]

The linear-size threshold circuit accepts precisely when at least q inputs are true.

Linear population counting followed by comparison remains linear in the input count.