-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata.py
More file actions
979 lines (870 loc) · 30.6 KB
/
data.py
File metadata and controls
979 lines (870 loc) · 30.6 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
# coding=UTF-8
'''
Created on 05.04.2013
@author: sysoev
'''
from contextlib import contextmanager
import datetime
import json
import math
import re
import functools
import users_compat as users
from google.cloud import ndb
import loqc
import owqc
def force_unicode(value):
if isinstance(value, str):
return value
if isinstance(value, bytes):
return value.decode('utf-8')
return str(value)
def current_user_email():
user = users.get_current_user()
if not user:
return None
if hasattr(user, "email") and callable(user.email):
return user.email()
return force_unicode(user)
def normalize_user_value(value):
if not value:
return None
if isinstance(value, str):
return value
if isinstance(value, bytes):
return value.decode("utf-8")
if hasattr(value, "email") and callable(value.email):
return value.email()
if isinstance(value, dict):
email = value.get("email") or value.get("Email")
if email:
return force_unicode(email)
return None
ndb_client = ndb.Client()
@contextmanager
def ndb_context():
try:
ndb.get_context()
except Exception:
with ndb_client.context():
yield
else:
yield
def with_ndb_context(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with ndb_context():
return func(*args, **kwargs)
return wrapper
_LEGACY_KEY_TOKEN = re.compile(
r"(u?'[^']*'|u?\"[^\"]*\"|-?\d+)"
)
def _parse_legacy_key_string(value):
if not value or not value.startswith("Key(") or not value.endswith(")"):
return None
inner = value[4:-1].strip()
if not inner:
return None
tokens = []
for match in _LEGACY_KEY_TOKEN.finditer(inner):
token = match.group(0)
if token.startswith(("u'", 'u"')):
token = token[1:]
if token.startswith(("'", '"')) and token.endswith(("'", '"')):
tokens.append(token[1:-1])
else:
try:
tokens.append(int(token))
except ValueError:
return None
if len(tokens) < 2 or len(tokens) % 2 != 0:
return None
try:
return ndb.Key(*tokens)
except Exception:
return None
def key_from_str(key_str):
if isinstance(key_str, ndb.Key):
return key_str
if key_str is None:
return None
value = force_unicode(key_str).strip()
if not value:
return None
try:
return ndb.Key(urlsafe=value)
except Exception:
return _parse_legacy_key_string(value)
def key_to_str(key):
if not key:
return ""
return key.urlsafe().decode("utf-8")
class BaseModel(ndb.Model):
@classmethod
def get(cls, key):
key_obj = key_from_str(key)
if not key_obj:
return None
return key_obj.get()
class Project(BaseModel):
name = ndb.StringProperty()
description = ndb.TextProperty()
user = ndb.StringProperty()
project_kind = ndb.StringProperty(default="lo")
created = ndb.DateTimeProperty(auto_now_add=True)
updated = ndb.DateTimeProperty(auto_now=True)
published = ndb.BooleanProperty(default=False)
png = ndb.TextProperty()
def _project_kind_value(proj):
"""Classify project for list filtering (LO vs One-Way)."""
has_lo = LODevice.query(ancestor=proj.key).count(1, keys_only=True) > 0
has_ow = OWDevice.query(ancestor=proj.key).count(1, keys_only=True) > 0
if has_ow and not has_lo:
return "ow"
if has_lo and not has_ow:
return "lo"
kind = getattr(proj, "project_kind", None)
if kind in ("lo", "ow"):
return kind
return "lo"
def _stamp_project_kind(proj, kind):
if proj and kind in ("lo", "ow") and getattr(proj, "project_kind", None) != kind:
proj.project_kind = kind
proj.put()
@with_ndb_context
def getParentProject():
proj = Project.query(Project.name == 'ParentOfAllProjects3717481125').fetch(1)
if not proj:
proj = Project(name="ParentOfAllProjects3717481125", description="This is not a project")
proj.put()
return proj
return proj[0]
@with_ndb_context
def getProjects():
user_email = current_user_email()
if not user_email:
return []
return Project.query(Project.user == user_email).fetch(1000)
@with_ndb_context
def getAllProjects():
return Project.query().fetch(10000)
@with_ndb_context
def PublishProject(project_key, png):
proj = Project.get(project_key)
if not proj:
return 'No project'
if proj.user != current_user_email():
return 'Not authorized!'
if proj.published:
proj.published = False
proj.put()
return 'OK'
if not GetCircuit(project_key) and not GetOWCircuit(project_key):
return 'Project is not ready. Simulate it first'
proj.png = png
proj.published = True
proj.put()
return 'OK'
@with_ndb_context
def GetMatrices(project_key):
proj = Project.get(project_key)
if not proj:
return json.dumps('No project')
if proj.user != current_user_email():
return json.dumps('Not authorized!')
circ = GetCircuit(project_key)
if not circ:
return json.dumps('Project is not ready. Simulate it first')
if not circ.matrix or not circ.inv:
return json.dumps('Project is not ready. Simulate it first')
res = "Direct: " + circ.matrix.replace("[[", "\\( \\begin{pmatrix}").replace("]]", "\\end{pmatrix} \\)").replace("]&", "\\\\").replace("]", "\\\\").replace("[", "").replace("\n", " ")
res_inv = "Inverse: " + circ.inv.replace("[[", "\\( \\begin{pmatrix}").replace("]]", "\\end{pmatrix} \\)").replace("]&", "\\\\").replace("]", "\\\\").replace("[", "").replace("\n", " ")
regex = re.compile(r"[+-]0\.0+e\+00j", re.IGNORECASE)
res = regex.sub("", res)
res_inv = regex.sub("", res_inv)
regex = re.compile(r"[+-]\d+\.\d+e-[123456789]\dj", re.IGNORECASE)
res = regex.sub("", res)
res_inv = regex.sub("", res_inv)
regex = re.compile(r"\.0+e[+-]00", re.IGNORECASE)
res = regex.sub("", res)
res_inv = regex.sub("", res_inv)
regex = re.compile(r"\d+\.\d+e-[123456789]\d", re.IGNORECASE)
res = regex.sub("0", res)
res_inv = regex.sub("0", res_inv)
regex = re.compile(r"[+-]0\.j", re.IGNORECASE)
res = regex.sub("", res)
res_inv = regex.sub("", res_inv)
return json.dumps("<br><br>" + res + "<br><br><br> " + res_inv + "<br><br>", default=str)
@with_ndb_context
def CopyProject(project_key):
try:
proj = Project.get(project_key)
if not proj:
return 'No project'
if not proj.published:
return 'You can\'t copy an un-published project!'
parent_project = getParentProject()
new_proj = Project(parent=parent_project.key)
new_proj.name = 'Copy of ' + proj.name
new_proj.description = proj.description
new_proj.user = current_user_email()
new_proj.project_kind = _project_kind_value(proj)
new_proj.put()
objects = LODevice.query(ancestor=proj.key).fetch(1000)
for o in objects:
new_device = LODevice(parent=new_proj.key)
new_device.id = o.id
new_device.type = o.type
new_device.input_type = o.input_type
new_device.theta = o.theta
new_device.phi = o.phi
new_device.n = o.n
new_device.x = o.x
new_device.y = o.y
new_device.project_key = o.project_key
new_device.put()
conns = LOConnection.query(ancestor=proj.key).fetch(1000)
for c in conns:
new_conn = LOConnection(parent=new_proj.key)
new_conn.line_json = c.line_json
new_conn.put()
ow_objects = OWDevice.query(ancestor=proj.key).fetch(1000)
for o in ow_objects:
new_device = OWDevice(parent=new_proj.key)
new_device.id = o.id
new_device.type = o.type
new_device.index = o.index
new_device.angle = o.angle
new_device.result = o.result
new_device.x = o.x
new_device.y = o.y
new_device.put()
ow_conns = OWConnection.query(ancestor=proj.key).fetch(1000)
for c in ow_conns:
new_conn = OWConnection(parent=new_proj.key)
new_conn.line_json = c.line_json
new_conn.put()
ow_circuits = OWCircuit.query(ancestor=proj.key).fetch(1000)
for c in ow_circuits:
new_circ = OWCircuit(parent=new_proj.key)
new_circ.name = c.name
new_circ.result = c.result
new_circ.put()
return 'Project is copied!'
except Exception as ex:
return str(ex)
@with_ndb_context
def GetLibrary():
projects = Project.query(Project.published == True).fetch()
res = [{'key': key_to_str(p.key), 'name': p.name, 'descr': p.description, 'user': p.user} for p in projects]
return json.dumps(res, default=str)
'''
' 2022. Common methods
'
'
'''
object_types = ['Project', 'LODevice', 'LOConnection', 'LOCircuit', 'OWDevice', 'OWConnection', 'OWCircuit']
@with_ndb_context
def CallCheck(project_key=None):
user_email = current_user_email()
if not user_email:
return False
if project_key:
proj = Project.get(project_key)
if not proj:
return False
if proj.user != user_email:
return False
return True
@with_ndb_context
def GetObjectByKey(object_type, key):
try:
if not CallCheck():
return
if object_type not in object_types:
return
data_class = globals()[object_type]
obj = data_class.get(key)
if obj and getattr(obj, "name", None) != "ParentOfAllProjects3717481125":
return json.dumps(obj.to_dict(), default=str)
return json.dumps({})
except Exception as ex:
return str(ex)
@with_ndb_context
def CycleObjects(object_type, parent_key, project_kind=None):
try:
if not CallCheck():
return
if object_type not in object_types:
return
data_class = globals()[object_type]
if parent_key == "current_user":
parent_key = current_user_email()
if not parent_key:
return json.dumps([])
parent_project = getParentProject()
objs = data_class.query(
data_class.user == parent_key,
ancestor=parent_project.key
).fetch(1000)
objs = [o for o in objs if getattr(o, "name", None) != "ParentOfAllProjects3717481125"]
if object_type == "Project":
kind_filter = (project_kind or "").strip().lower()
if kind_filter not in ("lo", "ow"):
return json.dumps([])
objs = [o for o in objs if _project_kind_value(o) == kind_filter]
else:
ancestor_key = key_from_str(parent_key)
if not ancestor_key:
return json.dumps([])
objs = data_class.query(ancestor=ancestor_key).fetch(1000)
if objs:
return json.dumps([[key_to_str(e.key), e.to_dict()] for e in objs], default=str)
return json.dumps([])
except Exception as ex:
return str(ex)
@with_ndb_context
def DeleteCycleObject(key, object_type):
try:
if not CallCheck():
return
if object_type not in object_types:
return
data_class = globals()[object_type]
obj = data_class.get(key)
if obj and getattr(obj, "name", None) != "ParentOfAllProjects3717481125":
obj.key.delete()
return 'OK'
except Exception as ex:
return str(ex)
@with_ndb_context
def AddCycleObject(data, object_type, parent_key):
try:
if not CallCheck():
return
if object_type not in object_types:
return
obj = json.loads(data)
data_class = globals()[object_type]
if parent_key == "current_user":
parent_project = getParentProject()
instance = data_class(parent=parent_project.key)
else:
# check rights
proj = Project.get(parent_key)
if not proj:
return 'No project'
if proj.user != current_user_email():
return 'Not authorized'
instance = data_class(parent=key_from_str(parent_key))
for key in obj:
setattr(instance, key, obj[key])
if object_type == "Project":
if not getattr(instance, "user", None):
instance.user = current_user_email()
kind = obj.get("project_kind")
if kind in ("lo", "ow"):
instance.project_kind = kind
elif not getattr(instance, "project_kind", None):
instance.project_kind = "lo"
instance.put()
return 'OK'
except Exception as ex:
return str(ex)
# It is used only for projects! Hence the security check
@with_ndb_context
def EditCycleObject(key, data, object_type):
try:
if not CallCheck():
return
# check rights
proj = Project.get(key)
if proj.user != current_user_email():
return 'Not authorized'
if object_type not in object_types:
return
obj = json.loads(data)
data_class = globals()[object_type]
instance = data_class.get(key)
if getattr(instance, "name") == "ParentOfAllProjects3717481125":
return 'Wrong key'
for k in obj:
setattr(instance, k, obj[k])
instance.put()
return 'OK'
except Exception as ex:
return str(ex)
@with_ndb_context
def MigrateProjectUsers(fallback_email=None, limit=1000):
try:
projects = Project.query().fetch(limit)
updated = 0
skipped = 0
for proj in projects:
normalized = normalize_user_value(proj.user)
if not normalized and fallback_email:
normalized = fallback_email
if not normalized:
skipped += 1
continue
if proj.user != normalized:
proj.user = normalized
proj.put()
updated += 1
return json.dumps({"updated": updated, "skipped": skipped, "total": len(projects)}, default=str)
except Exception as ex:
return json.dumps({"error": True, "data": str(ex)}, default=str)
'''
' LOQC Data
'
'
'''
class LODevice(BaseModel):
id = ndb.StringProperty()
type = ndb.StringProperty()
theta = ndb.StringProperty()
phi = ndb.StringProperty()
n = ndb.StringProperty()
input_type = ndb.StringProperty(default="0")
x = ndb.IntegerProperty(default=100)
y = ndb.IntegerProperty(default=100)
project_key = ndb.StringProperty()
class LOConnection(BaseModel):
line_json = ndb.TextProperty()
class OWDevice(BaseModel):
id = ndb.StringProperty()
type = ndb.StringProperty()
index = ndb.StringProperty()
angle = ndb.StringProperty()
result = ndb.StringProperty()
x = ndb.IntegerProperty(default=100)
y = ndb.IntegerProperty(default=100)
class OWConnection(BaseModel):
line_json = ndb.TextProperty()
class LOCircuit(BaseModel):
name = ndb.StringProperty()
modes = ndb.IntegerProperty()
matrix = ndb.TextProperty()
inv = ndb.TextProperty()
result = ndb.TextProperty()
fidelities = ndb.TextProperty()
cgate_run_x = ndb.TextProperty()
cgate_run_y = ndb.TextProperty()
cgate_run_z = ndb.TextProperty()
class OWCircuit(BaseModel):
name = ndb.StringProperty()
result = ndb.TextProperty()
@with_ndb_context
def ClearProjectDesign(key):
try:
proj = Project.get(key)
if not proj:
return "No project"
if proj.user != current_user_email():
return "Not authorized"
_stamp_project_kind(proj, "lo")
objects = LODevice.query(ancestor=proj.key).fetch(1000)
for o in objects:
o.key.delete()
conns = LOConnection.query(ancestor=proj.key).fetch(1000)
for c in conns:
c.key.delete()
circuit = LOCircuit.query(ancestor=proj.key).fetch(1000)
for c in circuit:
c.key.delete()
return "OK"
except Exception as ex:
return str(ex)
@with_ndb_context
def ClearOWDesign(key):
try:
proj = Project.get(key)
if not proj:
return "No project"
if proj.user != current_user_email():
return "Not authorized"
_stamp_project_kind(proj, "ow")
objects = OWDevice.query(ancestor=proj.key).fetch(1000)
for o in objects:
o.key.delete()
conns = OWConnection.query(ancestor=proj.key).fetch(1000)
for c in conns:
c.key.delete()
circuits = OWCircuit.query(ancestor=proj.key).fetch(1000)
for c in circuits:
c.key.delete()
return "OK"
except Exception as ex:
return str(ex)
def ProcessConns(conns, devices):
devs = [{"id": d.id, "type": d.type, "theta": d.theta, "phi": d.phi, "n": d.n, "input_type": d.input_type, "x": d.x, "y": d.y,
"inputs": [], "outputs": [], "step": -1, "modes": [], "project_key": d.project_key} for d in devices]
for c in conns:
con = json.loads(c.line_json)
source_node = con[0]['source']['node']
source_port = con[0]['source']['port']
target_node = con[0]['target']['node']
target_port = con[0]['target']['port']
source_dev = None
target_dev = None
for d in devs:
if d['id'] == source_node:
source_dev = d
if d['id'] == target_node:
target_dev = d
if (not source_dev) or not (target_dev):
return None
if source_dev['type'] == "OUT" or target_dev['type'] == "IN":
source_node = con[0]['target']['node']
source_port = con[0]['target']['port']
target_node = con[0]['source']['node']
target_port = con[0]['source']['port']
tmp = source_dev
source_dev = target_dev
target_dev = tmp
elif source_dev['type'] == "IN" or target_dev['type'] == "OUT":
pass
elif source_port in ["hybrid" + str(2*i) for i in range(50)]:
source_node = con[0]['target']['node']
source_port = con[0]['target']['port']
target_node = con[0]['source']['node']
target_port = con[0]['source']['port']
tmp = source_dev
source_dev = target_dev
target_dev = tmp
source_dev['outputs'].append(target_node)
target_dev['inputs'].append(source_node)
target_dev['modes'].append({'my_port': target_port, 'source': source_node, 'source_port': source_port, 'mode': -1})
return devs
def ProcessDevStep(d, devs, processed_devs):
if d['step'] >= 0:
return d['step']
if d['id'] in processed_devs:
return -1
processed_devs.append(d['id'])
if len(d['inputs']) == 0:
d['step'] = 0
return 0
cur_step = -1
for input in d['inputs']:
for d1 in devs:
if d1['id'] == input:
if d1['step'] > 0:
cur_step = max(d1['step'] + 1, cur_step)
else:
cur_step = max(ProcessDevStep(d1, devs, processed_devs) + 1, cur_step)
break
d['step'] = cur_step
return cur_step
def ProcessSteps(devs):
devs_sorted = sorted(devs, key=lambda d: d['x'])
processed_devs = []
for d in devs_sorted:
ProcessDevStep(d, devs, processed_devs)
def ProcessDevMode(dev, devs, processed_devs):
# check for recursive definitions
if dev['id'] in processed_devs:
return
processed_devs.append(dev['id'])
# iterate all modes in device
for mode in dev['modes']:
# if the mode not yet assigned
if mode['mode'] < 0:
# find source
source = None
for d in devs:
if d['id'] == mode['source']:
source = d
break
if not source:
return
# find source mode
source_mode = None
for m in source['modes']:
# this is dirty, fix later
source_port = m['my_port']
if source['type'] == "User Project":
for i in range(0, 50, 2):
source_port = source_port.replace(str(i), str(i+1))
if source['type'] == "BS":
source_port = source_port.replace("2", "3")
source_port = source_port.replace("0", "1")
if source['type'] == "PS":
source_port = source_port.replace("0", "1")
if mode['source_port'] == source_port:
source_mode = m
break
if not source_mode:
return
# recursive call
if source_mode['mode'] < 0:
ProcessDevMode(source, devs, processed_devs)
mode['mode'] = source_mode['mode']
@with_ndb_context
def GetCircuit(project_key):
try:
proj = Project.get(project_key)
if not proj:
return None
circuit = LOCircuit.query(ancestor=proj.key).fetch(1)
if not circuit:
return None
if len(circuit) == 0:
return None
return circuit[0]
except Exception:
return None
@with_ndb_context
def GetOWCircuit(project_key):
try:
proj = Project.get(project_key)
if not proj:
return None
circuit = OWCircuit.query(ancestor=proj.key).fetch(1)
if not circuit:
return None
if len(circuit) == 0:
return None
return circuit[0]
except Exception:
return None
def ProcessModes(devs):
# sort inputs from top to bottom
inputs = [a for a in devs if a['type'] == "IN"]
ins = sorted(inputs, key=lambda d: d['y'])
# assign modes to inputs
processed_devs = []
mode = 0
for i in ins:
i['modes'] = [{'my_port': 'hybrid0', 'source': None, 'mode': mode}]
processed_devs.append(i['id'])
mode += 1
# sort devs from left to right
sorted_devs = sorted(devs, key=lambda d: d['x'])
# process dev modes
for d in sorted_devs:
ProcessDevMode(d, devs, processed_devs)
# check if everything is correctly defined
for d in devs:
if d['type'] == "IN" and len(d['modes']) != 1:
return 0
if d['type'] == "OUT" and len(d['modes']) != 1:
return 0
if d['type'] == "BS" and len(d['modes']) != 2:
return 0
if d['type'] == "PS" and len(d['modes']) != 1:
return 0
if d['type'] == "User Project":
circ = GetCircuit(d['project_key'])
if not circ:
return 0
if circ.modes != len(d['modes']):
return 0
for m in d['modes']:
if m['mode'] < 0:
return 0
# check passed!
return len(ins)
@with_ndb_context
def getDevsModes(proj):
try:
devices = LODevice.query(ancestor=proj.key).fetch(1000)
conns = LOConnection.query(ancestor=proj.key).fetch(1000)
devs = ProcessConns(conns, devices)
if not devs:
return None, None, json.dumps("The circuit didn't pass the completeness check! It might lack inputs/outputs or it is recursive")
ProcessSteps(devs)
modes = ProcessModes(devs)
if modes == 0:
return None, None, json.dumps("The circuit didn't pass the completeness check! It might lack inputs/outputs or it is recursive")
return devs, modes, ""
except Exception as ex:
return None, None, str(ex)
def _parse_ow_device_index(device):
raw = device.index
if raw is None or raw == "" or str(raw) == "-1":
return None
return int(raw)
def _ow_angle_specified(device):
return device.angle is not None and str(device.angle).strip() != ""
def _ow_result_specified(device):
return str(device.result).strip() in ("0", "1")
def _ow_simulation_node_index(device, n_q):
"""Q nodes use designer index; IN nodes use Hilbert-space index after entanglement."""
idx = _parse_ow_device_index(device)
if device.type == "IN":
return n_q + idx
return idx
def _build_ow_simulation_inputs(devices, conns):
by_id = {d.id: d for d in devices}
graph = []
seen_edges = set()
for c in conns:
con = json.loads(c.line_json)
src_id = con[0]["source"]["node"]
tgt_id = con[0]["target"]["node"]
src = by_id.get(src_id)
tgt = by_id.get(tgt_id)
if not src or not tgt:
continue
if src.type != "Q" or tgt.type != "Q":
continue
i = _parse_ow_device_index(src)
j = _parse_ow_device_index(tgt)
if i is None or j is None:
return None, "Each Q node in a connection must have a valid index."
edge = (min(i, j), max(i, j))
if edge not in seen_edges:
seen_edges.add(edge)
graph.append([edge[0], edge[1]])
n_q = 0
if graph:
n_q = max(max(edge) for edge in graph)
in_to_q = {}
for c in conns:
con = json.loads(c.line_json)
src_id = con[0]["source"]["node"]
tgt_id = con[0]["target"]["node"]
src = by_id.get(src_id)
tgt = by_id.get(tgt_id)
if not src or not tgt:
continue
if src.type == "IN" and tgt.type == "Q":
in_dev, q_dev = src, tgt
elif src.type == "Q" and tgt.type == "IN":
in_dev, q_dev = tgt, src
else:
continue
in_idx = _parse_ow_device_index(in_dev)
q_idx = _parse_ow_device_index(q_dev)
if in_idx is None or q_idx is None:
return None, "Each IN/Q node in a connection must have a valid index."
if in_idx in in_to_q and in_to_q[in_idx] != q_idx:
return None, "IN %s is connected to more than one Q node." % in_idx
in_to_q[in_idx] = q_idx
observables = []
results = []
in_indices_seen = set()
q_indices_seen = set()
for d in devices:
idx = _parse_ow_device_index(d)
if idx is None:
continue
if d.type == "IN":
if idx in in_indices_seen:
return None, "Duplicate IN index: %s" % idx
in_indices_seen.add(idx)
elif d.type == "Q":
if idx in q_indices_seen:
return None, "Duplicate Q index: %s" % idx
q_indices_seen.add(idx)
node = _ow_simulation_node_index(d, n_q)
if _ow_angle_specified(d):
angle_deg = float(d.angle)
angle_rad = angle_deg * math.pi / 180.0
observables.append([node, angle_rad])
if _ow_result_specified(d):
results.append([node, int(d.result)])
ins = []
for in_idx in sorted(in_indices_seen):
if in_idx not in in_to_q:
return None, "IN %s is not connected to a Q node." % in_idx
ins.append(in_to_q[in_idx])
observables.sort(key=lambda x: x[0])
results.sort(key=lambda x: x[0])
return (graph, ins, observables, results), None
def _format_ow_vector_latex(mat):
import sympy as sp
rows = []
for i in range(mat.rows):
val = mat[i, 0]
try:
rows.append(sp.latex(val.evalf()))
except Exception:
rows.append(str(val))
if not rows:
return "\\[ \\mathbf{0} \\]"
body = " \\\\ ".join(rows)
return "\\[ \\begin{pmatrix} " + body + " \\end{pmatrix} \\]"
@with_ndb_context
def ConstructOWCircuit(project_key):
try:
proj = Project.get(project_key)
if not proj:
return json.dumps({"error": True, "data": "No project"})
user_email = current_user_email()
proj_user = normalize_user_value(proj.user)
if proj_user and proj_user != user_email:
return json.dumps({"error": True, "data": "Not authorized"})
if not proj_user and user_email:
proj.user = user_email
proj.put()
_stamp_project_kind(proj, "ow")
circuit = OWCircuit.query(ancestor=proj.key).fetch(1)
if circuit:
return json.dumps({"error": False, "data": circuit[0].result}, default=str)
devices = OWDevice.query(ancestor=proj.key).fetch(1000)
conns = OWConnection.query(ancestor=proj.key).fetch(1000)
inputs, error = _build_ow_simulation_inputs(devices, conns)
if error:
return json.dumps({"error": True, "data": error})
graph, ins, observables, results = inputs
vec = owqc.universal_transformation(graph, ins, observables, results)
result = _format_ow_vector_latex(vec)
circ = OWCircuit(parent=proj.key)
circ.result = result
circ.name = proj.name
circ.put()
return json.dumps({"error": False, "data": result}, default=str)
except Exception as ex:
return json.dumps({"error": True, "data": str(ex)}, default=str)
@with_ndb_context
def ConstructCircuit(project_key):
try:
proj = Project.get(project_key)
if not proj:
return "No project"
user_email = current_user_email()
proj_user = normalize_user_value(proj.user)
if proj_user and proj_user != user_email:
return "Not authorized"
if not proj_user and user_email:
proj.user = user_email
proj.put()
_stamp_project_kind(proj, "lo")
# check if we already did the calculation
circuit = LOCircuit.query(ancestor=proj.key).fetch(1)
if circuit:
return json.dumps(circuit[0].result, default=str)
# prepare circuit
devs, modes, error = getDevsModes(proj)
if error:
return error
# run circuit
lx, matrix, inv = loqc.ConstructCircuit(devs, modes)
# splitting
terms_p = lx.split("+")
res = ""
for t in terms_p:
terms_m = t.split("-")
res_m = ""
if len(terms_m) <= 1:
res_m = "\\(" + t + "\\)" + "-"
else:
for t1 in terms_m:
res_m += "\\(" + t1 + "\\)" + "-"
res += res_m[:-1] + "+"
# save result
circ = LOCircuit(parent=proj.key)
circ.modes = modes
circ.matrix = matrix
circ.inv = inv
circ.result = res[:-1]
#cgate_res, error = loqc.get_cgate_run(project_key)
#if not error:
# circ.result += cgate_res
circ.name = proj.name
circ.put()
return json.dumps(circ.result, default=str)
except Exception as ex:
return str(ex)