Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
8-bit parity function. Outputs 1 if odd number of inputs are high.
parity8(b0..b7) = b0 XOR b1 XOR b2 XOR b3 XOR b4 XOR b5 XOR b6 XOR b7
Balanced tree of 7 XOR2 gates:
b0 b1 b2 b3 b4 b5 b6 b7
\/ \/ \/ \/
xor01 xor23 xor45 xor67 (Level 1: 4 gates)
\ / \ /
xor0123 xor4567 (Level 2: 2 gates)
\ /
xor_final (Level 3: 1 gate)
Each XOR2 uses OR-NAND-AND structure (3 neurons, 9 params, magnitude 10).
| Inputs | 8 |
| Outputs | 1 |
| Neurons | 21 |
| Layers | 6 |
| Parameters | 63 |
| Magnitude | 70 |
from safetensors.torch import load_file
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 parity8(bits):
x01 = xor2(bits[0], bits[1], 'xor_01')
x23 = xor2(bits[2], bits[3], 'xor_23')
x45 = xor2(bits[4], bits[5], 'xor_45')
x67 = xor2(bits[6], bits[7], 'xor_67')
x0123 = xor2(x01, x23, 'xor_0123')
x4567 = xor2(x45, x67, 'xor_4567')
return xor2(x0123, x4567, 'xor_final')
print(parity8([1, 0, 1, 0, 1, 0, 1, 0])) # 0 (even)
print(parity8([1, 1, 1, 0, 0, 0, 0, 0])) # 1 (odd)
MIT