Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
5-bit parity function. Outputs 1 if odd number of inputs are high.
parity5(a, b, c, d, e) = a XOR b XOR c XOR d XOR e
Hybrid tree-cascade: parity5 = XOR(XOR(XOR(a,b), XOR(c,d)), e)
Four XOR2 gates:
| Inputs | 5 |
| Outputs | 1 |
| Neurons | 12 |
| Layers | 6 |
| Parameters | 36 |
| Magnitude | 40 |
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 parity5(a, b, c, d, e):
xor_ab = xor2(a, b, 'xor_ab')
xor_cd = xor2(c, d, 'xor_cd')
xor_abcd = xor2(xor_ab, xor_cd, 'xor_abcd')
return xor2(xor_abcd, e, 'xor_final')
print(parity5(1, 0, 1, 0, 1)) # 1 (odd)
print(parity5(1, 1, 1, 1, 0)) # 0 (even)
MIT