Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
4-bit population count. Counts the number of 1 bits in a 4-bit input.
popcount4(a, b, c, d) = binary count of 1 bits
Output [y2, y1, y0] is the 3-bit binary representation of the count (0-4).
| a | b | c | d | count | y2 | y1 | y0 |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 | 2 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 | 3 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 4 | 1 | 0 | 0 |
a b c d
| | | |
+----+---+---+---+----+
| |
Layer 1: |
y2 = (sum >= 4) |
ge2 = (sum >= 2) |
le3 = (sum <= 3) |
XOR(a,b) components |
XOR(c,d) components |
| |
Layer 2: |
y1 = AND(ge2, le3) |
xor_ab = XOR(a,b) |
xor_cd = XOR(c,d) |
| |
Layer 3: |
XOR(xor_ab, xor_cd) |
components |
| |
Layer 4: |
y0 = XOR(xor_ab, xor_cd)
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 13 |
| Layers | 4 |
| Parameters | 53 |
| Magnitude | 55 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
# See model.py for full implementation
# popcount4(1, 0, 1, 1) returns [0, 1, 1] (count=3 = binary 011)
MIT