threshold-nand / README.md
CharlesCNorton
Add optimality note: exhaustive enumeration confirms magnitude 3 is minimum
57725cf
metadata
license: mit
tags:
  - pytorch
  - safetensors
  - threshold-logic
  - neuromorphic
  - functionally-complete

threshold-nand

The universal gate. Any Boolean function can be constructed from NAND alone.

Circuit

    x   y
    β”‚   β”‚
    β””β”€β”¬β”€β”˜
      β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚w: -1,-1β”‚
  β”‚ b: +1  β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
  NAND(x,y)

Mechanism

Negative weights mean inputs subtract from the sum. The positive bias starts us above threshold, and inputs pull us down:

x y sum output
0 0 +1 1
0 1 0 1
1 0 0 1
1 1 -1 0

Only when both inputs are active do we fall below threshold. This is AND with inverted output.

Parameters

Weights [-1, -1]
Bias +1
Total 3 parameters

Optimality

Exhaustive enumeration of all 25 weight configurations at magnitudes 0-3 confirms this circuit is already at minimum magnitude (3). There is exactly one valid configuration at magnitude 3, and no valid configurations exist below it.

Functional Completeness

NAND can build any Boolean function:

  • NOT(x) = NAND(x, x)
  • AND(x,y) = NOT(NAND(x,y)) = NAND(NAND(x,y), NAND(x,y))
  • OR(x,y) = NAND(NOT(x), NOT(y))

This is why NAND gates dominate digital logic fabrication.

Usage

from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def nand_gate(x, y):
    inputs = torch.tensor([float(x), float(y)])
    return int((inputs * w['weight']).sum() + w['bias'] >= 0)

Files

threshold-nand/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ config.json
└── README.md

License

MIT