Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
Exactly-2-out-of-8 detector. Fires when precisely two inputs are active. The pair detector.
xβ xβ xβ xβ xβ xβ
xβ xβ
β β β β β β β β
ββββ΄βββ΄βββ΄βββΌβββ΄βββ΄βββ΄βββ
β
βββββββββ΄ββββββββ
βΌ βΌ
βββββββββββ βββββββββββ
β AtLeast2β β AtMost2 β
β HW β₯ 2 β β HW β€ 2 β
βββββββββββ βββββββββββ
β β
βββββββββ¬ββββββββ
βΌ
βββββββββββ
β AND β
βββββββββββ
β
βΌ
(HW = 2?)
Detecting exactly two active inputs is useful in:
There are C(8,2) = 28 input patterns that fire this circuit.
Two threshold neurons work in concert:
AtLeast2 (lower bound):
sum = xβ + xβ + ... + xβ - 2
fires when sum β₯ 0, i.e., HW β₯ 2
AtMost2 (upper bound):
sum = -xβ - xβ - ... - xβ + 2
fires when sum β₯ 0, i.e., -HW + 2 β₯ 0, i.e., HW β€ 2
The AND gate computes the intersection: HW β₯ 2 AND HW β€ 2 = HW = 2.
| HW | AtLeast2 | AtMost2 | Output |
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 2 | 1 | 1 | 1 |
| 3 | 1 | 0 | 0 |
| 4+ | 1 | 0 | 0 |
| Component | Weights | Bias |
|---|---|---|
| AtLeast2 | [+1, +1, +1, +1, +1, +1, +1, +1] | -2 |
| AtMost2 | [-1, -1, -1, -1, -1, -1, -1, -1] | +2 |
| AND | [+1, +1] | -2 |
Total: 3 neurons, 21 parameters, 2 layers
| Circuit | Condition | Fires on HW |
|---|---|---|
| 2-out-of-8 | HW β₯ 2 | 2, 3, 4, 5, 6, 7, 8 |
| Exactly-2 | HW = 2 | 2 only |
The existing threshold-2outof8 fires on 247 of 256 inputs. This circuit fires on only 28.
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def exactly2(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)
# Pair: positions 2 and 5 active
bits = [0, 0, 1, 0, 0, 1, 0, 0]
print(exactly2(bits)) # 1
# Three active - not a pair
bits = [1, 0, 1, 0, 0, 1, 0, 0]
print(exactly2(bits)) # 0
threshold-exactly2outof8/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT