Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
4-bit magnitude comparator. Compares two 4-bit unsigned integers.
compare(A, B) -> (GT, LT, EQ)
| Input | Description |
|---|---|
| A3-A0 | 4-bit number A (A3 is MSB) |
| B3-B0 | 4-bit number B (B3 is MSB) |
A3 A2 A1 A0 B3 B2 B1 B0
| | | | | | | |
+--+--+--+--+--+--+--+
| |
v v
[GT: A-B >= 1] [LT: B-A >= 1]
| |
+--------+--------+
|
v
[EQ: NOR(GT,LT)]
Layer 1:
Layer 2:
| Inputs | 8 |
| Outputs | 3 (GT, LT, EQ) |
| Neurons | 3 |
| Layers | 2 |
| Parameters | 21 |
| Magnitude | 64 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def compare4(a3, a2, a1, a0, b3, b2, b1, b0):
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0),
float(b3), float(b2), float(b1), float(b0)])
gt = int((inp @ w['gt.weight'].T + w['gt.bias'] >= 0).item())
lt = int((inp @ w['lt.weight'].T + w['lt.bias'] >= 0).item())
gt_lt = torch.tensor([float(gt), float(lt)])
eq = int((gt_lt @ w['eq.weight'].T + w['eq.bias'] >= 0).item())
return gt, lt, eq
# Compare 5 vs 3
gt, lt, eq = compare4(0, 1, 0, 1, 0, 0, 1, 1)
print(f"5 vs 3: GT={gt}, LT={lt}, EQ={eq}") # GT=1, LT=0, EQ=0
MIT