--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - arithmetic --- # threshold-ripplecarry2bit Adds two 2-bit numbers with carry-in. The classic ripple-carry architecture using cascaded full adders. ## Circuit ``` a0 b0 cin a1 b1 │ │ │ │ │ └───┼───┘ └───┼───┘ ▼ ▼ ┌─────────┐ ┌─────────┐ │ FA0 │──────────│ FA1 │ └─────────┘ c0 └─────────┘ │ │ │ ▼ ▼ ▼ s0 s1 cout Input: (a1 a0) + (b1 b0) + cin Output: (cout s1 s0) ``` The carry ripples from FA0 to FA1, hence "ripple-carry." ## Example ``` 11 (a1=1, a0=1) + 11 (b1=1, b0=1) + 0 (cin=0) ──── 110 (cout=1, s1=1, s0=0) ``` 3 + 3 + 0 = 6 = 0b110 ## Architecture | Component | Neurons | |-----------|---------| | FA0 | 9 | | FA1 | 9 | **Total: 18 neurons, 42 parameters, 8 layers** ## Inputs/Outputs | Type | Bits | Meaning | |------|------|---------| | a | a1, a0 | First 2-bit number | | b | b1, b0 | Second 2-bit number | | cin | 1 bit | Carry in | | s | s1, s0 | 2-bit sum | | cout | 1 bit | Carry out | ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') def ripple_carry_2bit(a0, a1, b0, b1, cin): # FA0: a0 + b0 + cin -> s0, c0 # FA1: a1 + b1 + c0 -> s1, cout # See model.py for full implementation pass ``` ## Scaling This 2-bit adder demonstrates the pattern. For n bits: - n full adders in cascade - Depth grows linearly (slow for large n) - Carry-lookahead or prefix adders would be faster but more complex ## Files ``` threshold-ripplecarry2bit/ ├── model.safetensors ├── model.py ├── config.json └── README.md ``` ## License MIT