online_nb.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import math
  2. def online_network_benchmarking(network, path_list, bounces):
  3. # Initialization
  4. candidate_set = path_list
  5. s = 0 # Phase
  6. C = 0.01 # Constant
  7. delta = 0.1 # Error
  8. cost = 0
  9. estimated_fidelities = {}
  10. # error_probability = {} # Map bounces number to the probability that the arm with largest mean is not the best arm
  11. # epoch_len = 20
  12. # epoch = 0
  13. # correct_times = 0
  14. while len(candidate_set) > 1:
  15. s += 1
  16. Ns = math.ceil(C * 2**(2 * s) * math.log2(2**s * len(candidate_set) / delta))
  17. if Ns < 4:
  18. Ns = 4
  19. # print(f"Ns: {Ns}")
  20. sample_times = {}
  21. for i in bounces:
  22. sample_times[i] = Ns
  23. p_s = {}
  24. for path in candidate_set:
  25. p, bounces_num = network.benchmark_path(path, bounces, sample_times)
  26. # print(f"Estimated Fidelity of path {path}: {p}")
  27. # Convert the estimated depolarizing parameter `p` into fidelity
  28. estimated_fidelities[path] = p + (1 - p) / 2
  29. p_s[path] = p
  30. cost += bounces_num
  31. p_max = max(p_s.values())
  32. # current_best_path = max(p_s, key=p_s.get)
  33. # if current_best_path == network.best_path:
  34. # correct_times += 1
  35. new_candidate_set = []
  36. for path in candidate_set:
  37. # print(f"p_s[path] + 2**(-s): {p_s[path] + 2**(-s)}")
  38. # print(f"p_max - 2**(-s): {p_max - 2**(-s)}")
  39. if p_s[path] + 2**(-s) > p_max - 2**(-s):
  40. new_candidate_set.append(path)
  41. candidate_set = new_candidate_set
  42. assert len(candidate_set) == 1
  43. best_path = candidate_set[0]
  44. correctness = best_path == network.best_path
  45. best_path_fidelity = estimated_fidelities[best_path]
  46. # print(f"Best path: {best_path}, estimated parameter p: {p_s[best_path]}, cost: {cost}")
  47. return correctness, cost, best_path_fidelity