Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
Minimum-magnitude threshold circuit for XNOR (equivalence). Magnitude 7 is optimal via exhaustive enumeration of all 224,143 configurations.
x1 x2
β β
β β
ββββββ΄ββββββββ΄βββββ
β β
βΌ βΌ
βββββββ βββββββ
β N1 β β N2 β Layer 1
β[-1,-1] β[-1,-1]
β b=0 β β b=1 β
ββββ¬βββ ββββ¬βββ
β β
β ββββββββββββββ
β β
βΌ βΌ
ββββββββ
β OUT β Layer 2
β[1,-1]β
β b=0 β
ββββ¬ββββ
β
βΌ
XNOR(x1,x2)
| Magnitude | Valid XNOR Circuits |
|---|---|
| 0-6 | 0 (exhaustively verified) |
| 7 | 2 (both included here) |
| x1 | x2 | N1 | N2 | OUT |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
Both solutions use weights from {-1, 0, 1} only. They are related by neuron permutation symmetry (swap N1 β N2).
| Solution | Neuron 1 | Neuron 2 | Output |
|---|---|---|---|
| 1 | [-1,-1] b=0 | [-1,-1] b=1 | [1,-1] b=0 |
| 2 | [-1,-1] b=1 | [-1,-1] b=0 | [-1,1] b=0 |
Interpretation:
| Property | XOR-mag7 | XNOR-mag7 |
|---|---|---|
| Optimal magnitude | 7 | 7 |
| Solutions at optimal | 6 | 2 |
| Solution families | 2 | 1 |
| Zero biases | 2 | 3 |
XNOR has fewer solutions because OR at layer 2 creates tighter constraints than XOR's AND.
Original XNOR (magnitude 9):
NOR: [-1,-1], b=0 β magnitude 2
AND: [1, 1], b=-2 β magnitude 4 β costly -2 bias
OR: [1, 1], b=-1 β magnitude 3
Optimized (magnitude 7, solution 1):
N1: [-1,-1], b=0 β magnitude 2
N2: [-1,-1], b=1 β magnitude 3
OUT: [1,-1], b=0 β magnitude 2 β zero bias
22% magnitude reduction (9 β 7).
from safetensors.torch import load_file
import torch
w = load_file('solution1.safetensors')
def xnor_gate(x1, x2):
inp = torch.tensor([float(x1), float(x2)])
n1 = int((inp * w['layer1.neuron1.weight']).sum() + w['layer1.neuron1.bias'] >= 0)
n2 = int((inp * w['layer1.neuron2.weight']).sum() + w['layer1.neuron2.bias'] >= 0)
hid = torch.tensor([float(n1), float(n2)])
return int((hid * w['layer2.weight']).sum() + w['layer2.bias'] >= 0)
# Test
assert xnor_gate(0, 0) == 1 # same
assert xnor_gate(0, 1) == 0 # different
assert xnor_gate(1, 0) == 0 # different
assert xnor_gate(1, 1) == 1 # same
threshold-xnor-mag7/
βββ solution1.safetensors # First solution
βββ solution2.safetensors # Second solution (neuron swap)
βββ model.py # Python implementation
βββ config.json # Metadata
βββ create_safetensors.py # Script to generate weights
βββ README.md
MIT