def simulate_state(self): n_qubits = self.n_qubits circuit = self.qc buckets, data_dict, bra_vars, ket_vars = qtree.optimizer.circ2buckets( n_qubits, circuit) graph = qtree.graph_model.buckets2graph(buckets, ignore_variables=ket_vars+bra_vars) peo_ints, treewidth = utils.get_locale_peo(graph, utils.n_neighbors) peo = [qtree.optimizer.Var(var, size=graph.nodes[var]['size'], name=graph.nodes[var]['name']) for var in peo_ints] peo = ket_vars + bra_vars + peo perm_buckets, perm_dict = qtree.optimizer.reorder_buckets(buckets, peo) ket_vars = sorted([perm_dict[idx] for idx in ket_vars], key=str) bra_vars = sorted([perm_dict[idx] for idx in bra_vars], key=str) initial_state = target_state = 0 slice_dict = qtree.utils.slice_from_bits(initial_state, ket_vars) slice_dict.update( qtree.utils.slice_from_bits(target_state, bra_vars) ) sliced_buckets = qtree.np_framework.get_sliced_np_buckets( perm_buckets, data_dict, slice_dict) result = qtree.optimizer.bucket_elimination( sliced_buckets, qtree.np_framework.process_bucket_np) return result
def simulate_circ(circuit, n_qubits): buckets, data_dict, bra_vars, ket_vars = qtree.optimizer.circ2buckets( n_qubits, circuit) graph = qtree.graph_model.buckets2graph( buckets, ignore_variables=bra_vars+ket_vars) peo, nghs = utils.get_locale_peo(graph, utils.n_neighbors) peo = qtree.graph_model.indices_to_vars(peo, graph) # place bra and ket variables to beginning, so these variables # will be contracted first #peo, treewidth = qtree.graph_model.get_peo(graph) peo = ket_vars + bra_vars + peo perm_buckets, perm_dict = qtree.optimizer.reorder_buckets(buckets, peo) # extract bra and ket variables from variable list and sort according # to qubit order ket_vars = sorted([perm_dict[idx] for idx in ket_vars], key=str) bra_vars = sorted([perm_dict[idx] for idx in bra_vars], key=str) # Take the subtensor corresponding to the initial state initial_state = target_state = 0 slice_dict = qtree.utils.slice_from_bits(initial_state, ket_vars) slice_dict.update( qtree.utils.slice_from_bits(target_state, bra_vars) ) sliced_buckets = qtree.np_framework.get_sliced_np_buckets( perm_buckets, data_dict, slice_dict) with timing('time_raw', callback=append_to(profile)): result = qtree.optimizer.bucket_elimination( sliced_buckets, qtree.np_framework.process_bucket_np)
def get_qbb_peo(graph): try: peo, tw = qtree.graph_model.get_peo(graph) fail = False except: print('QBB fail, nodes count:', graph.number_of_nodes()) peo, nghs = utils.get_locale_peo(graph, utils.n_neighbors) fail = True return peo, fail
def optimize_order(graph, ordering_algo='qbb'): if ordering_algo == 'qbb': peo, tw = qtree.graph_model.get_peo(graph, int_vars=True) if ordering_algo == 'nghs': peo, _nghs = utils.get_locale_peo(graph, utils.n_neighbors) tw = max(_nghs) - 1 mapping = {v: i for i, v in enumerate(peo)} tensors = expr.get_tensors_from_graph(graph) reorder_tensors(tensors, peo) graph = nx.relabel_nodes(graph, mapping, copy=True) return peo, tw, graph
def _optimise_graph(graph): peo, nghs = utils.get_locale_peo(graph, utils.n_neighbors) graph_opt, slice_dict = utils.reorder_graph(graph, peo) return graph_opt, nghs
plt.savefig('figures/rect_cost_vs_nodes_T_p1.png') # - # # Finding biggest tacklable task # ## Full ordering # + graph, N = qaoa.get_test_expr_graph(25, 1, type='randomreg', degree=3) print(N) # - # ### Naive (degree-based local) peo # %%time peo, nghs = utils.get_locale_peo(graph, utils.n_neighbors) graph_relabel, slice_dict = utils.reorder_graph(graph, peo) # + costs, flops = qtree.graph_model.cost_estimator(graph_relabel) print(max(costs)/1e9) print(max(flops)/1e9) utils.plot_cost(costs, flops) #nx.draw_kamada_kawai(graph, node_size=3) # - neigh_plot(nghs)
def n_peo(graph): return utils.get_locale_peo(graph, utils.n_neighbors)