3-bit population count (Hamming weight). Counts the number of 1-bits in a 3-bit input, producing a 2-bit output (0-3).
x0 x1 x2
│ │ │
└───┬───┴───┬───┘
│ │
┌────┴────┐ │
│ Layer 1 │ │
│ atleast1│ │ (sum >= 1)
│ atleast2│ │ (sum >= 2)
│ atleast3├──┘ (sum >= 3)
└────┬────┘
│
┌────┴────┐
│ Layer 2 │
│ XOR │ out1 = atleast1 XOR atleast2
│ pass │ out0 = atleast2 XOR atleast3
└────┬────┘
│
▼
[out1, out0]
popcount3(x0, x1, x2) -> (out1, out0)
where output = 2*out1 + out0 = number of 1-bits in input
The circuit uses threshold gates to detect "at least k" conditions, then XOR gates to convert to binary:
-
out1 (2's place) = atleast2 XOR atleast3 = (sum >= 2) XOR (sum >= 3)
- True when sum is exactly 2 or 3
- Actually: out1 = atleast2 (since atleast3 implies atleast2)
- Simplified: out1 = atleast2
-
out0 (1's place) = atleast1 XOR atleast2 XOR atleast3
- True when sum is 1 or 3 (odd from {1,2,3} perspective)
- Simplified: out0 = parity of threshold outputs
1from safetensors.torch import load_file
2import torch
3
4w = load_file('model.safetensors')
5
6def popcount3(x0, x1, x2):
7 inp = torch.tensor([float(x0), float(x1), float(x2)])
8
9 # Layer 1: Threshold detection
10 at1 = int((inp @ w['atleast1.weight'].T + w['atleast1.bias'] >= 0).item())
11 at2 = int((inp @ w['atleast2.weight'].T + w['atleast2.bias'] >= 0).item())
12
13 # out1 = atleast2 (sum >= 2 means bit 1 is set)
14 out1 = at2
15
16 # out0 = atleast1 XOR atleast2
17 l1 = torch.tensor([float(at1), float(at2)])
18 or_out = int((l1 @ w['xor.or.weight'].T + w['xor.or.bias'] >= 0).item())
19 nand_out = int((l1 @ w['xor.nand.weight'].T + w['xor.nand.bias'] >= 0).item())
20 l2 = torch.tensor([float(or_out), float(nand_out)])
21 out0 = int((l2 @ w['xor.and.weight'].T + w['xor.and.bias'] >= 0).item())
22
23 return out1, out0
24
25# Examples
26print(popcount3(0, 0, 0)) # (0, 0) = 0
27print(popcount3(1, 0, 0)) # (0, 1) = 1
28print(popcount3(1, 1, 0)) # (1, 0) = 2
29print(popcount3(1, 1, 1)) # (1, 1) = 3
threshold-popcount3/
├── model.safetensors
├── model.py
├── create_safetensors.py
├── config.json
└── README.md