Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
2-to-4 binary-to-one-hot encoder. Converts a 2-bit binary value to a 4-bit one-hot representation. The inverse of the one-hot decoder.
a1 a0
β β
βββββββββββ€
β β
βββββ΄ββββ βββββ΄ββββ
β β β β
βΌ βΌ βΌ βΌ
βββββ βββββ βββββ βββββ
βy0 β βy1 β βy2 β βy3 β
βNORβ βANDβ βANDβ βANDβ
β β βΒ¬a1β βa1 β βa1 β
β β β a0β βΒ¬a0β βa0 β
βββββ βββββ βββββ βββββ
β β β β
βΌ βΌ βΌ βΌ
y0 y1 y2 y3
(00) (01) (10) (11)
binary_to_onehot(a1, a0) -> (y0, y1, y2, y3)
Exactly one output is high, corresponding to the binary input value.
| a1 | a0 | Value | y0 | y1 | y2 | y3 |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 2 | 0 | 0 | 1 | 0 |
| 1 | 1 | 3 | 0 | 0 | 0 | 1 |
Output encoding: y[i] = 1 iff input value equals i.
Each output neuron detects a specific binary pattern:
| Output | Pattern | Weights [a1, a0] | Bias | Logic |
|---|---|---|---|---|
| y0 | 00 | [-1, -1] | 0 | NOR(a1, a0) |
| y1 | 01 | [-1, +1] | -1 | Β¬a1 AND a0 |
| y2 | 10 | [+1, -1] | -1 | a1 AND Β¬a0 |
| y3 | 11 | [+1, +1] | -2 | a1 AND a0 |
Analysis:
Single-layer implementation with 4 parallel neurons:
| Neuron | Weights | Bias | Detects |
|---|---|---|---|
| y0 | [-1, -1] | 0 | Binary 00 |
| y1 | [-1, +1] | -1 | Binary 01 |
| y2 | [+1, -1] | -1 | Binary 10 |
| y3 | [+1, +1] | -2 | Binary 11 |
| Inputs | 2 (a1, a0) |
| Outputs | 4 (y0, y1, y2, y3) |
| Neurons | 4 |
| Layers | 1 |
| Parameters | 12 |
| Magnitude | 12 |
This circuit is the inverse of threshold-onehot-decoder:
Binary [a1, a0] βββΊ binary-to-onehot βββΊ One-hot [y0, y1, y2, y3]
One-hot [y0, y1, y2, y3] βββΊ onehot-decoder βββΊ Binary [a1, a0]
Together they form a codec pair for binary β one-hot conversion.
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def binary_to_onehot(a1, a0):
inp = torch.tensor([float(a1), float(a0)])
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
return y0, y1, y2, y3
# Examples
print(binary_to_onehot(0, 0)) # (1, 0, 0, 0) - value 0
print(binary_to_onehot(0, 1)) # (0, 1, 0, 0) - value 1
print(binary_to_onehot(1, 0)) # (0, 0, 1, 0) - value 2
print(binary_to_onehot(1, 1)) # (0, 0, 0, 1) - value 3
For n-bit binary to 2^n one-hot:
Each additional input bit doubles the outputs.
threshold-binary-to-onehot/
βββ model.safetensors
βββ model.py
βββ create_safetensors.py
βββ config.json
βββ README.md
MIT