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

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

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

Collection including phanerozoic/threshold-multiplier2x2