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:

  1. Layer 1 (8 neurons): For each bit i, compute diff_hi_i = a_i AND NOT b_i and diff_lo_i = NOT a_i AND b_i
  2. Layer 2 (4 neurons): XOR result diff_i = diff_hi_i OR diff_lo_i
  3. Layer 3 (4 neurons): Threshold detectors ge_k = at least k differences
  4. Layer 4 (4 neurons): Binary encoding logic
  5. 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

Downloads last month
10
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Collection including phanerozoic/threshold-hammingdistance4