threshold-xnor-mag7

Minimum-magnitude threshold circuit for XNOR (equivalence). Magnitude 7 is optimal via exhaustive enumeration of all 224,143 configurations.

Circuit Diagram

        x1      x2
        β”‚       β”‚
        β”‚       β”‚
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
   β”‚                 β”‚
   β–Ό                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”
β”‚ N1  β”‚           β”‚ N2  β”‚    Layer 1
β”‚[-1,-1]          β”‚[-1,-1]
β”‚ b=0 β”‚           β”‚ b=1 β”‚
β””β”€β”€β”¬β”€β”€β”˜           β””β”€β”€β”¬β”€β”€β”˜
   β”‚                 β”‚
   β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   β”‚    β”‚
   β–Ό    β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”
  β”‚ OUT  β”‚               Layer 2
  β”‚[1,-1]β”‚
  β”‚ b=0  β”‚
  β””β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
   XNOR(x1,x2)

Key Results

Magnitude Valid XNOR Circuits
0-6 0 (exhaustively verified)
7 2 (both included here)

Truth Table

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

The 2 Solutions

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:

  • N1 (b=0): fires when x1 + x2 = 0 (both zero) β€” NOR
  • N2 (b=1): fires when x1 + x2 ≀ 1 (at most one) β€” NAND
  • OUT: fires when N1=1 OR N2=0 β€” equivalence detector

Comparison with XOR

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.

Improvement Over Original

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).

Benefits of Lower Magnitude

  • Fewer bits to represent weights (all weights ∈ {-1, 0, 1})
  • Less energy for multiply-accumulate
  • 3 zero biases (vs 1 in original)
  • Simpler hardware implementation

Usage

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

Files

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

License

MIT

Downloads last month
18
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including phanerozoic/threshold-xnor-mag7