-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitUse.qll
1142 lines (986 loc) · 39.1 KB
/
BitUse.qll
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 qiskit.Circuit
import qiskit.Register
import qiskit.BitDef
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.TaintTracking
import semmle.python.ApiGraphs
class EmptySetForString extends string {
EmptySetForString() {
this = "a" and this = "b" // this returns the empty set
}
}
class EmptySetForInt extends int {
EmptySetForInt() {
this = 1 and this = 2 // this returns the empty set
}
}
class EmptySetForRegisterV2 extends RegisterV2 {
EmptySetForRegisterV2() {
this.getName() != this.getName() // this returns the empty set
}
}
class EmptySetForQuantumCircuit extends QuantumCircuit {
EmptySetForQuantumCircuit() {
this.getName() != this.getName() // this returns the empty set
}
}
/** Usage of a qubit or bit. */
abstract class BitUse extends DataFlow::LocalSourceNode {
int getAnIndex() {
if exists(int i | i = this.getAnIndexIfAny())
then exists(int i | i = this.getAnIndexIfAny() | result = i)
else result = -1
}
int getAnAbsoluteIndex() {
if exists(int i | i = this.getAnAbsoluteIndexIfAny())
then exists(int i | i = this.getAnAbsoluteIndexIfAny() | result = i)
else result = -1
}
string getARegisterName() {
if exists(RegisterV2 reg | reg = this.getARegister())
then exists(RegisterV2 reg | reg = this.getARegister() | result = reg.getName())
else result = "anonymous register"
}
string getACircuitName() {
if exists(QuantumCircuit circ | circ = this.getACircuit())
then exists(QuantumCircuit circ | circ = this.getACircuit() | result = circ.getName())
else result = "anonymous circuit"
}
abstract int getAnIndexIfAny();
abstract int getAnAbsoluteIndexIfAny();
abstract RegisterV2 getARegister();
abstract QuantumCircuit getACircuit();
abstract QuantumOperator getAGate();
abstract string getAGateName();
// boolean mayFollow(BitUse other) {
// }
/** Holds if the current bit follows the other qubit in any scenario. */
predicate mustFollow(BitUse other) {
// case: qc.h(0); qc.rx(3.14, 0)
exists(
QuantumOperator currentGate, QuantumOperator otherGate, int commontIndex,
string commonCircuitName, string commonRegisterName
|
// either they act on the same register
this.getARegisterName() = commonRegisterName and
other.getARegisterName() = commonRegisterName
or
// they act on different ones, but one is anonymous
this.getARegisterName() = "anonymous register" and
other.getARegisterName() = commonRegisterName
or
this.getARegisterName() = commonRegisterName and
other.getARegisterName() = "anonymous register"
|
// they act on the same index in the register
this.getAnIndex() = commontIndex and
other.getAnIndex() = commontIndex and
// they act on the same circuit
this.getACircuitName() = commonCircuitName and
other.getACircuitName() = commonCircuitName and
// the current gate is after the other gate
this.getAGate() = currentGate and
other.getAGate() = otherGate and
otherGate.getNode().strictlyDominates(currentGate.getNode())
)
or
// case: qc.h(0); qc.ch(0, 2); qc.x(2)
exists(
QuantumOperator intermediateGate, BitUse intermediateBitUseA, BitUse intermediateBitUseB
|
intermediateBitUseA.getAGate() = intermediateGate and
intermediateBitUseB.getAGate() = intermediateGate and
intermediateBitUseA.mustFollow(other) and
this.mustFollow(intermediateBitUseB)
)
// or
// // case: qc.h(0); qc.x(qreg[0]); with qreg only register
// exists(
// Gate currentGate, Gate otherGate,
// int commontIndex
// |
// // the act on the same circuit, register and index
// this.getAnIndex() = commontIndex and
// other.getAnIndex() = commontIndex and
// forall(string circNameThis, string circNameOther
// |
// circNameThis = this.getACircuitName() and
// circNameOther = other.getACircuitName()
// |
// circNameThis = circNameOther
// ) and
// // the current gate is after the other gate
// this.getAGate() = currentGate and
// other.getAGate() = otherGate and
// otherGate.getNode().strictlyDominates(currentGate.getNode()) and
// // one of the two is anonymous and the other is not
// exists(RegisterV2 reg
// |
// (
// reg = this.getARegister() and other.getARegisterName() = "anonymous register"
// or
// reg = other.getARegister() and this.getARegisterName() = "anonymous register"
// ) and
// this.likelySameCircuit(other)
// )
// )
}
// boolean mayFollowDirectly(BitUse other) {
// }
// boolean mustFollowDirectly(BitUse other) {
// }
/** Holds if the current bit refers to a specific BitDef. */
predicate refersTo(BitDefinition referencedBitDef) {
this.getARegisterName() = referencedBitDef.getARegisterName() and
this.getAnIndexIfAny() = referencedBitDef.getAnIndexIfAny() and
this.getACircuitName() = referencedBitDef.getACircuitName() and
this.getARegister() = referencedBitDef.getARegister() and
this.getACircuit() = referencedBitDef.getACircuit()
}
/** Holds if the two BitUse refer to the same position and circuit. */
predicate equals(BitUse other) {
this.getARegisterName() = other.getARegisterName() and
this.getAnIndexIfAny() = other.getAnIndexIfAny() and
this.getACircuitName() = other.getACircuitName() and
this.getARegister() = other.getARegister() and
this.getACircuit() = other.getACircuit()
}
/** Holds if the two BitUse refer to the same circuit. */
private predicate sameCircuit(BitUse other) {
// case: qc.h(0); qc.h(qreg[2])
this.getACircuitName() = other.getACircuitName() and
this.getACircuit() = other.getACircuit()
}
/** Holds if the two BitUse refer to the same register. */
predicate sameRegister(BitUse other) {
// case: qc.h(qreg[0]); qc.h(qreg[1])
this.getARegisterName() = other.getARegisterName() and
this.getARegister() = other.getARegister()
or
// case: qc.h(0); qc.h(1)
this.getARegisterName() = "anonymous register" and
other.getARegisterName() = "anonymous register" and
this.sameCircuit(other)
}
/** Holds if one is anonymous register and other is on the only quantum register. */
predicate hasCircuitSingleAnonymousRegister() {
// case QubitUse
this instanceof QubitUse and
this.getARegisterName() = "anonymous register" and
count(QuantumRegisterV2 qreg | qreg = this.getACircuit().(QuantumCircuit).getAQuantumRegister()) =
1
or
// case ClbitUse
this instanceof ClbitUse and
this.getARegisterName() = "anonymous register" and
count(ClassicalRegisterV2 creg |
creg = this.getACircuit().(QuantumCircuit).getAClassicalRegister()
) = 1
}
/** Holds if the other BitUse MUST refers to the same position, register and circuit. */
predicate mustReferToSameBitOf(BitUse other) {
this.mustReferToSameRegAndCircOf(other) and
this.getAnIndexIfAny() = other.getAnIndexIfAny()
}
/**
* Holds if the other BitUse MAY refers to the same position, register and circuit.
*
* Note that this caputre also cases where the variable might not be modeled
* (e.g. qc.h(i) and qc.h(5 + 7)).
*/
predicate mayReferToSameBitOf(BitUse other) {
this.mustReferToSameRegAndCircOf(other) and
this.getAnIndex() = other.getAnIndex() // this might be -1 for both
}
/** Holds if the other BitUse MUST refers to the same register and circuit. */
predicate mustReferToSameRegAndCircOf(BitUse other) {
// same type
(
this instanceof QubitUse and other instanceof QubitUse
or
this instanceof ClbitUse and other instanceof ClbitUse
) and
// case: same circuit -> qc.h(0); qc.h(qreg[2])
this.sameCircuit(other) and
(
// case: same default register -> qc.h(0); qc.h(1)
this.getARegisterName() = "anonymous register" and
other.getARegisterName() = "anonymous register"
or
// case: mixed but this is anonymous -> qc.h(0); qc.h(qreg[1])
this.hasCircuitSingleAnonymousRegister()
or
// case: mixed but other is anonymous -> qc.h(qreg[0]); qc.h(1)
other.hasCircuitSingleAnonymousRegister()
)
or
// case: same circuit and register -> qc.h(qreg[0]); qc.h(qreg[1])
this.sameCircuit(other) and this.sameRegister(other)
}
}
/** Resolves both sum and subtraction of integer literals. */
pragma[inline]
int resolveBinArithmetic(BinaryExpr binExpr) {
// case:
// 4 + 2
// >> 6
// case:
// n=1
// 4 - n
// >> 3
exists(
IntegerLiteral i, DataFlow::LocalSourceNode source, DataFlow::Node sink, IntegerLiteral iRight,
DataFlow::LocalSourceNode sourceRight, DataFlow::Node sinkRight
|
// LEFT
source.asExpr() = i and
source.flowsTo(sink) and
// source.getLocation().getFile().getAbsolutePath().matches("%concept_bit_expr.py") and
// RIGHT
sourceRight.asExpr() = iRight and
sourceRight.flowsTo(sinkRight) and
// sourceRight.getLocation().getFile().getAbsolutePath().matches("%concept_bit_expr.py") and
// BOTH PART OF THE TWO SIDES OF THE EXPRESSION
binExpr.getLeft() = sink.asExpr() and
binExpr.getRight() = sinkRight.asExpr()
|
binExpr.getOp() instanceof Add and
result = i.getValue() + iRight.getValue()
or
binExpr.getOp() instanceof Sub and
result = i.getValue() - iRight.getValue()
or
binExpr.getOp() instanceof Mult and
result = i.getValue() * iRight.getValue()
)
}
/**
* DEPRECATED - use resolveBinArithmetic instead.
*
* Solve only sum of direct integers.
*/
int resolveBinArithmeticFast(BinaryExpr binExpr) {
// case:
// 4 + 2
// >> 6
binExpr.getOp() instanceof Add and
result =
binExpr.getLeft().(IntegerLiteral).getValue() + binExpr.getRight().(IntegerLiteral).getValue()
or
binExpr.getOp() instanceof Sub and
result =
binExpr.getLeft().(IntegerLiteral).getValue() - binExpr.getRight().(IntegerLiteral).getValue()
or
binExpr.getOp() instanceof Mult and
result =
binExpr.getLeft().(IntegerLiteral).getValue() * binExpr.getRight().(IntegerLiteral).getValue()
}
/** Resolves ranges of integers. */
int resolveRange(Call call) {
// case:
// range(2)
// >> [0, 1]
// case:
// range(2, 4)
// >> [2, 3]
exists(
Value functionValue, DataFlow::CallCfgNode callCfg, IntegerLiteral iEnd,
DataFlow::LocalSourceNode sourceEnd, DataFlow::Node sinkEnd, int i, int startValue, int endValue
|
functionValue.getName() = "range" and
functionValue.getACall().getNode() = call and
callCfg.asExpr() = call and
(
exists(
IntegerLiteral iStart, DataFlow::LocalSourceNode sourceStart, DataFlow::Node sinkStart
|
//case: range(2, 4)
// START
sourceStart.asExpr() = iStart and
sourceStart.flowsTo(sinkStart) and
// END
sourceEnd.asExpr() = iEnd and
sourceEnd.flowsTo(sinkEnd) and
call.getArg(0) = sinkStart.asExpr() and
call.getArg(1) = sinkEnd.asExpr() and
startValue = iStart.getValue() and
endValue = iEnd.getValue()
)
or
// case: range(2)
// END
sourceEnd.asExpr() = iEnd and
sourceEnd.flowsTo(sinkEnd) and
call.getArg(0) = sinkEnd.asExpr() and
endValue = iEnd.getValue() and
startValue = 0
)
|
i in [startValue .. endValue - 1] and
result = i
)
}
/** Use of a qubit. */
abstract class QubitUse extends BitUse {
pragma[inline]
override int getAnIndexIfAny() {
// case: qc.h(0)
// > 0
// case: qc.h(quantum_register[7])
// > 7
exists(IntegerLiteral lit, DataFlow::LocalSourceNode litSource |
not this.asExpr() instanceof Subscript and
litSource.asExpr() = lit and
litSource.flowsTo(this)
|
result = lit.getValue()
)
or
// case: qc.h(quantum_register) with quantum_register = QuantumRegister(2)
// > [0, 1]
exists(QuantumRegisterV2 qreg, int i |
qreg.flowsTo(this) and
i in [0 .. qreg.getSize() - 1]
|
result = i
)
or
// case: a = 0; qc.h(qreg[a])
exists(
QuantumRegisterV2 qreg, IntegerLiteral lit, DataFlow::LocalSourceNode litSource,
DataFlow::LocalSourceNode indexDest
|
qreg = this.getARegister() and
litSource.asExpr() = lit and
indexDest.asExpr() = this.asExpr().(Subscript).getIndex() and
litSource.flowsTo(indexDest)
|
result = lit.getValue()
)
or
// case: qc.h(range(2))
// case: qc.h(range(2, 4))
exists(Value val, Call call, DataFlow::CallCfgNode callCfg |
val.getName() = "range" and
callCfg.asExpr() = call and
callCfg.flowsTo(this)
|
result = resolveRange(call)
)
or
// case: qc.h(3+1)
// case: qc.h(4-1)
// case: qc.rx(3.14, n+1) with n=8
exists(BinaryExpr binOp |
(
binOp.getOp() instanceof Add or
binOp.getOp() instanceof Sub or
binOp.getOp() instanceof Mult
) and
this.asExpr() = binOp
|
result = resolveBinArithmetic(binOp)
)
or
// case: qc.h(quantum_register[0:2])
exists(Slice slice, QuantumRegisterV2 qreg |
qreg = this.getARegister() and
// connect superscript with its slice
this.asExpr().(Subscript).getIndex() = slice
|
result = qreg.resolveSlice(slice)
)
or
// case: qc.measure([0, 1], [0, 1])
// case: qc.measure(qubit=[0, 1], cbit=[0, 1])
exists(
List list, DataFlow::LocalSourceNode integerSource, DataFlow::LocalSourceNode indexDest,
IntegerLiteral integerLiteral
|
// the current qubit use is expressed as a list
this.asExpr() = list and
// there is an integer flowing in the integer position
integerSource.asExpr() = integerLiteral and
list.getAnElt() = indexDest.asExpr() and
integerSource.flowsTo(indexDest)
|
result = integerLiteral.getValue()
)
// case: qc.h(qreg[3+1])
// else result instanceof EmptySetForInt
}
pragma[inline]
override int getAnAbsoluteIndexIfAny() {
// if this belongs to a no register it is like the relative index
count(QuantumRegisterV2 qreg | qreg = this.getARegister()) = 0 and
this.getAnIndexIfAny() = result
or
// if this belongs to a register,
count(QuantumRegisterV2 qreg | qreg = this.getARegister()) > 0 and
// the register is shift by the amount of positions of the previous registers
exists(
QuantumCircuitConstructor circ, QuantumRegisterV2 currentReg, int sizePreceedingRegs,
int posCurrentReg
|
// the circuit is of this qubit use as well
circ = this.getACircuit() and
// connects the qubit use and its register
currentReg = this.getARegister() and
// it flows to the register as a specific position of the circuit constructor
currentReg.flowsTo(circ.getArg(posCurrentReg)) and
// the sum of the preceeding registers is...
sizePreceedingRegs =
sum(int iSize |
exists(QuantumRegisterV2 iReg, int iRegPos |
iSize = iReg.getSize() and
// the register is part of the same circuit
iReg.getACircuit() = circ and
// the register flows in the same circuit constructor
iReg.flowsTo(circ.getArg(iRegPos)) and
// it position comes before the current register
iRegPos < posCurrentReg
)
)
|
result = sizePreceedingRegs + this.getAnIndexIfAny()
)
or
// if it is not a QuantumCircuitConstructor
// then it returns -1
not this.getACircuit() instanceof QuantumCircuitConstructor and
result = -1
}
override RegisterV2 getARegister() {
// case: qc.h(0)
// > EmptySetForRegisterV2
// case: qc.h(quantum_register[7])
// > quantum_register
// case: qc.h(quantum_register)
// > quantum_register
// exists(RegisterV2 reg
// |
// reg.getScope() = this.getScope() and
// reg.getName() = this.asExpr().(Subscript).getObject().toString()
// |
// result = reg
// )
// or
exists(RegisterV2 reg, Value regValue |
regValue.getOrigin() = reg.getNode() and
this.asExpr().(Subscript).getObject().pointsTo(regValue)
|
result = reg
)
or
exists(RegisterV2 reg | reg.flowsTo(this) | result = reg)
or
result instanceof EmptySetForRegisterV2
// if
// exists(RegisterV2 reg |
// reg.getScope() = this.getScope() and
// reg.getName() = this.asExpr().(Subscript).getObject().toString()
// )
// then
// exists(RegisterV2 reg |
// reg.getScope() = this.getScope() and
// reg.getName() = this.asExpr().(Subscript).getObject().toString()
// |
// result = reg
// )
// else
// if exists(RegisterV2 reg | reg.flowsTo(this))
// then exists(RegisterV2 reg | reg.flowsTo(this) | result = reg)
// else result instanceof EmptySetForRegisterV2
}
}
/** Use of a clbit. */
abstract class ClbitUse extends BitUse { }
/** Use of a qubit as attribute call on the circuit object. */
class QubitUseViaAttribute extends QubitUse {
QubitUseViaAttribute() {
exists(
QuantumCircuit circ, OperatorSpecification gs, DataFlow::LocalSourceNode qubitListSource,
DataFlow::CallCfgNode call
|
// detect qc.h(0)
call = circ.getAnAttributeRead(gs).getACall() and
// avoid to consider the values defined in the __get_item__() method
// of the Register class in qiskit
not this.getLocation()
.getFile()
.getAbsolutePath()
.matches("%site-packages/qiskit/circuit/register.py")
|
exists(int i | i = gs.getAnArgumentIndexOfQubit() |
call.(API::CallNode).getParameter(i).getAValueReachingSink() = qubitListSource and
// CASE: qc.h(0)
qubitListSource.asExpr() = this.asExpr()
// or
// CASE: qc.measure([0, 1], [0, 1])
// qubitListSource.asExpr() instanceof List and
// qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
)
or
exists(string kyw | kyw = gs.getAnArgumentNameOfQubit() |
call.(API::CallNode).getKeywordParameter(kyw).getAValueReachingSink() = qubitListSource and
// CASE: qc.h(qubit=0)
qubitListSource.asExpr() = this.asExpr()
// or
// // CASE: qc.measure(qubit=[0, 1], cbit=[0, 1])
// qubitListSource.asExpr() instanceof List and
// qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
)
)
}
override string getAGateName() {
exists(QuantumCircuit circ, OperatorSpecification gs, DataFlow::CallCfgNode call |
// detect qc.h(0)
call = circ.getAnAttributeRead(gs).getACall() and
(
exists(int i | i = gs.getAnArgumentIndexOfQubit() |
call.(API::CallNode).getParameter(i).getAValueReachingSink() = this
)
or
exists(string kyw | kyw = gs.getAnArgumentNameOfQubit() |
call.(API::CallNode).getKeywordParameter(kyw).getAValueReachingSink() = this
)
)
|
result = gs
)
}
override QuantumOperator getAGate() {
exists(QuantumCircuit circ, OperatorSpecification gs, DataFlow::CallCfgNode call |
// detect qc.h(0)
call = circ.getAnAttributeRead(gs).getACall() and
(
exists(int i | i = gs.getAnArgumentIndexOfQubit() |
call.(API::CallNode).getParameter(i).getAValueReachingSink() = this
)
or
exists(string kyw | kyw = gs.getAnArgumentNameOfQubit() |
call.(API::CallNode).getKeywordParameter(kyw).getAValueReachingSink() = this
)
)
|
result = call
)
}
override QuantumCircuit getACircuit() {
exists(QuantumCircuit circ, OperatorSpecification gs, DataFlow::CallCfgNode call |
// detect qc.h(0)
call = circ.getAnAttributeRead(gs).getACall() and
(
exists(int i | i = gs.getAnArgumentIndexOfQubit() |
call.(API::CallNode).getParameter(i).getAValueReachingSink() = this
)
or
exists(string kyw | kyw = gs.getAnArgumentNameOfQubit() |
call.(API::CallNode).getKeywordParameter(kyw).getAValueReachingSink() = this
)
)
|
result = circ
)
}
}
/** Use of a qubit as appended call on the circuit object. */
class QubitUseViaAppend extends QubitUse {
QubitUseViaAppend() {
exists(
QuantumCircuit circ, OperatorSpecification gs, DataFlow::LocalSourceNode qubitListSource,
DataFlow::CallCfgNode appendCall, DataFlow::CallCfgNode gateCall
|
// detect qc.append(CXGate(), [0, 1])
appendCall = circ.getAnAttributeRead("append").getACall() and
gateCall = appendCall.(API::CallNode).getParameter(0, "instruction").getAValueReachingSink() and
checkCallAndSpecLinkedInAPIGraph(gateCall, gs) and
qubitListSource = appendCall.(API::CallNode).getParameter(1, "qargs").getAValueReachingSink() and
// avoid to consider the values defined in the __get_item__() method
// of the Register class in qiskit
not this.getLocation()
.getFile()
.getAbsolutePath()
.matches("%site-packages/qiskit/circuit/register.py")
|
// CASE: qc.append(CXGate(), [0, 1])
qubitListSource.asExpr() instanceof List and
qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
or
// CASE: qc.append(CXGate(), 0)
qubitListSource.asExpr() = this.asExpr()
)
}
override string getAGateName() {
exists(
QuantumCircuit circ, OperatorSpecification gs, DataFlow::LocalSourceNode qubitListSource,
DataFlow::CallCfgNode appendCall, DataFlow::CallCfgNode gateCall
|
// detect qc.append(CXGate(), [0, 1])
appendCall = circ.getAnAttributeRead("append").getACall() and
gateCall = appendCall.(API::CallNode).getParameter(0, "instruction").getAValueReachingSink() and
checkCallAndSpecLinkedInAPIGraph(gateCall, gs) and
qubitListSource = appendCall.(API::CallNode).getParameter(1, "qargs").getAValueReachingSink() and
// qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
if qubitListSource.asExpr() instanceof List
then qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
else qubitListSource.asExpr() = this.asExpr()
|
result = gs
)
}
override QuantumOperator getAGate() {
exists(
QuantumCircuit circ, OperatorSpecification gs, DataFlow::LocalSourceNode qubitListSource,
DataFlow::CallCfgNode appendCall, DataFlow::CallCfgNode gateCall
|
// detect qc.append(CXGate(), [0, 1])
appendCall = circ.getAnAttributeRead("append").getACall() and
gateCall = appendCall.(API::CallNode).getParameter(0, "instruction").getAValueReachingSink() and
checkCallAndSpecLinkedInAPIGraph(gateCall, gs) and
qubitListSource = appendCall.(API::CallNode).getParameter(1, "qargs").getAValueReachingSink() and
// qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
if qubitListSource.asExpr() instanceof List
then qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
else qubitListSource.asExpr() = this.asExpr()
|
result = gateCall
)
}
override QuantumCircuit getACircuit() {
exists(
QuantumCircuit circ, OperatorSpecification gs, DataFlow::LocalSourceNode qubitListSource,
DataFlow::CallCfgNode appendCall, DataFlow::CallCfgNode gateCall
|
// detect qc.append(CXGate(), [0, 1])
appendCall = circ.getAnAttributeRead("append").getACall() and
gateCall = appendCall.(API::CallNode).getParameter(0, "instruction").getAValueReachingSink() and
checkCallAndSpecLinkedInAPIGraph(gateCall, gs) and
qubitListSource = appendCall.(API::CallNode).getParameter(1, "qargs").getAValueReachingSink() and
// qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
if qubitListSource.asExpr() instanceof List
then qubitListSource.asExpr().(List).getAnElt() = this.asExpr()
else qubitListSource.asExpr() = this.asExpr()
|
result = circ
)
}
}
/** Use of a qubit in a measure_all call. */
class QubitUseViaMeasureAll extends QubitUse {
QubitUseViaMeasureAll() {
exists(QuantumCircuit circ |
// detect qc.measure_all()
this = circ.getAnAttributeRead("measure_all").getACall()
)
}
override string getAGateName() { result = "measure_all" }
override QuantumOperator getAGate() { result = this }
override QuantumCircuit getACircuit() {
exists(QuantumCircuit circ |
// detect qc.measure_all()
this = circ.getAnAttributeRead("measure_all").getACall()
|
result = circ
)
}
override int getAnIndexIfAny() {
exists(QuantumCircuit circ, int i |
this = circ.getAnAttributeRead("measure_all").getACall() and
if circ.getNumberOfQubits() > 0 then i in [0 .. circ.getNumberOfQubits() - 1] else i = -1
|
result = i
)
}
}
// GATE SPECIFICATIONS
// TODO: support mcrx, mcry, mcrz
// TODO: cu1 and cu3 are deprecated, support different versions of Qiskit
abstract class OperatorSpecification extends string {
OperatorSpecification() {
this instanceof OperatorSpecificationObjectName or
this instanceof OperatorSpecificationAttributeName
}
string getName() { result = this }
/** The argument position pointing to a classical bit. */
int getAnArgumentIndexOfClbit() {
exists(int i |
this.getNumberOfClbits() > 0 and
i in [0 .. this.getNumberOfClbits() - 1]
|
// shift the index if there are qubits
if this.getNumberOfQubits() > 0 then result = i + this.getNumberOfQubits() else result = i
)
}
/** The named argument pointing to a classical bit. */
string getAnArgumentNameOfClbit() { result instanceof EmptySetForString }
/** The argument position pointing to a qubit. */
int getAnArgumentIndexOfQubit() {
exists(int i | this.getNumberOfQubits() > 0 and i in [0 .. this.getNumberOfQubits() - 1] |
// shift the index if there are parameters
if this.getNumberOfParams() > 0 then result = i + this.getNumberOfParams() else result = i
)
}
/** The named argument pointing to a qubit. */
string getAnArgumentNameOfQubit() { result instanceof EmptySetForString }
/** The argument position pointing to a parameter. */
int getAnArgumentIndexOfParam() {
exists(int i |
this.getNumberOfParams() > 0 and
i in [0 .. this.getNumberOfParams() - 1]
|
result = i
)
}
/** The named argument pointing to a parameter. */
string getAnArgumentNameOfParam() { result instanceof EmptySetForString }
/** Number of bits used in the gate. */
int getNumberOfBits() { result = this.getNumberOfQubits() + this.getNumberOfClbits() }
/** Number of parameters used in the gate. */
int getNumberOfParams() { result = count(string s | s = this.getAnArgumentNameOfParam()) }
/** Number of classical bits used in the gate. */
int getNumberOfClbits() { result = count(string s | s = this.getAnArgumentNameOfClbit()) }
/** Number of qubits used in the gate. */
int getNumberOfQubits() { result = count(string s | s = this.getAnArgumentNameOfQubit()) }
}
class OperatorSpecificationAttributeName extends string {
OperatorSpecificationAttributeName() {
this in [
// single bit operations
"x", "y", "z", "h", "s", "sdg", "t", "tdg", "rx", "ry", "rz", "rv", "u1", "u2", "u3", "id",
"u", "iden", "i", "sx", "p",
// controlled operations
"cx", "cnot", "cy", "cz", "ch", "cs", "csdg", "csx", "crz", "cry", "crx", "cu1", "cu3",
"cu", "ccx", "ccz", "toffoli", "cswap", "fredkin", "mct", "rccx", "rcccx", "cp",
// multi bit operations
"rxx", "ryy", "rzz", "rzx", "swap", "iswap", "ms", "cr", "r", "rccx", "ecr",
// measurements
"measure", "measure_all",
// reset
"reset",
// unitary
"unitary",
// initialize
"initialize"
]
}
}
class OperatorSpecificationObjectName extends string {
OperatorSpecificationObjectName() {
this in [
// single operations
"XGate", "YGate", "ZGate", "HGate", "SGate", "SdgGate", "TGate", "TdgGate", "RXGate",
"RYGate", "RZGate", "RVGate", "U1Gate", "U2Gate", "U3Gate", "IGate", "SXGate", "PhaseGate",
"UGate",
// controlled operations
"CXGate", "CYGate", "CZGate", "CHGate", "CSGate", "CSdgGate", "CSXGate", "CRZGate",
"CRYGate", "CRXGate", "CU1Gate", "CU3Gate", "CCXGate", "CCZGate", "CSwapGate", "MCXGate",
"RCCXGate", "RC3XGate", "CPhaseGate",
// multi bit operations
"RXXGate", "RYYGate", "RZZGate", "RZXGate", "SwapGate", "iSwapGate", "MSGate", "CRGate",
"RGate", "RCCXGate", "ECRGate",
// measurements
"Measure",
// reset
"Reset",
// unitary
"UnitaryGate",
// initialize
"Initialize"
]
}
}
/** Specification of gates that are unitary / reversible gates. */
abstract class OperatorSpecificationUnitary extends OperatorSpecification { }
/** Specification of gates that are not unitary and destroy the quantum state. */
abstract class OperatorSpecificationNonUnitary extends OperatorSpecification { }
// NON-UNITARY GATES
class OperatorSpecificationReset extends OperatorSpecificationNonUnitary {
OperatorSpecificationReset() { this in ["reset", "Reset"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
}
class OperatorSpecificationMeasureAll extends OperatorSpecificationNonUnitary {
OperatorSpecificationMeasureAll() { this in ["measure_all"] }
}
class OperatorSpecificationMeasure extends OperatorSpecificationNonUnitary {
OperatorSpecificationMeasure() { this in ["measure", "Measure"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfClbit() { result = "cbit" }
}
class OperatorSpecificationInitialize extends OperatorSpecificationNonUnitary {
OperatorSpecificationInitialize() { this in ["initialize", "Initialize"] }
override string getAnArgumentNameOfQubit() { result = "qubits" }
override string getAnArgumentNameOfParam() { result = "params" }
}
// UNITARY GATES
class OperatorSpecificationUnitaryGateObj extends OperatorSpecificationUnitary {
OperatorSpecificationUnitaryGateObj() { this = "UnitaryGate" }
override string getAnArgumentNameOfParam() { result = "data" }
}
class OperatorSpecificationUnitaryCall extends OperatorSpecificationUnitary {
OperatorSpecificationUnitaryCall() { this = "unitary" }
override string getAnArgumentNameOfParam() { result = "obj" }
override string getAnArgumentNameOfQubit() { result = "qubits" }
}
class OperatorSpecificationSingleQubitNoParam extends OperatorSpecificationUnitary {
OperatorSpecificationSingleQubitNoParam() {
this in [
"h", "x", "y", "z", "s", "sdg", "t", "tdg", "sx", "i", "id", "iden", "HGate", "XGate",
"YGate", "ZGate", "SGate", "SdgGate", "TGate", "TdgGate", "SXGate", "IGate"
]
}
override string getAnArgumentNameOfQubit() { result = "qubit" }
}
class OperatorSpecificationPGate extends OperatorSpecificationUnitary {
OperatorSpecificationPGate() { this in ["p", "PhaseGate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result = "theta" }
}
class OperatorSpecificationRXGate extends OperatorSpecificationUnitary {
OperatorSpecificationRXGate() { this in ["rx", "RXGate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result = "theta" }
}
class OperatorSpecificationRYGate extends OperatorSpecificationUnitary {
OperatorSpecificationRYGate() { this in ["ry", "RYGate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result = "theta" }
}
class OperatorSpecificationRZGate extends OperatorSpecificationUnitary {
OperatorSpecificationRZGate() { this in ["rz", "RZGate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result = "phi" }
}
class OperatorSpecificationRVGate extends OperatorSpecificationUnitary {
OperatorSpecificationRVGate() { this in ["rv", "RVGate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result in ["vx", "vy", "vz"] }
}
class OperatorSpecificationU1Gate extends OperatorSpecificationUnitary {
OperatorSpecificationU1Gate() { this in ["u1", "U1Gate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result = "theta" }
}
class OperatorSpecificationU2Gate extends OperatorSpecificationUnitary {
OperatorSpecificationU2Gate() { this in ["u2", "U2Gate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result in ["phi", "lam"] }
}
class OperatorSpecificationU3Gate extends OperatorSpecificationUnitary {
OperatorSpecificationU3Gate() { this in ["u3", "U3Gate", "u", "UGate"] }
override string getAnArgumentNameOfQubit() { result = "qubit" }
override string getAnArgumentNameOfParam() { result in ["theta", "phi", "lam"] }
}
// TODO: CHECK IF ALL GATES WITH PARAMS ARE PRESENT
class OperatorSpecificationCPGate extends OperatorSpecificationUnitary {
OperatorSpecificationCPGate() { this in ["cp", "CPhaseGate"] }
override string getAnArgumentNameOfQubit() { result in ["control_qubit", "target_qubit"] }
override string getAnArgumentNameOfParam() { result = "theta" }
}
class OperatorSpecificationCXGate extends OperatorSpecificationUnitary {
OperatorSpecificationCXGate() { this in ["cx", "CXGate", "cnot"] }
override string getAnArgumentNameOfQubit() { result in ["control_qubit", "target_qubit"] }
}
class OperatorSpecificationCYGate extends OperatorSpecificationUnitary {
OperatorSpecificationCYGate() { this in ["cy", "CYGate"] }
override string getAnArgumentNameOfQubit() { result in ["control_qubit", "target_qubit"] }
}
class OperatorSpecificationCZGate extends OperatorSpecificationUnitary {
OperatorSpecificationCZGate() { this in ["cz", "CZGate"] }
override string getAnArgumentNameOfQubit() { result in ["control_qubit", "target_qubit"] }
}