49
49
)
50
50
from qualtran .resource_counting import BloqCountDictT , SympySymbolAllocator
51
51
from qualtran .simulation .classical_sim import ClassicalValT
52
- from qualtran .symbolics .types import HasLength , is_symbolic
52
+ from qualtran .symbolics .types import HasLength , is_symbolic , SymbolicInt
53
53
54
54
from .ec_point import ECPoint
55
55
@@ -80,8 +80,8 @@ class _ECAddStepOne(Bloq):
80
80
Fig 10.
81
81
"""
82
82
83
- n : int
84
- mod : int
83
+ n : 'SymbolicInt'
84
+ mod : 'SymbolicInt'
85
85
86
86
@cached_property
87
87
def signature (self ) -> 'Signature' :
@@ -214,9 +214,9 @@ class _ECAddStepTwo(Bloq):
214
214
Fig 10.
215
215
"""
216
216
217
- n : int
218
- mod : int
219
- window_size : int = 1
217
+ n : 'SymbolicInt'
218
+ mod : 'SymbolicInt'
219
+ window_size : 'SymbolicInt' = 1
220
220
221
221
@cached_property
222
222
def signature (self ) -> 'Signature' :
@@ -251,7 +251,9 @@ def on_classical_vals(
251
251
f1 = 0
252
252
else :
253
253
lam = QMontgomeryUInt (self .n ).montgomery_product (
254
- int (y ), QMontgomeryUInt (self .n ).montgomery_inverse (int (x ), self .mod ), self .mod
254
+ int (y ),
255
+ QMontgomeryUInt (self .n ).montgomery_inverse (int (x ), int (self .mod )),
256
+ int (self .mod ),
255
257
)
256
258
# TODO(https://github.com/quantumlib/Qualtran/issues/1461): Fix bug in circuit
257
259
# which flips f1 when lam and lam_r are equal.
@@ -299,7 +301,7 @@ def build_composite_bloq(
299
301
# If ctrl = 1 and x != a: lam = (y - b) / (x - a) % p.
300
302
z4_split = bb .split (z4 )
301
303
lam_split = bb .split (lam )
302
- for i in range (self .n ):
304
+ for i in range (int ( self .n ) ):
303
305
ctrls = [f1 , ctrl , z4_split [i ]]
304
306
ctrls , lam_split [i ] = bb .add (
305
307
MultiControlX (cvs = [0 , 1 , 1 ]), controls = ctrls , target = lam_split [i ]
@@ -311,7 +313,7 @@ def build_composite_bloq(
311
313
312
314
# If ctrl = 1 and x = a: lam = lam_r.
313
315
lam_r_split = bb .split (lam_r )
314
- for i in range (self .n ):
316
+ for i in range (int ( self .n ) ):
315
317
ctrls = [f1 , ctrl , lam_r_split [i ]]
316
318
ctrls , lam_split [i ] = bb .add (
317
319
MultiControlX (cvs = [1 , 1 , 1 ]), controls = ctrls , target = lam_split [i ]
@@ -383,9 +385,9 @@ class _ECAddStepThree(Bloq):
383
385
Fig 10.
384
386
"""
385
387
386
- n : int
387
- mod : int
388
- window_size : int = 1
388
+ n : 'SymbolicInt'
389
+ mod : 'SymbolicInt'
390
+ window_size : 'SymbolicInt' = 1
389
391
390
392
@cached_property
391
393
def signature (self ) -> 'Signature' :
@@ -455,7 +457,7 @@ def build_composite_bloq(
455
457
z1 = bb .add (IntState (bitsize = self .n , val = 0 ))
456
458
a_split = bb .split (a )
457
459
z1_split = bb .split (z1 )
458
- for i in range (self .n ):
460
+ for i in range (int ( self .n ) ):
459
461
a_split [i ], z1_split [i ] = bb .add (CNOT (), ctrl = a_split [i ], target = z1_split [i ])
460
462
a = bb .join (a_split , QMontgomeryUInt (self .n ))
461
463
z1 = bb .join (z1_split , QMontgomeryUInt (self .n ))
@@ -472,7 +474,7 @@ def build_composite_bloq(
472
474
z1 = bb .add (ModDbl (QMontgomeryUInt (self .n ), mod = self .mod ).adjoint (), x = z1 )
473
475
a_split = bb .split (a )
474
476
z1_split = bb .split (z1 )
475
- for i in range (self .n ):
477
+ for i in range (int ( self .n ) ):
476
478
a_split [i ], z1_split [i ] = bb .add (CNOT (), ctrl = a_split [i ], target = z1_split [i ])
477
479
a = bb .join (a_split , QMontgomeryUInt (self .n ))
478
480
z1 = bb .join (z1_split , QMontgomeryUInt (self .n ))
@@ -520,9 +522,9 @@ class _ECAddStepFour(Bloq):
520
522
Fig 10.
521
523
"""
522
524
523
- n : int
524
- mod : int
525
- window_size : int = 1
525
+ n : 'SymbolicInt'
526
+ mod : 'SymbolicInt'
527
+ window_size : 'SymbolicInt' = 1
526
528
527
529
@cached_property
528
530
def signature (self ) -> 'Signature' :
@@ -538,10 +540,10 @@ def on_classical_vals(
538
540
self , x : 'ClassicalValT' , y : 'ClassicalValT' , lam : 'ClassicalValT'
539
541
) -> Dict [str , 'ClassicalValT' ]:
540
542
x = (
541
- x - QMontgomeryUInt (self .n ).montgomery_product (int (lam ), int (lam ), self .mod )
543
+ x - QMontgomeryUInt (self .n ).montgomery_product (int (lam ), int (lam ), int ( self .mod ) )
542
544
) % self .mod
543
545
if lam > 0 :
544
- y = QMontgomeryUInt (self .n ).montgomery_product (int (x ), int (lam ), self .mod )
546
+ y = QMontgomeryUInt (self .n ).montgomery_product (int (x ), int (lam ), int ( self .mod ) )
545
547
return {'x' : x , 'y' : y , 'lam' : lam }
546
548
547
549
def build_composite_bloq (
@@ -554,7 +556,7 @@ def build_composite_bloq(
554
556
z4 = bb .add (IntState (bitsize = self .n , val = 0 ))
555
557
lam_split = bb .split (lam )
556
558
z4_split = bb .split (z4 )
557
- for i in range (self .n ):
559
+ for i in range (int ( self .n ) ):
558
560
lam_split [i ], z4_split [i ] = bb .add (CNOT (), ctrl = lam_split [i ], target = z4_split [i ])
559
561
lam = bb .join (lam_split , QMontgomeryUInt (self .n ))
560
562
z4 = bb .join (z4_split , QMontgomeryUInt (self .n ))
@@ -584,7 +586,7 @@ def build_composite_bloq(
584
586
)
585
587
lam_split = bb .split (lam )
586
588
z4_split = bb .split (z4 )
587
- for i in range (self .n ):
589
+ for i in range (int ( self .n ) ):
588
590
lam_split [i ], z4_split [i ] = bb .add (CNOT (), ctrl = lam_split [i ], target = z4_split [i ])
589
591
lam = bb .join (lam_split , QMontgomeryUInt (self .n ))
590
592
z4 = bb .join (z4_split , QMontgomeryUInt (self .n ))
@@ -602,7 +604,7 @@ def build_composite_bloq(
602
604
# y = y_r + b % p.
603
605
z3_split = bb .split (z3 )
604
606
y_split = bb .split (y )
605
- for i in range (self .n ):
607
+ for i in range (int ( self .n ) ):
606
608
z3_split [i ], y_split [i ] = bb .add (CNOT (), ctrl = z3_split [i ], target = y_split [i ])
607
609
z3 = bb .join (z3_split , QMontgomeryUInt (self .n ))
608
610
y = bb .join (y_split , QMontgomeryUInt (self .n ))
@@ -659,9 +661,9 @@ class _ECAddStepFive(Bloq):
659
661
Fig 10.
660
662
"""
661
663
662
- n : int
663
- mod : int
664
- window_size : int = 1
664
+ n : 'SymbolicInt'
665
+ mod : 'SymbolicInt'
666
+ window_size : 'SymbolicInt' = 1
665
667
666
668
@cached_property
667
669
def signature (self ) -> 'Signature' :
@@ -720,7 +722,7 @@ def build_composite_bloq(
720
722
# If ctrl: lam = 0.
721
723
z4_split = bb .split (z4 )
722
724
lam_split = bb .split (lam )
723
- for i in range (self .n ):
725
+ for i in range (int ( self .n ) ):
724
726
ctrls = [ctrl , z4_split [i ]]
725
727
ctrls , lam_split [i ] = bb .add (
726
728
MultiControlX (cvs = [1 , 1 ]), controls = ctrls , target = lam_split [i ]
@@ -801,8 +803,8 @@ class _ECAddStepSix(Bloq):
801
803
Fig 10.
802
804
"""
803
805
804
- n : int
805
- mod : int
806
+ n : 'SymbolicInt'
807
+ mod : 'SymbolicInt'
806
808
807
809
@cached_property
808
810
def signature (self ) -> 'Signature' :
@@ -866,7 +868,7 @@ def build_composite_bloq(
866
868
# Set (x, y) to (a, b) if f4 is set.
867
869
a_split = bb .split (a )
868
870
x_split = bb .split (x )
869
- for i in range (self .n ):
871
+ for i in range (int ( self .n ) ):
870
872
toff_ctrl = [f4 , a_split [i ]]
871
873
toff_ctrl , x_split [i ] = bb .add (Toffoli (), ctrl = toff_ctrl , target = x_split [i ])
872
874
f4 = toff_ctrl [0 ]
@@ -875,7 +877,7 @@ def build_composite_bloq(
875
877
x = bb .join (x_split , QMontgomeryUInt (self .n ))
876
878
b_split = bb .split (b )
877
879
y_split = bb .split (y )
878
- for i in range (self .n ):
880
+ for i in range (int ( self .n ) ):
879
881
toff_ctrl = [f4 , b_split [i ]]
880
882
toff_ctrl , y_split [i ] = bb .add (Toffoli (), ctrl = toff_ctrl , target = y_split [i ])
881
883
f4 = toff_ctrl [0 ]
@@ -888,11 +890,11 @@ def build_composite_bloq(
888
890
xy = bb .join (np .concatenate ([bb .split (x ), bb .split (y )]), dtype = QMontgomeryUInt (2 * self .n ))
889
891
ab , xy , f4 = bb .add (Equals (QMontgomeryUInt (2 * self .n )), x = ab , y = xy , target = f4 )
890
892
ab_split = bb .split (ab )
891
- a = bb .join (ab_split [: self .n ], dtype = QMontgomeryUInt (self .n ))
892
- b = bb .join (ab_split [self .n :], dtype = QMontgomeryUInt (self .n ))
893
+ a = bb .join (ab_split [: int ( self .n ) ], dtype = QMontgomeryUInt (self .n ))
894
+ b = bb .join (ab_split [int ( self .n ) :], dtype = QMontgomeryUInt (self .n ))
893
895
xy_split = bb .split (xy )
894
- x = bb .join (xy_split [: self .n ], dtype = QMontgomeryUInt (self .n ))
895
- y = bb .join (xy_split [self .n :], dtype = QMontgomeryUInt (self .n ))
896
+ x = bb .join (xy_split [: int ( self .n ) ], dtype = QMontgomeryUInt (self .n ))
897
+ y = bb .join (xy_split [int ( self .n ) :], dtype = QMontgomeryUInt (self .n ))
896
898
897
899
# Unset f3 if (a, b) = (0, 0).
898
900
ab_arr = np .concatenate ([bb .split (a ), bb .split (b )])
@@ -1000,9 +1002,9 @@ class ECAdd(Bloq):
1000
1002
Litinski. 2023. Fig 5.
1001
1003
"""
1002
1004
1003
- n : int
1004
- mod : int
1005
- window_size : int = 1
1005
+ n : 'SymbolicInt'
1006
+ mod : 'SymbolicInt'
1007
+ window_size : 'SymbolicInt' = 1
1006
1008
1007
1009
@cached_property
1008
1010
def signature (self ) -> 'Signature' :
@@ -1070,29 +1072,29 @@ def build_composite_bloq(
1070
1072
1071
1073
def on_classical_vals (self , a , b , x , y , lam_r ) -> Dict [str , Union ['ClassicalValT' , sympy .Expr ]]:
1072
1074
curve_a = (
1073
- QMontgomeryUInt (self .n ).montgomery_to_uint (lam_r , self .mod )
1075
+ QMontgomeryUInt (self .n ).montgomery_to_uint (lam_r , int ( self .mod ) )
1074
1076
* 2
1075
- * QMontgomeryUInt (self .n ).montgomery_to_uint (b , self .mod )
1076
- - (3 * QMontgomeryUInt (self .n ).montgomery_to_uint (a , self .mod ) ** 2 )
1077
+ * QMontgomeryUInt (self .n ).montgomery_to_uint (b , int ( self .mod ) )
1078
+ - (3 * QMontgomeryUInt (self .n ).montgomery_to_uint (a , int ( self .mod ) ) ** 2 )
1077
1079
) % self .mod
1078
1080
p1 = ECPoint (
1079
- QMontgomeryUInt (self .n ).montgomery_to_uint (a , self .mod ),
1080
- QMontgomeryUInt (self .n ).montgomery_to_uint (b , self .mod ),
1081
+ QMontgomeryUInt (self .n ).montgomery_to_uint (a , int ( self .mod ) ),
1082
+ QMontgomeryUInt (self .n ).montgomery_to_uint (b , int ( self .mod ) ),
1081
1083
mod = self .mod ,
1082
1084
curve_a = curve_a ,
1083
1085
)
1084
1086
p2 = ECPoint (
1085
- QMontgomeryUInt (self .n ).montgomery_to_uint (x , self .mod ),
1086
- QMontgomeryUInt (self .n ).montgomery_to_uint (y , self .mod ),
1087
+ QMontgomeryUInt (self .n ).montgomery_to_uint (x , int ( self .mod ) ),
1088
+ QMontgomeryUInt (self .n ).montgomery_to_uint (y , int ( self .mod ) ),
1087
1089
mod = self .mod ,
1088
1090
curve_a = curve_a ,
1089
1091
)
1090
1092
result = p1 + p2
1091
1093
return {
1092
1094
'a' : a ,
1093
1095
'b' : b ,
1094
- 'x' : QMontgomeryUInt (self .n ).uint_to_montgomery (result .x , self .mod ),
1095
- 'y' : QMontgomeryUInt (self .n ).uint_to_montgomery (result .y , self .mod ),
1096
+ 'x' : QMontgomeryUInt (self .n ).uint_to_montgomery (result .x , int ( self .mod ) ),
1097
+ 'y' : QMontgomeryUInt (self .n ).uint_to_montgomery (result .y , int ( self .mod ) ),
1096
1098
'lam_r' : lam_r ,
1097
1099
}
1098
1100
0 commit comments