-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfactory_script.py
More file actions
639 lines (602 loc) · 27.9 KB
/
factory_script.py
File metadata and controls
639 lines (602 loc) · 27.9 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import collections
import itertools
import json
import pathlib
import urllib.parse
from typing import Literal, Any, Iterable, cast
from latte.factory_script_instruction import FactoryScriptInstruction, \
PauliStringTarget, TQubitTarget
from latte.vec_sim import VecSim
class FactoryScript:
"""Stores a parsed script defining a distillation factory.
An example script:
PROMISE num_t_outputs=1
PROMISE distance=3
PROMISE assume_checks_fail_with_certainty=True
PROMISE num_t_used=15
PROMISE max_storage=5
PROMISE num_t_checks=4
ALLOC XXXXX
T ZZZ__
T ZZ_Z_
T ZZ__Z
T Z_ZZ_
T Z_Z_Z
T Z__ZZ
T _ZZZ_
T _ZZ_Z
T _Z_ZZ
T __ZZZ
T ZZZZZ
FLIP XXXXX
POSTSELECT_RELEASE T0 T1 T2 T3
OUTPUT_RELEASE T4
"""
def __init__(
self,
*,
instructions: Iterable['FactoryScriptInstruction'],
name: str,
distance: int = -1,
num_t_used: int | Literal['auto'] = 'auto',
num_t_outputs: int | Literal['auto'] = 'auto',
num_checks: int | Literal['auto'] = 'auto',
max_storage: int | Literal['auto'] = 'auto',
num_t_checks: int | Literal['auto'] = 'auto',
assume_checks_fail_with_certainty: bool = False,
):
self.instructions = tuple(instructions)
assert all(isinstance(e, FactoryScriptInstruction) for e in self.instructions)
self.name = name
self.distance = distance
self.num_t_used: int = num_t_used if num_t_used != 'auto' else self._recompute_num_t_used()
self.num_t_checks: int = num_t_checks if num_t_checks != 'auto' else self._recompute_num_t_checks()
self.num_t_outputs: int = num_t_outputs if num_t_outputs != 'auto' else self._recompute_num_t_outputs()
self.num_checks: int = num_checks if num_checks != 'auto' else self._recompute_num_checks()
self.max_storage: int = max_storage if max_storage != 'auto' else self._recompute_max_storage()
self.assume_checks_fail_with_certainty = assume_checks_fail_with_certainty
self.max_qubit_index = self._recompute_max_qubit_index()
@staticmethod
def read_from_path(path: str | pathlib.Path):
with open(path, 'r') as f:
contents = f.read()
return FactoryScript.read_from_file_contents(name=pathlib.Path(path).name, contents=contents)
@staticmethod
def read_from_file_contents(*, name: str, contents: str):
instructions = []
promises = {}
for line in contents.splitlines():
if '#' in line:
line = line.split('#')[0]
line = line.strip()
if line.lower().startswith('promise'):
line = line[7:].strip()
k, v = line.split('=')
if v == 'True':
v = True
else:
v = int(v)
promises[k.strip()] = v
else:
parsed = FactoryScriptInstruction.from_line(line)
if parsed is not None:
instructions.append(parsed)
num_t_used = promises.pop('num_t_used', 'auto')
num_t_outputs = promises.pop('num_t_outputs', 'auto')
num_checks = promises.pop('num_checks', 'auto')
max_storage = promises.pop('max_storage', 'auto')
num_t_checks = promises.pop('num_t_checks', 'auto')
distance = promises.pop('distance', -1)
assume_checks_fail_with_certainty = promises.pop('assume_checks_fail_with_certainty', False)
if promises:
raise NotImplementedError(f'{promises=}')
return FactoryScript(
instructions=instructions,
name=name,
distance=distance,
num_t_used=num_t_used,
num_t_outputs=num_t_outputs,
num_checks=num_checks,
max_storage=max_storage,
num_t_checks=num_t_checks,
assume_checks_fail_with_certainty=assume_checks_fail_with_certainty,
)
@staticmethod
def from_quirk_url_approx(url: str) -> 'FactoryScript':
url = urllib.parse.unquote(url)
assert '#circuit=' in url
circuit_text = url.split('#circuit=')[1]
cols = json.loads(circuit_text)['cols']
instructions = []
for col in cols:
controls = []
swaps = []
parities: dict[int, Literal['X', 'Y', 'Z']] = {}
global_phase = 0
for k, c in enumerate(col):
if c == '•':
controls.append(k)
elif c == 'Swap':
swaps.append(k)
elif c == 'xpar':
parities[k] = 'X'
elif c == 'ypar':
parities[k] = 'Y'
elif c == 'zpar':
parities[k] = 'Z'
elif c == '√i':
global_phase += 1
elif c == '√-i':
global_phase -= 1
elif c == 'i':
global_phase += 2
elif c == '-i':
global_phase -= 2
else:
continue
col[k] = 1
if controls and global_phase:
raise NotImplementedError()
if len(swaps) == 2:
instructions.append(FactoryScriptInstruction(name='SWAP', targets=tuple(swaps)))
elif len(swaps) == 0:
pass
else:
raise NotImplementedError(f'{swaps=}')
if global_phase == 0:
pass
elif global_phase == 1:
instructions.append(FactoryScriptInstruction(name='T', targets=(PauliStringTarget(parities, sign=+1),)))
elif global_phase == -1:
instructions.append(FactoryScriptInstruction(name='T', targets=(PauliStringTarget(parities, sign=-1),)))
else:
raise NotImplementedError(f'{global_phase=}')
for k, c in enumerate(col):
if c == 1:
continue
elif c == 'H':
instructions.append(FactoryScriptInstruction(name='H', targets=(k,)))
elif c == 'X' or c == 'Z':
assert not parities
if len(controls) == 1:
instructions.append(FactoryScriptInstruction(name=cast(Any, f'C{c}'), targets=(controls[0], k)))
elif len(controls) == 0:
instructions.append(FactoryScriptInstruction(name='FLIP', targets=(PauliStringTarget({k: c}, sign=1),)))
else:
raise NotImplementedError(f'{c=}, {controls=}')
elif c == '|0⟩⟨0|':
assert not parities and not controls
instructions.append(FactoryScriptInstruction(name='POSTSELECT', targets=(k,)))
elif c == '|+⟩⟨+|':
assert not parities and not controls
instructions.append(FactoryScriptInstruction(name='POSTSELECT', targets=(PauliStringTarget({k: 'X'}, sign=1),)))
elif c == 'Z^-¼':
assert not controls and not parities
instructions.append(FactoryScriptInstruction(name='T', targets=(PauliStringTarget({k: 'Z'}, sign=-1),)))
elif c == 'Z^¼':
assert not controls and not parities
instructions.append(FactoryScriptInstruction(name='T', targets=(PauliStringTarget({k: 'Z'}, sign=1),)))
elif c == '…' or c == 'Amps1' or c == 'Amps3':
pass
else:
raise NotImplementedError(c)
return FactoryScript(name='quirk', instructions=instructions)
def _recompute_num_t_used(self) -> int:
n = 0
for instruction in self.instructions:
if instruction.name == 'T':
n += len(instruction.targets)
elif instruction.name == 'POSTSELECT_RELEASE' or instruction.name == 'ALLOC':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
n += 1
return n
def _recompute_num_t_outputs(self) -> int:
n = 0
for instruction in self.instructions:
if instruction.name == 'OUTPUT_RELEASE':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
n += 1
return n
def _recompute_num_t_checks(self) -> int:
n = 0
for instruction in self.instructions:
if instruction.name == 'POSTSELECT_RELEASE':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
n += 1
return n
def _recompute_num_checks(self) -> int:
n = 0
for instruction in self.instructions:
if instruction.name == 'POSTSELECT_RELEASE':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
n += len(t.q2i)
else:
n += 1
elif instruction.name == 'POSTSELECT':
n += len(instruction.targets)
return n
def _recompute_max_storage(self) -> int:
q = 0
max_q = 0
for instruction in self.instructions:
if instruction.name == 'ALLOC':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
q += len(t.q2i)
else:
q += 1
max_q = max(max_q, q)
if instruction.name == 'POSTSELECT_RELEASE' or instruction.name == 'OUTPUT_RELEASE' or instruction.name == 'RELEASE':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
q -= len(t.q2i)
elif isinstance(t, (int, TQubitTarget)):
q -= 1
return max_q
def _recompute_max_qubit_index(self) -> int:
max_q = 0
for instruction in self.instructions:
for t in instruction.targets:
if isinstance(t, int):
max_q = max(t, max_q)
elif isinstance(t, TQubitTarget):
max_q = max(t.q, max_q)
elif isinstance(t, PauliStringTarget):
max_q = max(max(t.q2i.keys()), max_q)
else:
raise NotImplementedError(f'{instruction=}')
return max_q
def verify(
self,
*,
count_uncaught_benign_as_error: bool,
require_distance_exact: bool,
skip_distance_check: bool = False,
):
if self.num_t_used != self._recompute_num_t_used():
raise ValueError(f'{self.num_t_used=} != {self._recompute_num_t_used()=}')
if self.num_t_checks != self._recompute_num_t_checks():
raise ValueError(f'{self.num_t_checks=} != {self._recompute_num_t_checks()=}')
if self.num_t_outputs != self._recompute_num_t_outputs():
raise ValueError(f'{self.num_t_outputs=} != {self._recompute_num_t_outputs()=}')
if self.num_checks != self._recompute_num_checks():
raise ValueError(f'{self.num_checks=} != {self._recompute_num_checks()=}')
if self.max_storage != self._recompute_max_storage():
raise ValueError(f'{self.max_storage=} != {self._recompute_max_storage()=}')
outs = self.simulate_with_injected_t_errors(
set(),
prefer_check_result=True,
prefer_output_result=True,
)
if any(flag for _, flag in outs):
raise ValueError("Noiseless execution didn't succeed unconditionally.")
if not skip_distance_check:
self._verify_distance(
count_uncaught_benign_as_error=count_uncaught_benign_as_error,
require_distance_exact=require_distance_exact,
)
def _verify_distance(self, *, count_uncaught_benign_as_error: bool, require_distance_exact: bool):
def _fail_distance_verify(err_msg: str):
raise ValueError(f"{err_msg}\n"
f" name={self.name}\n"
f" errors={err_set_str(inject_key, self.num_t_used)}\n"
f" checks={err_set_str(caught_key, self.num_checks)}\n"
f" output={err_set_str(fail_key, self.num_t_outputs)}")
seen_failures = collections.defaultdict(list)
saw_exact_distance_count = 0
for d in range(max(1, self.distance + require_distance_exact)):
if self.assume_checks_fail_with_certainty and d > self.distance / 2 + (0.5 if require_distance_exact else 0):
break
for injected in itertools.combinations(range(self.num_t_used), d):
inject_key = 0
for j in injected:
inject_key |= 1 << j
outs = self.simulate_with_injected_t_errors(
set(injected),
prefer_check_result=None if injected else True,
prefer_output_result=None if injected else True,
)
caught_key = 0
fail_key = 0
caught_index = 0
fail_index = 0
for out in outs:
if out[0] == 'CHECK':
if out[1]:
caught_key |= 1 << caught_index
caught_index += 1
elif out[0] == 'OUTPUT':
if out[1]:
fail_key |= 1 << fail_index
fail_index += 1
else:
raise NotImplementedError(f'{out=}')
if fail_key and not inject_key:
_fail_distance_verify("Noiseless case had bad outputs.")
elif caught_key and not inject_key:
_fail_distance_verify("Noiseless case had detection events.")
elif fail_key and not caught_key:
if len(injected) == d:
saw_exact_distance_count += 1
else:
_fail_distance_verify("Failed to catch a bad output.")
elif not caught_key and inject_key and count_uncaught_benign_as_error:
if len(injected) == d:
saw_exact_distance_count += 1
else:
_fail_distance_verify("Failed to catch an injected error, even though it was benign, because count_uncaught_benign_as_error=True.")
if self.assume_checks_fail_with_certainty:
self_inject_key = inject_key
self_caught_key = caught_key
self_fail_key = fail_key
if self_caught_key in seen_failures:
for other_inject_key, other_fail_key in seen_failures[self_caught_key]:
inject_key = self_inject_key ^ other_inject_key
caught_key = 0
fail_key = self_fail_key ^ other_fail_key
if fail_key:
if inject_key.bit_count() < self.distance:
_fail_distance_verify("Failed to catch a bad output.")
elif inject_key.bit_count() == self.distance:
saw_exact_distance_count += 1
if count_uncaught_benign_as_error:
for other_inject_key, other_fail_key in seen_failures[self_caught_key]:
if other_inject_key:
inject_key = self_inject_key ^ other_inject_key
caught_key = 0
fail_key = self_fail_key ^ other_fail_key
if inject_key.bit_count() < self.distance:
_fail_distance_verify("Failed to catch an injected error, even though it was benign, because count_uncaught_benign_as_error=True.")
elif inject_key.bit_count() == self.distance:
saw_exact_distance_count += 1
seen_failures[self_caught_key].append((self_inject_key, self_fail_key))
if require_distance_exact and saw_exact_distance_count == 0 and self.distance != -1:
raise ValueError("Distance is too low.")
def __str__(self) -> str:
return self.name
def to_instructions(self) -> str:
return '\n'.join(str(e) for e in self.instructions)
def to_quirk_url(self) -> str:
circuit_cols = []
for instruction in self.instructions:
cols = [{}, {}, {}]
col = cols[0]
if instruction.name == 'ALLOC':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
for q, p in t.q2i.items():
if p == 'X':
col[q] = 'H'
elif p == 'Z':
col[q] = 1
elif p == 'Y':
col[q] = 'X^½'
elif isinstance(t, TQubitTarget):
cols[0][t.q] = 'H'
cols[1][t.q] = 'Z^-¼' if t.dag else 'Z^¼'
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'FLIP':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
for q, p in t.q2i.items():
col[q] = p
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'CX' or instruction.name == 'CZ':
assert all(isinstance(e, int) for e in instruction.targets)
assert len(instruction.targets) % 2 == 0
for k in range(0, len(instruction.targets), 2):
a = instruction.targets[k]
b = instruction.targets[k + 1]
while len(cols) <= k // 2:
cols.append({})
cols[k // 2][a] = '•'
cols[k // 2][b] = 'X' if instruction.name == 'CX' else 'Z'
elif instruction.name == 'SWAP':
assert all(isinstance(e, int) for e in instruction.targets)
assert len(instruction.targets) % 2 == 0
for k in range(0, len(instruction.targets), 2):
a = instruction.targets[k]
b = instruction.targets[k + 1]
while len(cols) <= k // 2:
cols.append({})
if a > b:
a, b = b, a
if b == a + 1:
cols[k // 2][a] = '<<2'
else:
cols[k // 2][a] = 'Swap'
cols[k // 2][b] = 'Swap'
elif instruction.name == 'T' or instruction.name == 'MAGIC_PERFECT_T':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
free_q = set(range(min(16, self.max_qubit_index + 2)))
for q, p in t.q2i.items():
col[q] = p.lower() + 'par'
free_q.remove(q)
anc_q = max(free_q)
col[anc_q] = '√-i' if t.sign == -1 else '√i'
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'H':
for t in instruction.targets:
if isinstance(t, int):
col[t] = 'H'
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'MAGIC_PERFECT_CCZ':
a, b, c = instruction.targets
col[a] = '•'
col[b] = '•'
col[c] = 'Z'
elif instruction.name == 'MAGIC_PERFECT_CS':
a, b = instruction.targets
col[a] = '•'
col[b] = 'Z^½'
elif instruction.name == 'S':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
if len(t.q2i) == 1:
(q, p), = t.q2i.items()
col[q] = p + '^' + ('-' if t.sign == -1 else '') + '½'
else:
free_q = set(range(min(16, self.max_qubit_index + 2)))
for q, p in t.q2i.items():
col[q] = p.lower() + 'par'
free_q.remove(q)
anc_q = max(free_q)
col[anc_q] = '-i' if t.sign == -1 else 'i'
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'MAGIC_DETERMINISTIC_PREP' or instruction.name == 'POSTSELECT':
for t in instruction.targets:
if isinstance(t, PauliStringTarget):
free_q = set(range(min(16, self.max_qubit_index + 2)))
for q, p in t.q2i.items():
if len(t.q2i) == 1:
if p == 'X':
col[q] = '|+⟩⟨+|'
elif p == 'Y':
col[q] = '|X⟩⟨X|'
elif p == 'Z':
col[q] = '|0⟩⟨0|'
else:
raise NotImplementedError(f'{instruction=}')
else:
col[q] = p.lower() + 'par'
free_q.remove(q)
if not free_q:
if cols[0][0] == 'xpar':
cols[0][0] = 'Z'
cols[1][0] = '|+⟩⟨+|'
elif cols[0][0] == 'ypar':
cols[0][0] = 'Z'
cols[1][0] = '|X⟩⟨X|'
elif cols[0][0] == 'zpar':
cols[0][0] = 'X'
cols[1][0] = '|0⟩⟨0|'
cols[2] = cols[0]
elif len(t.q2i) != 1:
col[max(free_q)] = '0'
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'POSTSELECT_RELEASE':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
cols[0][t.q] = 'Z^¼' if t.dag else 'Z^-¼'
cols[1][t.q] = 'H'
cols[2][t.q] = '|0⟩⟨0|'
elif isinstance(t, PauliStringTarget):
for q, p in t.q2i.items():
if p == 'X':
cols[1][q] = 'H'
cols[2][q] = '|0⟩⟨0|'
elif p == 'Y':
cols[1][q] = 'X^½'
cols[2][q] = '|0⟩⟨0|'
elif p == 'Z':
cols[2][q] = '|0⟩⟨0|'
else:
raise NotImplementedError(f'{instruction=}')
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'OUTPUT_RELEASE':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
cols[0][t.q] = 'Z^¼' if t.dag else 'Z^-¼'
cols[1][t.q] = 'H'
cols[2][t.q] = '…'
else:
raise NotImplementedError(f'{instruction=}')
elif instruction.name == 'RELEASE':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
cols[0][t.q] = 'Z^¼' if t.dag else 'Z^-¼'
cols[1][t.q] = 'H'
cols[2][t.q] = 'NeGate'
elif isinstance(t, PauliStringTarget):
for q, p in t.q2i.items():
if p == 'X':
cols[1][q] = 'H'
cols[2][q] = 'NeGate'
elif p == 'Y':
cols[1][q] = 'X^½'
cols[2][q] = 'NeGate'
elif p == 'Z':
cols[2][q] = 'NeGate'
else:
raise NotImplementedError(f'{instruction=}')
else:
raise NotImplementedError(f'{instruction=}')
else:
raise NotImplementedError(f'{instruction=}')
for c in cols:
if not c:
continue
col2 = []
for k, v in c.items():
while len(col2) <= k:
col2.append(1)
col2[k] = v
circuit_cols.append(col2)
return 'https://algassert.com/quirk#circuit=' + urllib.parse.quote(json.dumps({'cols': circuit_cols}))
def simulate_with_injected_t_errors(
self,
injected: set[int],
*,
prefer_check_result: bool | None = None,
prefer_output_result: bool | None = None,
) -> list[Any]:
sim = VecSim()
err_index = 0
outs = []
def should_apply_next_error() -> bool:
nonlocal err_index
result = err_index in injected
err_index += 1
return result
def forward(sub_instruction: FactoryScriptInstruction):
outs.extend(sub_instruction.apply_to(
sim,
prefer_check_result=prefer_check_result,
prefer_output_result=prefer_output_result))
for instruction in self.instructions:
if instruction.name == 'T':
for t in instruction.targets:
if should_apply_next_error():
if isinstance(t, PauliStringTarget):
sim.do_paulis(t.q2i)
elif isinstance(t, int):
sim.do_z(t)
else:
raise NotImplementedError(f'{t=}')
forward(FactoryScriptInstruction(instruction.name, (t,)))
elif instruction.name == 'POSTSELECT_RELEASE':
for t in instruction.targets:
if isinstance(t, TQubitTarget):
if should_apply_next_error():
sim.do_z(t.q)
forward(FactoryScriptInstruction(instruction.name, (t,)))
elif instruction.name == 'ALLOC':
for t in instruction.targets:
forward(FactoryScriptInstruction(instruction.name, (t,)))
if isinstance(t, TQubitTarget):
if should_apply_next_error():
sim.do_z(t.q)
else:
forward(instruction)
return outs
def err_set_str(x: int, n: int) -> str:
assert x < 2**n, (x, 2**n)
if n == 0:
return ''
ks = []
for k in range(n):
if x & (1 << k):
ks.append(str(k))
return bin(x)[2:].rjust(n, '0').replace('0', '_').replace('1', 'E')[::-1] + '{' + ','.join(ks) + '}'