--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - parity - error-detection --- # threshold-parity6 6-bit parity function. Outputs 1 if an odd number of inputs are high. Fundamental building block for error detection codes. ## Circuit ``` x0 x1 x2 x3 x4 x5 │ │ │ │ │ │ └──┬─┘ └──┬─┘ └──┬─┘ │ │ │ ▼ ▼ ▼ ┌─────┐ ┌─────┐ ┌─────┐ │XOR01│ │XOR23│ │XOR45│ Layer 1-2 └─────┘ └─────┘ └─────┘ │ │ │ └─────┬─────┘ │ │ │ ▼ │ ┌─────┐ │ │XOR │ │ Layer 3-4 │0123 │ │ └─────┘ │ │ │ └────────┬────────┘ │ ▼ ┌───────┐ │ XOR │ Layer 5-6 │ final │ └───────┘ │ ▼ parity ``` ## Function ``` parity6(x0, x1, x2, x3, x4, x5) = x0 XOR x1 XOR x2 XOR x3 XOR x4 XOR x5 ``` Returns 1 when the Hamming weight is odd (1, 3, or 5). ## Truth Table (by Hamming Weight) | HW | Example Input | Parity | |:--:|---------------|:------:| | 0 | 000000 | 0 | | 1 | 000001 | 1 | | 2 | 000011 | 0 | | 3 | 000111 | 1 | | 4 | 001111 | 0 | | 5 | 011111 | 1 | | 6 | 111111 | 0 | Pattern: Parity = HW mod 2 ## Mechanism Parity is computed using a tree of XOR gates. Each XOR gate is implemented as: ``` XOR(a, b) = AND(OR(a,b), NAND(a,b)) ``` **XOR Implementation (3 neurons per gate):** | Gate | Weights | Bias | Function | |------|---------|------|----------| | OR | [1, 1] | -1 | a + b >= 1 | | NAND | [-1, -1] | +1 | -(a + b) + 1 >= 0 | | AND | [1, 1] | -2 | OR + NAND >= 2 | **Tree Structure:** - Level 1: XOR(x0,x1), XOR(x2,x3), XOR(x4,x5) - 3 parallel XOR gates - Level 2: XOR(XOR01, XOR23) - combines first 4 bits - Level 3: XOR(XOR0123, XOR45) - final result ## Architecture | Component | Neurons | Layers | |-----------|---------|--------| | XOR01 | 3 | 2 | | XOR23 | 3 | 2 | | XOR45 | 3 | 2 | | XOR0123 | 3 | 2 | | XOR_final | 3 | 2 | **Total: 15 neurons across 5 XOR gates** Note: Effective depth is 6 layers (3 XOR stages × 2 layers each), but XOR01, XOR23, XOR45 execute in parallel. ## Parameters | | | |---|---| | Inputs | 6 | | Outputs | 1 | | Neurons | 15 | | Layers | 6 | | Parameters | 45 | | Magnitude | 50 | ## Comparison with Other Parity Circuits | Circuit | Inputs | XOR Gates | Neurons | Depth | |---------|--------|-----------|---------|-------| | parity3 | 3 | 2 | 6 | 4 | | parity4 | 4 | 3 | 9 | 4 | | parity5 | 5 | 4 | 12 | 6 | | **parity6** | **6** | **5** | **15** | **6** | | parity7 | 7 | 6 | 18 | 6 | | parity8 | 8 | 7 | 21 | 6 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def xor2(a, b, prefix): or_out = int(a * w[f'{prefix}.or.weight'][0] + b * w[f'{prefix}.or.weight'][1] + w[f'{prefix}.or.bias'] >= 0) nand_out = int(a * w[f'{prefix}.nand.weight'][0] + b * w[f'{prefix}.nand.weight'][1] + w[f'{prefix}.nand.bias'] >= 0) return int(or_out * w[f'{prefix}.and.weight'][0] + nand_out * w[f'{prefix}.and.weight'][1] + w[f'{prefix}.and.bias'] >= 0) def parity6(x0, x1, x2, x3, x4, x5): xor01 = xor2(x0, x1, 'xor_01') xor23 = xor2(x2, x3, 'xor_23') xor45 = xor2(x4, x5, 'xor_45') xor0123 = xor2(xor01, xor23, 'xor_0123') return xor2(xor0123, xor45, 'xor_final') # Test print(parity6(1, 0, 1, 0, 1, 0)) # 1 (odd: 3 ones) print(parity6(1, 1, 1, 1, 0, 0)) # 0 (even: 4 ones) print(parity6(0, 0, 0, 0, 0, 1)) # 1 (odd: 1 one) ``` ## Applications - Error detection in data transmission - RAID parity calculation - Checksum generation - Memory ECC systems - Serial communication protocols ## Files ``` threshold-parity6/ ├── model.safetensors ├── model.py ├── create_safetensors.py ├── config.json └── README.md ``` ## License MIT