Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
Converts 7-bit thermometer code to 3-bit binary. The inverse of BinaryToThermometer.
tβ tβ tβ tβ tβ tβ
tβ
β β β β β β β
β β β β β β β
βββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ
β β β β
β βββββββββΌββββββββΌβββββββββββΊ bβ (direct from tβ)
β β β β
β β βββββ΄ββββ β
β β βtββ§Β¬tβ β β
β β βββββ¬ββββ β
β β β β
β β ββββORβββ΄βββββββββββΊ bβ
β β β β
β β β tβ
β β β
βββββ΄ββββ βββ΄ββ βββββ
βtββ§Β¬tββ β...β βtβ β
βββββ¬ββββ βββ¬ββ βββ¬ββ
β β β
βββββββββ΄βORβββ΄βββββββββββββΊ bβ
| Thermometer | Value | Binary |
|---|---|---|
| 0000000 | 0 | 000 |
| 1000000 | 1 | 001 |
| 1100000 | 2 | 010 |
| 1110000 | 3 | 011 |
| 1111000 | 4 | 100 |
| 1111100 | 5 | 101 |
| 1111110 | 6 | 110 |
| 1111111 | 7 | 111 |
For valid thermometer (monotonic ones then zeros), tα΅’ = 1 iff value > i.
bβ (bit 2): Directly equals tβ.
bβ (bit 1): Fires for values {2, 3, 6, 7}.
bβ = (tβ AND NOT(tβ)) OR tβ
bβ (bit 0): Fires for odd values {1, 3, 5, 7}.
bβ = (tβ AND NOT(tβ)) OR (tβ AND NOT(tβ)) OR (tβ AND NOT(tβ
)) OR tβ
Each AND-NOT term detects a transition from 1 to 0 at an odd position.
In valid thermometer code, the value equals the position of the last 1. The formula detects "where does the thermometer stop?"
| Value | Last 1 at | Detected by |
|---|---|---|
| 1 | tβ | tβ AND NOT(tβ) |
| 3 | tβ | tβ AND NOT(tβ) |
| 5 | tβ | tβ AND NOT(tβ ) |
| 7 | tβ | tβ (no tβ to check) |
| Output | Neurons | Parameters |
|---|---|---|
| bβ | 1 | 8 |
| bβ | 2 | 11 |
| bβ | 4 | 29 |
| Total | 7 | 48 |
Layers: 2
BinaryToThermometer is single-layer (7 parallel thresholds). ThermometerToBinary requires 2 layers because extracting individual bits from a sum needs non-trivial logic.
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def therm_to_binary(therm):
"""therm: 7-element list"""
# See model.py for full implementation
pass
# Thermometer for 5 -> binary 101
therm = [1, 1, 1, 1, 1, 0, 0]
b2, b1, b0 = therm_to_binary(therm)
print(b2, b1, b0) # 1, 0, 1
threshold-thermometertobinary/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT