File size: 2,522 Bytes
0fc2474 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- arithmetic
---
# threshold-halfadder
Adds two 1-bit inputs, producing sum and carry. The building block for all multi-bit adders.
## Circuit
```
a b
β β
βββββ¬ββββ€
β β β
βΌ β βΌ
βββββββββββββββββ
β OR βββ NAND β
βββββββββββββββββ
β β β
βββββΌββββ
βΌ a b
ββββββββ β β
β AND β βββ¬ββ
ββββββββ βΌ
β ββββββββ
βΌ β AND β
sum ββββββββ
β
βΌ
carry
```
The sum output uses XOR (2 layers), the carry uses AND (1 layer).
## Truth Table
| a | b | sum | carry |
|---|---|-----|-------|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
Binary: a + b = (carry Γ 2) + sum
## Components
| Output | Function | Neurons | Layers |
|--------|----------|---------|--------|
| sum | XOR(a, b) | 3 | 2 |
| carry | AND(a, b) | 1 | 1 |
**Total: 4 neurons, 12 parameters**
## Arithmetic
The half adder computes a + b where a, b β {0, 1}:
- 0 + 0 = 0 (sum=0, carry=0)
- 0 + 1 = 1 (sum=1, carry=0)
- 1 + 0 = 1 (sum=1, carry=0)
- 1 + 1 = 2 (sum=0, carry=1)
The carry represents the 2s place.
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def half_adder(a, b):
inp = torch.tensor([float(a), float(b)])
# XOR for sum
or_out = int((inp @ w['xor.layer1.or.weight'] + w['xor.layer1.or.bias']).sum() >= 0)
nand_out = int((inp @ w['xor.layer1.nand.weight'] + w['xor.layer1.nand.bias']).sum() >= 0)
xor_inp = torch.tensor([float(or_out), float(nand_out)])
sum_out = int((xor_inp @ w['xor.layer2.weight'] + w['xor.layer2.bias']).sum() >= 0)
# AND for carry
carry_out = int((inp @ w['carry.weight'] + w['carry.bias']).sum() >= 0)
return sum_out, carry_out
```
## Files
```
threshold-halfadder/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
```
## License
MIT
|