threshold-demux8 / model.py
CharlesCNorton
1:8 demultiplexer threshold circuit, magnitude 52
86ab3ed
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def demux8(d, s2, s1, s0, weights):
"""1:8 Demultiplexer: routes d to output y[s] where s = 4*s2 + 2*s1 + s0"""
inp = torch.tensor([float(d), float(s2), float(s1), float(s0)])
outputs = []
for i in range(8):
y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
outputs.append(y)
return outputs
if __name__ == '__main__':
w = load_model()
print('DEMUX8 verification:')
for s in range(8):
s2, s1, s0 = (s >> 2) & 1, (s >> 1) & 1, s & 1
result = demux8(1, s2, s1, s0, w)
print(f' d=1, s={s} ({s2}{s1}{s0}) -> {"".join(map(str, result))}')