lonline_nb.py~ 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. def _online_network_benchmarking_truly_strict(network, path_list, bounces, C_budget):
  2. candidate_set = list(path_list)
  3. s = 0
  4. C_const = 0.01
  5. delta = 0.1
  6. cost = 0
  7. estimated_fidelities = {}
  8. cost_per_sample_unit = sum(bounces)
  9. if cost_per_sample_unit == 0:
  10. cost_per_sample_unit = 1
  11. while cost < C_budget and len(candidate_set) > 1:
  12. s += 1
  13. Ns = math.ceil(C_const * 2**(2 * s) * math.log2(2**s * len(candidate_set) / delta))
  14. if Ns < 4:
  15. Ns = 4
  16. cost_needed_for_one_path = Ns * cost_per_sample_unit
  17. if cost + cost_needed_for_one_path > C_budget and s > 1:
  18. break
  19. p_s = {}
  20. sample_times = {i: Ns for i in bounces}
  21. for path in candidate_set:
  22. if cost + cost_needed_for_one_path > C_budget:
  23. continue
  24. p, bounces_num = network.benchmark_path(path, bounces, sample_times)
  25. cost += bounces_num
  26. estimated_fidelity = p + (1 - p) / 2
  27. estimated_fidelities[path] = estimated_fidelity
  28. p_s[path] = p
  29. if not p_s:
  30. break
  31. p_max = max(p_s.values())
  32. new_candidate_set = [path for path in candidate_set if path in p_s and p_s[path] + 2**(-s) > p_max - 2**(-s)]
  33. candidate_set = new_candidate_set
  34. if not estimated_fidelities:
  35. best_path = max(estimated_fidelities, key=estimated_fidelities.get)
  36. best_path_fidelity = estimated_fidelities[best_path]
  37. correctness = (best_path == network.best_path)