-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlattice_surgery_layer.py
More file actions
1486 lines (1374 loc) · 59.9 KB
/
lattice_surgery_layer.py
File metadata and controls
1486 lines (1374 loc) · 59.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
import dataclasses
import functools
import random
from typing import Literal, Any, cast, Iterable, TYPE_CHECKING, Callable
from typing import Sequence
import numpy as np
import stim
import gen
from latte.lattice_surgery_instruction import LatticeSurgeryInstruction, ErrorSource
from latte.zx_graph import ZXGraph
if TYPE_CHECKING:
import pygltflib
import networkx as nx
@dataclasses.dataclass(frozen=True, unsafe_hash=True)
class InjectedError:
pos: complex
layer: float
basis: Literal['X', 'Y', 'Z']
def time_shifted_by(self, dt: float) -> 'InjectedError':
return InjectedError(pos=self.pos, layer=self.layer + dt, basis=self.basis)
class LatticeSurgeryLayer:
def __init__(
self,
*,
nodes: dict[complex, Literal['.', 'X', 'Y', 'Z', 'T', 'S', 'XY', 'YZ', 'XZ']],
future_edges: Iterable[complex],
past_edges: Iterable[complex],
edges: Iterable[complex],
x_sink_edges: Iterable[complex],
z_sink_edges: Iterable[complex],
):
"""A layer of lattice surgery, with spacelike and timelike edges.
Args:
nodes: The locations and types of junctions. The locations should
be integer complex coordinates. The types are:
'.': Unused location. Surface code not present here.
'X': X-type junction. Reveals XX for each edge pair, and Z_all.
'Z': Z-type junction. Reveals ZZ for each edge pair, and X_all.
'Y': Y-type leaf, using the construction from
https://arxiv.org/abs/2302.07395 . This node must only have
timelike edges. If a past edge is present, this is a Y
basis measurement. If a future edge is present, this is a Y
basis reset.
'T': T state injection. Randomly prepares T|+> or T†|+>, with
the outcome stored in the measurement record.
'S': S state injection. Randomly prepares |i> or |j>, with
the outcome stored in the measurement record. Differs from
'Y' in that 'Y' is fault tolerant and considered
deterministic.
future_edges: The set of the nodes that will end up as logical
qubits sticking around (propagating into the next layer).
past_edges: The set of the nodes that entered as logical
qubits propagating from the previous layer.
edges: The set of spacelike edges between nodes. Each entry should
be a complex number with one integer coefficient and one half
integer coefficient. If c is a complex number with integer
coordinates, then c+0.5 being in this set means there is an
edge between c and c+1. Similarly, c+0.5j being in this set
means there is an edge between c and c+1j.
x_sink_edges: Locations where trapped Pauli X excitations can
be removed, creating a logical measurement result.
z_sink_edges: Locations where trapped Pauli Z excitations can
be removed, creating a logical measurement result.
"""
self.nodes: dict[complex, Literal['.', 'X', 'Y', 'Z', 'T', 'S', 'XY', 'YZ', 'XZ']] = nodes
self.future_edges: frozenset[complex] = frozenset(future_edges)
self.past_edges: frozenset[complex] = frozenset(past_edges)
self.edges: frozenset[complex] = frozenset(edges)
self.x_sink_edges: frozenset[complex] = frozenset(x_sink_edges)
self.z_sink_edges: frozenset[complex] = frozenset(z_sink_edges)
self._cached_tasks: tuple[LatticeSurgeryInstruction, ...] | None = None
self._cached_tasks_key = None
def list_edge_errors(self) -> list[InjectedError]:
result = []
for v in self.past_edges:
for b in 'XYZ':
result.append(InjectedError(pos=v, layer=-0.5, basis=cast(Any, b)))
for v in self.future_edges:
for b in 'XYZ':
result.append(InjectedError(pos=v, layer=+0.5, basis=cast(Any, b)))
for e in self.edges:
for b in 'XYZ':
result.append(InjectedError(pos=e, layer=0, basis=cast(Any, b)))
return result
def to_stim_circuit(self) -> stim.Circuit:
tasks = self.to_sim_instructions(layer_key='A')
result = stim.Circuit()
q2i = {}
m2i = {}
group2i = {}
def key_to_records(k: Any) -> list[stim.GateTarget]:
if k in group2i:
ms = group2i[k]
elif k in m2i:
ms = [k]
else:
raise ValueError(f'{k=}')
return [stim.target_rec(m2i[m] - len(m2i)) for m in ms]
coords = {}
for n in self.nodes:
q = len(q2i)
q2i[n] = q
coords[n] = (n.real, n.imag)
t_measure_waiting_for_x_herald = None
obs_index = 0
cur_bit = []
for task in tasks:
if t_measure_waiting_for_x_herald is not None and 'herald' not in task.action:
t_measure_waiting_for_x_herald = None
a = task.target
b = task.target2
c = task.action
if c == 'mxx':
result.append('MXX', [q2i[a], q2i[b]])
m2i[task.measure_key] = len(m2i)
elif c == 'mzz':
result.append('MZZ', [q2i[a], q2i[b]])
m2i[task.measure_key] = len(m2i)
elif c == 'cx':
result.append('CX', [q2i[a], q2i[b]])
elif c == 'qalloc_x':
result.append('RX', [q2i[a]])
elif c == 'qalloc_y':
result.append('RY', [q2i[a]])
elif c == 'qalloc_z':
result.append('RZ', [q2i[a]])
elif c == 'm_discard_x':
result.append('MRX', [q2i[a]])
m2i[task.measure_key] = len(m2i)
elif c == 'm_discard_y':
result.append('MRY', [q2i[a]])
m2i[task.measure_key] = len(m2i)
elif c == 'm_discard_z':
result.append('MRZ', [q2i[a]])
m2i[task.measure_key] = len(m2i)
elif c == 't':
q = ('T_INJECT', len(q2i))
q2i[q] = len(q2i)
coords[q] = (733, 57473, len(q2i))
result.append('CX', [q2i[a], q2i[q]])
result.append('MR', [q2i[q]])
m2i[q] = len(m2i)
t_measure_waiting_for_x_herald = q
elif c == 'heralded_random_x':
if t_measure_waiting_for_x_herald is not None:
m2i[task.measure_key] = m2i[t_measure_waiting_for_x_herald]
del m2i[t_measure_waiting_for_x_herald]
t_measure_waiting_for_x_herald = None
else:
result.append('X_ERROR', [q2i[a]], 1)
result.append('MPAD', [1])
m2i[task.measure_key] = len(m2i)
# result.append('HERALDED_PAULI_CHANNEL_1', [q2i[a]], [0, 0.5, 0, 0])
elif c == 'heralded_random_z':
result.append('Z_ERROR', [q2i[a]], 0)
result.append('MPAD', [0])
# result.append('HERALDED_PAULI_CHANNEL_1', [q2i[a]], [0, 0, 0, 0.5])
m2i[task.measure_key] = len(m2i)
elif c == 'accumulator_bit_clear':
cur_bit.clear()
elif c == 'accumulator_bit_xor':
cur_bit.append(task.measure_key)
elif c == 'accumulator_bit_save':
group2i[task.measure_key] = cur_bit
result.append('OBSERVABLE_INCLUDE', key_to_records(task.measure_key), obs_index)
obs_index += 1
elif c == 'feedback_m2x':
result.append('CX', [
x
for t in key_to_records(task.measure_key)
for x in [t, q2i[task.target]]
])
elif c == 'feedback_m2y':
result.append('CY', [
x
for t in key_to_records(task.measure_key)
for x in [t, q2i[task.target]]
])
elif c == 'feedback_m2z':
result.append('CZ', [
x
for t in key_to_records(task.measure_key)
for x in [t, q2i[task.target]]
])
else:
raise NotImplementedError(f'{task=}')
header = stim.Circuit()
for q, c in coords.items():
header.append('QUBIT_COORDS', q2i[q], c)
return header + result
def compute_node_degrees(self) -> collections.Counter:
degrees = collections.Counter()
for e in self.future_edges:
degrees[e] += 1
for e in self.past_edges:
degrees[e] += 1
for e in self.edges:
if e.real % 1 == 0.5:
degrees[e + 0.5] += 1
degrees[e - 0.5] += 1
if e.imag % 1 == 0.5:
degrees[e + 0.5j] += 1
degrees[e - 0.5j] += 1
return degrees
@staticmethod
def solve_lattice_surgery_orientations(
layers: Sequence['LatticeSurgeryLayer'],
ignore_contradictions: bool = False,
) -> dict[tuple[complex, float], bool]:
result = {}
progress = True
while progress:
progress = False
for k in range(len(layers)):
progress |= layers[k]._partial_solve_lattice_surgery_orientations(k, result, ignore_contradictions=ignore_contradictions)
return result
def _partial_solve_lattice_surgery_orientations(
self, layer: int, known: dict[tuple[complex, float], bool],
*,
ignore_contradictions: bool) -> bool:
degrees = self.compute_node_degrees()
new_values = {}
for node, c in self.nodes.items():
if degrees[node] >= 3:
assert c == 'X' or c == 'Z'
has_real = node + 0.5 in self.edges or node - 0.5 in self.edges
has_imag = node + 0.5j in self.edges or node - 0.5j in self.edges
has_time = node in self.past_edges or node in self.future_edges
if has_real + has_time + has_imag != 2:
if ignore_contradictions:
continue
raise ValueError("Lattice surgery junctions must be planar.")
for d in [-0.5, 0.5]:
if node + d in self.edges:
new_values[(node + d, layer)] = (c == 'X') ^ has_time
for d in [-0.5j, 0.5j]:
if node + d in self.edges:
new_values[(node + d, layer)] = (c == 'X') ^ has_time
if node in self.future_edges:
new_values[(node, layer + 0.5)] = (c == 'X') ^ has_imag
if node in self.past_edges:
new_values[(node, layer - 0.5)] = (c == 'X') ^ has_imag
progress = False
for k, v in new_values.items():
v2 = known.get(k, None)
if v2 is None:
known[k] = v
progress = True
elif v != v2 and not ignore_contradictions:
raise ValueError("Inconsistent boundary requirements for lattice surgery.")
new_values.clear()
for node, c in self.nodes.items():
if degrees[node] == 2:
has_time = node in self.past_edges or node in self.future_edges
has_imag = node + 0.5j in self.edges or node - 0.5j in self.edges
has_real = node + 0.5 in self.edges or node - 0.5 in self.edges
inverted_keys = set()
keys = [
(node + d, float(layer))
for d in [-0.5, 0.5, -0.5j, 0.5j]
if node + d in self.edges
]
if node in self.past_edges:
keys.append((node, layer - 0.5))
if node in self.future_edges:
keys.append((node, layer + 0.5))
if has_time and has_imag:
inverted_keys = set(keys)
elif has_time and has_real:
inverted_keys = {k for k in keys if k[1] % 1 == 0.5}
v = None
for k in keys:
v = known.get(k)
if v is not None:
v ^= k in inverted_keys
break
if v is not None:
if c == 'H':
v ^= True
for k in keys:
new_values[k] = v ^ (k in inverted_keys)
for k, v in new_values.items():
v2 = known.get(k, None)
if v2 is None:
known[k] = v
progress = True
elif v != v2 and not ignore_contradictions:
raise ValueError("Inconsistent boundary requirements for lattice surgery.")
new_values.clear()
return progress
@staticmethod
def combined_3d_model_gltf(
layers: Iterable['LatticeSurgeryLayer'],
*,
wireframe: bool = False,
spacing: float = 2,
ignore_contradictions: bool = False,
injected_errors: Iterable[InjectedError],
) -> 'pygltflib.GLTF2':
layers = tuple(layers)
injected_errors = frozenset(injected_errors)
wireframe |= bool(injected_errors)
triangles = []
lines = []
if wireframe:
for k in range(len(layers)):
layers[k].add_wireframe_to_3d_skeleton_gltf(
k,
out_triangles=triangles,
out_lines=lines,
spacing=spacing,
injected_errors=frozenset(
err.time_shifted_by(-k)
for err in injected_errors
if err.layer == k or err.layer == k - 0.5
),
)
else:
orientations = LatticeSurgeryLayer.solve_lattice_surgery_orientations(layers, ignore_contradictions=ignore_contradictions)
for k in range(len(layers)):
layers[k].add_to_3d_model_gltf(k, orientations, out_triangles=triangles, out_lines=lines, spacing=spacing)
return gen.gltf_model_from_colored_triangle_data(triangles, colored_line_data=lines)
def to_3d_model_gltf(self, *, wireframe: bool = False) -> 'pygltflib.GLTF2':
return LatticeSurgeryLayer.combined_3d_model_gltf([self], wireframe=wireframe, injected_errors=frozenset())
def add_to_3d_model_gltf(
self,
layer: int,
orientations: dict[tuple[complex, float], bool],
out_triangles: list[gen.ColoredTriangleData],
out_lines: list[gen.ColoredLineData],
spacing: float,
):
degrees = self.compute_node_degrees()
pitch = spacing + 1
def xy(v):
return np.array([-v[2], v[0], -v[1]])
add_face = functools.partial(add_face_to, xy=xy, out_triangles=out_triangles, out_lines=out_lines)
add_cube = functools.partial(add_cube_to, xy=xy, out_triangles=out_triangles, out_lines=out_lines)
for node, c in self.nodes.items():
x = node.real * pitch
y = node.imag * pitch
z = layer * pitch
if c == 'X' or c == 'Z':
top_bottom = None
left_right = None
front_back = None
for d in [0.5, -0.5, 0.5j, -0.5j]:
v = orientations.get((node + d, layer))
if v is not None:
top_bottom = v
if d.real == 0:
left_right = not v
else:
front_back = not v
v = orientations.get((node, layer - 0.5))
if v is None:
v = orientations.get((node, layer + 0.5))
if v is not None:
front_back = v
left_right = not v
if left_right is None:
left_right = c == 'X'
if front_back is None:
front_back = c == 'X'
if top_bottom is None:
top_bottom = c == 'X'
z_scale = 1
if degrees[node] == 1:
has_real = node + 0.5 in self.edges or node - 0.5 in self.edges
has_imag = node + 0.5j in self.edges or node - 0.5j in self.edges
has_time = node in self.past_edges or node in self.future_edges
if has_real:
left_right = c != 'X'
if has_imag:
front_back = c != 'X'
if has_time:
top_bottom = c != 'X'
if node in self.past_edges:
z_scale = 0.1
left_right = (1, 0, 0, 1) if left_right else (0, 0, 1, 1)
top_bottom = (1, 0, 0, 1) if top_bottom else (0, 0, 1, 1)
front_back = (1, 0, 0, 1) if front_back else (0, 0, 1, 1)
for d in [-0.5, 0.5]:
if node + d not in self.edges:
add_face([x + d, y, z - (1 - z_scale) / 2], d1=(0, 1, 0), d2=(0, 0, z_scale), rgba=left_right, include_lines=True, include_triangles=True)
if node + d * 1j not in self.edges:
add_face([x, y + d, z - (1 - z_scale) / 2], d1=(1, 0, 0), d2=(0, 0, z_scale), rgba=front_back, include_lines=True, include_triangles=True)
for (b, d) in [(node in self.past_edges, -z_scale*0.5), (node in self.future_edges, +z_scale*0.5)]:
if not b:
add_face([x, y, z + d - (1 - z_scale) / 2], d1=(1, 0, 0), d2=(0, 1, 0), rgba=top_bottom, include_lines=True, include_triangles=True)
add_cube([x, y, z - (1 - z_scale) / 2], sz=z_scale, rgba=(0, 0, 0, 1), include_triangles=False, include_lines=True)
elif c == 'H':
add_cube([x, y, z], rgba=(1, 1, 0, 1), include_triangles=True, include_lines=True)
elif len(c) >= 2 and set(c) <= set('XYZ'):
if c[0] == 'X':
rgba1 = (0, 0, 1, 1) if degrees[node] == 1 else (1, 0, 0, 1)
elif c[0] == 'Z':
rgba1 = (0, 0, 1, 1) if degrees[node] != 1 else (1, 0, 0, 1)
elif c[0] == 'Y':
rgba1 = (0, 1, 0, 1)
else:
rgba1 = (0.5, 0.5, 0.5, 1)
if c[1] == 'X':
rgba2 = (0, 0, 1, 1) if degrees[node] == 1 else (1, 0, 0, 1)
elif c[1] == 'Z':
rgba2 = (0, 0, 1, 1) if degrees[node] != 1 else (1, 0, 0, 1)
elif c[1] == 'Y':
rgba2 = (0, 1, 0, 1)
else:
rgba2 = (0.5, 0.5, 0.5, 1)
_draw_interlocking_double_color_cube(
x=x,
y=y,
z=z - 0.25 if degrees[node] == 1 else 0.5,
sx=0.5,
sy=0.5,
sz=0.25 if degrees[node] == 1 else 0.5,
xy=xy,
rgba1=rgba1,
rgba2=rgba2,
out_triangles=out_triangles,
out_lines=out_lines,
)
continue
elif c != '.':
if c == 'T':
rgba = (1, 0, 1, 1)
elif c == 'Y':
rgba = (0, 1, 0, 1)
did_work = False
if node in self.past_edges:
_draw_y_node(
xz_orientation=orientations.get((node, layer - 0.5)),
xy=lambda dx, dy, dz: xy([x + dx, y + dy, z - 0.1 + dz * 0.8]),
out_lines=out_lines,
out_triangles=out_triangles,
)
did_work = True
if node in self.future_edges:
_draw_y_node(
xz_orientation=orientations.get((node, layer + 0.5)),
xy=lambda dx, dy, dz: xy([x + dx, y + dy, z + 0.1 - dz * 0.8]),
out_lines=out_lines,
out_triangles=out_triangles,
)
did_work = True
if did_work:
continue
else:
rgba = (0, 0, 0, 1)
add_cube([x, y, z], rgba=rgba, include_triangles=True, include_lines=True)
for node in self.nodes:
for (b, dz) in [(node in self.past_edges, -1), (node in self.future_edges, +1)]:
if not b:
continue
orientation = orientations.get((node, layer + dz*0.5))
x = node.real * pitch
y = node.imag * pitch
z = layer * pitch
c1 = (0, 0, 0, 1) if orientation is None else (1, 0, 0, 1) if orientation else (0, 0, 1, 1)
c2 = (0, 0, 0, 1) if orientation is None else (0, 0, 1, 1) if orientation else (1, 0, 0, 1)
s2 = spacing / 2
add_face((x, y - 0.5, z + dz*0.5 + s2*dz*0.5), d1=(1, 0, 0), d2=(0, 0, s2), rgba=c1, include_lines=False, include_triangles=True)
add_face((x, y + 0.5, z + dz*0.5 + s2*dz*0.5), d1=(1, 0, 0), d2=(0, 0, s2), rgba=c1, include_lines=False, include_triangles=True)
add_face((x - 0.5, y, z + dz*0.5 + s2*dz*0.5), d1=(0, 1, 0), d2=(0, 0, s2), rgba=c2, include_lines=False, include_triangles=True)
add_face((x + 0.5, y, z + dz*0.5 + s2*dz*0.5), d1=(0, 1, 0), d2=(0, 0, s2), rgba=c2, include_lines=False, include_triangles=True)
out_lines.append(gen.ColoredLineData(
rgba=(0, 0, 0, 1),
edge_list=np.array(
[
xy([x2, y2, z2])
for x2 in [x - 0.5, x + 0.5]
for y2 in [y - 0.5, y + 0.5]
for z2 in [z + dz*0.5, z + dz*0.5 + dz*s2]
],
dtype=np.float32,
),
))
add_face((x, y - 0.5, z + dz*0.5 + s2*dz*0.5), d1=(1, 0, 0), d2=(0, 0, s2), rgba=c1, include_lines=False, include_triangles=True)
add_face((x, y + 0.5, z + dz*0.5 + s2*dz*0.5), d1=(1, 0, 0), d2=(0, 0, s2), rgba=c1, include_lines=False, include_triangles=True)
add_face((x - 0.5, y, z + dz*0.5 + s2*dz*0.5), d1=(0, 1, 0), d2=(0, 0, s2), rgba=c2, include_lines=False, include_triangles=True)
add_face((x + 0.5, y, z + dz*0.5 + s2*dz*0.5), d1=(0, 1, 0), d2=(0, 0, s2), rgba=c2, include_lines=False, include_triangles=True)
for edge in self.edges:
x = edge.real * pitch
y = edge.imag * pitch
z = layer * pitch
orientation = orientations.get((edge, layer))
d = 1 if edge.real % 1 == 0.5 else 1j
c1 = (0, 0, 0, 1) if orientation is None else (1, 0, 0, 1) if orientation else (0, 0, 1, 1)
c2 = (0, 0, 0, 1) if orientation is None else (0, 0, 1, 1) if orientation else (1, 0, 0, 1)
add_face((x, y, z - 0.5), d1=(d.real*spacing, d.imag*spacing, 0), d2=(-d.imag, d.real, 0), rgba=c1, include_lines=True, include_triangles=True)
add_face((x, y, z + 0.5), d1=(d.real*spacing, d.imag*spacing, 0), d2=(-d.imag, d.real, 0), rgba=c1, include_lines=True, include_triangles=True)
add_face((x-d.imag*0.5, y+d.real*0.5, z), d1=(d.real*spacing, d.imag*spacing, 0), d2=(0, 0, 1), rgba=c2, include_lines=True, include_triangles=True)
add_face((x+d.imag*0.5, y-d.real*0.5, z), d1=(d.real*spacing, d.imag*spacing, 0), d2=(0, 0, 1), rgba=c2, include_lines=True, include_triangles=True)
def add_wireframe_to_3d_skeleton_gltf(
self,
layer: int,
spacing: float,
out_lines: list[gen.ColoredLineData],
out_triangles: list[gen.ColoredTriangleData],
injected_errors: frozenset[InjectedError],
) -> None:
pitch = spacing + 1
def xy(v):
return np.array([-v[2], v[0], -v[1]])
degs = self.compute_node_degrees()
for node, c in self.nodes.items():
if degs[node] == 2 and (c == 'X' or c == 'Z'):
continue
x = node.real * pitch
y = node.imag * pitch
z = layer * pitch
if c != '.':
if c == 'X':
rgba = (1, 1, 1, 1)
elif c == 'Z':
rgba = (0, 0, 0, 1)
elif c == 'H':
rgba = (1, 1, 0, 1)
elif c == 'T':
rgba = (1, 0, 1, 1)
elif c == 'Y':
rgba = (0, 1, 0, 1)
elif len(c) == 2 and set(c) <= set('XYZ'):
if c[0] == 'X':
rgba1 = (1, 1, 1, 1)
elif c[0] == 'Z':
rgba1 = (0, 0, 0, 1)
elif c[0] == 'Y':
rgba1 = (0, 1, 0, 1)
else:
rgba1 = (0.5, 0.5, 0.5, 1)
if c[1] == 'X':
rgba2 = (1, 1, 1, 1)
elif c[1] == 'Z':
rgba2 = (0, 0, 0, 1)
elif c[1] == 'Y':
rgba2 = (0, 1, 0, 1)
else:
rgba2 = (0.5, 0.5, 0.5, 1)
_draw_interlocking_double_color_cube(
x=x,
y=y,
z=z,
sx=0.25,
sy=0.25,
sz=0.25,
xy=xy,
rgba1=rgba1,
rgba2=rgba2,
out_triangles=out_triangles,
out_lines=out_lines,
)
continue
else:
rgba = (0, 0, 0, 0)
add_cube_to(
[x, y, z],
sx=0.5,
sy=0.5,
sz=0.5,
rgba=rgba,
xy=xy,
out_triangles=out_triangles,
out_lines=out_lines,
include_lines=True,
include_triangles=rgba != (0, 0, 0, 0),
)
def jitter() -> float:
return 0.2 * (random.random() * 2 - 1)
for err in injected_errors:
x = err.pos.real
y = err.pos.imag
z = layer + err.layer
if err.basis == 'X':
rgba = (1, 0, 0, 1)
x -= 0.1 * (x % 1)
y -= 0.1 * (y % 1)
z -= 0.1 * (z % 1)
elif err.basis == 'Z':
rgba = (0, 0, 1, 1)
x += 0.1 * (x % 1)
y += 0.1 * (y % 1)
z += 0.1 * (z % 1)
elif err.basis == 'Y':
rgba = (0, 1, 0, 1)
else:
raise NotImplementedError(f'{err.basis=}')
x *= pitch
y *= pitch
z *= pitch
out_triangles.append(gen.ColoredTriangleData(
rgba=rgba,
triangle_list=np.array([
xy([x + jitter(), y + jitter(), z + jitter()])
for _ in range(48)
], dtype=np.float32),
))
edge_xyzs = []
for edge in self.edges:
edge_xyzs.append((edge.real, edge.imag, layer))
for edge in self.future_edges:
edge_xyzs.append((edge.real, edge.imag, layer + 0.5))
for edge in self.past_edges:
edge_xyzs.append((edge.real, edge.imag, layer - 0.5))
for x, y, z in edge_xyzs:
dx = dy = dz = 0
if x % 1 == 0.5:
dx = 0.5
if y % 1 == 0.5:
dy = 0.5
if z % 1 == 0.5:
dz = 0.5
c1 = xy([x - dx, y - dy, z - dz]) * pitch
c2 = xy([x + dx, y + dy, z + dz]) * pitch
out_lines.append(gen.ColoredLineData(
rgba=(0, 0, 0, 0),
edge_list=np.array([c1, c2], dtype=np.float32)
))
def to_nx_zx_graph(self, *, show: bool = False) -> 'nx.Graph':
"""Converts the lattice surgery into a nearly-ZX-calculus graph.
The graph has the advantage of not treating space and time differently.
"""
import networkx as nx
graph = nx.Graph()
for node, c in self.nodes.items():
if c == 'Z':
graph.add_node(node, type='Z', phase=+1)
elif c == 'X':
graph.add_node(node, type='X', phase=+1)
elif c == 'Y':
if node in self.past_edges:
graph.add_node(node, type='Z', phase=1j)
if node in self.future_edges:
graph.add_node((node, 'prep'), type='Z', phase=1j)
elif c == 'T':
space_deg = sum(node + d / 2 in self.edges for d in [1, -1, 1j, -1j])
time_deg = (node in self.past_edges) + (node in self.future_edges)
if time_deg + space_deg == 1:
graph.add_node(node, type='T_port', phase=1j**0.5)
else:
graph.add_node(node, type='Z', phase=+1)
graph.add_node((node, 'T_in'), type='T_port', phase=1j**0.5)
graph.add_edge(node, (node, 'T_in'))
elif c == 'S':
space_deg = sum(node + d / 2 in self.edges for d in [1, -1, 1j, -1j])
time_deg = (node in self.past_edges) + (node in self.future_edges)
if time_deg + space_deg == 1:
graph.add_node(node, type='S_port', phase=1j)
else:
graph.add_node(node, type='Z', phase=+1)
graph.add_node((node, 'S_in'), type='S_port', phase=1j)
graph.add_edge(node, (node, 'S_in'))
elif c == 'H':
space_deg = sum(node + d / 2 in self.edges for d in [1, -1, 1j, -1j])
time_deg = (node in self.past_edges) + (node in self.future_edges)
assert space_deg == 0 and time_deg == 2
graph.add_node(node, type='H', phase=+1)
elif c == '.':
pass
elif len(c) == 2 and set(c) <= set('XYZ'):
raise ValueError(f"{c} node must be resolved before converting into a graph.")
else:
raise NotImplementedError(f'{c=}')
if node in self.past_edges:
graph.add_node((node, 'past_in'), type='in_port', phase=+1)
if node in self.future_edges:
graph.add_node((node, 'future_out'), type='out_port', phase=+1)
for node, c in self.nodes.items():
is_instant_measure = True
for d in [-1, 1, -1j, +1j]:
e = node + d/2
if e in self.edges:
graph.add_edge(node, node + d, x_sink=e in self.x_sink_edges, z_sink=e in self.z_sink_edges)
is_instant_measure = False
if node in self.future_edges:
graph.add_edge((node, 'future_out'), node if c != 'Y' else (node, 'prep'))
is_instant_measure = False
if node in self.past_edges:
graph.add_edge(
(node, 'past_in'),
node,
x_sink=c == 'Y' or (c == 'X' and is_instant_measure),
z_sink=c == 'Z' and is_instant_measure)
if show:
import matplotlib.pyplot as plt
n2color = {'X': 'red', 'Z': 'blue', 'in_port': 'white', 'out_port': 'white', 'T_port': 'white'}
n2pos = {}
node_labels = {}
for node, vals in graph.nodes.items():
if isinstance(node, complex):
c = node
else:
c, r = node
d = 0.1 - 0.15j
c += d * {'T_in': -1, 'future_out': +2, 'future': +1, 'past': -1, 'past_in': -2, 'prep': +0.15}[r]
n2pos[node] = (c.real, -c.imag)
t = vals['type']
if t == 'X' or t == 'Z':
if vals['phase'] == 1j:
label = '½π'
elif vals['phase'] == -1j:
label = '-½π'
elif vals['phase'] == -1:
label = 'π'
else:
label = ''
elif t == 'in_port' or t == 'out_port':
label = ''
elif t == 'T_port':
label = 'T*'
elif t == 'S_port':
label = 'S*'
else:
raise NotImplementedError(f'{node=}, {vals=}')
node_labels[node] = label
nx.draw_networkx(
graph,
pos=n2pos,
labels=node_labels,
font_color='gray',
node_color=[n2color[vals['type']] for node, vals in graph.nodes.items()],
)
nx.draw_networkx_edge_labels(
graph,
pos=n2pos,
edge_labels={
(a, b): '*' * bool(graph.edges[(a, b)].get('x_sink')) + '@' * bool(graph.edges[(a, b)].get('z_sink'))
for (a, b), vals in graph.edges.items()
if graph.edges[(a, b)].get('x_sink') or graph.edges[(a, b)].get('z_sink')
},
font_color='gray',
)
plt.show()
return graph
def __str__(self) -> str:
grid = {}
for n, c in self.nodes.items():
v = n * 4 + 2 + 2j
grid[v] = c
for e in self.future_edges:
v = e * 4 + 2 + 2j
grid[v + 1 - 1j] = '/'
for e in self.past_edges:
v = e * 4 + 2 + 2j
grid[v - 1 + 1j] = '/'
for e in self.edges:
if e.real % 1 == 0.5:
v = (e - 0.5) * 4 + 2 + 2j
grid[v + 1] = '-'
grid[v + 2] = '-'
grid[v + 3] = '-'
elif e.imag % 1 == 0.5:
v = (e - 0.5j) * 4 + 2 + 2j
grid[v + 1j] = '|'
grid[v + 2j] = '|'
grid[v + 3j] = '|'
else:
raise NotImplementedError(f'{e=}')
max_r = int(max([e.real for e in grid.keys()], default=0))
max_i = int(max([e.imag for e in grid.keys()], default=0))
chars = []
for y in range(max_i + 1):
for x in range(max_r + 1):
chars.append(grid.get(x + 1j*y, ' '))
chars.append('\n')
return ''.join(chars)
def to_zx_graph(self) -> ZXGraph:
return ZXGraph.from_nx_graph(self.to_nx_zx_graph())
@staticmethod
def from_text(text: str) -> 'LatticeSurgeryLayer':
xs = collections.Counter()
ys = collections.Counter()
x2x: dict[int, int] = {}
y2y: dict[int, int] = {}
c2n: dict[complex, complex] = {}
nodes: dict[complex, Literal['.', 'X', 'Y', 'Z', 'T']] = {}
m = {
x + 1j*y: c
for y, line in enumerate(text.splitlines())
for x, c in enumerate(line)
}
edges = set()
x_sink_edges = set()
z_sink_edges = set()
future_edges = set()
past_edges = set()
ds = [-1, -1j, 1j, 1]
for p, c in m.items():
if m.get(p - 1, ' ') in '.XYZTSH':
pass
elif c in '.XYZTSH':
x = int(p.real)
y = int(p.imag)
xs[x] += 1
ys[y] += 1
x2x.setdefault(x, len(x2x))
y2y.setdefault(y, len(y2y))
n = x2x[x] + y2y[y] * 1j
c2n[x + 1j*y] = n
offset = 1
while m.get(p + offset, ' ') in '.XYZTSH':
c += m.get(p + offset, ' ')
offset += 1
nodes[n] = c
for d in ds:
edge_char = '-' if d.imag == 0 else '|'
k = 1
has_x_excitation = False
has_z_excitation = False
while True:
ec = m.get(p + d*k)
if ec == edge_char:
pass
elif ec == '*':
has_x_excitation = True
elif ec == '@':
has_z_excitation = True
else:
break
k += 1
if k > 1:
edges.add(n + d / 2)
if has_x_excitation:
x_sink_edges.add(n + d / 2)
if has_z_excitation:
z_sink_edges.add(n + d / 2)
if m.get(p - 1 + 1j) == '/':
past_edges.add(n)
if m.get(p + 1 - 1j) == '/':
future_edges.add(n)
elif c in ' -|/*@':
pass
else:
raise NotImplementedError(f'Unrecognized node type: {c=} at {p=} in\n{text}')
if len(set(xs.values())) != 1 or len(set(ys.values())) != 1:
raise ValueError(f"The qubits didn't form a complete grid:\n{text}")
return LatticeSurgeryLayer(
nodes=nodes,
edges=frozenset(edges),
x_sink_edges=frozenset(x_sink_edges),
z_sink_edges=frozenset(z_sink_edges),
future_edges=frozenset(future_edges),
past_edges=frozenset(past_edges),
)
def _to_quantum_instructions(
self,
*,
layer_key: Any,
include_error_mechanisms: bool,
injected_errors: frozenset[InjectedError],
) -> list[LatticeSurgeryInstruction]:
ds = [-1, -1j, 1j, 1]
order = sorted(self.nodes, key=lambda n: (
n in self.future_edges,
sum(n + d / 2 in self.edges for d in ds),
n.real,
n.imag,
))
def ensure_init(n: Any) -> None:
if n not in initialized:
tasks.append(LatticeSurgeryInstruction(
action=cast(Any, 'qalloc_z' if self.nodes[n] == 'X' else 'qalloc_x'),
target=n,
))
initialized.add(n)
initialized = set(self.past_edges)
edge_done = set()
tasks = []
for n in order:
if n in self.past_edges:
for b in 'XYZ':
if InjectedError(basis=cast(Any, b), pos=n, layer=-0.5) in injected_errors:
tasks.append(LatticeSurgeryInstruction(
cast(Any, b.lower()),
target=n,
))
if include_error_mechanisms:
for b in 'XZ':
tasks.append(LatticeSurgeryInstruction(cast(Any, f'error_mechanism_{b.lower()}'), target=n, error_source=ErrorSource(
error_type='timelike_edge_error',
error_basis=cast(Any, b),
error_location=n,
error_initiative='before',
error_layer=layer_key,
)))
for n in order:
c = self.nodes[n]
if c == 'H':
tasks.append(LatticeSurgeryInstruction(
action='h',
target=n,
))
for n in order:
c = self.nodes[n]
space_deg = sum(n + d / 2 in self.edges for d in ds)
time_deg = (n in self.past_edges) + (n in self.future_edges)
if c == '.':
assert space_deg == 0
assert time_deg == 0
elif c == 'Y':
assert space_deg == 0
for n in order:
c = self.nodes[n]
if c == 'Y' and n in self.past_edges:
tasks.append(LatticeSurgeryInstruction(
action='m_discard_y',
target=n,
measure_key=(f'MY', n, layer_key),
))
for n in order:
c = self.nodes[n]
if c == 'Y' or c == '.':
continue
ensure_init(n)
if c == 'T' or c == 'S':
if c == 'T':
tasks.append(LatticeSurgeryInstruction('t', n))
else:
tasks.append(LatticeSurgeryInstruction('s', n))
tasks.append(LatticeSurgeryInstruction('heralded_random_x', n, measure_key=('heralded_random_x', n, layer_key)))
tasks.append(LatticeSurgeryInstruction('heralded_random_z', n, measure_key=('heralded_random_z', n, layer_key)))
if include_error_mechanisms:
tasks.append(LatticeSurgeryInstruction(
'error_mechanism_x',
target=n,
error_source=ErrorSource(
error_type='inject_error',
error_basis='X',
error_location=n,
error_initiative='during',
error_layer=layer_key,
),
))
tasks.append(LatticeSurgeryInstruction(