--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - arithmetic - multiplier --- # threshold-multiplier2x2 2×2 binary multiplier. Multiplies two 2-bit numbers to produce a 4-bit product. ## Circuit ``` a1 a0 × b1 b0 ───────── a1b0 a0b0 (partial products row 0) a1b1 a0b1 (partial products row 1) ───────────────── p3 p2 p1 p0 a0──┬───────┬───────────────────────────────────► p0 = a0b0 │ │ b0──┼───┬───┼───┬───────────────────────────────┐ │ │ │ │ │ ▼ │ ▼ │ │ ┌─────┐│┌─────┐│ │ │a0b0 │││a1b0 ││ │ └─────┘│└─────┘│ │ │ │ │ │ a1──────┴───┼───┴───┬───────────────────────────┤ │ │ │ b1──────────┴───┬───┴───────────────────────────┤ │ │ │ ▼ ▼ │ ┌─────┐┌─────┐ │ │a0b1 ││a1b1 │ │ └─────┘└─────┘ │ │ │ │ │ │ │ a1b0────────┼──────┼────┐ │ │ │ │ │ a0b1────────┴──────┼────┼────┐ │ │ │ │ │ │ ▼ ▼ │ │ ┌────────┐ │ │ │ HA │ │ │ │ p1, c1 │ │ │ └────────┘ │ │ │ │ │ │ │ │ │ a1b1───────────────┴────┼────┼────┐ │ │ │ │ │ c1 ─────────────────────┴────┼────┼────┐ │ │ │ │ │ │ ▼ ▼ │ │ ┌────────┐ │ │ │ HA │ │ │ │ p2, p3 │ │ │ └────────┘ │ │ │ │ │ ▼ ▼ ▼ ▼ p1 p2 p3 p0 ``` ## Multiplication Table | a | b | a × b | |---|---|-------| | 0 | 0 | 0 | | 0 | 1 | 0 | | 0 | 2 | 0 | | 0 | 3 | 0 | | 1 | 1 | 1 | | 1 | 2 | 2 | | 1 | 3 | 3 | | 2 | 2 | 4 | | 2 | 3 | 6 | | 3 | 3 | 9 | Maximum: 3 × 3 = 9 (binary 1001) ## Algorithm Standard pencil-and-paper multiplication: 1. **Compute partial products** (4 AND gates): - a0b0, a1b0, a0b1, a1b1 2. **Add column 1** (half adder): - p1, c1 = HA(a1b0, a0b1) 3. **Add column 2** (half adder): - p2, p3 = HA(a1b1, c1) 4. **Assemble result**: - p0 = a0b0 - p1-p3 from adders ## Architecture | Component | Neurons | Parameters | |-----------|---------|------------| | AND × 4 | 4 | 12 | | HA (p1) | 4 | 12 | | HA (p2) | 4 | 12 | | **Total** | **12** | **36** | **Layers: 5** (AND → XOR L1 → XOR L2 → XOR L1 → XOR L2) ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') def multiply_2x2(a, b): """a, b: 2-bit integers (0-3) Returns: 4-bit product""" a0, a1 = a & 1, (a >> 1) & 1 b0, b1 = b & 1, (b >> 1) & 1 # See model.py for full implementation pass result = multiply_2x2(3, 3) # Returns 9 ``` ## Files ``` threshold-multiplier2x2/ ├── model.safetensors ├── model.py ├── config.json └── README.md ``` ## License MIT