"""\********************************************************************************* Copyright (c) 2025 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********************************************************************************/"""fromqrispimport*importnumpyasnpfromscipy.optimizeimportminimizefromsympyimportSymbolimportitertoolsfromnumbaimportnjit,prangedefmaxcut_obj(x,G):returnmaxcut_obj_jitted(int(x[::-1],2),list(G.edges()))@njit(cache=True)defmaxcut_obj_jitted(x,edge_list):cut=0fori,jinedge_list:# the edge is cutif((x>>i)^(x>>j))&1:# if x[i] != x[j]: cut-=1returncut@njit(parallel=True,cache=True)defmaxcut_energy(outcome_array,count_array,edge_list):res_array=np.zeros(len(outcome_array))foriinprange(len(outcome_array)):res_array[i]=maxcut_obj_jitted(outcome_array[i],edge_list)*count_array[i]returnnp.sum(res_array)
[docs]defcreate_maxcut_cl_cost_function(G):""" Creates the classical cost function for an instance of the maximum cut problem for a given graph ``G``. Parameters ---------- G : nx.Graph The Graph for the problem instance. Returns ------- cl_cost_function : function The classical cost function for the problem instance, which takes a dictionary of measurement results as input. """defcl_cost_function(counts):edge_list=np.array(list(G.edges()),dtype=np.uint32)counts_keys=list(counts.keys())int_list=[]ifnotisinstance(counts_keys[0],str):forc_arrayincounts_keys:integer=int("".join([cforcinc_array])[::-1],2)int_list.append(integer)else:forc_strincounts_keys:integer=int(c_str[::-1],2)int_list.append(integer)counts_array=np.array(list(counts.values()))outcome_array=np.array(int_list,dtype=np.uint32)returnmaxcut_energy(outcome_array,counts_array,edge_list)returncl_cost_function
[docs]defcreate_maxcut_cost_operator(G):r""" Creates the cost operator for an instance of the maximum cut problem for a given graph ``G``. Parameters ---------- G : nx.Graph The Graph for the problem instance. Returns ------- cost_operator : function A function receiving a :ref:`QuantumVariable` and a real parameter $\gamma$. This function performs the application of the cost operator. """defmaxcut_cost_operator(qv,gamma):iflen(G)!=len(qv):raiseException(f"Tried to call MaxCut cost Operator for graph of size {len(G)} on argument of invalid size {len(qv)}")forpairinlist(G.edges()):rzz(2*gamma,qv[pair[0]],qv[pair[1]])# cx(qv[pair[0]], qv[pair[1]])# rz(2 * gamma, qv[pair[1]])# cx(qv[pair[0]], qv[pair[1]])# barrier(qv)returnmaxcut_cost_operator
[docs]defmaxcut_problem(G):""" Creates a QAOA problem instance with appropriate phase separator, mixer, and classical cost function. Parameters ---------- G : nx.Graph The graph for the problem instance. Returns ------- :ref:`QAOAProblem` A QAOA problem instance for MaxCut for a given graph ``G``. """fromqrisp.qaoaimportQAOAProblem,RX_mixerreturnQAOAProblem(create_maxcut_cost_operator(G),RX_mixer,create_maxcut_cl_cost_function(G))
Get in touch!
If you are interested in Qrisp or high-level quantum algorithm research in general connect with us on our
Slack workspace.