-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlattice_surgery_layer_with_feedback.py
More file actions
370 lines (325 loc) · 13.4 KB
/
lattice_surgery_layer_with_feedback.py
File metadata and controls
370 lines (325 loc) · 13.4 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
import dataclasses
from typing import Iterable, Literal, cast, Any
from latte.lattice_surgery_layer import LatticeSurgeryLayer, InjectedError
from latte.vec_sim import VecSim
@dataclasses.dataclass
class Expression:
variables: tuple[str, ...]
op: Literal['', '^', '&', '<', '+', '&&', '^^']
def __str__(self):
return self.op.join(self.variables)
def eval(self, state: dict[str, bool | int]) -> int | complex:
v = self.try_eval(state)
if v is None:
for r in self.variables:
if r not in state:
raise ValueError(f"Variable not defined yet: {r}")
assert False, (self, state)
return v
def try_eval(self, state: dict[str, bool | int]) -> int | complex | None:
vals = []
for k in self.variables:
try:
vals.append(int(k))
except ValueError:
try:
vals.append(complex(k))
except ValueError:
if k not in state:
return None
vals.append(state[k])
if self.op == '':
assert len(vals) == 1
return vals[0]
elif self.op == '^':
acc = 0
for v in vals:
acc ^= v
return acc
elif self.op == '^^':
acc = False
for v in vals:
acc ^= bool(v)
return bool(acc)
elif self.op == '+':
acc = 0
for v in vals:
acc += v
return acc
elif self.op == '<':
acc = 1
for k in range(1, len(vals)):
acc &= vals[k - 1] < vals[k]
return acc
elif self.op == '&':
acc = -1
for v in vals:
acc &= v
return acc
elif self.op == '&&':
for v in vals:
if not v:
return False
return True
else:
raise NotImplementedError(f'{self.op=}')
@dataclasses.dataclass
class DiscardShotAction:
condition: Expression
@dataclasses.dataclass
class PrintAction:
condition: Expression
@dataclasses.dataclass
class MeasureAction:
name: str
ground: bool
@dataclasses.dataclass
class ReplaceAction:
pattern: str
false_result: str
true_result: str
expression: Expression
@dataclasses.dataclass
class ResolveAction:
location: complex
expression: Expression
@dataclasses.dataclass
class LetAction:
name: str
expression: Expression
def try_assign(self, state: dict[str, bool | int]) -> bool:
new_val = self.expression.try_eval(state)
if new_val is None:
return False
old_val = state.setdefault(self.name, new_val)
assert old_val == new_val
return True
@dataclasses.dataclass
class FeedbackAction:
target: Expression
condition: Expression
action: Literal['X', 'Y', 'Z', 'S', 'H', 'SQRT_X', 'SQRT_X_DAG']
def parse_single_op_expression(line: str, variables: list[str]) -> Expression:
seen_ops = set()
var_line = ''.join(variables)
for candidate in ['+', '^', '<', '&', '&&', '^^']:
if candidate in var_line:
seen_ops.add(candidate)
if '&&' in seen_ops:
seen_ops.remove('&')
if '^^' in seen_ops:
seen_ops.remove('^')
if len(seen_ops) > 1:
raise NotImplementedError(f'{line=}')
if not seen_ops:
assert len(variables) == 1
return Expression(tuple(variables), op='')
op: Literal['+', '^', '<', '&', '&&', '^^'] = cast(Any, next(iter(seen_ops)))
variables = [e for e in ' '.join(variables).replace(op, ' ' + op + ' ').split(' ') if e]
assert all(e == op for e in variables[1::2])
assert len(variables) % 2 == 1
return Expression(variables=tuple(variables[0::2]), op=op)
class LatticeSurgeryLayerWithFeedback:
def __init__(
self,
*,
layer_diagram: str,
measure_actions: Iterable[MeasureAction],
outputs: Iterable[str],
checks: Iterable[DiscardShotAction],
replace_actions: Iterable[ReplaceAction],
resolve_actions: Iterable[ResolveAction],
let_actions: Iterable[LetAction],
feedback_actions: Iterable[FeedbackAction],
print_actions: Iterable[PrintAction],
):
self.layer_diagram: str = layer_diagram
self.measure_actions: tuple[MeasureAction, ...] = tuple(measure_actions)
self.replace_actions: tuple[ReplaceAction, ...] = tuple(replace_actions)
self.resolve_actions: tuple[ResolveAction, ...] = tuple(resolve_actions)
self.let_actions: tuple[LetAction, ...] = tuple(let_actions)
self.feedback_actions: tuple[FeedbackAction, ...] = tuple(feedback_actions)
self.outputs: tuple[str, ...] = tuple(outputs)
self.checks: tuple[DiscardShotAction, ...] = tuple(checks)
self.print_actions: tuple[PrintAction, ...] = tuple(print_actions)
self._fixed_layer: LatticeSurgeryLayer | None = None
def run(self, sim: VecSim, state: dict[str, bool], *, injected_errors: frozenset[InjectedError]):
layer = self.make_layer(state)
ground = any(m.ground for m in self.measure_actions)
layer_results = sim.do_lattice_surgery_layer(layer, prefer_result=False if ground else None, injected_errors=injected_errors)
# Record named measurement results.
if len(layer_results) != len(self.measure_actions):
raise ValueError(f'The number of "measures" annotations differs from the number of measurements.\n'
f'measurements={[e.name for e in self.measure_actions]}.\n'
f'{len(layer_results)=} != {len(self.measure_actions)=}')
for k, v in zip(self.measure_actions, layer_results):
state[k.name] = bool(v)
# Assign variables.
for action in self.let_actions:
worked = action.try_assign(state)
assert worked, action
# Run feedback expressions.
for action in self.feedback_actions:
target = action.target.eval(state)
condition = action.condition.eval(state)
if condition:
if action.action in 'XYZ':
sim.do_paulis({target: action.action})
elif action.action == 'S':
sim.do_s(target)
elif action.action == 'H':
sim.do_s(target)
elif action.action == 'SQRT_X':
sim.do_h(target)
sim.do_s(target)
sim.do_h(target)
elif action.action == 'SQRT_X_DAG':
sim.do_h(target)
sim.do_s_dag(target)
sim.do_h(target)
elif action.action == 'T':
sim.do_t(target)
else:
raise NotImplementedError(f'{action}')
# Print values.
for print_action in self.print_actions:
print(f'{print_action.condition}={print_action.condition.eval(state)}')
# Check checks.
for check in self.checks:
if check.condition.eval(state):
return 'reject'
# Check outputs.
for out in self.outputs:
if out.upper() == 'CCZ':
assert len(sim.q2i) == 3
a, b, c = sim.q2i
sim.do_ccz(a, b, c)
ma = sim.do_mx_discard(a)
mb = sim.do_mx_discard(b)
mc = sim.do_mx_discard(c)
if ma or mb or mc:
return 'fail'
elif out.upper() == 'TT':
assert len(sim.q2i) == 2
a, b = sim.q2i
sim.do_t(a)
sim.do_t(b)
ma = sim.do_mx_discard(a)
mb = sim.do_mx_discard(b)
if ma or mb:
return 'fail'
elif out == 'print':
qs = sorted(sim.q2i.keys(), key=lambda e: (e.real, e.imag))
print()
print()
print(sim.state_str(order=qs.index))
print()
print()
assert False, '\n\n' + sim.state_str(order=qs.index) + '\n\n'
else:
raise NotImplementedError(f'{out=}')
return 'pass'
def make_layer(self, measurements: dict[str, bool], *, skip_resolving: bool = False) -> LatticeSurgeryLayer:
if self._fixed_layer is not None:
return self._fixed_layer
c = self.layer_diagram
for r in self.let_actions:
r.try_assign(measurements)
for r in self.replace_actions:
val = r.expression.eval(measurements) & 1
c = c.replace(r.pattern, (r.true_result if val else r.false_result).replace('_', ' '))
result = LatticeSurgeryLayer.from_text(c)
if not skip_resolving:
for r in self.resolve_actions:
v = r.expression.eval(measurements)
c = result.nodes[r.location]
assert len(c) == 2
result.nodes[r.location] = cast(Any, c[v & 1])
if not self.replace_actions and not self.resolve_actions:
self._fixed_layer = result
return result
@staticmethod
def from_content(content: str) -> 'LatticeSurgeryLayerWithFeedback':
measure_actions: list[MeasureAction] = []
outputs: list[str] = []
checks: list[DiscardShotAction] = []
prints: list[PrintAction] = []
replace_actions: list[ReplaceAction] = []
resolve_actions: list[ResolveAction] = []
let_actions: list[LetAction] = []
feedback_actions: list[FeedbackAction] = []
kept_lines = []
for line in content.splitlines():
words = [e for e in line.split(' ') if e]
if not words:
words.append('')
if words[0] == 'measures':
assert len(words) == 2, words
measure_actions.append(MeasureAction(name=words[1], ground=False))
elif words[0] == 'ground_measures':
assert len(words) == 2
measure_actions.append(MeasureAction(name=words[1], ground=True))
elif words[0] == 'resolve_at':
_resolve_at, location, _with, *variables = words
assert _resolve_at == 'resolve_at'
assert _with == 'with'
expression = parse_single_op_expression(line, variables)
resolve_actions.append(ResolveAction(location=complex(location), expression=expression))
elif words[0] == 'replace':
_replace, pattern, _with, true_result, _else, false_result, _if, *variables = words
assert _replace == 'replace'
assert _with == 'with'
assert _else == 'else'
assert _if == 'if'
expression = parse_single_op_expression(line, variables)
replace_actions.append(ReplaceAction(pattern=pattern, false_result=false_result, true_result=true_result, expression=expression))
elif words[0] == 'let':
_let, name, _eq, *variables = words
assert _let == 'let'
assert _eq == '='
expression = parse_single_op_expression(line, variables)
let_actions.append(LetAction(name=name, expression=expression))
elif words[0] == 'feedback':
variable: str | None
if len(words) == 4:
_feedback, action, _at, location = words
assert _feedback == 'feedback'
assert _at == 'at'
condition = Expression(('1',), '')
else:
_feedback, action, _at, location, _if, *variables = words
assert _feedback == 'feedback'
assert _at == 'at'
assert _if == 'if'
condition = parse_single_op_expression(line, variables)
assert action in ['X', 'Y', 'Z', 'S', 'H', 'SQRT_X', 'SQRT_X_DAG', 'T']
feedback_actions.append(FeedbackAction(
condition=condition,
target=parse_single_op_expression(line, [location]),
action=cast(Any, action),
))
elif words[0] == 'discard_shot_if':
_check, *variables = words
condition = parse_single_op_expression(line, variables)
checks.append(DiscardShotAction(condition))
elif words[0] == 'print':
_check, *variables = words
assert _check == 'print'
condition = parse_single_op_expression(line, variables)
prints.append(PrintAction(condition))
elif words[0] == 'output_should_be':
assert len(words) == 2
outputs.append(words[1])
else:
kept_lines.append(line)
return LatticeSurgeryLayerWithFeedback(
layer_diagram='\n'.join(kept_lines),
measure_actions=measure_actions,
outputs=outputs,
checks=checks,
replace_actions=replace_actions,
let_actions=let_actions,
feedback_actions=feedback_actions,
print_actions=prints,
resolve_actions=resolve_actions,
)