threshold-parity5 / model.py
CharlesCNorton
5-bit parity threshold circuit, magnitude 40
c371536
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def xor2(a, b, prefix, w):
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, weights):
xor_ab = xor2(a, b, 'xor_ab', weights)
xor_cd = xor2(c, d, 'xor_cd', weights)
xor_abcd = xor2(xor_ab, xor_cd, 'xor_abcd', weights)
return xor2(xor_abcd, e, 'xor_final', weights)
if __name__ == '__main__':
w = load_model()
print('parity5 selected outputs:')
for n_ones in range(6):
bits = [1 if j < n_ones else 0 for j in range(5)]
print(f' {n_ones} ones: {bits} -> {parity5(*bits, w)}')