""" Threshold Network for Implication Gate A formally verified single-neuron threshold network computing material conditional. Weights are integer-constrained and activation uses the Heaviside step function. """ import torch from safetensors.torch import load_file class ThresholdImplies: """ Implication gate implemented as a threshold neuron. Circuit: output = (w1*x + w2*y + bias >= 0) With weights=[-1,1], bias=0: fails only when x=1 and y=0. """ def __init__(self, weights_dict): self.weight = weights_dict['weight'] self.bias = weights_dict['bias'] def __call__(self, x, y): inputs = torch.tensor([float(x), float(y)]) 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)) def forward(x, weights): """ Forward pass with Heaviside activation. Args: x: Input tensor of shape [..., 2] weights: Dict with 'weight' and 'bias' tensors Returns: Implies(x[0], x[1]) """ x = torch.as_tensor(x, dtype=torch.float32) weighted_sum = (x * weights['weight']).sum(dim=-1) + weights['bias'] return (weighted_sum >= 0).float() if __name__ == "__main__": weights = load_file("model.safetensors") model = ThresholdImplies(weights) print("Implication Gate Truth Table:") print("-" * 30) for x in [0, 1]: for y in [0, 1]: out = int(model(x, y).item()) expected = 1 if (x == 0 or y == 1) else 0 status = "OK" if out == expected else "FAIL" print(f"Implies({x}, {y}) = {out} [{status}]")