Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
4-bit parallel prefix XOR (running parity). Computes cumulative XOR from MSB to each position. Essential for parity-based error detection.
x3 x2 x1 x0
β β β β
βΌ β β β
βββββ β β β
βy3 β β β β
β=x3β β β β
βββββ βΌ β β
β βββββββ β β
ββββββΊβ XOR β β β
β y2 β β β
βββββββ βΌ β
β βββββββ β
ββββββΊβ XOR β β
β y1 β β
βββββββ βΌ
β βββββββ
ββββββΊβ XOR β
β y0 β
βββββββ
prefix_xor(x3, x2, x1, x0) -> (y3, y2, y1, y0)
y3 = x3
y2 = x3 XOR x2
y1 = x3 XOR x2 XOR x1
y0 = x3 XOR x2 XOR x1 XOR x0 (full parity)
Each output yi is the XOR (parity) of all inputs from x3 down to xi.
| x3 x2 x1 x0 | y3 y2 y1 y0 | y0 = parity |
|---|---|---|
| 0 0 0 0 | 0 0 0 0 | even (0) |
| 0 0 0 1 | 0 0 0 1 | odd (1) |
| 0 0 1 1 | 0 0 1 0 | even (0) |
| 0 1 1 1 | 0 1 0 1 | odd (1) |
| 1 1 1 1 | 1 0 1 0 | even (0) |
| 1 0 1 0 | 1 1 0 0 | even (0) |
| 1 1 0 0 | 1 0 0 0 | even (0) |
Unlike prefix-AND and prefix-OR, prefix-XOR requires sequential XOR gates because XOR is not a simple threshold function.
Architecture: Chain of 3 XOR gates (each XOR = 3 neurons)
| Stage | Computes | Neurons |
|---|---|---|
| 1 | y3 = x3 (passthrough) | 0 |
| 2 | y2 = y3 XOR x2 | 3 |
| 3 | y1 = y2 XOR x1 | 3 |
| 4 | y0 = y1 XOR x0 | 3 |
| Inputs | 4 |
| Outputs | 4 |
| Neurons | 9 |
| Layers | 6 |
| Parameters | 27 |
| Magnitude | 30 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def xor2(a, b, prefix):
inp = torch.tensor([float(a), float(b)])
or_out = int((inp @ w[f'{prefix}.or.weight'].T + w[f'{prefix}.or.bias'] >= 0).item())
nand_out = int((inp @ w[f'{prefix}.nand.weight'].T + w[f'{prefix}.nand.bias'] >= 0).item())
l1 = torch.tensor([float(or_out), float(nand_out)])
return int((l1 @ w[f'{prefix}.and.weight'].T + w[f'{prefix}.and.bias'] >= 0).item())
def prefix_xor(x3, x2, x1, x0):
y3 = x3
y2 = xor2(y3, x2, 'xor2')
y1 = xor2(y2, x1, 'xor1')
y0 = xor2(y1, x0, 'xor0')
return y3, y2, y1, y0
print(prefix_xor(1, 1, 1, 1)) # (1, 0, 1, 0) - even parity
print(prefix_xor(1, 0, 0, 0)) # (1, 1, 1, 1) - odd parity
MIT