Source code for qrisp.qiro.qiroproblems.qiroMaxClique
"""\********************************************************************************* Copyright (c) 2024 the Qrisp authors** This program and the accompanying materials are made available under the* terms of the Eclipse Public License 2.0 which is available at* http://www.eclipse.org/legal/epl-2.0.** This Source Code may also be made available under the following Secondary* Licenses when the conditions for such availability set forth in the Eclipse* Public License, v. 2.0 are satisfied: GNU General Public License, version 2* with the GNU Classpath Exception which is* available at https://www.gnu.org/software/classpath/license.html.** SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0********************************************************************************/"""fromqrispimportrz,rzz,ximportnumpyasnpimportcopyimportnetworkxasnxfromqrisp.algorithms.qiro.qiroproblems.qiro_utilsimport*
[docs]defcreate_max_clique_replacement_routine(res,problem_updated):""" Creates a replacement routine for the problem structure, i.e., defines the replacement rules. See the `original paper <https://journals.aps.org/prxquantum/abstract/10.1103/PRXQuantum.5.020327>`_ for a description of the update rules. Parameters ---------- res : dict Result dictionary of QAOA optimization procedure. problem_updated : List Updates that happened during the QIRO routine. Consits of the updated problem, a list of Qubits which were found to be positively correlated, i.e. part of the problem solution, and a list Qubits which were found to be negatively correlated, i.e. they contradict solution qubits in accordance with the update rules. Returns ------- new_graph : nx.Graph Updated graph for the problem instance. solutions : list Updated set of solutions to the problem. sign : int The sign of the correlation. exclusions : list Updated set of exclusions for the problem. """graph=problem_updated[0]solutions=problem_updated[1]exclusions=problem_updated[2]orig_edges=[list(item)foritemingraph.edges()]orig_nodes=list(graph.nodes())#get the max_edge and eval the sum and signmax_item,sign=find_max(orig_nodes,orig_edges,res,solutions)ifmax_item==None:returngraph,solutions,0,exclusionsnew_graph=copy.deepcopy(graph)# we just directly remove vertices from the graph ifisinstance(max_item,int):ifsign<0:border=list(graph.adj[max_item].keys())border.append(max_item)to_remove=[int(item)foritemingraph.nodes()ifitemnotinborder]new_graph.remove_nodes_from(to_remove)solutions.append(max_item)exclusions+=to_removeelifsign>0:#remove itemnew_graph.remove_node(max_item)exclusions.append(max_item)else:ifsign>0:#keep the two items in solution and remove all that are not adjacent to bothintersect=list(set(list(graph.adj[max_item[0]].keys()))&set(list(graph.adj[max_item[0]].keys())))intersect.append(max_item[0])intersect.append(max_item[1])to_remove=[int(item)foritemingraph.nodes()ifitemnotinintersect]new_graph.remove_nodes_from([itemforitemingraph.nodes()ifitemnotinintersect])solutions.append(max_item[0])solutions.append(max_item[1])exclusions+=to_removeelifsign<0:#remove all that do not border on either! nodeunion=list(graph.adj[max_item[0]].keys())union+=list(graph.adj[max_item[1]].keys())union.append(max_item[0])union.append(max_item[1])to_remove=[int(item)foritemingraph.nodes()ifitemnotinunion]#to_delete = [item for item in graph.nodes() if item not in union]new_graph.remove_nodes_from(to_remove)exclusions+=to_removereturnnew_graph,solutions,sign,exclusions
[docs]defcreate_max_clique_cost_operator_reduced(problem_updated):r""" Creates the ``cost_operator`` for the problem instance. This operator is adjusted to consider qubits that were found to be a part of the problem solution. Parameters ---------- problem_updated : List Updates that happened during the QIRO routine. Consits of the updated problem, a list of Qubits which were found to be positively correlated, i.e. part of the problem solution, and a list Qubits which were found to be negatively correlated, i.e. they contradict solution qubits in accordance with the update rules. Returns ------- cost_operator : function A function receiving a :ref:`QuantumVariable` and a real parameter $\gamma$. This function performs the application of the cost operator. """problem=problem_updated[0]solutions=problem_updated[1]G_compl=nx.complement(problem)defcost_operator(qv,gamma):forpairinlist(G_compl.edges()):rzz(3*gamma,qv[pair[0]],qv[pair[1]])rz(-gamma,qv[pair[0]])rz(-gamma,qv[pair[1]])foriinproblem.nodes():ifnotiinsolutions:rz(gamma,qv[i])returncost_operator
Get in touch!
If you are interested in Qrisp or high-level quantum algorithm research in general connect with us on our
Slack workspace.