Repository navigation
Expand file tree
/
Copy pathdecoders.py
More file actions
501 lines (415 loc) · 19.3 KB
/
Copy pathdecoders.py
File metadata and controls
501 lines (415 loc) · 19.3 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
from __future__ import annotations
import itertools
import math
import random
from collections.abc import Callable, Iterator
from typing import TYPE_CHECKING
import numpy as np
import scipy as sp
from ldpc import BpDecoder, BpOsdDecoder
from sage.all import GF, vector
from sage.coding.decoder import DecodingError
from sage.coding.grs_code import GeneralizedReedSolomonCode, GRSBerlekampWelchDecoder
from sage.coding.guruswami_sudan.gs_decoder import GRSGuruswamiSudanDecoder
from sage.coding.information_set_decoder import LinearCodeInformationSetDecoder
from sage.coding.linear_code import (
LinearCodeNearestNeighborDecoder,
LinearCodeSyndromeDecoder,
)
from sage.misc.randstate import randstate
if TYPE_CHECKING:
from max_lin_sat import MaxLinSat
class BenchmarkResult:
def __init__(self, correct: list, incorrect: list, epsilon: float):
self.correct = correct
self.incorrect = incorrect
self.epsilon = epsilon
for e in correct:
e.set_immutable()
for e in incorrect:
e.set_immutable()
self.correct_set = set(correct)
self.incorrect_set = set(incorrect)
def __str__(self) -> str:
return f"BenchmarkResult(#correct = {len(self.correct)}, #incorrect = {len(self.incorrect)}, epsilon = {self.epsilon})"
def generate_all_errors(field: GF, n: int, k: int) -> Iterator[vector]:
els = [e for e in field if e != 0]
for error_indices in (np.array(comb) for comb in itertools.combinations(range(n), k)):
for error_digits in itertools.product(els, repeat=k):
e = vector(field, [0] * n)
for i, idx in enumerate(error_indices):
e[idx] += error_digits[i]
yield e
def generate_errors(field: GF, n: int, k: int, n_tries: int | None) -> Iterator[vector]:
els = [e for e in field if e != 0]
n_possible_errors = math.comb(n, k) * len(els) ** k
if n_tries is None or n_possible_errors <= n_tries:
for e in generate_all_errors(field, n, k):
yield e
elif 2 * n_tries >= n_possible_errors:
all_errors = list(generate_all_errors(field, n, k))
random.shuffle(all_errors)
for i in range(n_tries):
yield all_errors[i]
else:
seen = set()
n_produced = 0
while n_produced < n_tries:
e = vector(field, [0] * n)
error_indices = np.random.choice(n, k, replace=False)
for i in error_indices:
e[i] += random.choice(els)
e.set_immutable()
if e in seen:
continue
seen.add(e)
n_produced += 1
yield e
class AbstractDecoder:
"""Base class of all decoders.
A subclass implements *exactly one* of the two decoding interfaces:
* ``decode_syndrome(s, l)`` -- the natural interface for DQI: the decoder
only sees the syndrome ``s = H e`` (with ``H = B^T`` the parity check
matrix of the DQI code ``ker(B^T)``) and returns an error estimate of
length ``m``, or ``None`` if decoding fails.
* ``decode_codeword(y, l)`` -- the classical word based interface: given a
received word ``y`` of length ``m`` return the nearest codeword.
Word based decoders are turned into syndrome decoders by
:meth:`error_estimate` via a fixed coset representative ``rho(s)`` with
``H rho(s) = s``: the estimate is ``rho - decode_codeword(rho)``. This is
what the DQI circuit does, and in particular it returns exactly one error
per syndrome (a word based decoder fed the error itself breaks ties per
word and would report several errors of the same coset as correct).
"""
def __init__(self, instance: MaxLinSat):
self.instance = instance
self.benchmarks = {}
cls = type(self)
has_syndrome = cls.decode_syndrome is not AbstractDecoder.decode_syndrome
has_codeword = cls.decode_codeword is not AbstractDecoder.decode_codeword
if has_syndrome and has_codeword:
raise TypeError(
f"{cls.__name__} overrides both decode_syndrome and decode_codeword; "
"a decoder must implement exactly one of them"
)
if not has_syndrome and not has_codeword:
raise TypeError(
f"{cls.__name__} implements neither decode_syndrome nor decode_codeword; "
"a decoder must implement exactly one of them"
)
self.decodes_syndromes = has_syndrome
# H = B^T is the parity check matrix of the code ker(B^T) used by DQI.
self.H = self.instance.get_B().T
self._estimate_cache = {}
if has_codeword:
self._setup_representative()
def _setup_representative(self):
"""Precompute a linear map s -> rho(s) with H rho(s) == s."""
H = self.H
# eliminate redundant parity-checks
R = list(H.T.pivots())
H_R = H.matrix_from_rows(R)
# select a basis for the syndrome space
P = list(H_R.pivots())
self._rep_rows = R
self._rep_cols = P
self._rep_inverse = H_R.matrix_from_columns(P).inverse()
def representative(self, s: vector) -> vector:
"""A fixed word rho of length m with H rho == s (linear in s)."""
field = self.instance.field
rho = vector(field, [0] * self.instance.get_m())
if self._rep_cols:
values = self._rep_inverse * vector(field, [s[i] for i in self._rep_rows])
for j, col in enumerate(self._rep_cols):
rho[col] = values[j]
return rho
def decoding_radius(self) -> int | None:
raise NotImplementedError()
def _complete_decoding_radius(self) -> int | None:
"""
Returns how many errors would be returned by a "complete" decoder, that is one which (by excaustive search or similar) always decodes to the nearest codeword.
Complete decoders can use this as their decoding_radius implementation.
"""
d = self.instance.get_minimum_distance()
if d <= 0:
return None
return (d - 1) // 2
def decode_syndrome(self, s: vector, l: int) -> vector | None:
"""Return an error estimate for the syndrome s, or None on failure."""
raise NotImplementedError()
def decode_codeword(self, y: vector, l: int) -> vector:
"""Return the codeword closest to the received word y."""
raise NotImplementedError()
def error_estimate(self, s: vector, l: int) -> vector | None:
"""The unique error estimate the decoder assigns to the syndrome s.
Returns ``None`` if the decoder fails on ``s``.
"""
s.set_immutable()
if self.decodes_syndromes:
return self.decode_syndrome(s, l)
# if the decoder is a codeword decoder:
# 1. compute syndrome representative rho
# 2. decode rho -> c
# 3. report rho - c
key = (l, s)
if key not in self._estimate_cache:
rho = self.representative(s)
try:
codeword = self.decode_codeword(rho, l)
except DecodingError:
estimate = None
else:
estimate = rho - codeword
estimate.set_immutable()
self._estimate_cache[key] = estimate
return self._estimate_cache[key]
def compute_benchmarks(self, l: int, n_errors: int, n_tries: int | None):
correct = []
incorrect = []
m = self.instance.get_m()
for e in generate_errors(self.instance.field, m, n_errors, n_tries):
s = self.H * e
estimate = self.error_estimate(s, l)
if estimate is not None and estimate == e:
correct.append(e)
else:
incorrect.append(e)
n_total = len(correct) + len(incorrect)
if n_total == 0:
raise ValueError(f"No errors of weight {n_errors} exist for a code of length {m}")
epsilon = len(incorrect) / n_total
self.benchmarks[(l, n_errors, n_tries)] = BenchmarkResult(correct, incorrect, epsilon)
def get_benchmarks(self, l: int, n_errors: int, n_tries: int | None = 100) -> BenchmarkResult:
key = (l, n_errors, n_tries)
if key not in self.benchmarks:
self.compute_benchmarks(l, n_errors, n_tries)
return self.benchmarks[key]
class NearestNeighborDecoder(AbstractDecoder):
@staticmethod
def constructor() -> Callable[[MaxLinSat], AbstractDecoder]:
return NearestNeighborDecoder
def __init__(self, instance: MaxLinSat):
super().__init__(instance)
self.decoder = LinearCodeNearestNeighborDecoder(instance.get_code())
def decoding_radius(self) -> int | None:
return self._complete_decoding_radius()
def decode_codeword(self, y: vector, l: int) -> vector:
return self.decoder.decode_to_code(y)
class SyndromeDecoder(AbstractDecoder):
@staticmethod
def constructor(**decoder_parameters) -> Callable[[MaxLinSat], AbstractDecoder]:
return lambda instance: SyndromeDecoder(instance, **decoder_parameters)
def __init__(self, instance: MaxLinSat, **decoder_parameters):
super().__init__(instance)
# accepted for interface compatibility, LinearCodeSyndromeDecoder has no options
self.decoder_parameters = decoder_parameters
self.decoders = {}
self.syndrome_tables = {}
self.syndrome_arrays = {}
def get_decoder(self, l: int) -> LinearCodeSyndromeDecoder:
valid_ls = [k for k in self.decoders if k >= l]
if len(valid_ls) > 0:
return self.decoders[min(valid_ls)]
decoder = LinearCodeSyndromeDecoder(self.instance.get_code(), l)
self.decoders[l] = decoder
# The syndrome decoder is based on code.parity_check_matrix(),
# which can differ from B^T.
# We must therefore create our own table
table = {}
for err in decoder.syndrome_table().values():
e = vector(self.instance.field, err)
e.set_immutable()
s = self.H * e
s.set_immutable()
table[s] = e
self.syndrome_tables[l] = table
return decoder
def get_syndrome_table(self, l: int) -> dict:
valid_ls = [k for k in self.syndrome_tables if k >= l]
if len(valid_ls) > 0:
return self.syndrome_tables[min(valid_ls)]
self.get_decoder(l)
return self.syndrome_tables[l]
def get_syndrome_error_array(self, l: int) -> np.ndarray:
"""The coset leaders of ``get_syndrome_table(l)`` as one (T, m) integer array.
The table is a map from syndrome to error, and the error determines the
syndrome (``s = H e``), so the errors alone carry all of it -- a consumer
that wants the syndromes gets them with one integer matrix product.
Converting the Sage vectors entry by entry costs O(T m) Python calls,
which is more than any numpy code that consumes the result, so the array
is built once per table and cached next to it. Rows are in the
insertion order of the table.
"""
table = self.get_syndrome_table(l)
level = min(k for k in self.syndrome_tables if k >= l)
if level not in self.syndrome_arrays:
self.syndrome_arrays[level] = self._to_array(list(table.values()))
return self.syndrome_arrays[level]
def _to_array(self, errors: list) -> np.ndarray:
"""Stack error vectors into an (T, m) integer array."""
m = self.instance.get_m()
if len(errors) == 0:
return np.zeros((0, m), dtype=np.int64)
if self.instance.field.order() != 2:
return np.array([[int(c) for c in e] for e in errors], dtype=np.int64)
# Over GF(2) an error *is* its support, and support() runs inside Sage,
# so only the few non-zero positions of each error (at most the table's
# error weight) pass through Python instead of all m coordinates. On a
# table of 500 000 leaders at m = 60 that is 1.3 s instead of 8 s.
supports = [e.support() for e in errors]
lengths = np.fromiter(map(len, supports), dtype=np.int64, count=len(supports))
array = np.zeros((len(errors), m), dtype=np.int64)
array[
np.repeat(np.arange(len(errors), dtype=np.int64), lengths),
np.fromiter(
itertools.chain.from_iterable(supports),
dtype=np.int64,
count=int(lengths.sum()),
),
] = 1
return array
def decoding_radius(self) -> int | None:
return self._complete_decoding_radius()
def decode_syndrome(self, s: vector, l: int) -> vector | None:
s.set_immutable()
return self.get_syndrome_table(l).get(s)
class InformationSetDecoder(AbstractDecoder):
@staticmethod
def constructor(**decoder_parameters) -> Callable[[MaxLinSat], AbstractDecoder]:
return lambda instance: InformationSetDecoder(instance, **decoder_parameters)
def __init__(
self,
instance: MaxLinSat,
seed: int | None = 0,
search_size: int | None = None,
**decoder_parameters,
):
"""Information-set decoding (Sage's Lee-Brickell by default).
seed parameter is used for decoder's RNG.
A value of None results in a random seed
Set search_size to an explicit value (<= l) to make results fully reproducible, potentially at the cost of speed.
If search_size is None, decoder hyper-parameters are selected automatically,
depending on the hardware.
"""
super().__init__(instance)
self.decoder_parameters = decoder_parameters
self.search_size = search_size
self._randstate = None if seed is None else randstate(seed)
self.decoders = {}
def get_decoder(self, l: int) -> LinearCodeInformationSetDecoder:
if l not in self.decoders:
parameters = dict(self.decoder_parameters)
if self.search_size is not None:
# Sage accepts algorithm parameters only when explicitly selecting an algorithm
parameters.setdefault("algorithm", "Lee-Brickell")
parameters["search_size"] = min(self.search_size, l)
self.decoders[l] = LinearCodeInformationSetDecoder(
self.instance.get_code(), l, **parameters
)
return self.decoders[l]
def decoding_radius(self):
return None
def decode_codeword(self, y: vector, l: int) -> vector:
if self._randstate is None:
return self.get_decoder(l).decode_to_code(y)
with self._randstate:
return self.get_decoder(l).decode_to_code(y)
class BeliefPropagationDecoder(AbstractDecoder):
@staticmethod
def constructor(**decoder_parameters) -> Callable[[MaxLinSat], AbstractDecoder]:
return lambda instance: BeliefPropagationDecoder(instance, **decoder_parameters)
def __init__(self, instance: MaxLinSat, **decoder_parameters):
super().__init__(instance)
# the ldpc decoder must use the same parity check matrix (and hence the
# same syndrome basis) as the framework: H = B^T
H = np.array(self.H.list(), dtype=np.int8).reshape(self.H.nrows(), self.H.ncols())
self.H_sparse = sp.sparse.csc_matrix(H)
self.decoder_parameters = decoder_parameters
self.decoders = {}
def decoding_radius(self) -> None:
return None
def get_decoder(self, l: int) -> BpDecoder:
if l not in self.decoders:
error_rate = l / self.instance.get_m()
parameters = dict(self.decoder_parameters)
parameters["input_vector_type"] = "syndrome"
self.decoders[l] = BpDecoder(self.H_sparse, error_rate=error_rate, **parameters)
return self.decoders[l]
def decode_syndrome(self, s: vector, l: int) -> vector:
syndrome_np = np.array([int(x) for x in s], dtype=np.uint8)
decoder = self.get_decoder(l)
result_np = decoder.decode(syndrome_np)
return vector(self.instance.field, result_np)
class BeliefPropagationOsdDecoder(AbstractDecoder):
@staticmethod
def constructor(**decoder_parameters) -> Callable[[MaxLinSat], AbstractDecoder]:
return lambda instance: BeliefPropagationOsdDecoder(instance, **decoder_parameters)
def decoding_radius(self) -> None:
return None
def __init__(self, instance: MaxLinSat, **decoder_parameters):
super().__init__(instance)
H = np.array(self.H.list(), dtype=np.int8).reshape(self.H.nrows(), self.H.ncols())
self.H_sparse = sp.sparse.csc_matrix(H)
self.decoder_parameters = decoder_parameters
self.decoders = {}
def get_decoder(self, l: int) -> BpOsdDecoder:
if l not in self.decoders:
error_rate = l / self.instance.get_m()
self.decoders[l] = BpOsdDecoder(
self.H_sparse, error_rate=error_rate, **self.decoder_parameters
)
return self.decoders[l]
def decode_syndrome(self, s: vector, l: int) -> vector:
syndrome_np = np.array([int(x) for x in s], dtype=np.uint8)
decoder = self.get_decoder(l)
result_np = decoder.decode(syndrome_np)
return vector(self.instance.field, result_np)
class GeneralizedReedSolomonDecoder(AbstractDecoder):
@staticmethod
def constructor() -> Callable[[MaxLinSat], AbstractDecoder]:
return GeneralizedReedSolomonDecoder
def __init__(self, instance: MaxLinSat):
super().__init__(instance)
code = self.instance.get_code()
if not isinstance(code, GeneralizedReedSolomonCode):
raise TypeError(
f"GeneralizedReedSolomonDecoder needs a GRS code, got {type(code).__name__}"
)
self.code = code
self.decoder = GRSBerlekampWelchDecoder(code)
self.list_decoders = {}
def get_decoder_by_l(self, l: int) -> GRSBerlekampWelchDecoder | GRSGuruswamiSudanDecoder:
radius = self.decoding_radius()
if l <= radius:
return self.decoder
if l not in self.list_decoders:
# use unique decoder as fallback
decoder = self.decoder
for tau in range(l, radius, -1):
# Using a decoding radius beyond the theoretical list-decoding limit (Johnson bound) fails
try:
decoder = GRSGuruswamiSudanDecoder(self.code, tau)
break
except ValueError:
continue
self.list_decoders[l] = decoder
return self.list_decoders[l]
def decoding_radius(self) -> int:
return (self.instance.get_minimum_distance() - 1) // 2
def decode_codeword(self, y: vector, l: int) -> vector:
try:
result = self.get_decoder_by_l(l).decode_to_code(y)
except ValueError as ex:
# Workaround for a (potential) Sage bug:
# The Berlekamp-Welch decoder can raise a ValueError instead of a DecodingError when given a word beyond its radius
raise DecodingError(str(ex)) from ex
if isinstance(result, list):
# The list decoder producing an empty list is a decoding error
if len(result) == 0:
raise DecodingError("list decoding returned no codeword")
return min(result, key=lambda c: (y - c).hamming_weight())
return result
def DEFAULT_DECODER_CONSTRUCTOR(instance):
if instance.field.order() == 2:
return BeliefPropagationDecoder(instance)
return InformationSetDecoder(instance)