Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks. • 270 items • Updated • 1
Exactly-1-out-of-8 detector. Fires when exactly one input is active.
x₀ x₁ x₂ x₃ x₄ x₅ x₆ x₇
│ │ │ │ │ │ │ │
└──┴──┴──┴──┼──┴──┴──┴──┘
│
┌───────┴───────┐
▼ ▼
┌─────────┐ ┌─────────┐
│ AtLeast1│ │ AtMost1 │
│ w: +1×8 │ │ w: -1×8 │
│ b: -1 │ │ b: +1 │
└─────────┘ └─────────┘
│ │
└───────┬───────┘
▼
┌─────────┐
│ AND │
│ w: 1, 1 │
│ b: -2 │
└─────────┘
│
▼
(HW = 1?)
The circuit uses two threshold neurons in parallel:
AtLeast1: Fires when HW ≥ 1 (at least one input active)
AtMost1: Fires when HW ≤ 1 (at most one input active)
AND: Combines the two conditions
| HW | AtLeast1 | AtMost1 | Exactly1 |
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 1 | 0 | 0 |
| 3 | 1 | 0 | 0 |
| ... | 1 | 0 | 0 |
| 8 | 1 | 0 | 0 |
| Circuit | AtLeast bias | AtMost bias |
|---|---|---|
| Exactly1 | -1 | +1 |
| Exactly2 | -2 | +2 |
| Exactly3 | -3 | +3 |
| ... | -k | +k |
| Exactly7 | -7 | +7 |
All use the same structure: two threshold detectors + AND.
| Component | Neurons | Parameters |
|---|---|---|
| AtLeast1 | 1 | 9 |
| AtMost1 | 1 | 9 |
| AND | 1 | 3 |
| Total | 3 | 21 |
Layers: 2
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def exactly1(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)
return int((torch.tensor([float(atleast), float(atmost)]) * w['and.weight']).sum() + w['and.bias'] >= 0)
bits = [0, 0, 0, 1, 0, 0, 0, 0] # HW=1
print(exactly1(bits)) # 1
threshold-exactly1outof8/
├── model.safetensors
├── model.py
├── config.json
└── README.md
MIT