def _optimize(self, c): g = c.to_graph() zx.full_reduce(g, quiet=True) g.normalize() gs = pivot_anneal(g, iters=5, cool=0.0025, score=self.twoq_score) c_opt = zx.extract_circuit(gs) c_opt = self.post_process(c_opt) return c_opt
def sa_with_restarts(g, n_restarts=3, n_iters=2500, quiet=True): best_g = g.copy() best_score = g_score(best_g) for i in range(n_restarts): g_sa, _ = pivot_anneal(g, iters=n_iters, quiet=quiet) trial_score = g_score(g_sa) if trial_score < best_score or i == 0: best_score = trial_score best_g = g_sa.copy() return best_g
g_tmp = g.copy() if REDUCE_METHOD == "TR": zx.teleport_reduce(g_tmp) c_tr = zx.Circuit.from_graph(g_tmp.copy()).to_basic_gates() c_tr = zx.basic_optimization(c_tr) g_simp = c_tr.to_graph() elif REDUCE_METHOD == "FR": zx.full_reduce(g_tmp) g_simp = g_tmp.copy() else: raise RuntimeError(f"Invalid REDUCE_METHOD: {REDUCE_METHOD}") to_graph_like(g_simp) _, scores = pivot_anneal(g_simp, iters=N_ITERS) final_score = scores[-1] for x in range(0, N_ITERS, INTERVAL): if final_score < scores[x]: improvement_after[x].append(1) else: improvement_after[x].append(0) pdb.set_trace() improvement_after_probs = {k: np.mean(v) for k, v in improvement_after.items()} xs = list(improvement_after_probs.keys()) ys = list(improvement_after_probs.values()) plt.rcParams.update({'font.size': 14}) plt.scatter(xs, ys)
axes[0].plot(list(range(len(reductions))), reductions) axes[0].set_xlabel("Generation") axes[0].set_ylabel("Reduction") axes[0].set_title(f"GA: {N_MUTANTS} mutants, {N_GENS} generations") axes[0].legend() ## SA part reductions = list() N_TRIALS = 10 N_ITERS = 1000 init_score = 4 * c_tr.twoqubitcount() + c_tr.tcount() for i in tqdm(range(N_TRIALS)): g_anneal, _ = sa.pivot_anneal(g_tr.copy(), iters=N_ITERS, score=sa.c_score) zx.full_reduce(g_anneal) c_anneal = zx.extract_circuit(g_anneal.copy()).to_basic_gates() c_anneal = zx.basic_optimization(c_anneal) trial_score = 4 * c_anneal.twoqubitcount() + c_anneal.tcount() reduction = (init_score - trial_score) / init_score * 100 reductions.append(reduction) # print(f"\n----- trial {i} -----") # print(c_anneal.stats()) """ plt.hist(reductions) plt.xlabel("Reduction") plt.ylabel("Frequency")