Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
A 4-of-4 threshold gate. All four inputs must be active to reach the firing threshold.
x1 x2 x3 x4
β β β β
βββββ΄ββββ΄ββββ
β
βΌ
βββββββββββ
βw: 1,1,1,1β
β b: -4 β
βββββββββββ
β
βΌ
AND4(x1,x2,x3,x4)
Each input contributes +1 to the sum. The bias of -4 means exactly four contributions are required to reach zero:
| x1 | x2 | x3 | x4 | sum | output |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | -4 | 0 |
| 0 | 0 | 0 | 1 | -3 | 0 |
| ... | ... | ... | ... | ... | 0 |
| 1 | 1 | 1 | 0 | -1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 1 |
Only the all-ones input reaches the threshold.
| Weights | [1, 1, 1, 1] |
| Bias | -4 |
| Magnitude | 8 |
| Total | 5 parameters |
This circuit is at minimum magnitude (8). The pattern generalizes: n-input AND requires weights all 1 and bias -n, giving magnitude 2n.
| Gate | Weights | Bias | Magnitude |
|---|---|---|---|
| AND2 | [1, 1] | -2 | 4 |
| AND3 | [1, 1, 1] | -3 | 6 |
| AND4 | [1, 1, 1, 1] | -4 | 8 |
| ANDn | [1, ..., 1] | -n | 2n |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def and4_gate(x1, x2, x3, x4):
inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
return int((inputs * w['weight']).sum() + w['bias'] >= 0)
# Test
assert and4_gate(1, 1, 1, 1) == 1
assert and4_gate(1, 1, 1, 0) == 0
assert and4_gate(0, 0, 0, 0) == 0
threshold-and4/
βββ model.safetensors
βββ model.py
βββ config.json
βββ create_safetensors.py
βββ README.md
MIT