-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcost.py
executable file
·864 lines (704 loc) · 26.2 KB
/
cost.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Quantum and Classical Nearest Neighbor Cost.
"""
from mpmath import mp
from collections import namedtuple
from utils import load_probabilities, PrecomputationRequired
from config import MagicConstants
from probabilities import W, C, pf, ngr_pf, ngr
from ge19 import estimate_abstract_to_physical
"""
COSTS
"""
"""
Logical Quantum Costs
:param label: arbitrary label
:param qubits_in: number of input qubits
:param qubits_out: number of output qubits
:param qubits_max:
:param depth: longest path from input to output (including identity gates)
:param gates: gates except identity gates
:param dw: not necessarily depth*qubits
:param toffoli_count: number of Toffoli gates
:param t_count: number of T gates
:param t_depth: T gate depth
"""
LogicalCosts = namedtuple(
"LogicalCosts",
(
"label",
"qubits_in",
"qubits_out",
"qubits_max", # NOTE : not sure if this is useful
"depth",
"gates",
"dw",
"toffoli_count", # NOTE: not sure if this is useful
"t_count",
"t_depth",
),
)
"""
Classic Costs
:param label: arbitrary label
:param gates: number of gates
:param depth: longest path from input to output
"""
ClassicalCosts = namedtuple("ClassicalCosts", ("label", "gates", "depth"))
"""
METRICS
"""
ClassicalMetrics = {
"classical", # gate count
"naive_classical", # query cost
}
QuantumMetrics = {
"g", # gate count
"dw", # depth x width
"ge19", # depth x width x physical qubit measurements Gidney Ekera
"t_count", # number of T-gates
"naive_quantum", # query cost
}
Metrics = ClassicalMetrics | QuantumMetrics
SizeMetrics = {"vectors", "bits"}
def log2(x):
return mp.log(x) / mp.log(2)
def local_min(f, low=None, high=None):
"""
Search the neighborhood around ``f(x)`` for a local minimum between ``low`` and ``high``.
:param f: function to call
:param low: lower bound on input space
:param high: upper bound on input space
"""
from scipy.optimize import fminbound
def ff(x):
try:
return float(f(float(x)))
except AssertionError:
return mp.mpf("inf")
return fminbound(ff, float(low), float(high))
def null_costf(qubits_in=0, qubits_out=0):
"""
Cost of initialization/measurement.
"""
return LogicalCosts(
label="null",
qubits_in=qubits_in,
qubits_out=qubits_out,
qubits_max=max(qubits_in, qubits_out),
gates=0,
depth=0,
dw=0,
toffoli_count=0,
t_count=0,
t_depth=0,
)
def delay(cost, depth, label="_"):
# delay only affects the dw cost
dw = cost.dw + cost.qubits_out * depth
return LogicalCosts(
label=label,
qubits_in=cost.qubits_in,
qubits_out=cost.qubits_out,
qubits_max=cost.qubits_max,
gates=cost.gates,
depth=cost.depth + depth,
dw=dw,
toffoli_count=cost.toffoli_count,
t_count=cost.t_count,
t_depth=cost.t_depth,
)
def reverse(cost):
return LogicalCosts(
label=cost.label,
qubits_in=cost.qubits_out,
qubits_out=cost.qubits_in,
qubits_max=cost.qubits_max,
gates=cost.gates,
depth=cost.depth,
dw=cost.dw,
toffoli_count=cost.toffoli_count,
t_count=cost.t_count,
t_depth=cost.t_depth,
)
def compose_k_sequential(cost, times, label="_"):
# Ensure that sequential composition makes sense
assert cost.qubits_in == cost.qubits_out
return LogicalCosts(
label=label,
qubits_in=cost.qubits_in,
qubits_out=cost.qubits_out,
qubits_max=cost.qubits_max,
gates=cost.gates * times,
depth=cost.depth * times,
dw=cost.dw * times,
toffoli_count=cost.toffoli_count * times,
t_count=cost.t_count * times,
t_depth=cost.t_depth * times,
)
def compose_k_parallel(cost, times, label="_"):
return LogicalCosts(
label=label,
qubits_in=times * cost.qubits_in,
qubits_out=times * cost.qubits_out,
qubits_max=times * cost.qubits_max,
gates=times * cost.gates,
depth=cost.depth,
dw=times * cost.dw,
toffoli_count=times * cost.toffoli_count,
t_count=times * cost.t_count,
t_depth=cost.t_depth,
)
def compose_sequential(cost1, cost2, label="_"):
# Ensure that sequential composition makes sense
assert cost1.qubits_out >= cost2.qubits_in
# Pad unused wires with identity gates
dw = cost1.dw + cost2.dw
if cost1.qubits_out > cost2.qubits_in:
dw += (cost1.qubits_out - cost2.qubits_in) * cost2.depth
qubits_out = cost1.qubits_out - cost2.qubits_in + cost2.qubits_out
qubits_max = max(cost1.qubits_max, cost1.qubits_out - cost2.qubits_in + cost2.qubits_max)
return LogicalCosts(
label=label,
qubits_in=cost1.qubits_in,
qubits_out=qubits_out,
qubits_max=qubits_max,
gates=cost1.gates + cost2.gates,
depth=cost1.depth + cost2.depth,
dw=dw,
toffoli_count=cost1.toffoli_count + cost2.toffoli_count,
t_count=cost1.t_count + cost2.t_count,
t_depth=cost1.t_depth + cost2.t_depth,
)
def compose_parallel(cost1, cost2, label="_"):
# Pad wires from shallower circuit with identity gates
dw = cost1.dw + cost2.dw
if cost1.depth >= cost2.depth:
dw += (cost1.depth - cost2.depth) * cost2.qubits_out
else:
dw += (cost2.depth - cost1.depth) * cost1.qubits_out
return LogicalCosts(
label=label,
qubits_in=cost1.qubits_in + cost2.qubits_in,
qubits_out=cost1.qubits_out + cost2.qubits_out,
qubits_max=cost1.qubits_max + cost2.qubits_max,
gates=cost1.gates + cost2.gates,
depth=max(cost1.depth, cost2.depth),
dw=dw,
toffoli_count=cost1.toffoli_count + cost2.toffoli_count,
t_count=cost1.t_count + cost2.t_count,
t_depth=max(cost1.t_depth, cost2.t_depth),
)
def classical_popcount_costf(n, k, metric):
"""
Classical gate count for popcount.
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on ≤ k
"""
if metric == "naive_classical":
cc = ClassicalCosts(label="popcount", gates=1, depth=1)
return cc
ell = mp.ceil(mp.log(n, 2))
gates = n + (n - ell - 1)*5 + ell # 5 = gates per full adder
depth = 2 * ell
cc = ClassicalCosts(label="popcount", gates=gates, depth=depth)
return cc
def adder_costf(i, ci=False):
"""
Logical cost of i bit adder (Cuccaro et al). With Carry Input if ci=True
"""
adder_cnots = 6 if i == 1 else (5 * i + 1 if ci else 5 * i - 3)
adder_depth = 7 if i == 1 else (2 * i + 6 if ci else 2 * i + 4)
adder_nots = 0 if i == 1 else (2 * i - 2 if ci else 2 * i - 4)
adder_tofs = 2 * i - 1
adder_qubits_in = 2 * i + 1
adder_qubits_out = 2 * i + 2
adder_qubits_max = 2 * i + 2
adder_t_depth = adder_tofs * MagicConstants.t_depth_div_toffoli
adder_t_count = adder_tofs * MagicConstants.t_div_toffoli
adder_gates = adder_cnots + adder_nots + adder_tofs * MagicConstants.gates_div_toffoli
return LogicalCosts(
label=str(i) + "-bit adder",
qubits_in=adder_qubits_in,
qubits_out=adder_qubits_out,
qubits_max=adder_qubits_max,
gates=adder_gates,
depth=adder_depth,
dw=adder_qubits_in * adder_depth,
toffoli_count=adder_tofs,
t_count=adder_t_count,
t_depth=adder_t_depth,
)
def hamming_wt_costf(n):
"""
Logical cost of mapping |v>|0> to |v>|H(v)>.
.. note :: The adder tree uses in-place addition, so some of the bits of |v> overlap |H(v)> and
there are ancilla as well.
:param n: number of bits in v
"""
b = int(mp.floor(log2(n)))
qc = null_costf(qubits_in=n, qubits_out=n)
if bin(n + 1).count("1") == 1:
# When n = 2**(b+1) - 1 the adder tree is "packed". We can use every input bit including
# carry inputs.
for i in range(1, b + 1):
L = compose_k_parallel(adder_costf(i, ci=True), 2 ** (b - i))
qc = compose_sequential(qc, L)
else:
# Decompose into packed adder trees joined by adders.
# Use one adder tree on (2**b - 1) bits and one on max(1, n - 2**b) bits.
# Reserve one bit for carry input of adder (unless n = 2**b).
carry_in = n != 2 ** b
qc = compose_sequential(
qc, compose_parallel(hamming_wt_costf(2 ** b - 1), hamming_wt_costf(max(1, n - 2 ** b)))
)
qc = compose_sequential(qc, adder_costf(b, ci=carry_in))
qc = compose_parallel(qc, null_costf(), label=str(n) + "-bit hamming weight")
return qc
def carry_costf(m):
"""
Logical cost of mapping |x> to (-1)^{(x+c)_m}|x> where (x+c)_m is the m-th bit (zero indexed) of
x+c for an arbitrary m bit constant c.
.. note :: numbers here are adapted from Fig 3 of https://arxiv.org/pdf/1611.07995.pdf
m is equivalent to ell in the LaTeX
"""
if m < 2:
raise NotImplementedError("Case m==1 not implemented.")
carry_cnots = 2 * m
carry_depth = 8 * m - 8
carry_nots = 2 * (m - 1)
carry_tofs = 4 * (m - 2) + 2
carry_qubits_in = 2 * m
carry_qubits_out = 2 * m
carry_qubits_max = 2 * m
carry_dw = carry_qubits_max * carry_depth
carry_t_depth = carry_tofs * MagicConstants.t_depth_div_toffoli
carry_t_count = carry_tofs * MagicConstants.t_div_toffoli
carry_gates = carry_cnots + carry_nots + carry_tofs * MagicConstants.gates_div_toffoli
return LogicalCosts(
label="carry",
qubits_in=carry_qubits_in,
qubits_out=carry_qubits_out,
qubits_max=carry_qubits_max,
gates=carry_gates,
depth=carry_depth,
dw=carry_dw,
toffoli_count=carry_tofs,
t_count=carry_t_count,
t_depth=carry_t_depth,
)
def popcount_costf(L, n, k):
"""
Logical cost of mapping |i> to (-1)^{popcount(u,v_i)}|i> for fixed u.
:param L: length of the list, i.e. |L|
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on ≤ k
"""
assert 0 <= k and k <= n
index_wires = int(mp.ceil(log2(L)))
# Initialize space for |v_i>
qc = null_costf(qubits_in=index_wires, qubits_out=n + index_wires)
# Query table index i
# NOTE: We're skipping a qRAM call here.
qc = delay(qc, 1)
# XOR in the fixed sketch "u"
# NOTE: We're skipping ~ n NOT gates for mapping |v> to |u^v>
qc = delay(qc, 1)
# Use tree of adders compute hamming weight
# |i>|u^v_i>|0> -> |i>|u^v_i>|wt(u^v_i)>
hamming_wt = hamming_wt_costf(n)
qc = compose_sequential(
qc, null_costf(qubits_in=qc.qubits_out, qubits_out=index_wires + hamming_wt.qubits_in)
)
qc = compose_sequential(qc, hamming_wt)
# Compute the high bit of (2^ceil(log(n)) - k) + hamming_wt
# |i>|v_i>|wt(u^v_i)> -> (-1)^popcnt(u,v_i) |i>|u^v_i>|wt(u^v_i)>
qc = compose_sequential(qc, carry_costf(int(mp.ceil(log2(n)))))
# Uncompute hamming weight.
qc = compose_sequential(qc, reverse(hamming_wt))
# Uncompute XOR
# NOTE: We're skipping ~ n NOT gates for mapping |u^v> to |v>
qc = delay(qc, 1)
# Uncompute table entry
# NOTE: We're skipping a qRAM call here.
qc = delay(qc, 1)
# Discard ancilla
# (-1)^popcnt(u,v_i) |i>|0>|0> -> (-1)^popcnt(u,v_i) |i>
qc = compose_sequential(qc, null_costf(qubits_in=qc.qubits_out, qubits_out=index_wires))
qc = compose_parallel(qc, null_costf(), label="popcount" + str((n, k)))
return qc
def n_toffoli_costf(n, have_ancilla=False):
"""
Logical cost of toffoli with n-1 controls.
.. note :: Table I of Maslov arXiv:1508.03273v2 (Source = "Ours", Optimization goal = "T/CNOT")
"""
assert n >= 3
if n >= 5 and not have_ancilla:
# Use Barenco et al (1995) Lemma 7.3 split into two smaller Toffoli gates.
n1 = int(mp.ceil((n - 1) / 2.0)) + 1
n2 = n - n1 + 1
return compose_sequential(
compose_parallel(
null_costf(qubits_in=n - n1, qubits_out=n - n1), n_toffoli_costf(n1, True)
),
compose_parallel(
null_costf(qubits_in=n - n2, qubits_out=n - n2), n_toffoli_costf(n2, True)
),
)
if n == 3: # Normal toffoli gate
n_tof_t_count = MagicConstants.AMMR12_tof_t_count
n_tof_t_depth = MagicConstants.AMMR12_tof_t_depth
n_tof_gates = MagicConstants.AMMR12_tof_gates
n_tof_depth = MagicConstants.AMMR12_tof_depth
n_tof_dw = n_tof_depth * (n + 1)
elif n == 4:
"""
Note: the cost can be smaller if using "clean" ancillas
(see first "Ours" in Table 1 of Maslov's paper)
"""
n_tof_t_count = 16
n_tof_t_depth = 16
n_tof_gates = 36
n_tof_depth = 36 # Maslov Eq. (5), Figure 3 (dashed), Eq. (3) (dashed).
n_tof_dw = n_tof_depth * (n + 1)
elif n >= 5:
n_tof_t_count = 8 * n - 16
n_tof_t_depth = 8 * n - 16
n_tof_gates = (8 * n - 16) + (8 * n - 20) + (4 * n - 10)
n_tof_depth = (8 * n - 16) + (8 * n - 20) + (4 * n - 10)
n_tof_dw = n_tof_depth * (n + 1)
n_tof_qubits_max = n if have_ancilla else n + 1
return LogicalCosts(
label=str(n) + "-toffoli",
qubits_in=n,
qubits_out=n,
qubits_max=n_tof_qubits_max,
gates=n_tof_gates,
depth=n_tof_depth,
dw=n_tof_dw,
toffoli_count=0,
t_count=n_tof_t_count,
t_depth=n_tof_t_depth,
)
def diffusion_costf(L):
"""
Logical cost of the diffusion operator D R_0 D^-1
where D samples the uniform distribution on {1,...,L} R_0 is the unitary I - 2|0><0|
:param L: length of the list, i.e. |L|
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on ≤ k
"""
index_wires = int(mp.ceil(log2(L)))
H = LogicalCosts(
label="H",
qubits_in=1,
qubits_out=1,
qubits_max=1,
gates=1,
depth=1,
dw=1,
toffoli_count=0,
t_count=0,
t_depth=0,
)
Hn = compose_k_parallel(H, index_wires)
anc = null_costf(qubits_in=index_wires, qubits_out=index_wires + 1)
qc = compose_sequential(Hn, anc)
qc = compose_sequential(qc, n_toffoli_costf(index_wires + 1))
qc = compose_sequential(qc, reverse(anc))
qc = compose_sequential(qc, Hn)
qc = compose_parallel(qc, null_costf(), label="diffusion")
return qc
def popcount_grover_iteration_costf(L, n, k, metric):
"""
Logical cost of G(popcount) = (D R_0 D^-1) R_popcount.
where D samples the uniform distribution on {1,...,L} (D R_0 D^-1) is the diffusion operator.
R_popcount maps |i> to (-1)^{popcount(u,v_i)}|i> for some fixed u
:param L: length of the list, i.e. |L|
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on <= k
"""
if metric == "naive_quantum":
return LogicalCosts(
label="oracle",
qubits_in=1,
qubits_out=1,
qubits_max=1,
depth=1,
gates=1,
dw=1,
toffoli_count=1,
t_count=1,
t_depth=1,
)
popcount_cost = popcount_costf(L, n, k)
diffusion_cost = diffusion_costf(L)
return compose_sequential(diffusion_cost, popcount_cost, label="oracle")
def popcounts_dominate_cost(positive_rate, d, n, metric):
ip_div_pc = (MagicConstants.word_size ** 2) * d / float(n)
if metric in ClassicalMetrics:
return 1.0 / positive_rate > ip_div_pc
else:
return 1.0 / positive_rate > ip_div_pc ** 2
def raw_cost(cost, metric):
if metric == "g":
result = cost.gates
elif metric == "dw":
result = cost.dw
elif metric == "ge19":
phys = estimate_abstract_to_physical(
cost.toffoli_count,
cost.qubits_max,
cost.depth,
prefers_parallel=False,
prefers_serial=True,
)
result = cost.dw * phys[0] ** 2
elif metric == "t_count":
result = cost.t_count
elif metric == "classical":
result = cost.gates
elif metric == "naive_quantum":
return cost.gates
elif metric == "naive_classical":
return cost.gates
else:
raise ValueError("Unknown metric '%s'" % metric)
return result
AllPairsResult = namedtuple(
"AllPairsResult", ("d", "n", "k", "log_cost", "pf_inv", "eta", "metric", "detailed_costs")
)
def all_pairs(d, n=None, k=None, optimize=True, metric="dw", allow_suboptimal=False):
"""
Nearest Neighbor Search via a quadratic search over all pairs.
:param d: search in S^{d-1}
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on ≤ k
:param optimize: optimize `n`
:param metric: target metric
:param allow_suboptimal: when ``optimize=True``, return the best possible set of parameters given what is precomputed
"""
if n is None:
n = 1
while n < d:
n = 2 * n
k = k if k else int(MagicConstants.k_div_n * (n - 1))
pr = load_probabilities(d, n - 1, k)
def cost(pr):
N = 2 / ((1 - pr.eta) * C(pr.d, mp.pi / 3))
if metric in ClassicalMetrics:
look_cost = classical_popcount_costf(pr.n, pr.k, metric)
looks = (N ** 2 - N) / 2.0
search_one_cost = ClassicalCosts(
label="search", gates=look_cost.gates * looks, depth=look_cost.depth * looks
)
else:
look_cost = popcount_grover_iteration_costf(N, pr.n, pr.k, metric)
looks_factor = 11.0 / 15
looks = int(mp.ceil(looks_factor * N ** (3 / 2.0)))
search_one_cost = compose_k_sequential(look_cost, looks)
full_cost = raw_cost(search_one_cost, metric)
return full_cost, look_cost
positive_rate = pf(pr.d, pr.n, pr.k)
while optimize and not popcounts_dominate_cost(positive_rate, pr.d, pr.n, metric):
try:
pr = load_probabilities(
pr.d, 2 * (pr.n + 1) - 1, int(MagicConstants.k_div_n * (2 * (pr.n + 1) - 1))
)
except PrecomputationRequired as e:
if allow_suboptimal:
break
else:
raise e
positive_rate = pf(pr.d, pr.n, pr.k)
fc, dc = cost(pr)
return AllPairsResult(
d=pr.d,
n=pr.n,
k=pr.k,
log_cost=float(log2(fc)),
pf_inv=int(1 / positive_rate),
eta=pr.eta,
metric=metric,
detailed_costs=dc,
)
RandomBucketsResult = namedtuple(
"RandomBucketsResult",
("d", "n", "k", "theta", "log_cost", "pf_inv", "eta", "metric", "detailed_costs"),
)
def random_buckets(
d, n=None, k=None, theta1=None, optimize=True, metric="dw", allow_suboptimal=False
):
"""
Nearest Neighbor Search using random buckets as in BGJ1.
:param d: search in S^{d-1}
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on ≤ k
:param theta1: bucket angle
:param optimize: optimize `n`
:param metric: target metric
:param allow_suboptimal: when ``optimize=True``, return the best possible set of parameters
given what is precomputed
"""
if n is None:
n = 1
while n < d:
n = 2 * n
k = k if k else int(MagicConstants.k_div_n * (n - 1))
theta = theta1 if theta1 else 1.2860
pr = load_probabilities(d, n - 1, k)
ip_cost = MagicConstants.word_size ** 2 * d
def cost(pr, T1):
eta = 1 - ngr_pf(pr.d, pr.n, pr.k, beta=T1) / ngr(pr.d, beta=T1)
N = 2 / ((1 - eta) * C(pr.d, mp.pi / 3))
W0 = W(pr.d, T1, T1, mp.pi / 3)
buckets = 1.0 / W0
bucket_size = N * C(pr.d, T1)
if metric in ClassicalMetrics:
look_cost = classical_popcount_costf(pr.n, pr.k, metric)
looks_per_bucket = (bucket_size ** 2 - bucket_size) / 2.0
search_one_cost = ClassicalCosts(
label="search",
gates=look_cost.gates * looks_per_bucket,
depth=look_cost.depth * looks_per_bucket,
)
else:
look_cost = popcount_grover_iteration_costf(bucket_size, pr.n, pr.k, metric)
looks_factor = (2 * W0) / (5 * C(pr.d, T1)) + 1.0 / 3
looks_per_bucket = int(looks_factor * bucket_size ** (3 / 2.0))
search_one_cost = compose_k_sequential(look_cost, looks_per_bucket)
fill_bucket_cost = N * ip_cost
search_bucket_cost = raw_cost(search_one_cost, metric)
full_cost = buckets * (fill_bucket_cost + search_bucket_cost)
return full_cost, look_cost, eta
if optimize:
theta = local_min(lambda T: cost(pr, T)[0], low=mp.pi / 6, high=mp.pi / 2)
positive_rate = pf(pr.d, pr.n, pr.k, beta=theta)
while not popcounts_dominate_cost(positive_rate, pr.d, pr.n, metric):
try:
n = 2 * (pr.n + 1) - 1
k = int(MagicConstants.k_div_n * n)
pr = load_probabilities(pr.d, n, k)
except PrecomputationRequired as e:
if allow_suboptimal:
break
else:
raise e
theta = local_min(lambda T: cost(pr, T)[0], low=mp.pi / 6, high=mp.pi / 2)
positive_rate = pf(pr.d, pr.n, pr.k, beta=theta)
else:
positive_rate = pf(pr.d, pr.n, pr.k, beta=theta)
fc, dc, eta = cost(pr, theta)
return RandomBucketsResult(
d=pr.d,
n=pr.n,
k=pr.k,
theta=float(theta),
log_cost=float(log2(fc)),
pf_inv=int(1 / positive_rate),
eta=eta,
metric=metric,
detailed_costs=dc,
)
ListDecodingResult = namedtuple(
"ListDecodingResult",
("d", "n", "k", "theta1", "theta2", "log_cost", "pf_inv", "eta", "metric", "detailed_costs"),
)
def list_decoding(
d, n=None, k=None, theta1=None, theta2=None, optimize=True, metric="dw", allow_suboptimal=False
):
"""
Nearest Neighbor Search via a decodable buckets as in BDGL16.
:param d: search in S^{d-1}
:param n: number of entries in popcount filter
:param k: we accept if two vectors agree on ≤ k
:param theta1: filter creation angle
:param theta2: filter query angle
:param optimize: optimize `n`
:param metric: target metric
:param allow_suboptimal: when ``optimize=True``, return the best possible set of parameters
given what is precomputed
"""
if n is None:
n = 1
while n < d:
n = 2 * n
k = k if k else int(MagicConstants.k_div_n * (n - 1))
theta = theta1 if theta1 else mp.pi / 3
pr = load_probabilities(d, n - 1, k)
def cost(pr, T1):
eta = 1 - ngr_pf(pr.d, pr.n, pr.k, beta=T1) / ngr(pr.d, beta=T1)
T2 = T1
N = 2 / ((1 - eta) * C(d, mp.pi / 3))
W0 = W(d, T1, T2, mp.pi / 3)
filters = 1.0 / W0
m = log2(d)
ip_cost = d/m * MagicConstants.word_size ** 2
Z = filters**(1/m) # number of vectors per subcode
# we assume a cost of one word addition (five gates per bit)
# + dealing with a pointer into asubcode per iteration node.
COST_TREE_ITER = 5 * MagicConstants.word_size + log2(Z)
# we assume a cost of one word operation for the sorting + dealing with a pointer
COST_COMPARE_SWAP = MagicConstants.word_size + log2(Z)
# cost of inner products and cost of sorting the lists
preprocess_cost = m * Z * ip_cost + m * Z * log2(Z) * COST_COMPARE_SWAP
# We assume the enumeration procedure from the "Report on the Security of LWE: Improved Dual
# Lattice Attack" https://doi.org/10.5281/zenodo.6412487 such that number of enumeration
# nodes is a constant multiple of the number of solutions.
insert_cost = preprocess_cost + filters * C(d , T2) * COST_TREE_ITER
query_cost = preprocess_cost + filters * C(d , T1) * COST_TREE_ITER
bucket_size = (filters * C(d, T1)) * (N * C(d, T2))
if metric in ClassicalMetrics:
look_cost = classical_popcount_costf(pr.n, pr.k, metric)
looks_per_bucket = bucket_size
search_one_cost = ClassicalCosts(
label="search",
gates=look_cost.gates * looks_per_bucket,
depth=look_cost.depth * looks_per_bucket,
)
else:
look_cost = popcount_grover_iteration_costf(bucket_size, pr.n, pr.k, metric)
looks_per_bucket = bucket_size ** (1 / 2.0)
search_one_cost = compose_k_sequential(look_cost, looks_per_bucket)
search_cost = raw_cost(search_one_cost, metric)
return N * insert_cost + N * query_cost + N * search_cost, search_one_cost, eta
if optimize:
theta = local_min(lambda T: cost(pr, T)[0], low=mp.pi / 6, high=mp.pi / 2)
positive_rate = pf(pr.d, pr.n, pr.k, beta=theta)
while not popcounts_dominate_cost(positive_rate, pr.d, pr.n, metric):
try:
pr = load_probabilities(
pr.d, 2 * (pr.n + 1) - 1, int(MagicConstants.k_div_n * (2 * (pr.n + 1) - 1))
)
except PrecomputationRequired as e:
if allow_suboptimal:
break
else:
raise e
theta = local_min(lambda T: cost(pr, T)[0], low=mp.pi / 6, high=mp.pi / 2)
positive_rate = pf(pr.d, pr.n, pr.k, beta=theta)
else:
positive_rate = pf(pr.d, pr.n, pr.k, beta=theta)
fc, dc, eta = cost(pr, theta)
return ListDecodingResult(
d=pr.d,
n=pr.n,
k=pr.k,
theta1=float(theta),
theta2=float(theta),
log_cost=float(log2(fc)),
pf_inv=int(1 / positive_rate),
eta=eta,
metric=metric,
detailed_costs=dc,
)
SieveSizeResult = namedtuple("SieveSizeResult", ("d", "log2_size", "metric", "detailed_costs"))
def sieve_size(d, metric=None):
N = 2 / (C(d, mp.pi / 3))
if metric == "vectors":
log2_size = log2(N)
elif metric == "bits":
log2_size = log2(N) + log2(d)
return SieveSizeResult(d=d, log2_size=log2_size, metric=metric, detailed_costs=(0,))