threshold-fullsubtractor

Subtracts three 1-bit inputs (a, b, borrow_in), producing difference and borrow_out. The subtraction counterpart to the full adder.

Circuit

        a       b
        β”‚       β”‚
        β””β”€β”€β”€β”¬β”€β”€β”€β”˜
            β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚   HS1   β”‚  First half subtractor
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚       β”‚
       d1       b1
        β”‚        \
        β”‚   bin   \
        β””β”€β”€β”¬β”€β”€β”˜    \
           β–Ό        \
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    \
      β”‚   HS2   β”‚     β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
       β”‚       β”‚      β”‚
      diff     b2     β”‚
               β”‚      β”‚
               β””β”€β”€β”¬β”€β”€β”€β”˜
                  β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”
              β”‚  OR  β”‚
              β””β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
                  β–Ό
                bout

Half Subtractor Detail

      x       y
      β”‚       β”‚
      β”œβ”€β”€β”€β”¬β”€β”€β”€β”€
      β”‚   β”‚   β”‚
      β–Ό   β”‚   β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”β”‚β”Œβ”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”
  β”‚  OR  β”‚β”‚β”‚ NAND β”‚           β”‚ Β¬x ∧ yβ”‚
  β”‚w:1,1 β”‚β”‚β”‚w:-1,-1β”‚          β”‚w:-1,1 β”‚
  β”‚b: -1 β”‚β”‚β”‚b: +1 β”‚           β”‚b: -1  β”‚
  β””β”€β”€β”€β”€β”€β”€β”˜β”‚β””β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚   β”‚   β”‚                   β”‚
      β””β”€β”€β”€β”Όβ”€β”€β”€β”˜                   β”‚
          β–Ό                       β”‚
      β”Œβ”€β”€β”€β”€β”€β”€β”                    β”‚
      β”‚ AND  β”‚                    β”‚
      β””β”€β”€β”€β”€β”€β”€β”˜                    β”‚
          β”‚                       β”‚
          β–Ό                       β–Ό
        diff                   borrow

Truth Table

a b bin diff bout
0 0 0 0 0
0 0 1 1 1
0 1 0 1 1
0 1 1 0 1
1 0 0 1 0
1 0 1 0 0
1 1 0 0 0
1 1 1 1 1

Binary: a - b - bin = diff - (bout Γ— 2)

Composition

d1, b1 = HalfSubtractor(a, b)
diff, b2 = HalfSubtractor(d1, bin)
bout = OR(b1, b2)

A borrow propagates if either half subtractor produces one.

Architecture

Component Neurons
HS1 (a - b) 4
HS2 (d1 - bin) 4
OR (b1, b2) 1

Total: 9 neurons, 27 parameters, 4 layers

Contrast with Full Adder

Circuit Carry/Borrow HS function
FullAdder cout = OR(c1, c2) c = a AND b
FullSubtractor bout = OR(b1, b2) b = NOT(a) AND b

The only structural difference: the borrow circuit uses NOT(a) AND b instead of AND.

Usage

from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def full_subtractor(a, b, bin_in):
    inp = torch.tensor([float(a), float(b)])

    # HS1
    hs1_l1 = (inp @ w['hs1.xor.layer1.weight'].T + w['hs1.xor.layer1.bias'] >= 0).float()
    d1 = (hs1_l1 @ w['hs1.xor.layer2.weight'].T + w['hs1.xor.layer2.bias'] >= 0).float().item()
    b1 = (inp @ w['hs1.borrow.weight'].T + w['hs1.borrow.bias'] >= 0).float().item()

    # HS2
    inp2 = torch.tensor([d1, float(bin_in)])
    hs2_l1 = (inp2 @ w['hs2.xor.layer1.weight'].T + w['hs2.xor.layer1.bias'] >= 0).float()
    diff = int((hs2_l1 @ w['hs2.xor.layer2.weight'].T + w['hs2.xor.layer2.bias'] >= 0).item())
    b2 = (inp2 @ w['hs2.borrow.weight'].T + w['hs2.borrow.bias'] >= 0).float().item()

    # Final borrow
    bout = int((torch.tensor([b1, b2]) @ w['bout.weight'].T + w['bout.bias'] >= 0).item())

    return diff, bout

Files

threshold-fullsubtractor/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ config.json
└── README.md

License

MIT

Downloads last month
10
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including phanerozoic/threshold-fullsubtractor