Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
Exactly-3-out-of-8 detector. Fires when precisely three inputs are active. The triple detector.
xβ xβ xβ xβ xβ xβ
xβ xβ
β β β β β β β β
ββββ΄βββ΄βββ΄βββΌβββ΄βββ΄βββ΄βββ
β
βββββββββ΄ββββββββ
βΌ βΌ
βββββββββββ βββββββββββ
β β₯ 3 β β β€ 3 β
β b = -3 β β b = +3 β
βββββββββββ βββββββββββ
β β
βββββββββ¬ββββββββ
βΌ
βββββββββββ
β AND β
βββββββββββ
β
βΌ
exactly 3?
Three is the smallest odd number greater than one. In voting:
There are C(8,3) = 56 input patterns that fire this circuit - exactly double the pairs.
The circuit squeezes Hamming weight between two bounds:
Lower bound (AtLeast3): Upper bound (AtMost3):
+xβ + xβ + ... + xβ -xβ - xβ - ... - xβ
βββββββββββββββββββββ βββββββββββββββββββββ
-3 +3
fires if HW β₯ 3 fires if HW β€ 3
The AND gate demands both conditions simultaneously. Only HW = 3 satisfies both.
| HW | Count | Exactly3 |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 8 | 0 |
| 2 | 28 | 0 |
| 3 | 56 | 1 |
| 4 | 70 | 0 |
| 5 | 56 | 0 |
| 6 | 28 | 0 |
| 7 | 8 | 0 |
| 8 | 1 | 0 |
The binomial distribution peaks at HW=4, but HW=3 and HW=5 are tied at 56 patterns each.
| Component | Weights | Bias |
|---|---|---|
| AtLeast3 | all +1 | -3 |
| AtMost3 | all -1 | +3 |
| AND | [+1, +1] | -2 |
Total: 3 neurons, 21 parameters, 2 layers
Exactly3 and Exactly5 are complementary around the center:
| Circuit | Fires on | Count | Complement |
|---|---|---|---|
| Exactly3 | HW = 3 | 56 | HW = 5 |
| Exactly5 | HW = 5 | 56 | HW = 3 |
If you flip all 8 bits, an Exactly3 input becomes Exactly5, and vice versa.
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def exactly3(bits):
inp = torch.tensor([float(b) for b in bits])
atleast = int((inp * w['atleast.weight']).sum() + w['atleast.bias'] >= 0)
atmost = int((inp * w['atmost.weight']).sum() + w['atmost.bias'] >= 0)
comb = torch.tensor([float(atleast), float(atmost)])
return int((comb * w['and.weight']).sum() + w['and.bias'] >= 0)
# Triangle: positions 0, 3, 7 active
bits = [1, 0, 0, 1, 0, 0, 0, 1]
print(exactly3(bits)) # 1
threshold-exactly3outof8/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT