Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
4-bit parity function. Outputs 1 if odd number of inputs are high.
parity4(a, b, c, d) = a XOR b XOR c XOR d
Tree structure: parity(a,b,c,d) = XOR(XOR(a,b), XOR(c,d))
Three XOR2 gates, each using OR-NAND-AND structure:
Layer structure:
| Inputs | 4 |
| Outputs | 1 |
| Neurons | 9 |
| Layers | 4 |
| Parameters | 27 |
| Magnitude | 30 |
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 parity4(a, b, c, d):
return xor2(xor2(a, b, 'xor_ab'), xor2(c, d, 'xor_cd'), 'xor_final')
print(parity4(1, 0, 1, 0)) # 0 (even)
print(parity4(1, 1, 1, 0)) # 1 (odd)
MIT