-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1112 lines (991 loc) · 52.5 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from telnetlib import AYT
import numpy as np
import statistics
from scipy import linalg
from scipy import optimize
import math
from numpy import arange
import time as time_this
import scipy
import json
import os
from skopt import gbrt_minimize
from skopt.space import Real, Integer
from skopt.utils import use_named_args
from compilers import CompositeSim, TrotterSim, QDriftSim, LRsim
MC_SAMPLES_DEFAULT = 100
COST_LOOP_DEPTH = 30
ITERATION_BOUNDS_LOOP_DEPTH = 100 # specifies power of 2 for maximum number of iterations to search through
CROSSOVER_CUTOFF_PERCENTAGE = 0.5
POSSIBLE_PARTITIONS = ["first_order_trotter", "second_order_trotter", "qdrift"]
# A simple function that computes the graph distance between two sites
def dist(site1, site2):
distance_vec = site1 - site2
distance = np.abs(distance_vec[0]) + np.abs(distance_vec[1])
return distance
# A simple function that initializes a graph in the form of an np.array of coordinates
def initialize_graph(x_sites, y_sites):
coord_list = []
for i in range(x_sites):
for j in range(y_sites):
coord_list.append([i,j])
return np.array(coord_list)
#A funciton that initializes a Pauli operator in the correct space, acting on a specific qubit
def initialize_operator(operator_2d, acting_space, space_dimension):
I = np.array([[1, 0],
[0, 1]])
if acting_space>space_dimension:
return 'error'
for i in range(acting_space):
operator_2d = np.kron(operator_2d, I)
for j in range(space_dimension - acting_space-1):
operator_2d = np.kron(I, operator_2d)
return operator_2d
#Initialize Hamiltonian
def graph_hamiltonian(x_dim, y_dim, rng_seed):
X = np.array([[0, 1],
[1, 0]])
Z = np.array([[1, 0],
[0, -1]])
Y = np.array([[0, -1j],
[1j, 0]])
I = np.array([[1, 0],
[0, 1]])
np.random.seed(rng_seed)
hamiltonian_list = []
graph = initialize_graph(x_dim, y_dim)
for i in range(x_dim*y_dim):
for j in range(y_dim*x_dim):
if i != j:
alpha = np.random.normal()
hamiltonian_list.append(alpha *
np.matmul(initialize_operator(Z, i, x_dim*y_dim), initialize_operator(Z, j, x_dim*y_dim)) *
10.0**(-dist(graph[i], graph[j])))
alpha = np.random.normal()
hamiltonian_list.append(4* alpha * initialize_operator(X, i, x_dim*y_dim))
return np.array(hamiltonian_list)
#A function to calculate the trace distance between two numpy arrays (density operators)
def trace_distance(rho, sigma):
if (rho.shape[0] != rho.shape[1]) or (rho.shape != sigma.shape):
print("[trace_distance] Improper shapes were given:", rho.shape, sigma.shape)
raise Exception("Incompatible shapes for Trace Distance.")
diff = rho - sigma
tot = scipy.linalg.sqrtm(diff @ np.copy(diff).conj().T)
return 0.5 * np.abs(np.trace(tot)) # Note: absolute value after trace is because we have 'complex' variables, so taking the norm should be fine??
def infidelity(rho, sigma):
# Density matrices
if (rho.shape[0] == rho.shape[1]) and (rho.shape == sigma.shape):
exact_sqrt = scipy.linalg.sqrtm(rho)
tot_sqrt = scipy.linalg.sqrtm(np.linalg.multi_dot([exact_sqrt, sigma, np.copy(exact_sqrt)]))
fidelity = np.abs(np.trace(tot_sqrt))
# print("[single_infidelity_sample] fidelity:", fidelity)
return 1. - fidelity ** 2
elif (rho.shape[1] == 1 or rho.shape[0] == 1) and (rho.shape == sigma.shape):
return 1 - (np.abs(np.dot(rho.conj().T, sigma)).flat[0])**2
else:
print("[infidelity] Could not parse shapes:", rho.shape, sigma.shape)
def exact_time_evolution_density(hamiltonian_list, time, initial_rho):
'''
Computes the exact time evolution of a density matrix given a Hamiltonian in real time.
'''
if len(hamiltonian_list) == 0:
print("[exact_time_evolution] pls give me hamiltonian")
return 1
exp_op = linalg.expm(1j * sum(hamiltonian_list) * time)
return exp_op @ initial_rho @ exp_op.conj().T
# Inputs are self explanatory except simulator which can be any of
# TrotterSim, QDriftSim, CompositeSim
# Outputs: a single shot estimate of the infidelity according to the exact output provided.
def single_infidelity_sample(simulator, time, exact_final_state, iterations = 1, nbsamples = 1):
sim_output = []
# exact_output = simulator.simulate_exact_output(time)
if type(simulator) == QDriftSim:
sim_output = simulator.simulate(time, nbsamples)
if type(simulator) == TrotterSim:
sim_output = simulator.simulate(time, iterations)
if type(simulator) == CompositeSim:
simulator.nb = nbsamples #Fixed
sim_output = simulator.simulate(time, iterations)
if type(simulator) == LRsim:
sim_output = simulator.simulate(time, iterations)
if simulator.use_density_matrices == False:
infidelity = 1 - (np.abs(np.dot(exact_final_state.conj().T, sim_output)).flat[0])**2
else:
# check that shapes match
if exact_final_state.shape != sim_output.shape:
print("[single_infidelity_sample] tried computing density matrix infidelity with incorrect shapes.")
print("[single_infidelity_sample] exact output shape =", exact_final_state.shape, ", sim output shape =", sim_output.shape)
return 1.
exact_sqrt = scipy.linalg.sqrtm(exact_final_state)
tot_sqrt = scipy.linalg.sqrtm(np.linalg.multi_dot([exact_sqrt, sim_output, np.copy(exact_sqrt)]))
fidelity = np.abs(np.trace(tot_sqrt))
# print("[single_infidelity_sample] fidelity:", fidelity)
infidelity = 1. - (np.abs(np.trace(tot_sqrt)) ** 2)
return (infidelity, simulator.gate_count)
def multi_infidelity_sample(simulator, time, exact_final_state, iterations=1, nbsamples=1, mc_samples=MC_SAMPLES_DEFAULT):
ret = []
# No need to sample TrotterSim, just return single element list
if type(simulator) == TrotterSim:
ret.append(single_infidelity_sample(simulator, time, exact_final_state, iterations=iterations, nbsamples=nbsamples))
ret *= mc_samples
else:
for samp in range(mc_samples):
ret.append(single_infidelity_sample(simulator, time, exact_final_state, iterations=iterations, nbsamples=nbsamples))
return ret
def single_trace_distance_sample(simulator, time, exact_final_state, iterations=1, nbsamples=1):
if type(simulator) == QDriftSim:
sim_output = simulator.simulate(time, nbsamples)
else:
sim_output = simulator.simulate(time, iterations)
if simulator.use_density_matrices == False:
dist = trace_distance(np.outer(sim_output, np.copy(sim_output).conj().T), np.outer(exact_final_state, np.copy(exact_final_state).conj().T))
return (dist, simulator.gate_count)
else:
return (trace_distance(sim_output, exact_final_state), simulator.gate_count)
def multi_trace_distance_sample(simulator, time, exact_final_state, iterations=1, nbsamples=1, mc_samples=MC_SAMPLES_DEFAULT):
if type(simulator) == TrotterSim or len(simulator.qdrift_norms) == 0:
ret = single_trace_distance_sample(simulator, time, exact_final_state, iterations=iterations, nbsamples=nbsamples)
else:
# sim_out = np.zeros(simulator.initial_state.shape, dtype='complex128')
# for _ in range(mc_samples):
# sim_out += simulator.simulate(time, iterations)
# sim_out /= mc_samples
if type(simulator) != CompositeSim: raise Exception("Error please use composite simulators")
simulator.set_exact_qd(True)
sim_out = simulator.simulate(time, iterations)
if simulator.use_density_matrices == False:
dist = trace_distance(np.outer(sim_out, np.copy(sim_out).conj().T), np.outer(exact_final_state, np.copy(exact_final_state).conj().T))
else:
dist = trace_distance(sim_out, exact_final_state)
ret = (dist, simulator.gate_count)
return ret
def exact_time_evolution(sim, time):
'''
Given a simulator, compute the exact time evolution of the Hamiltonian (no channel approximation algorithms) in real or imaginary time depending on `imag_time` attribute.
'''
#framework did not accept a simulator previously, this may break something somewhere in the state_vec sampling pic
if sim.use_density_matrices == True:
if sim.imag_time == False:
return linalg.expm(1j * sum(sim.unparsed_hamiltonian) * time) @ sim.initial_state @ linalg.expm(1j * sum(sim.unparsed_hamiltonian) * time).conj().T
else:
un_normed_state = linalg.expm(-1 * sum(sim.unparsed_hamiltonian) * time) @ sim.initial_state @ linalg.expm(-1 * sum(sim.unparsed_hamiltonian) * time)
return un_normed_state / np.trace(un_normed_state)
else:
if sim.imag_time == False:
return linalg.expm(1j * sum(sim.unparsed_hamiltonian) * time) @ sim.initial_state
else:
un_normed_state = linalg.expm(-1 * sum(sim.unparsed_hamiltonian) * time) @ sim.initial_state
return un_normed_state / np.linalg.norm(un_normed_state)
def get_iteration_bounds(is_iteration_good, heuristic, verbose=False):
"""
Performs exponential backoff to find iteration bounds for use in gate cost estimators.
WARNING: With randomized composite channels there is a possibility this does not converge correctly. If you get a "good" sample at
a heuristic that should be bad then you will search for a lower bound until you exit.\n
Inputs
- is_iteration_good: a callable that takes in an iteration and returns a boolean if threshold is met
- heuristic: the guess as to where a good place to start is
\n
Outputs:\n
(iteration lower bound, iteration upper bound)
"""
if heuristic < 10:
if is_iteration_good(10):
return (0, 10)
else:
iter_lower = 5
iter_upper = -1
else:
# This is to help with randomized partitions (aka qdrift heavy)
heuristic = math.floor(0.8 * heuristic)
if is_iteration_good(heuristic):
iter_lower = -1
iter_upper = heuristic
else:
iter_lower = heuristic
iter_upper = -1
curr_guess = max(iter_lower, iter_upper)
if verbose:
print("[get_iteration_bounds] Beginning search with iter_lower=", iter_lower, ", iter_upper=", iter_upper, ", curr_guess=",curr_guess)
for i in range(ITERATION_BOUNDS_LOOP_DEPTH):
search_for_upper_bound = (iter_lower > 0) and (iter_upper < 0)
search_for_lower_bound = (iter_lower < 0) and (iter_upper > 0)
if verbose:
print("[get_iteration_bounds] iteration: ", i, ", current guess: ", curr_guess)
if search_for_lower_bound:
# step = min(100, math.floor(curr_guess / 2.0))
step = int(curr_guess)
new_guess = int(curr_guess - step)
if is_iteration_good(new_guess):
# lower bound requires the threshold to NOT be met
curr_guess = new_guess
else:
# threshold was NOT met so we have found our iter_lower
iter_lower = new_guess
continue
elif search_for_upper_bound:
# step = min(100, math.ceil(curr_guess * 2))
step = math.ceil(curr_guess)
new_guess = int(curr_guess + step)
if is_iteration_good(new_guess):
# upper bound needs to meet threshold so we are good
iter_upper = new_guess
continue
else:
curr_guess = new_guess
else:
return (iter_lower, iter_upper)
print("[get_iteration_bounds] Iteration depth reached, unclear what to do.")
raise Exception("get_iteration_bounds")
def find_optimal_cost(simulator, time, epsilon, heuristic = -1, use_infidelity = False, mc_samples=MC_SAMPLES_DEFAULT, num_state_samples=10, verbose=False):
"""
Varies the number of iterations needed for a simulator to acheive its infidelity threshold.
# WARNING
will modify the input simulator starting state to a random basis state! Probably need to change this.
## Inputs
- simulator: Either Trotter, QDrift, or Composite simulator. If a QDrift simulator is used, then the number
of samples is varied. If a Composite simulator is used then iterations is varied while QDrift
samples are held fixed.
- mc_samples: Monte Carlo samples for wave function states, shouldn't be necessary with density matrices?
## Returns
- (gate_cost, iterations) a tuple consisting of the gate cost and number of iterations needed to satisfy the epsilon
"""
if verbose:
print("*" * 75)
print("[find_optimal_cost] computing cost for simulator with partitioning:")
simulator.print_partition()
print("[find_optimal_cost] time = ", time)
print("epsilon =", epsilon)
print("use_infidelity=", use_infidelity)
print("mc_samples=", mc_samples)
# Make sure that density matrices are turned on if we use trace distance
if use_infidelity == False:
simulator.set_density_matrices(True)
# Helper function to average over random initial states and perform monte carlo averaging for infidelity.
def get_err_avg_std_cost(iterations):
inf_avg_tot, inf_std_tot, cost_tot = 0, 0, 0
for _ in range(num_state_samples):
simulator.randomize_initial_state()
exact_final_state = simulator.exact_final_state(time)
if use_infidelity:
if type(simulator) == QDriftSim:
infs, costs = zip(*multi_infidelity_sample(simulator, time, exact_final_state, nbsamples=iterations, mc_samples=mc_samples))
else:
infs, costs = zip(*multi_infidelity_sample(simulator, time, exact_final_state, iterations=iterations, mc_samples=mc_samples))
else:
if type(simulator) == QDriftSim:
infs, costs = multi_trace_distance_sample(simulator, time, exact_final_state, nbsamples=iterations, mc_samples=mc_samples)
else:
infs, costs = multi_trace_distance_sample(simulator, time, exact_final_state, iterations=iterations, mc_samples=mc_samples)
inf_avg_tot += np.mean(infs)
inf_std_tot += np.std(infs)
cost_tot += np.mean(costs)
return (inf_avg_tot / num_state_samples, inf_std_tot / num_state_samples, cost_tot / num_state_samples)
def is_iteration_good(iter):
avg, std, _ = get_err_avg_std_cost(iter)
return epsilon > avg + 2 * std
iter_lower, iter_upper = get_iteration_bounds(is_iteration_good, heuristic, verbose=verbose)
if verbose:
print("[find_optimal_cost] found iteration bounds: lower = ", iter_lower, ", upper =", iter_upper)
# bisection search until we find it.
mid = 1
count = 0
costs = 0
while iter_upper - iter_lower > 1 and count < COST_LOOP_DEPTH:
count += 1
mid = (iter_upper + iter_lower) / 2.0
iters = math.ceil(mid)
if verbose:
print("[find_optimal_cost] searching midpoint: ", iters)
# upper bounds always satisfy the threshold.
if is_iteration_good(iters):
iter_upper = iters
else:
iter_lower = iters
if count == COST_LOOP_DEPTH:
print("[find_optimal_cost] Reached loop depth, results may be inaccurate")
ret = get_err_avg_std_cost(iter_upper) # a tuple of inf_mean, inf_std, and cost
if verbose:
print("[find_optimal_cost] converged to iterations = ", iter_upper)
print("[find_optimal_cost] final infidelity =", ret[0], " +- ", ret[1])
print("[find_optimal_cost] final gate cost: ", ret[-1])
# Write to intermediate file.
json_path = os.getenv("SCRATCH")
if json_path[-1] != '/':
json_path += '/'
json_path += "outputs/gate_cost_" + str(len(simulator.trotter_norms)) + "_" + str(len(simulator.qdrift_norms)) + "_" + str(time) + ".json"
try:
r = {}
r["time"] = time
r["cost"] = ret[-1]
r["iters"] = iter_upper
r["avg_err"] = ret[0]
json.dump(r, open(json_path, 'w'))
except:
print("[find_crossover_time] tried to dump json it didn't work")
print("file name was:", json_path)
return (ret[-1], iter_upper)
def crossover_criteria_met(cost1, cost2):
diff = np.abs(cost1 - cost2)
avg = np.mean([cost1, cost2])
if diff / avg < CROSSOVER_CUTOFF_PERCENTAGE:
return True
else:
return False
def find_crossover_time(simulator, partition1, partition2, time_left, time_right, epsilon=0.05, use_infidelity=True, verbose=False, mc_samples=MC_SAMPLES_DEFAULT):
"""
## Computes the time where the cost between partitions is less than 5% of their difference.
### Inputs:
- simulator - a Composite sim to be partitioned
- partition1 - first partition to evaluate
- partition2 - second partition to evaluate
- time_left - left endpoint for search
- time_right - right endpoint for search
### Returns:
- either computed time or the best guess. Probably should fix this to indicate the cost difference between the partitions
"""
partition_sim(simulator, partition_type=partition1)
cost_left_1, _ = find_optimal_cost(simulator, time_left, epsilon, verbose=verbose, mc_samples=mc_samples)
cost_right_1, _ = find_optimal_cost(simulator, time_right, epsilon, verbose=verbose, mc_samples=mc_samples)
partition_sim(simulator, partition_type=partition2)
cost_left_2, _ = find_optimal_cost(simulator, time_left, epsilon, verbose=verbose, mc_samples=mc_samples)
cost_right_2, _ = find_optimal_cost(simulator, time_right, epsilon, verbose=verbose, mc_samples=mc_samples)
# Tells us if we start on the lower times with partition1 being cheaper than partition2
start_with_1 = cost_left_1 < cost_left_2
# Check that they actually cross
if start_with_1 and (cost_right_1 < cost_right_2):
print("[find_crossover_time] Partitions do not actually cross!")
return -1.
elif (start_with_1 == False) and (cost_right_2 < cost_right_1):
print("[find_crossover_time] Partitions do not actually cross!")
return -1.
# Check if either endpoints cross
if crossover_criteria_met(cost_left_1, cost_left_2):
return time_left
if crossover_criteria_met(cost_right_1, cost_right_2):
return time_right
# Bisection search
t_lower = time_left
t_upper = time_right
t_mid = np.mean([t_lower, t_upper])
if verbose:
print("[find_crossover_time] beginning search with:")
print("t_lower = ", t_lower)
print("t_upper = ", t_upper)
print("t_mid = ", t_mid)
print("start_with_1 = ", start_with_1, flush=True)
json_path = os.getenv("SCRATCH")
if json_path[-1] != '/':
json_path += '/'
json_path += "partial_result.json"
try:
r = {}
r["t_lower"] = t_lower
r["t_upper"] = t_upper
json.dump(r, open(json_path, 'w'))
except:
print("[find_crossover_time] tried to dump json it didn't work")
print("file name was:", json_path)
for _ in range(COST_LOOP_DEPTH):
if verbose:
print("[find_crossover_time] evaluating midpoint: ", t_mid, flush=True)
try:
json_path = os.getenv("SCRATCH")
if json_path[-1] != '/':
json_path += '/'
json_path += "partial_result.json"
r = {}
r["midpoint_" + str(_ + 1)] = t_mid
curr = json.load(open(json_path, 'r'))
r.update(curr)
print("json file name was:", json_path)
json.dump(r, open(json_path, 'w'))
except:
print("[find_crossover_time] tried to dump json it didn't work")
t_mid = np.mean([t_upper, t_lower])
partition_sim(simulator, partition_type=partition1)
c1, _ = find_optimal_cost(simulator, t_mid, epsilon, verbose=verbose, mc_samples=mc_samples)
partition_sim(simulator, partition_type=partition2)
c2, _ = find_optimal_cost(simulator, t_mid, epsilon, verbose=verbose, mc_samples=mc_samples)
if crossover_criteria_met(c1, c2):
return t_mid
if start_with_1 and (c1 < c2):
t_lower = t_mid
elif start_with_1 and (c2 < c1):
t_upper = t_mid
elif (start_with_1 == False) and (c1 < c2):
t_upper = t_mid
elif (start_with_1 == False) and (c2 < c1):
t_lower = t_mid
print("[find_crossover_time] Could not find acceptable crossover within loop bounds. Returning best guess")
return t_mid
# Return the probabilities for partitioning and the expected cost output of gbrt_minimize
def find_optimal_partition(simulator, time, infidelity_threshold):
return partition_sim(simulator, "gbrt_prob", time=time, epsilon=infidelity_threshold)
# Computes the expected cost of a probabilistic partitioning scheme.
def expected_cost(simulator, partition_probs, time, infidelity_threshold, heuristic = -1, num_samples = MC_SAMPLES_DEFAULT):
print("#" * 75)
if type(simulator) != CompositeSim:
print("[expected_cost] Currently only defined for composite simulators.")
return 1
hamiltonian_list = simulator.get_hamiltonian_list()
if len(hamiltonian_list) != len(partition_probs):
print("[expected_cost] Incorrect length probabilities. # of Hamiltonian terms:", len(hamiltonian_list), ", # of probabailities:", len(partition_probs))
return 1
costs, iters = [], []
for _ in range(num_samples):
start_time = time_this.time()
trotter, qdrift = sample_probabilistic_partition(hamiltonian_list, partition_probs)
simulator.set_partition(trotter, qdrift)
simulator.print_partition()
if len(iters) > 0:
prior_iters = iters[-1]
else:
prior_iters = -1
sampled_cost, sampled_iters = find_optimal_cost(simulator, time, infidelity_threshold, heuristic=prior_iters)
costs.append(sampled_cost)
iters.append(sampled_iters)
print("[expected_cost] completed iteration ", _, ", seconds taken: ", time_this.time() - start_time)
print("[expected_cost] cost avg and std:", np.mean(costs), " +- (", np.std(costs), ")")
return np.mean(costs)
def partition_sim(simulator, partition_type = "prob", chop_threshold = 0.5, optimize = False, nb_scaling = 0.0, time=0.01, epsilon=0.05, q_tile = 85):
"""
# Partitioning
Computes and sets a partition
## Partition schemes
### Possible strings and their function:
- "chop" - chop partition given a `chop_threshold`
- "exact_optimal_chop" - returns an optimal (upon convergence) partition given a `time`, `epsilon`, and `qtile`. See `readme.md` for more info.
- "prob" - computes the partition in lemma 2.1 given in "Composite Quantum Simulations" by Hagan and Wiebe.
- "random" - random partition
## Parameters
- simulator: A composite simulator, not type checked for flexibility later on
- partition_type: A string describing what partition method to use.
- chop_threshold: for "chop" partition, determines the spectral norm cutoff for each term to end up in QDrift
- optimize: artefact of an earlier Nelder-Mead partitioning method. Deprecated: leave false
- nb_scaling: a parametrization of nb within it's lower bound. Follows the scaling (1 + c)^2 * lower_bound. see paper for lower_bound
- time: required for probabilistic
- epsilon: required for probabilistic
- q_tile: calculates the inputed percentile of the spectral norm distribution to upper bound search space for "exact_optimal_chop". See readme.md for more info
"""
if type(partition_type) != type("string"):
print("[partition_sim] We only accept strings to describe the partition_type")
return 1
partition_type = partition_type.lower()
if partition_type == "prob":
partition_sim_prob(simulator, time, epsilon, nb_scaling, optimize)
simulator.partition_type = "prob"
elif partition_type == "optimize":
partition_sim_optimize(simulator)
simulator.partition_type = "optimize"
elif partition_type == "random":
partition_sim_random(simulator)
simulator.partition_type = "random"
elif partition_type == "chop":
partition_sim_chop(simulator, chop_threshold)
simulator.partition_type = "chop"
elif partition_type == "optimal_chop":
partition_sim_optimal_chop(simulator, time, epsilon)
simulator.partition_type = "optimal_chop"
elif partition_type=="exact_optimal_chop":
exact_optimal_chop(simulator, time, epsilon, q_tile)
simulator.partition_type = "exact_optimal_chop"
elif partition_type == "trotter":
partition_sim_trotter(simulator)
simulator.partition_type = "trotter"
elif partition_type == "first_order_trotter":
simulator.set_trotter_order(1)
partition_sim_trotter(simulator)
simulator.partition_type = "trotter"
elif partition_type == "second_order_trotter":
simulator.set_trotter_order(2)
partition_sim_trotter(simulator)
simulator.partition_type = "trotter"
elif partition_type == "qdrift":
partition_sim_qdrift(simulator)
simulator.partition_type = "qdrift"
elif partition_type == "gbrt_prob":
partition_sim_gbrt_prob(simulator, time, epsilon)
simulator.partition_type = "gbrt_prob"
else:
print("[partition_sim] Did not recieve valid partition. Valid options are: 'prob', 'optimize', 'random', 'chop', 'optimal_chop', 'trotter', and 'qdrift'.")
return 1
def partition_sim_prob(simulator, time, epsilon, nb_scaling, optimize):
if simulator.trotter_sim.order > 1:
k = simulator.trotter_sim.order/2
else:
print("[partition_sim] partition not defined for this order")
return 1
upsilon = 2*(5**(k -1))
lamb = simulator.get_lambda()
hamiltonian = simulator.get_hamiltonian_list()
coefficient_1 = (lamb * time / epsilon)**(1 - (1 / (2 * k)))
coefficient_2 = ((2 * k + 1) / (2 * k + upsilon))**(1 / (2 * k))
coefficient_3 = 2**(1-(1/k))/ upsilon**(1/(2 * k))
nb = int( coefficient_1 * coefficient_2 * coefficient_3 *((1 + nb_scaling)**2) )
simulator.nb = nb
# below value for chi is based on nb being computed as (1 + c)^2 * lower_bound, which gives chi this nice form
chi = lamb * nb_scaling / len(hamiltonian)
probs = [1 - min(1, chi / np.linalg.norm(hamiltonian[ix])) for ix in range(len(hamiltonian))]
trotter, qdrift = sample_probabilistic_partition(hamiltonian, probs)
simulator.set_partition(trotter, qdrift)
# TODO: how to optimize this quantity? not sure what optimal is without computing gate counts?
# MATT.H - Not really sure if this is optimizing nb or how it's going about it. Need to fix. -- Squishes probability distribution
if optimize and False:
optimal_nb = optimize.minimize(prob_nb_optima, nb, method='Nelder-Mead', bounds = optimize.Bounds([0], [np.inf], keep_feasible = False)) #Nb attribute serves as an inital geuss in this partition
nb_high = int(optimal_nb.x +1)
nb_low = int(optimal_nb.x)
prob_high = self.prob_nb_optima(nb_high) #check higher, (nb must be int)
prob_low = self.prob_nb_optima(nb_low) #check lower
if prob_high > prob_low:
self.nb = nb_low
else:
self.nb = nb_high
return 0
def partition_sim_optimize(simulator, weight_threshold):
if simulator.nb_optimizer == True: #if Nb is a parameter we wish to numerically optimize
guess = [0.5 for x in range(len(simulator.spectral_norms))] #guess for the weights
guess.append(2) #initial guess for Nb
upper_bound = [1 for x in range(len(simulator.spectral_norms))]
upper_bound.append(20) #no upper bound for Nb but set to some number we can compute instead of np.inf
lower_bound = [0 for x in range(len(simulator.spectral_norms) + 1)] #lower bound for Nb is 0
optimized_weights = optimize.minimize(simulator.nb_first_order_cost, guess, method='Nelder-Mead', bounds=optimize.Bounds(lower_bound, upper_bound))
print(optimized_weights.x)
for i in range(len(simulator.spectral_norms)):
if optimized_weights.x[i] >= weight_threshold:
simulator.a_norms.append([i, simulator.spectral_norms[i]])
elif optimized_weights.x[i] < weight_threshold:
simulator.b_norms.append([i, simulator.spectral_norms[i]])
simulator.a_norms = np.array(simulator.a_norms, dtype='complex')
simulator.b_norms = np.array(simulator.b_norms, dtype='complex')
simulator.nb = int(optimized_weights.x[-1] + 1) #nb must be of type int so take the ceiling
return 0
if simulator.nb_optimizer == False: #same as above leaving Nb as user-defined
guess = [0.5 for x in range(len(simulator.spectral_norms))] #guess for the weights
upper_bound = [1 for x in range(len(simulator.spectral_norms))]
lower_bound = [0 for x in range(len(simulator.spectral_norms))]
optimized_weights = optimize.minimize(simulator.first_order_cost, guess, method='Nelder-Mead', bounds=optimize.Bounds(lower_bound, upper_bound))
print(optimized_weights.x)
for i in range(len(simulator.spectral_norms)):
if optimized_weights.x[i] >= weight_threshold:
simulator.a_norms.append([i, simulator.spectral_norms[i]])
elif optimized_weights.x[i] < weight_threshold:
simulator.b_norms.append([i, simulator.spectral_norms[i]])
simulator.a_norms = np.array(simulator.a_norms, dtype='complex')
simulator.b_norms = np.array(simulator.b_norms, dtype='complex')
return 0
def partition_sim_random(simulator):
hamiltonian = simulator.get_hamiltonian_list()
trotter, qdrift = sample_probabilistic_partition(hamiltonian, [0.5] * len(hamiltonian))
simulator.set_partition(trotter, qdrift)
return 0
def partition_sim_chop(simulator, weight_threshold):
hamiltonian = simulator.get_hamiltonian_list()
trotter, qdrift = [], []
for ix in range(len(hamiltonian)):
norm = np.linalg.norm(hamiltonian[ix], ord=2)
if norm >= weight_threshold:
trotter.append(hamiltonian[ix])
else:
qdrift.append(hamiltonian[ix])
simulator.set_partition(trotter, qdrift)
return 0
#MATT P- added spectral norms back to the class to make this more convinient. Did this without having to recompute norms, just keep track of them.
def partition_sim_optimal_chop(simulator, time, epsilon):
dimensions = [Real(name='weight', low = 0, high = max(simulator.spectral_norms))]
@use_named_args(dimensions=dimensions)
def obj_fn(weight):
partition_sim_chop(simulator, weight)
return find_optimal_cost(simulator, time, epsilon)[0] # [0] gets the costs throws away iters
result = gbrt_minimize(obj_fn, dimensions=dimensions, n_calls=30, n_initial_points=5, random_state=4, verbose=True, acq_func="LCB")
print("result.fun: ", result.fun)
print("result.x: ", result.x)
print("result:", result)
def exact_optimal_chop(simulator, time, epsilon, q_tile):
'''an optimizer that uses GBRT to return a tuple containing the optimized gate count and its location like so: (gate count, [nb, w])'''
#q_tile computes the quartile of the norms and tries to prevent silly qdrift guesses
#with long computation times (ie placing all terms in qdrift at a large time)
if type(simulator) != CompositeSim: raise TypeError("this partition only makes sense for simulators of the class CompositeSim")
norm = np.linalg.norm(np.sum(simulator.unparsed_hamiltonian, axis=0), ord=2)
dim1 = Integer(name = "nb", low=1, high = int(1/2 * len(simulator.spectral_norms)))
if (time * norm > 1):
dim2 = Real(name = "w", low=min(simulator.spectral_norms), high = np.percentile(simulator.spectral_norms, q_tile))
else:
dim2 = Real(name = "w", low=min(simulator.spectral_norms), high = max(simulator.spectral_norms) + min(simulator.spectral_norms)/10)
guess_point1 = [int((1/4)*len(simulator.spectral_norms)), statistics.median(simulator.spectral_norms)]
guess_point2 = [max(int((1/20)*len(simulator.spectral_norms)), 1), statistics.median(simulator.spectral_norms)]
guess_points = [guess_point1, guess_point2]
dimensions = [dim1, dim2]
@use_named_args(dimensions=dimensions)
def obj_func(nb, w):
simulator.nb = nb
partition_sim(simulator, "chop", chop_threshold=w)
return exact_cost(simulator, time, nb, epsilon)
result = gbrt_minimize(func=obj_func,dimensions=dimensions, n_calls=50, n_initial_points = 20,
random_state=4, verbose = False, acq_func = "LCB", x0 = guess_points, n_jobs=1)
opt_nb, opt_w = result.x
opt_w = float(opt_w) #because int64 return type is not json serializable
opt_nb = int(opt_nb)
simulator.gate_count = (int(result.fun), [opt_nb, opt_w])
#print("result.fun: ", result.fun)
#print("result.x: ", result.x)
# Let boosted regression trees try their best to come up with good probabilities
# Inputs: self-explanatory
# Returns: (probability list, nb, expected cost at optimal)
def partition_sim_gbrt_prob(simulator, time, epsilon):
hamiltonian = simulator.get_hamiltonian_list()
dimensions = [Real(0.0, 1.0)] * len(hamiltonian)
dimensions += [Integer(1, len(hamiltonian))]
def obj_fn(dims):
probs = dims[:-1]
nb = dims[-1]
simulator.nb = nb
return expected_cost(simulator, probs, time, epsilon)
result = gbrt_minimize(obj_fn, dimensions=dimensions, x0=[1.0]*len(hamiltonian) + [1], n_calls=20, verbose=True, acq_func="LCB", n_jobs=-1)
print("results:")
print("fun:", result.fun)
print("x:", result.x)
return (result.x[:-1], result.x[-1], result.fun)
def partition_sim_trotter(simulator):
ham = simulator.get_hamiltonian_list()
simulator.set_partition(ham, [])
return 0
def partition_sim_qdrift(simulator):
ham = simulator.get_hamiltonian_list()
simulator.set_partition([], ham)
return 0
# Inputs
# - hamiltonian_list: list of terms to be partitioned
# - prob_list: list of probabilities to sample a partition. Standard we are using is prob = 1.0 means Trotter, 0.0 means QDrift
# Returns a tuple of lists (trotter, qdrift) to be used in a partitioner.
# TODO- refactor existing code to use this function.
def sample_probabilistic_partition(hamiltonian_list, prob_list):
if len(hamiltonian_list) != len(prob_list):
print("[sample_probabilistic_partition] lengths of lists do not match")
return 1
trotter, qdrift = [], []
for ix in range(len(hamiltonian_list)):
if prob_list[ix] > 1.0 or prob_list[ix] < 0.0:
print("[sample_probabilistic_partition] probability not within [0.0, 1.0] encountered")
return 1
sample = np.random.random()
if sample <= prob_list[ix]:
trotter.append(hamiltonian_list[ix])
else:
qdrift.append(hamiltonian_list[ix])
return (trotter, qdrift)
#### FIRST ORDER COST FUNCTIONS FROM SIMULATOR
# These turn out to not modify the simulator directly/are necessary for simulator functioning so moved here. Currently not used so they are prunable.
def nb_first_order_cost(simulator, weight): #first order cost, currently computes equation 31 from paper. Weight is a list of all weights with Nb in the last entry
cost = 0.0 #Error with this function, it may not be possible to optimize Nb with this structure given the expression of the function
qd_sum = 0.0
for i in range(len(simulator.spectral_norms)):
qd_sum += (1-weight[i]) * simulator.spectral_norms[i]
for j in range(len(simulator.spectral_norms)):
commutator_norm = np.linalg.norm(np.matmul(simulator.hamiltonian_list[i], simulator.hamiltonian_list[j]) - np.matmul(simulator.hamiltonian_list[j], simulator.hamiltonian_list[i]), ord = 2)
cost += (2/(5**(1/2))) * ((weight[i] * weight[j] * simulator.spectral_norms[i] * simulator.spectral_norms[j] * commutator_norm) +
(weight[i] * (1-weight[j]) * simulator.spectral_norms[i] * simulator.spectral_norms[j] * commutator_norm))
cost += (qd_sum**2) * 4/weight[-1] #dividing by Nb at the end (this form is just being used so I can easily optimize Nb as well)
return cost
def first_order_cost(simulator, weight): #first order cost, currently computes equation 31 from paper. Function does not have nb as an omptimizable parameter
cost = 0.0
qd_sum = 0.0
for i in range(len(simulator.spectral_norms)):
qd_sum += (1-weight[i]) * simulator.spectral_norms[i]
for j in range(len(simulator.spectral_norms)):
commutator_norm = np.linalg.norm(np.matmul(simulator.hamiltonian_list[i], simulator.hamiltonian_list[j]) - np.matmul(simulator.hamiltonian_list[j], simulator.hamiltonian_list[i]), ord = 2)
cost += (2/(5**(1/2))) * ((weight[i] * weight[j] * simulator.spectral_norms[i] * simulator.spectral_norms[j] * commutator_norm) +
(weight[i] * (1-weight[j]) * simulator.spectral_norms[i] * simulator.spectral_norms[j] * commutator_norm))
cost += (qd_sum**2) * 4/simulator.nb #dividing by Nb at the end (this form is just being used so I can easily optimize Nb as well)
return cost
#Function that allows for the optimization of the nb parameter in the probabilistic partitioning scheme (at each timestep)
def prob_nb_optima(simulator, test_nb):
k = simulator.inner_order/2
upsilon = 2*(5**(k -1))
lamb = sum(simulator.spectral_norms)
test_chi = (lamb/len(simulator.spectral_norms)) * ((test_nb * (simulator.epsilon/(lamb * simulator.time))**(1-(1/(2*k))) *
((2*k + upsilon)/(2*k +1))**(1/(2*k)) * (upsilon**(1/(2*k)) / 2**(1-(1/k))))**(1/2) - 1)
test_probs = []
for i in range(len(simulator.spectral_norms)):
test_probs.append(float(np.abs((1/simulator.spectral_norms[i])*test_chi))) #ISSUE
return max(test_probs)
#a function to decide on a good number of monte carlo samples for the following simulation functions (avoid noise errors)
def sample_decider(simulator, time, samples, iterations, mc_sample_guess, epsilon):
exact_state = exact_time_evolution(simulator.unparsed_hamiltonian, time, simulator.initial_state)
sample_guess = mc_sample_guess
inf_samples = [1, 0]
for k in range(1, 25):
inf_samples[k%2] = simulator.sample_channel_inf(time, samples, iterations, sample_guess, exact_state)
print(inf_samples)
if np.abs((inf_samples[0] - inf_samples[1])) < (0.1 * epsilon): #choice of precision
break
else:
sample_guess *= 2
return int(sample_guess/2)
########################## LR Functions ###########################
def hamiltonian_localizer_1d(local_hamiltonian, sub_block_size):
#A function to do an m=1 block decomposition of a nearest neighbour hamiltonian. Takes a tupel as input where
# indices 0, 1, 2 are the Hamiltonian terms, 1d lattice indices, and legnth of the lattice respectfully.
#The funciton outputs 3 new "local" lists that can then be partitioned
a_terms, a_index = [], []
y_terms, y_index = [], []
b_terms, b_index = [], []
terms, indices, length = local_hamiltonian
midpoint = int(length/2)
# start = midpoint - (int(sub_block_size/2))
# stop = start + sub_block_size
# if start < 1:
# raise Exception("sub block is the size of or larger than the Hamiltonian")
temp_norms = []
for k in terms:
temp_norms.append(np.linalg.norm(k, ord=2))
h = max(temp_norms) #normalization factor (LR algorithm requires spectral norm < 1)
bound = []
upper = midpoint
lower = midpoint
iv = 1
bound.append(midpoint)
while iv < sub_block_size:
if iv % 2 == 0:
bound.append(upper + 1)
upper +=1
else:
bound.append(lower - 1)
lower += -1
if midpoint - iv < 0: raise Exception("sub block is the size of or larger than the Hamiltonian")
iv += 1
bound = set(bound)
print(bound)
ix = 0
while ix < len(terms):
if sub_block_size == 1:
if set(indices[ix]) == bound:
y_terms.append(1/h * terms[ix])
y_index.append(indices[ix])
else:
if set(indices[ix]).issubset(bound):
y_terms.append(1/h * terms[ix])
y_index.append(indices[ix])
if set(indices[ix]).issubset(set(arange(0, midpoint)).union(bound)): #A region
a_terms.append(1/h * terms[ix])
a_index.append(indices[ix])
if set(indices[ix]).issubset(set(arange(midpoint, length)).union(bound)): #B region
b_terms.append(1/h *terms[ix])
b_index.append(indices[ix])
ix += 1
# if set(indices[ix]).issubset(arange(0, midpoint)):
# a_terms.append(1/h * terms[ix])
# a_index.append(indices[ix])
# else:
# b_terms.append(1/h *terms[ix])
# b_index.append(indices[ix])
# if not set(indices[ix]).isdisjoint(arange(start, stop)) == True: #Y region
# y_terms.append(1/h * terms[ix])
# y_index.append(indices[ix])
#print(a_index)
#print(y_index)
#print(b_index)
if ((len(a_terms) == 0) or (len(b_terms) == 0) or (len(y_terms) == 0)):
raise Exception("poor block choice, one of the blocks is empty")
return (np.array(a_terms), np.array(y_terms), np.array(b_terms)) #A and B here are really the AUB and BUC from the paper
def local_partition(simulator, partition, weights = None, time = 0.01, epsilon = 0.001): #weights is a list with ordering A, Y, B
if type(simulator) != LRsim: raise TypeError("only works on LRsims")
if partition == "chop":
local_chop(simulator, weights)
simulator.partition_type = "chop"
elif partition == "optimal_chop":
optimal_local_chop(simulator, time, epsilon)
simulator.partition_type = "optimal_chop"
elif partition == "trotter":
local_trotter(simulator)
simulator.partition_type = "trotter"
elif partition == "qdrift":
local_qdrift(simulator)
simulator.partition_type = "qdrift"
else:
raise Exception("this is not a valid partition")
return 0
def local_trotter(simulator):
if type(simulator) != LRsim: raise TypeError("only works on LRsims")
for i in range(3):
a_temp = []
b_temp = []
for j in range(len(simulator.local_hamiltonian[i])):
a_temp.append(simulator.local_hamiltonian[i][j])
simulator.internal_sims[i].set_partition(a_temp, b_temp)
return 0
def local_qdrift(simulator):
if type(simulator) != LRsim: raise TypeError("only works on LRsims")
for i in range(3):
a_temp = []
b_temp = []
for j in range(len(simulator.local_hamiltonian[i])):
b_temp.append(simulator.local_hamiltonian[i][j])
simulator.internal_sims[i].set_partition(a_temp, b_temp)
return 0
def local_chop(simulator, weights):
if type(simulator) != LRsim: raise TypeError("only works on LRsims")
for i in range(3):
a_temp = []
b_temp = []
for j in range(len(simulator.local_hamiltonian[i])):
if simulator.spectral_norms[i][j] >= weights[i]:
a_temp.append(simulator.local_hamiltonian[i][j]) ###should be appending terms not the spectral norms
else:
b_temp.append(simulator.local_hamiltonian[i][j])
simulator.internal_sims[i].set_partition(a_temp, b_temp)
#print("block " + str(i) + " has " + str(len(simulator.internal_sims[i].trotter_norms)) +
#" trotter terms and " + str(len(simulator.internal_sims[i].qdrift_norms)) + " qdrift terms")
simulator.partition_type = "local_chop"
return 0
def optimal_local_chop(simulator, time, epsilon): ### needs exact cost function to be operational
if type(simulator) != LRsim: raise TypeError("only works on LRsims")
guess_points = []
dimensions = []
delta = min(simulator.spectral_norms[0] + simulator.spectral_norms[1] + simulator.spectral_norms[2])/10
dim1 = Integer(name = "nb_a", low=1, high = len(simulator.spectral_norms[0]))
dim2 = Integer(name="nb_y", low=1, high = len(simulator.spectral_norms[1]))
dim3 = Integer(name="nb_b", low=1, high = len(simulator.spectral_norms[2]))
dim4 = Real(name = "w_a", low=min(simulator.spectral_norms[0]), high = max(simulator.spectral_norms[0]) + delta)
dim5 = Real(name = "w_y", low=min(simulator.spectral_norms[1]), high = max(simulator.spectral_norms[1]) + delta)
dim6 = Real(name = "w_b", low=min(simulator.spectral_norms[2]), high = max(simulator.spectral_norms[2]) + delta) # delta is too allow everything to go into QDrift (bed on the \geq chop condition)
for j in range(3):
guess_points.append(int((1/2)*len(simulator.spectral_norms[j])))
for i in range(3):
guess_points.append(statistics.median(simulator.spectral_norms[i]))
dimensions = [dim1, dim2, dim3, dim4, dim5, dim6]
@use_named_args(dimensions=dimensions)
def obj_func(nb_a, nb_y, nb_b, w_a, w_y, w_b):
nb_list = [nb_a, nb_y, nb_b]
weights = [w_a, w_y, w_b]
#set_local_nb(simulator, nb_list)
local_partition(simulator, partition = "chop", weights=weights, time=time, epsilon=epsilon)
return exact_cost(simulator, time, nb_list, epsilon)
result = gbrt_minimize(func=obj_func,dimensions=dimensions, n_calls=50, n_initial_points = 15,
random_state=4, verbose = False, acq_func = "LCB", x0 = guess_points, n_jobs=1)
simulator.gate_count = result.fun
print("(nba, nby, nbb, wa, wy, wb): " +str(result.x))
return 0
def exact_cost(simulator, time, nb, epsilon): #relies on the use of density matrices
'''
Solves the search problem for the exact number of iterations of a given composite channel to achieve an error epsilon.
Requires monotonicity of the trace distance w.r.t iterations, so density matrices are required
'''
simulator.gate_count = 0
if type(simulator.partition_type) == type(None): raise TypeError("call a partition function before calling this function")
if type(simulator) == (CompositeSim):
if type(nb) == list: raise TypeError("this requires a single integer nb")
simulator.nb = nb #redundancy
if simulator.partition_type == "qdrift":
get_trace_dist = lambda x : sim_trace_distance(simulator=simulator, time=time, iterations=1, nb = x)
elif simulator.partition_type == 'trotter':
get_trace_dist = lambda x : sim_trace_distance(simulator=simulator, time=time, iterations=x, nb = 1)
else:
get_trace_dist = lambda x : sim_trace_distance(simulator=simulator, time=time, iterations=x, nb = simulator.nb)
elif type(simulator) == (LRsim):
if type(nb) != type([]): raise TypeError("this requires a list of nbs")
set_local_nb(simulator, nb) #redundancy
if simulator.partition_type == "qdrift":
get_trace_dist = lambda x : sim_trace_distance(simulator=simulator, time=time, iterations=1, nb = [x,x,x])