succ_elim_nb.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import math
  2. def successive_elimination_network_benchmarking(network, path_list, bounces):
  3. # Initialization
  4. L = len(path_list)
  5. active_set = path_list
  6. C = 0.15
  7. N = 4
  8. cost = 0
  9. delta = 0.1
  10. sample_times = {}
  11. for i in bounces:
  12. sample_times[i] = N
  13. mean = {path: 0 for path in path_list}
  14. n = {path: 0 for path in path_list}
  15. t = 0
  16. while len(active_set) > 1:
  17. t += 1
  18. ucb = {}
  19. lcb = {}
  20. for path in active_set:
  21. p, bounces_num = network.benchmark_path(path, bounces, sample_times)
  22. if p > 1.5:
  23. print(f"Get an abnormal p={p}")
  24. cost += bounces_num
  25. mean[path] = (mean[path] * n[path] + p) / (n[path] + 1)
  26. n[path] += 1
  27. r = C * math.sqrt(math.log(4 * L * t * t / delta) / (2 * t))
  28. # print(f"r={r}, {math.log(4 * L * t * t / delta)}")
  29. ucb[path] = mean[path] + r
  30. lcb[path] = mean[path] - r
  31. # print(f"mean[{path}] = {mean[path]}")
  32. # print(f"ucb[{path}] = {ucb[path]}")
  33. # print(f"lcb[{path}] = {lcb[path]}")
  34. new_active_set = []
  35. for path1 in active_set:
  36. ok = True
  37. for path2 in active_set:
  38. if path1 != path2 and ucb[path1] < lcb[path2]:
  39. ok = False
  40. break
  41. if ok:
  42. new_active_set.append(path1)
  43. active_set = new_active_set
  44. # print(f"Length of active set: {len(active_set)}")
  45. assert len(active_set) == 1
  46. best_path = active_set[0]
  47. correctness = best_path == network.best_path
  48. # print(f"Succ Elim NB: Best path: {best_path}, estimated parameter p: {mean[best_path]}, cost: {cost}")
  49. estimated_fidelity = {}
  50. for path in path_list:
  51. p = mean[path]
  52. # Convert the estimated depolarizing parameter `p` into fidelity
  53. estimated_fidelity[path] = p + (1 - p) / 2
  54. best_path_fidelity = estimated_fidelity[best_path]
  55. return correctness, cost, best_path_fidelity