Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks. β’ 269 items β’ Updated
β’ 1
6-bit parity function. Outputs 1 if an odd number of inputs are high. Fundamental building block for error detection codes.
x0 x1 x2 x3 x4 x5
β β β β β β
ββββ¬ββ ββββ¬ββ ββββ¬ββ
β β β
βΌ βΌ βΌ
βββββββ βββββββ βββββββ
βXOR01β βXOR23β βXOR45β Layer 1-2
βββββββ βββββββ βββββββ
β β β
βββββββ¬ββββββ β
β β
βΌ β
βββββββ β
βXOR β β Layer 3-4
β0123 β β
βββββββ β
β β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββ
β XOR β Layer 5-6
β final β
βββββββββ
β
βΌ
parity
parity6(x0, x1, x2, x3, x4, x5) = x0 XOR x1 XOR x2 XOR x3 XOR x4 XOR x5
Returns 1 when the Hamming weight is odd (1, 3, or 5).
| HW | Example Input | Parity |
|---|---|---|
| 0 | 000000 | 0 |
| 1 | 000001 | 1 |
| 2 | 000011 | 0 |
| 3 | 000111 | 1 |
| 4 | 001111 | 0 |
| 5 | 011111 | 1 |
| 6 | 111111 | 0 |
Pattern: Parity = HW mod 2
Parity is computed using a tree of XOR gates. Each XOR gate is implemented as:
XOR(a, b) = AND(OR(a,b), NAND(a,b))
XOR Implementation (3 neurons per gate):
| Gate | Weights | Bias | Function |
|---|---|---|---|
| OR | [1, 1] | -1 | a + b >= 1 |
| NAND | [-1, -1] | +1 | -(a + b) + 1 >= 0 |
| AND | [1, 1] | -2 | OR + NAND >= 2 |
Tree Structure:
| Component | Neurons | Layers |
|---|---|---|
| XOR01 | 3 | 2 |
| XOR23 | 3 | 2 |
| XOR45 | 3 | 2 |
| XOR0123 | 3 | 2 |
| XOR_final | 3 | 2 |
Total: 15 neurons across 5 XOR gates
Note: Effective depth is 6 layers (3 XOR stages Γ 2 layers each), but XOR01, XOR23, XOR45 execute in parallel.
| Inputs | 6 |
| Outputs | 1 |
| Neurons | 15 |
| Layers | 6 |
| Parameters | 45 |
| Magnitude | 50 |
| Circuit | Inputs | XOR Gates | Neurons | Depth |
|---|---|---|---|---|
| parity3 | 3 | 2 | 6 | 4 |
| parity4 | 4 | 3 | 9 | 4 |
| parity5 | 5 | 4 | 12 | 6 |
| parity6 | 6 | 5 | 15 | 6 |
| parity7 | 7 | 6 | 18 | 6 |
| parity8 | 8 | 7 | 21 | 6 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def xor2(a, b, prefix):
or_out = int(a * w[f'{prefix}.or.weight'][0] + b * w[f'{prefix}.or.weight'][1] + w[f'{prefix}.or.bias'] >= 0)
nand_out = int(a * w[f'{prefix}.nand.weight'][0] + b * w[f'{prefix}.nand.weight'][1] + w[f'{prefix}.nand.bias'] >= 0)
return int(or_out * w[f'{prefix}.and.weight'][0] + nand_out * w[f'{prefix}.and.weight'][1] + w[f'{prefix}.and.bias'] >= 0)
def parity6(x0, x1, x2, x3, x4, x5):
xor01 = xor2(x0, x1, 'xor_01')
xor23 = xor2(x2, x3, 'xor_23')
xor45 = xor2(x4, x5, 'xor_45')
xor0123 = xor2(xor01, xor23, 'xor_0123')
return xor2(xor0123, xor45, 'xor_final')
# Test
print(parity6(1, 0, 1, 0, 1, 0)) # 1 (odd: 3 ones)
print(parity6(1, 1, 1, 1, 0, 0)) # 0 (even: 4 ones)
print(parity6(0, 0, 0, 0, 0, 1)) # 1 (odd: 1 one)
threshold-parity6/
βββ model.safetensors
βββ model.py
βββ create_safetensors.py
βββ config.json
βββ README.md
MIT