metadata
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
threshold-hammingdistance4
4-bit Hamming distance. Computes the number of bit positions where two 4-bit inputs differ.
Function
HD(a, b) = popcount(a XOR b)
Returns 3-bit binary encoding (d2, d1, d0) of the distance 0-4.
Architecture
5-layer circuit:
- Layer 1 (8 neurons): For each bit i, compute
diff_hi_i = a_i AND NOT b_ianddiff_lo_i = NOT a_i AND b_i - Layer 2 (4 neurons): XOR result
diff_i = diff_hi_i OR diff_lo_i - Layer 3 (4 neurons): Threshold detectors
ge_k= at least k differences - Layer 4 (4 neurons): Binary encoding logic
- Layer 5 (1 neuron): Final d0 bit
Parameters
| Inputs | 8 (a3,a2,a1,a0, b3,b2,b1,b0) |
| Outputs | 3 (d2, d1, d0) |
| Neurons | 21 |
| Layers | 5 |
| Parameters | 155 |
| Magnitude | 76 |
Truth Table (selected)
| a | b | HD |
|---|---|---|
| 0000 | 0000 | 0 |
| 1111 | 0000 | 4 |
| 1010 | 0101 | 4 |
| 1100 | 1010 | 2 |
| 1111 | 1110 | 1 |
Usage
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
# See model.py for full implementation
# Returns (d2, d1, d0) where distance = 4*d2 + 2*d1 + d0
License
MIT