Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
Exactly-4-out-of-8 detector. Fires when precisely half the inputs are active. The tie detector.
xβ xβ xβ xβ xβ xβ
xβ xβ
β β β β β β β β
ββββ΄βββ΄βββ΄βββΌβββ΄βββ΄βββ΄βββ
β
βββββββββ΄ββββββββ
βΌ βΌ
βββββββββββ βββββββββββ
β β₯ 4 β β β€ 4 β
β b = -4 β β b = +4 β
βββββββββββ βββββββββββ
β β
βββββββββ¬ββββββββ
βΌ
βββββββββββ
β AND β
βββββββββββ
β
βΌ
tie?
HW = 4 is special:
This circuit fires on 70 of 256 inputs - the mode of the distribution.
| Circuit | Condition | Fires at HW=4? |
|---|---|---|
| Majority | HW β₯ 5 | No |
| Minority | HW β€ 3 | No |
| Exactly4 | HW = 4 | Yes |
Exactly4 catches what both Majority and Minority miss. It's the gap between them - the deadlock.
AtLeast4: Sum all inputs with weight +1, subtract 4.
xβ + xβ + xβ + xβ + xβ + xβ
+ xβ + xβ - 4 β₯ 0
Fires when 4 or more inputs are active.
AtMost4: Sum all inputs with weight -1, add 4.
-xβ - xβ - xβ - xβ - xβ - xβ
- xβ - xβ + 4 β₯ 0
Fires when 4 or fewer inputs are active.
Their intersection is exactly 4.
| HW | C(8,k) | Exactly4? |
|---|---|---|
| 0 | 1 | - |
| 1 | 8 | - |
| 2 | 28 | - |
| 3 | 56 | - |
| 4 | 70 | YES |
| 5 | 56 | - |
| 6 | 28 | - |
| 7 | 8 | - |
| 8 | 1 | - |
The distribution is symmetric around 4. This circuit sits at the apex.
| Component | Weights | Bias |
|---|---|---|
| AtLeast4 | all +1 | -4 |
| AtMost4 | all -1 | +4 |
| AND | [+1, +1] | -2 |
Total: 3 neurons, 21 parameters, 2 layers
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def exactly4(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)
# Balanced: alternating pattern
bits = [1, 0, 1, 0, 1, 0, 1, 0]
print(exactly4(bits)) # 1
# Majority (5) - not a tie
bits = [1, 1, 1, 0, 1, 0, 1, 0]
print(exactly4(bits)) # 0
threshold-exactly4outof8/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT