| """
|
| Threshold Network for 6-out-of-8 Gate
|
|
|
| A formally verified single-neuron threshold network.
|
| Outputs 1 when at least 6 of the 8 inputs are true.
|
| """
|
|
|
| import torch
|
| from safetensors.torch import load_file
|
|
|
|
|
| class Threshold6OutOf8:
|
| """
|
| 6-out-of-8 threshold gate.
|
|
|
| Circuit: output = (sum of inputs - 6 >= 0)
|
| Fires when hamming weight >= 6.
|
| """
|
|
|
| def __init__(self, weights_dict):
|
| self.weight = weights_dict['weight']
|
| self.bias = weights_dict['bias']
|
|
|
| def __call__(self, bits):
|
| inputs = torch.tensor([float(b) for b in bits])
|
| weighted_sum = (inputs * self.weight).sum() + self.bias
|
| return (weighted_sum >= 0).float()
|
|
|
| @classmethod
|
| def from_safetensors(cls, path="model.safetensors"):
|
| return cls(load_file(path))
|
|
|
|
|
| if __name__ == "__main__":
|
| weights = load_file("model.safetensors")
|
| model = Threshold6OutOf8(weights)
|
|
|
| print("6-out-of-8 Gate Tests:")
|
| print("-" * 35)
|
| for hw in range(9):
|
| bits = [1]*hw + [0]*(8-hw)
|
| out = int(model(bits).item())
|
| expected = 1 if hw >= 6 else 0
|
| status = "OK" if out == expected else "FAIL"
|
| print(f"HW={hw}: {out} [{status}]")
|
|
|