File size: 4,291 Bytes
6a0af35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---

license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- parity
- error-detection
---


# threshold-parity7

7-bit parity function. Outputs 1 if an odd number of inputs are high. Essential for error detection in 7-bit data words.

## Circuit

```

x0  x1    x2  x3    x4  x5    x6

 β”‚   β”‚     β”‚   β”‚     β”‚   β”‚     β”‚

 β””β”€β”¬β”€β”˜     β””β”€β”¬β”€β”˜     β””β”€β”¬β”€β”˜     β”‚

   β”‚         β”‚         β”‚       β”‚

   β–Ό         β–Ό         β–Ό       β”‚

β”Œβ”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”    β”‚

β”‚XOR01β”‚   β”‚XOR23β”‚   β”‚XOR45β”‚    β”‚   Level 1

β””β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”˜    β”‚

   β”‚         β”‚         β”‚       β”‚

   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”¬β”€β”€β”€β”˜

        β”‚                  β”‚

        β–Ό                  β–Ό

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”

    β”‚XOR0123β”‚          β”‚XOR456 β”‚    Level 2

    β””β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”˜

        β”‚                  β”‚

        β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                 β”‚

                 β–Ό

            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”

            β”‚XOR_finalβ”‚              Level 3

            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                 β”‚

                 β–Ό

              parity

```

## Function

```

parity7(x0..x6) = x0 XOR x1 XOR x2 XOR x3 XOR x4 XOR x5 XOR x6

```

Returns 1 when the Hamming weight is odd (1, 3, 5, or 7).

## Truth Table (by Hamming Weight)

| HW | Example Input | Parity |
|:--:|---------------|:------:|
| 0 | 0000000 | 0 |
| 1 | 0000001 | 1 |
| 2 | 0000011 | 0 |
| 3 | 0000111 | 1 |
| 4 | 0001111 | 0 |
| 5 | 0011111 | 1 |
| 6 | 0111111 | 0 |
| 7 | 1111111 | 1 |

## Mechanism

**XOR Implementation (3 neurons per gate):**

Each XOR is built from OR, NAND, and AND gates:
```

XOR(a, b) = AND(OR(a,b), NAND(a,b))

```

| Gate | Weights | Bias | Fires when |
|------|---------|------|------------|
| OR | [1, 1] | -1 | a + b >= 1 |
| NAND | [-1, -1] | +1 | a + b <= 1 |
| AND | [1, 1] | -2 | both inputs = 1 |

**Tree Structure (6 XOR gates):**

| Level | Gates | Computes |
|-------|-------|----------|
| 1 | XOR01, XOR23, XOR45 | Pairs of inputs |
| 2 | XOR0123, XOR456 | XOR(XOR01,XOR23), XOR(XOR45,x6) |
| 3 | XOR_final | XOR(XOR0123, XOR456) |



## Architecture



| Component | Function | Neurons |

|-----------|----------|---------|

| xor_01 | x0 XOR x1 | 3 |
| xor_23 | x2 XOR x3 | 3 |

| xor_45 | x4 XOR x5 | 3 |
| xor_0123 | xor01 XOR xor23 | 3 |

| xor_456 | xor45 XOR x6 | 3 |
| xor_final | xor0123 XOR xor456 | 3 |



**Total: 18 neurons, 6 XOR gates**



## Parameters



| | |

|---|---|

| Inputs | 7 |

| Outputs | 1 |

| Neurons | 18 |

| Layers | 6 |

| Parameters | 54 |

| Magnitude | 60 |



## 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 parity7(x0, x1, x2, x3, x4, x5, x6):
    xor01 = xor2(x0, x1, 'xor_01')

    xor23 = xor2(x2, x3, 'xor_23')

    xor45 = xor2(x4, x5, 'xor_45')

    xor0123 = xor2(xor01, xor23, 'xor_0123')

    xor456 = xor2(xor45, x6, 'xor_456')

    return xor2(xor0123, xor456, 'xor_final')


# Examples
print(parity7(1, 0, 1, 0, 1, 0, 0))  # 1 (odd: 3 ones)
print(parity7(1, 1, 1, 1, 1, 1, 1))  # 1 (odd: 7 ones)
print(parity7(1, 1, 0, 0, 0, 0, 0))  # 0 (even: 2 ones)
```



## Applications



- Hamming(7,4) code parity bit

- 7-bit ASCII parity checking

- Memory word error detection

- Bus parity generation



## Files



```
threshold-parity7/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ create_safetensors.py

β”œβ”€β”€ config.json

└── README.md

```



## License



MIT