omega-principia
This project has not described itself yet.
Brought by drakestapleton
omega-principia / src / omsci_bell_compression.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note Bell correlation compression. See EXPERIMENT.md for wire format, frozen models and verdict rules. |
| 4 | note All angles in the test set are labeled by x,y. At x=y=1 the target parity is one. |
| 5 | global incoming uchar 1 |
| 6 | global stream uchar 16384 |
| 7 | global setting uchar 32768 |
| 8 | global leftbit uchar 32768 |
| 9 | global rightbit uchar 32768 |
| 10 | global draws ulong 2 |
| 11 | global counts ulong 4 |
| 12 | global wins ulong 4 |
| 13 | global cursor ulong 1 |
| 14 | global decimal uchar 32 |
| 15 | |
| 16 | fn bytein to ulong |
| 17 | k = read 0 incoming 0 1 |
| 18 | if ne k 1 |
| 19 | exit 2 |
| 20 | end |
| 21 | ret ulong get incoming 0 |
| 22 | end |
| 23 | |
| 24 | fn number n ulong to void |
| 25 | i = ulong 30 |
| 26 | put decimal i 10 |
| 27 | while ge n 10 bound 20 |
| 28 | i = sub i 1 |
| 29 | put decimal i uchar add 48 rem n 10 |
| 30 | n = div n 10 |
| 31 | end |
| 32 | i = sub i 1 |
| 33 | put decimal i uchar add 48 n |
| 34 | while lt i 31 bound 31 |
| 35 | k = write 1 decimal i sub ulong 31 i |
| 36 | if le k 0 |
| 37 | exit 3 |
| 38 | end |
| 39 | i = add i ulong k |
| 40 | end |
| 41 | ret |
| 42 | end |
| 43 | |
| 44 | fn randval lane ulong to ulong |
| 45 | v = rem mul get draws lane 48271 2147483647 |
| 46 | put draws lane v |
| 47 | ret v |
| 48 | end |
| 49 | |
| 50 | fn addbit bit ulong to void |
| 51 | p = get cursor 0 |
| 52 | i = div p 8 |
| 53 | off = rem p 8 |
| 54 | if eq off 0 |
| 55 | put stream i 0 |
| 56 | end |
| 57 | put stream i uchar or ulong get stream i shl bit off |
| 58 | put cursor 0 add p 1 |
| 59 | ret |
| 60 | end |
| 61 | |
| 62 | fn takebit to ulong |
| 63 | p = get cursor 0 |
| 64 | put cursor 0 add p 1 |
| 65 | ret and shr ulong get stream div p 8 rem p 8 1 |
| 66 | end |
| 67 | |
| 68 | fn flagnum v bool to ulong |
| 69 | if v |
| 70 | ret 1 |
| 71 | end |
| 72 | ret 0 |
| 73 | end |
| 74 | |
| 75 | fn predicted id ulong j ulong to ulong |
| 76 | if eq id 2 |
| 77 | ret flagnum eq get setting j 3 |
| 78 | end |
| 79 | ret ulong 0 |
| 80 | end |
| 81 | |
| 82 | fn residue id ulong j ulong to ulong |
| 83 | v = xor ulong get leftbit j ulong get rightbit j |
| 84 | ret xor v predicted id j |
| 85 | end |
| 86 | |
| 87 | fn encode2 id ulong packed ulong j ulong to void |
| 88 | h = add j 1 |
| 89 | addbit ulong get leftbit j |
| 90 | addbit ulong get leftbit h |
| 91 | e = residue id j |
| 92 | f = residue id h |
| 93 | if eq packed 0 |
| 94 | addbit e |
| 95 | addbit f |
| 96 | else |
| 97 | if eq or e f 0 |
| 98 | addbit 0 |
| 99 | else |
| 100 | addbit 1 |
| 101 | addbit e |
| 102 | if ne e 0 |
| 103 | addbit f |
| 104 | end |
| 105 | end |
| 106 | end |
| 107 | ret |
| 108 | end |
| 109 | |
| 110 | fn verify id ulong j ulong a ulong e ulong to void |
| 111 | b = xor xor a e predicted id j |
| 112 | if ne a ulong get leftbit j |
| 113 | exit 4 |
| 114 | end |
| 115 | if ne b ulong get rightbit j |
| 116 | exit 4 |
| 117 | end |
| 118 | ret |
| 119 | end |
| 120 | |
| 121 | fn decode2 decid ulong usecode ulong j ulong to void |
| 122 | h = add j 1 |
| 123 | a = takebit |
| 124 | b = takebit |
| 125 | e = ulong 0 |
| 126 | f = ulong 0 |
| 127 | if eq usecode 0 |
| 128 | e = takebit |
| 129 | f = takebit |
| 130 | else |
| 131 | flag = takebit |
| 132 | if ne flag 0 |
| 133 | e = takebit |
| 134 | f = ulong 1 |
| 135 | if ne e 0 |
| 136 | f = takebit |
| 137 | end |
| 138 | end |
| 139 | end |
| 140 | verify decid j a e |
| 141 | verify decid h b f |
| 142 | ret |
| 143 | end |
| 144 | |
| 145 | fn codec id ulong vis ulong to ulong |
| 146 | note Header is one literal byte for the shared codec ID and a second byte for the fitted model parameter. |
| 147 | put stream 0 uchar id |
| 148 | base = ulong 8 |
| 149 | if eq id 2 |
| 150 | put stream 1 uchar vis |
| 151 | base = ulong 16 |
| 152 | end |
| 153 | put cursor 0 base |
| 154 | note Use the prefix code when expected error at the test angle is below 3/8 (conservative switch). |
| 155 | note With visibility v/255 the mismatch is (1 - v/(255 sqrt(2)))/2. Switch at v >= 91. |
| 156 | packed = flagnum eq id 1 |
| 157 | if and eq id 2 ge vis 91 |
| 158 | packed = ulong 1 |
| 159 | end |
| 160 | for i ulong 16384 bound 16384 |
| 161 | encode2 id packed mul i 2 |
| 162 | end |
| 163 | total = get cursor 0 |
| 164 | note Decode using serialized header only, plus the public ordered setting list. |
| 165 | decid = ulong get stream 0 |
| 166 | put cursor 0 8 |
| 167 | usecode = flagnum eq decid 1 |
| 168 | if eq decid 2 |
| 169 | put cursor 0 16 |
| 170 | usecode = flagnum ge get stream 1 91 |
| 171 | end |
| 172 | for i ulong 16384 bound 16384 |
| 173 | decode2 decid usecode mul i 2 |
| 174 | end |
| 175 | if ne get cursor 0 total |
| 176 | exit 4 |
| 177 | end |
| 178 | ret div add total 7 8 |
| 179 | end |
| 180 | |
| 181 | fn trial mode ulong to ulong |
| 182 | v = ulong 0 |
| 183 | if eq mode 3 |
| 184 | v = bytein |
| 185 | if gt v 15 |
| 186 | exit 2 |
| 187 | end |
| 188 | else |
| 189 | x = flagnum gt randval 0 1073741823 |
| 190 | y = flagnum gt randval 0 1073741823 |
| 191 | a = flagnum gt randval 1 1073741823 |
| 192 | b = a |
| 193 | if eq mode 0 |
| 194 | b = flagnum gt randval 1 1073741823 |
| 195 | end |
| 196 | if eq mode 2 |
| 197 | loss = flagnum gt randval 1 1832991947 |
| 198 | b = xor xor a and x y loss |
| 199 | end |
| 200 | v = add add mul x 8 mul y 4 add mul a 2 b |
| 201 | end |
| 202 | ret v |
| 203 | end |
| 204 | |
| 205 | fn main to int |
| 206 | mode = bytein |
| 207 | if or lt mode 48 gt mode 51 |
| 208 | exit 2 |
| 209 | end |
| 210 | mode = sub mode 48 |
| 211 | note Synthetic fixture streams use distinct fixed seeds and deterministic generators for repeatable software checks. |
| 212 | put draws 0 312457 |
| 213 | put draws 1 917531 |
| 214 | agree = ulong 0 |
| 215 | for i ulong 4096 bound 4096 |
| 216 | v = ulong 0 |
| 217 | if eq mode 3 |
| 218 | v = bytein |
| 219 | if gt v 3 |
| 220 | exit 2 |
| 221 | end |
| 222 | else |
| 223 | a = flagnum gt randval 1 1073741823 |
| 224 | b = a |
| 225 | if eq mode 0 |
| 226 | b = flagnum gt randval 1 1073741823 |
| 227 | end |
| 228 | v = add mul a 2 b |
| 229 | end |
| 230 | if or eq v 0 eq v 3 |
| 231 | agree = add agree 1 |
| 232 | end |
| 233 | end |
| 234 | vis = ulong 0 |
| 235 | if gt agree 2048 |
| 236 | vis = div add mul sub mul 2 agree 4096 255 2048 4096 |
| 237 | end |
| 238 | note Emit calibration commitment before reading any test outcome. Stdout writes are unbuffered. |
| 239 | number vis |
| 240 | for i ulong 32768 bound 32768 |
| 241 | v = trial mode |
| 242 | c = shr v 2 |
| 243 | a = and shr v 1 1 |
| 244 | b = and v 1 |
| 245 | put setting i uchar c |
| 246 | put leftbit i uchar a |
| 247 | put rightbit i uchar b |
| 248 | put counts c add get counts c 1 |
| 249 | if eq xor a b flagnum eq c 3 |
| 250 | put wins c add get wins c 1 |
| 251 | end |
| 252 | end |
| 253 | for c ulong 4 bound 4 |
| 254 | number get counts c |
| 255 | number get wins c |
| 256 | end |
| 257 | for id ulong 3 bound 3 |
| 258 | number codec id vis |
| 259 | end |
| 260 | ret 0 |
| 261 | end |