-
Notifications
You must be signed in to change notification settings - Fork 2
/
animation.py
2312 lines (2089 loc) · 86.5 KB
/
animation.py
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
from matplotlib.animation import FFMpegWriter, FuncAnimation
from typing import Sequence, Mapping, Any
import matplotlib.pyplot as plt
import json
import matplotlib
import networkx as nx
import argparse
from abc import ABC, abstractmethod
# physics constants
R_B = 6 # rydberg range
AOD_SEP = 2 # min AOD separation
RYD_SEP = 15 # sufficient distance to avoid Rydberg
SITE_SLMS = 2 # number of SLMs in a site
SLM_SEP = AOD_SEP # separation of SLMs inside a site
SITE_WIDTH = 4 # total width of SLMs in a site
X_SITE_SEP = RYD_SEP + SITE_WIDTH # separation of sites in X direction
Y_SITE_SEP = RYD_SEP # separation of sites in Y direction
# padding of the figure
X_LOW_PAD = 2 * AOD_SEP
Y_LOW_PAD = 4 * AOD_SEP
X_HIGH_PAD = 2 * AOD_SEP
Y_HIGH_PAD = 4 * AOD_SEP
# constants for animation
FPS = 24 # frames per second
INIT_FRM = 24 # initial empty frames
PT_MICRON = 8 # scaling factor: points per micron
MUS_PER_FRM = 8 # microseconds per frame
T_RYDBERG = 0.15 # microseconds for Rydberg
T_ACTIVATE = 50 # microseconds for (de)activating AOD
# class for physical entities: qubits and AOD rows/cols
class Qubit():
def __init__(self, id: int):
self.array = 'SLM'
self.id = id
self.c = -1 # AOD coloumn index
self.r = -1 # AOD row index
self.x = -X_LOW_PAD-1 # real X coordinates in um
self.y = -Y_LOW_PAD-1 # real Y coordinates in um
class Row():
def __init__(self, id: int):
self.id = id
self.active = False
self.y = -Y_LOW_PAD-1 # real Y coordinates in um
class Col():
def __init__(self, id: int):
self.id = id
self.active = False
self.x = -X_LOW_PAD-1 # real X coordinates in um
class Inst(ABC):
"""abstract class of DPQA instructions.
In general, the __init__ of specific instruction classes looks like
def __init__(self, *):
super().__init__(*)
self.verify(*)
self.operate(*)
super().write_code(*)
"""
def __init__(
self,
type: str,
prefix = None,
stage: int = -1,
reduced_keys: Sequence[str] = [],
):
"""init method for instructions.
Args:
type (str):
prefix (str | None, optional): provide the big operation.
this Inst belongs to. Defaults to None.
stage (int, optional): stage the Inst belongs to. Defaults to -1.
reduced_keys (Sequence[str], optional): data to keep in emit()
from emit_full(). Defaults to [].
"""
self.type = type
self.name = prefix + ':' + type if prefix else type
self.stage = stage
self.reduced_keys = reduced_keys + ['type', ]
self.duration = -1
self.code = {'type': self.type, 'name': self.name, }
def write_code(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
data: Mapping[str, Any],
):
"""write self.code with provided info.
Args:
col_objs (Sequence[Col]): Col objects used.
row_objs (Sequence[Row]): Row objects used.
qubit_objs (Sequence[Qubit]): Qubit objects used.
data (Mapping[str, Any]): other info for the Inst.
"""
for k, v in data.items():
self.code[k] = v
# get the current state of DPQA
curr = {}
curr['qubits'] = [
{
'id': q.id,
'x': q.x,
'y': q.y,
'array': q.array,
'c': q.c,
'r': q.r
} for q in qubit_objs
]
curr['cols'] = [
{
'id': c.id,
'active': c.active,
'x': c.x
} for c in col_objs
]
curr['rows'] = [
{
'id': r.id,
'active': r.active,
'y': r.y
} for r in row_objs
]
self.code['state'] = curr
@abstractmethod
def verify(self):
"""verification of instructions. This is abstract because we require
each child class to provide its own verification method.
"""
pass
def operate(self):
"""perform operation of instructions on Col, Row, and Qubit objects."""
pass
def emit(self) -> Sequence[Mapping[str, Any]]:
"""emit the minimum code for executing instructions.
Returns:
Sequence[Mapping[str, Any]]: code in a dict.
"""
return ({k: self.code[k] for k in self.reduced_keys}, )
def emit_full(self) -> Sequence[Mapping[str, Any]]:
"""emit the code with full info for any purpose.
Returns:
Sequence[Mapping[str, Any]]: code with full info in a dict.
"""
return (self.code, )
def is_trivial(self) -> bool:
return True if self.duration == 0 else False
def remove_trivial_insts(self):
# this is used in ComboInst. Added here for convience.
pass
# classes for basic instructions: Init, Move, Activate, Deactivate, Rydberg
# todo: add class Raman (single-qubit gates)
class Init(Inst):
def __init__(self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
slm_qubit_idx: Sequence[int] = [],
slm_qubit_xys: Sequence[Sequence[int]] = [],
aod_qubit_idx: Sequence[int] = [],
aod_qubit_crs: Sequence[Sequence[int]] = [],
aod_col_act_idx: Sequence[int] = [],
aod_col_xs: Sequence[int] = [],
aod_row_act_idx: Sequence[int] = [],
aod_row_ys: Sequence[int] = [],
data: Mapping[str, Any] = {},):
super().__init__(
'Init',
reduced_keys=[
'slm_qubit_idx', 'slm_qubit_xys', 'aod_qubit_idx',
'aod_qubit_crs', 'aod_col_act_idx', 'aod_col_xs',
'aod_row_act_idx', 'aod_row_ys', 'n_q',
'x_high', 'y_high', 'c_high', 'r_high'
]
)
for k, v in data.items():
self.code[k] = v
self.all_slms = []
self.verify(slm_qubit_idx,
slm_qubit_xys,
aod_qubit_idx,
aod_qubit_crs,
aod_col_act_idx,
aod_col_xs,
aod_row_act_idx,
aod_row_ys,)
self.operate(col_objs,
row_objs,
qubit_objs,
slm_qubit_idx,
slm_qubit_xys,
aod_qubit_idx,
aod_qubit_crs,
aod_col_act_idx,
aod_col_xs,
aod_row_act_idx,
aod_row_ys,)
super().write_code(
col_objs,
row_objs,
qubit_objs,
{
'duration': INIT_FRM,
'slm_qubit_idx': slm_qubit_idx,
'slm_qubit_xys': slm_qubit_xys,
'aod_qubit_idx': aod_qubit_idx,
'aod_qubit_crs': aod_qubit_crs,
'aod_col_act_idx': aod_col_act_idx,
'aod_col_xs': aod_col_xs,
'aod_row_act_idx': aod_row_act_idx,
'aod_row_ys': aod_row_ys,
})
def add_slms(self, slms):
for slm in slms:
if slm not in self.all_slms:
self.all_slms.append(slm)
def verify(
self,
slm_qubit_idx: Sequence[int],
slm_qubit_xys: Sequence[Sequence[int]],
aod_qubit_idx: Sequence[int],
aod_qubit_crs: Sequence[Sequence[int]],
aod_col_act_idx: Sequence[int],
aod_col_xs: Sequence[int],
aod_row_act_idx: Sequence[int],
aod_row_ys: Sequence[int],
):
a = len(slm_qubit_idx)
b = len(slm_qubit_xys)
if a != b:
raise ValueError(
f'{self.name}: SLM qubit arguments invalid {a} idx, {b} xys.')
for i in slm_qubit_xys:
if len(i) != 2:
raise ValueError(f'{self.name}: SLM qubit xys {i} invalid.')
for i in range(len(slm_qubit_xys)):
for j in range(i+1, len(slm_qubit_xys)):
if slm_qubit_xys[i] == slm_qubit_xys[j]:
raise ValueError(
f'{self.name}: SLM qubits {slm_qubit_idx[i]} '
f'and {slm_qubit_idx[j]} xys are the same.'
)
# todo: the case when not all atoms are in SLM
def operate(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
slm_qubit_idx: Sequence[int],
slm_qubit_xys: Sequence[Sequence[int]],
aod_qubit_idx: Sequence[int],
aod_qubit_crs: Sequence[Sequence[int]],
aod_col_act_idx: Sequence[int],
aod_col_xs: Sequence[int],
aod_row_act_idx: Sequence[int],
aod_row_ys: Sequence[int],
):
for i, q_id in enumerate(slm_qubit_idx):
qubit_objs[q_id].array = 'SLM'
(qubit_objs[q_id].x, qubit_objs[q_id].y) = slm_qubit_xys[i]
self.all_slms.append(slm_qubit_xys[i])
for i, col_id in enumerate(aod_col_act_idx):
col_objs[col_id].activate = True
col_objs[col_id].x = aod_col_xs[i]
for i, row_id in enumerate(aod_col_act_idx):
row_objs[row_id].activate = True
row_objs[row_id].y = aod_row_ys[i]
for i, q_id in enumerate(aod_qubit_idx):
qubit_objs[q_id].array = 'AOD'
(qubit_objs[q_id].c, qubit_objs[q_id].r) = aod_qubit_crs[i]
qubit_objs[q_id].x = col_objs[qubit_objs[q_id].c].x
qubit_objs[q_id].y = row_objs[qubit_objs[q_id].r].y
def emit_full(self):
# all the used SLMs are counted during the whole codegen process,
# so the emit_full of Init needs to add this info
self.code['all_slms'] = self.all_slms
return super().emit_full()
class Move(Inst):
def __init__(self,
s: int,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int] = [],
col_begin: Sequence[int] = [],
col_end: Sequence[int] = [],
row_idx: Sequence[int] = [],
row_begin: Sequence[int] = [],
row_end: Sequence[int] = [],
prefix: str = ''):
super().__init__('Move', prefix=prefix, stage=s)
self.verify(
col_objs,
row_objs,
col_idx,
col_begin,
col_end,
row_idx,
row_begin,
row_end,
)
data = self.operate(
col_objs,
row_objs,
qubit_objs,
col_idx,
col_begin,
col_end,
row_idx,
row_begin,
row_end,)
super().write_code(col_objs, row_objs, qubit_objs, data)
def operate(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int],
col_begin: Sequence[int],
col_end: Sequence[int],
row_idx: Sequence[int],
row_begin: Sequence[int],
row_end: Sequence[int],
) -> Mapping[str, Any]:
data = {}
# calculate the max move distance of columns
data['cols'] = []
max_distance = 0
for i in range(len(col_idx)):
distance = abs(col_end[i]-col_begin[i])
if distance > 0:
data['cols'].append(
{'id': col_idx[i],
'shift': col_end[i]-col_begin[i],
'begin': col_begin[i],
'end': col_end[i]})
col_objs[col_idx[i]].x = col_end[i]
max_distance = max(max_distance, distance)
# calculate the max move distance of rows
data['rows'] = []
for i in range(len(row_idx)):
distance = abs(row_end[i]-row_begin[i])
if distance > 0:
data['rows'].append(
{'id': row_idx[i],
'shift': row_end[i]-row_begin[i],
'begin': row_begin[i],
'end': row_end[i]})
row_objs[row_idx[i]].y = row_end[i]
max_distance = max(max_distance, distance)
# movement time per Bluvstein et al. units are us and um.
self.duration = 200*((max_distance/110)**(1/2))
data['duration'] = self.duration
for qubit_obj in qubit_objs:
if qubit_obj.array == 'AOD':
qubit_obj.x = col_objs[qubit_obj.c].x
qubit_obj.y = row_objs[qubit_obj.r].y
return data
def verify(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
col_idx: Sequence[int],
col_begin: Sequence[int],
col_end: Sequence[int],
row_idx: Sequence[int],
row_begin: Sequence[int],
row_end: Sequence[int],
):
a = len(col_idx)
b = len(col_begin)
c = len(col_end)
if not (a == b and a == c):
raise ValueError(
f'{self.name}: col arguments invalid'
f' {a} idx, {b} begin, {c} end.'
)
a = len(row_idx)
b = len(row_begin)
c = len(row_end)
if not (a == b and a == c):
raise ValueError(
f'{self.name}: row arguments invalid'
f' {a} idx, {b} begin, {c} end.'
)
activated_col_idx = []
activated_col_xs = []
for col_obj in col_objs:
if col_obj.active:
if (activated_col_idx
and col_obj.x < activated_col_xs[-1] + AOD_SEP):
raise ValueError(
f'{self.name}: col beginning position invalid'
f' col {col_obj.id} at x={col_obj.x} while '
f'col {activated_col_idx[-1]} at'
f' x={activated_col_xs[-1]}.'
)
activated_col_idx.append(col_obj.id)
activated_col_xs.append(col_obj.x)
for i, moving_col_id in enumerate(col_idx):
if moving_col_id not in activated_col_idx:
raise ValueError(
f'{self.name}: col {moving_col_id} to move'
f' is not activated.'
)
j = activated_col_idx.index(moving_col_id)
if col_begin[i] != activated_col_xs[j]:
raise ValueError(
f'{self.name}: col {moving_col_id} beginning x not agree.')
activated_col_xs[j] = col_end[i]
for i in range(1, len(activated_col_xs)):
if activated_col_xs[i - 1] + AOD_SEP > activated_col_xs[i]:
raise ValueError(
f'{self.name}: col ending position invalid'
f' col {activated_col_idx[i-1]} at '
f'x={activated_col_xs[i-1]} while '
f'col {activated_col_idx[i]} at x={activated_col_xs[i]}.')
activated_row_idx = []
activated_row_ys = []
for row_obj in row_objs:
if row_obj.active:
if (activated_row_idx
and row_obj.y < activated_row_ys[-1] + AOD_SEP):
raise ValueError(
f'{self.name}: row beginning position invalid '
f'row {row_obj.id} at y={row_obj.y} while '
f'row {activated_row_idx[-1]} at '
f'y={activated_row_ys[-1]}.'
)
activated_row_idx.append(row_obj.id)
activated_row_ys.append(row_obj.y)
for i, moving_row_id in enumerate(row_idx):
if moving_row_id not in activated_row_idx:
raise ValueError(
f'{self.name}: row {moving_row_id} to move '
f'is not activated.'
)
j = activated_row_idx.index(moving_row_id)
if row_begin[i] != activated_row_ys[j]:
raise ValueError(
f'{self.name}: row {moving_row_id} beginning y not agree.')
activated_row_ys[j] = row_end[i]
for i in range(1, len(activated_row_ys)):
if activated_row_ys[i - 1] + AOD_SEP > activated_row_ys[i]:
raise ValueError(
f'{self.name}: row ending position invalid '
f'row {activated_row_idx[i-1]} at '
f'y={activated_row_ys[i-1]} while '
f'row {activated_row_idx[i]} at y={activated_row_ys[i]}.')
def emit(self):
code = {'type': self.type}
code['cols'] = [{k: v for k, v in tmp.items() if k in [
'id', 'shift']} for tmp in self.code['cols']]
code['rows'] = [{k: v for k, v in tmp.items() if k in [
'id', 'shift']} for tmp in self.code['rows']]
return (code,)
class Activate(Inst):
def __init__(self,
s: int,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int] = [],
col_xs: Sequence[int] = [],
row_idx: Sequence[int] = [],
row_ys: Sequence[int] = [],
pickup_qs: Sequence[int] = [],
prefix: str = '',):
super().__init__(
'Activate',
prefix=prefix,
stage=s,
reduced_keys=['col_idx', 'col_xs', 'row_idx', 'row_ys']
)
self.verify(
col_objs,
row_objs,
qubit_objs,
col_idx,
col_xs,
row_idx,
row_ys,
pickup_qs,
)
self.operate(
col_objs,
row_objs,
qubit_objs,
col_idx,
col_xs,
row_idx,
row_ys,
pickup_qs,
)
super().write_code(
col_objs,
row_objs,
qubit_objs,
{
'col_idx': col_idx,
'col_xs': col_xs,
'row_idx': row_idx,
'row_ys': row_ys,
'pickup_qs': pickup_qs,
'duration': T_ACTIVATE
}
)
def operate(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int],
col_xs: Sequence[int],
row_idx: Sequence[int],
row_ys: Sequence[int],
pickup_qs: Sequence[int],
):
for i in range(len(col_idx)):
col_objs[col_idx[i]].active = True
col_objs[col_idx[i]].x = col_xs[i]
for i in range(len(row_idx)):
row_objs[row_idx[i]].active = True
row_objs[row_idx[i]].y = row_ys[i]
for q_id in pickup_qs:
qubit_objs[q_id].array = 'AOD'
def verify(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int],
col_xs: Sequence[int],
row_idx: Sequence[int],
row_ys: Sequence[int],
pickup_qs: Sequence[int],
):
a = len(col_idx)
b = len(col_xs)
if a != b:
raise ValueError(
f'{self.name}: col arguments invalid {a} idx, {b} xs')
a = len(row_idx)
b = len(row_ys)
if a != b:
raise ValueError(
f'f{self.name}: row arguments invalid {a} idx, {b} ys.')
for i in range(len(col_idx)):
if col_objs[col_idx[i]].active:
raise ValueError(
f'{self.name}: col {col_idx[i]} already activated.')
for j in range(col_idx[i]):
if (col_objs[j].active
and col_objs[j].x > col_xs[i] - AOD_SEP):
raise ValueError(
f'{self.name}: col {j} at x={col_objs[j].x} is '
f'too left for col {col_idx[i]}'
f' to activate at x={col_xs[i]}.'
)
for j in range(col_idx[i] + 1, len(col_objs)):
if (col_objs[j].active
and col_objs[j].x - AOD_SEP < col_xs[i]):
raise ValueError(
f'{self.name}: col {j} at x={col_objs[j].x} is '
f'too right for col {col_idx[i]} '
f'to activate at x={col_xs[i]}.'
)
for i in range(len(row_idx)):
if row_objs[row_idx[i]].active:
raise ValueError(
f'{self.name}: row {row_idx[i]} already activated.')
for j in range(row_idx[i]):
if (row_objs[j].active
and row_objs[j].y > row_ys[i] - AOD_SEP):
raise ValueError(
f'{self.name}: row {j} at y={row_objs[j].y} is '
f'too high for row {row_idx[i]} '
f'to activate at y={row_ys[i]}.'
)
for j in range(row_idx[i] + 1, len(row_objs)):
if (row_objs[j].active
and row_objs[j].y-AOD_SEP < row_ys[i]):
raise ValueError(
f'{self.name}: row {j} at y={col_objs[j].y} is '
f'too low for row {row_idx[i]} '
f'to activate at y={row_ys[i]}.'
)
active_xys = [] # the traps that are newly activted by this Activate
active_xs = [col.x for col in col_objs if col.active]
active_ys = [row.y for row in row_objs if row.active]
for x in active_xs:
for y in row_ys:
active_xys.append((x, y))
for y in active_ys:
for x in col_xs:
active_xys.append((x, y))
for x in col_xs:
for y in row_ys:
active_xys.append((x, y))
for q_id in range(len(qubit_objs)):
if q_id in pickup_qs:
if (qubit_objs[q_id].x, qubit_objs[q_id].y) not in active_xys:
raise ValueError(
f'{self.name}: q {q_id} not picked up '
f'by col {qubit_objs[q_id].c} '
f'row {qubit_objs[q_id].r} at '
f'x={qubit_objs[q_id].x} y={qubit_objs[q_id].y}.'
)
else:
if (qubit_objs[q_id].x, qubit_objs[q_id].y) in active_xys:
raise ValueError(
f'{self.name}: q {q_id} wrongfully picked up by '
f'col {qubit_objs[q_id].c} row {qubit_objs[q_id].r}'
f' at x={qubit_objs[q_id].x} y={qubit_objs[q_id].y}.')
class Deactivate(Inst):
def __init__(self,
s: int,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int] = [],
col_xs: Sequence[int] = [],
row_idx: Sequence[int] = [],
row_ys: Sequence[int] = [],
dropoff_qs: Sequence[int] = [],
prefix: str = '',):
super().__init__(
'Deactivate',
prefix=prefix,
stage=s,
reduced_keys=['col_idx', 'row_idx']
)
self.verify(
col_objs,
row_objs,
qubit_objs,
col_idx,
col_xs,
row_idx,
row_ys,
dropoff_qs
)
self.operate(
col_objs,
row_objs,
qubit_objs,
col_idx,
row_idx,
dropoff_qs
)
super().write_code(
col_objs,
row_objs,
qubit_objs,
{
'col_idx': col_idx,
'col_xs': col_xs,
'row_idx': row_idx,
'row_ys': row_ys,
'dropoff_qs': dropoff_qs,
'duration': T_ACTIVATE
}
)
def operate(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int],
row_idx: Sequence[int],
dropoff_qs: Sequence[int],
):
for i in range(len(col_idx)):
col_objs[col_idx[i]].active = False
for i in range(len(row_idx)):
row_objs[row_idx[i]].active = False
for q_id in dropoff_qs:
qubit_objs[q_id].array = 'SLM'
def verify(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
col_idx: Sequence[int],
col_xs: Sequence[int],
row_idx: Sequence[int],
row_ys: Sequence[int],
dropoff_qs: Sequence[int],
):
a = len(col_idx)
b = len(col_xs)
if a != b:
raise ValueError(
f'{self.name}: col arguments invalid {a} idx, {b} xs')
a = len(row_idx)
b = len(row_ys)
if a != b:
raise ValueError(
f'{self.name}: row arguments invalid {a} idx, {b} ys.')
for i in range(len(col_idx)):
if not col_objs[col_idx[i]].active:
raise ValueError(
f'{self.name}: col {col_idx[i]} already dectivated.')
for j in range(col_idx[i]):
if (col_objs[j].active
and col_objs[j].x > col_xs[i] - AOD_SEP):
raise ValueError(
f'{self.name}: col {j} at x={col_objs[j].x} is '
f'too left for col {col_idx[i]} '
f'to deactivate at x={col_xs[i]}.')
for j in range(col_idx[i]+1, len(col_objs)):
if (col_objs[j].active
and col_objs[j].x - AOD_SEP < col_xs[i]):
raise ValueError(
f'{self.name}: col {j} at x={col_objs[j].x} is '
f'too right for col {col_idx[i]} '
f'to deactivate at x={col_xs[i]}.')
for i in range(len(row_idx)):
if not row_objs[row_idx[i]].active:
raise ValueError(
f'{self.name}: row {row_idx[i]} already deactivated.')
for j in range(row_idx[i]):
if (row_objs[j].active
and row_objs[j].y > row_ys[i] - AOD_SEP):
raise ValueError(
f'{self.name}: row {j} at y={row_objs[j].y} is '
f'too high for row {row_idx[i]} '
f'to deactivate at y={row_ys[i]}.'
)
for j in range(row_idx[i]+1, len(row_objs)):
if (row_objs[j].active
and row_objs[j].y-AOD_SEP < row_ys[i]):
raise ValueError(
f'{self.name}: row {j} at y={col_objs[j].y} is '
f'too low for row {row_idx[i]} '
f'to deactivate at y={row_ys[i]}.'
)
deactive_xys = []
active_xs = [col.x for col in col_objs if col.active]
for x in active_xs:
for y in row_ys:
deactive_xys.append((x, y))
for q_id in range(len(qubit_objs)):
if q_id in dropoff_qs:
if (qubit_objs[q_id].x, qubit_objs[q_id].y) not in deactive_xys:
raise ValueError(
f'{self.name}: q {q_id} not dropped off from '
f'col {qubit_objs[q_id].c} row {qubit_objs[q_id].r} '
f'at x={qubit_objs[q_id].x} y={qubit_objs[q_id].y}.'
)
elif qubit_objs[q_id].array == 'AOD':
if (qubit_objs[q_id].x, qubit_objs[q_id].y) in deactive_xys:
raise ValueError(
f'{self.name}: q {q_id} wrongfully dropped off from '
f'col {qubit_objs[q_id].c} row {qubit_objs[q_id].r} '
f'at x={qubit_objs[q_id].x} y={qubit_objs[q_id].y}.'
)
class Rydberg(Inst):
def __init__(self,
s: int,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
gates: Sequence[Mapping[str, int]]):
super().__init__('Rydberg', prefix=f'Rydberg_{s}', stage=s)
self.verify(gates, qubit_objs)
super().write_code(
col_objs,
row_objs,
qubit_objs,
{'gates': gates, 'duration': T_RYDBERG, }
)
def verify(
self,
gates: Sequence[Mapping[str, int]],
qubit_objs: Sequence[Qubit]):
# for g in gates:
# q0 = {'id': qubit_objs[g['q0']].id,
# 'x': qubit_objs[g['q0']].x,
# 'y': qubit_objs[g['q0']].y}
# q1 = {'id': qubit_objs[g['q1']].id,
# 'x': qubit_objs[g['q1']].x,
# 'y': qubit_objs[g['q1']].y}
# if (q0['x']-q1['x'])**2 + (q0['y']-q1['y'])**2 > R_B**2:
# raise ValueError(
# f"{self.name}: q{q0['id']} at x={q0['x']} y={q0['y']} "
# f"and q{q1['id']} at x={q1['x']} y={q1['y']} "
# f"are farther away than Rydberg range."
# )
return
# class for big ops: ReloadRow, Reload, OffloadRow, Offload, SwapPair, Swap
# internally, these are lists of basic operations.
# todo: is there verification needed on the ComboInst lebvel?
class ComboInst:
pass
class ComboInst():
"""class for combined instructions which is a sequence of combined
instructions or DPQA instructions.
"""
def __init__(
self,
type: str,
prefix = None,
suffix = None,
stage: int = -1,
):
"""init method for combined instructions.
Args:
type (str):
prefix (str | None, optional): Defaults to None.
suffix (str | None, optional): Defaults to None.
stage (int, optional): Defaults to -1.
"""
self.type = type
self.name = (f'{prefix}:' if prefix else '') + \
type + (f'_{suffix}' if suffix else '')
self.stage = stage
self.duration = -1
self.insts = []
def emit(self) -> Sequence[Mapping[str, Any]]:
"""combine the code of each Inst inside this ComboInst and return."""
code = []
for inst in self.insts:
code += inst.emit()
return code
def emit_full(self) -> Sequence[Mapping[str, Any]]:
code = []
for inst in self.insts:
code += inst.emit_full()
return code
def append_inst(self, inst):
self.insts.append(inst)
def prepend_inst(self, inst):
self.insts.insert(0, inst)
def is_trivial(self) -> bool:
for inst in self.insts:
if not inst.is_trivial():
return False
return True
def remove_trivial_insts(self):
nontrivial_insts = []
for inst in self.insts:
inst.remove_trivial_insts()
if not inst.is_trivial():
nontrivial_insts.append(inst)
self.insts = nontrivial_insts
class ReloadRow(ComboInst):
def __init__(self, s: int, r: int, prefix: str = ''):
super().__init__('ReloadRow', prefix=prefix, suffix=str(r), stage=s)
self.row_id = r
self.moving_cols_id = []
self.moving_cols_begin = []
self.moving_cols_end = []
def add_col_shift(self, id: int, begin: int, end: int):
self.moving_cols_id.append(id)
self.moving_cols_begin.append(begin)
self.moving_cols_end.append(end)
def generate_col_shift(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
):
self.insts.append(
Move(s=self.stage,
col_objs=col_objs,
row_objs=row_objs,
qubit_objs=qubit_objs,
col_idx=self.moving_cols_id,
col_begin=self.moving_cols_begin,
col_end=self.moving_cols_end,
row_idx=[],
row_begin=[],
row_end=[],
prefix=self.name + ':ColShift')
)
def generate_row_activate(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
cols: Sequence[int],
xs: Sequence[int],
y: int,
pickup_qs: Sequence[int],
):
self.insts.append(
Activate(s=self.stage,
col_objs=col_objs,
row_objs=row_objs,
qubit_objs=qubit_objs,
col_idx=cols,
col_xs=xs,
row_idx=[self.row_id, ],
row_ys=[y, ],
pickup_qs=pickup_qs,
prefix=self.name)
)
def generate_parking(
self,
col_objs: Sequence[Col],
row_objs: Sequence[Row],
qubit_objs: Sequence[Qubit],
shift_down: int,
col_idx: Sequence[int] = [],
col_begin: Sequence[int] = [],
col_end: Sequence[int] = []
):
self.insts.append(
Move(s=self.stage,
col_objs=col_objs,
row_objs=row_objs,
qubit_objs=qubit_objs,
col_idx=col_idx,
col_begin=col_begin,
col_end=col_end,
row_idx=[self.row_id, ],