From 20d6426d2e3e2ade86fed093c4ac5e90d401aec1 Mon Sep 17 00:00:00 2001 From: Rene Zander Date: Thu, 17 Oct 2024 22:41:13 +0200 Subject: [PATCH] Updated test for QAOAtrain_func --- tests/test_QAOAmaxClique.py | 14 +++--- tests/test_QAOAtrain_func.py | 87 +++++++++++------------------------- 2 files changed, 33 insertions(+), 68 deletions(-) diff --git a/tests/test_QAOAmaxClique.py b/tests/test_QAOAmaxClique.py index 6a92f124..9504543d 100644 --- a/tests/test_QAOAmaxClique.py +++ b/tests/test_QAOAmaxClique.py @@ -18,7 +18,7 @@ from qrisp import QuantumVariable -from qrisp.qaoa import QAOAProblem, RZ_mixer, approximation_ratio, create_max_indep_set_cl_cost_function, create_max_indep_set_mixer, max_indep_set_init_function +from qrisp.qaoa import QAOAProblem, RZ_mixer, create_max_indep_set_cl_cost_function, create_max_indep_set_mixer, max_indep_set_init_function, approximation_ratio import networkx as nx @@ -28,11 +28,11 @@ def test_QAOAmaxClique(): G_complement = nx.complement(G) qarg = QuantumVariable(G.number_of_nodes()) - qaoa_max_indep_set = QAOAProblem(cost_operator=RZ_mixer, - mixer=create_max_indep_set_mixer(G_complement), - cl_cost_function=create_max_indep_set_cl_cost_function(G_complement), - init_function=max_indep_set_init_function) - results = qaoa_max_indep_set.run(qarg=qarg, depth=5) + qaoa_max_clique = QAOAProblem(cost_operator=RZ_mixer, + mixer=create_max_indep_set_mixer(G_complement), + cl_cost_function=create_max_indep_set_cl_cost_function(G_complement), + init_function=max_indep_set_init_function) + results = qaoa_max_clique.run(qarg=qarg, depth=5) cl_cost = create_max_indep_set_cl_cost_function(G_complement) @@ -48,6 +48,6 @@ def test_QAOAmaxClique(): optimal_sol = "".join(["1" if index in cliques[max_index] else "0" for index in range(G.number_of_nodes())]) # approximation ratio test - assert approximation_ratio(results, optimal_sol, cl_cost)>=0.5 + assert approximation_ratio(results, optimal_sol, cl_cost)>=0.5 diff --git a/tests/test_QAOAtrain_func.py b/tests/test_QAOAtrain_func.py index 303bb6fd..23617a3a 100644 --- a/tests/test_QAOAtrain_func.py +++ b/tests/test_QAOAtrain_func.py @@ -17,76 +17,41 @@ """ -from qrisp.qaoa import QAOAProblem -from qrisp.qaoa.problems.maxCliqueInfrastr import maxCliqueCostfct,maxCliqueCostOp,init_state -from qrisp.qaoa.mixers import RX_mixer from qrisp import QuantumVariable +from qrisp.qaoa import QAOAProblem, RZ_mixer, create_max_indep_set_cl_cost_function, create_max_indep_set_mixer, max_indep_set_init_function, approximation_ratio import networkx as nx -import matplotlib.pyplot as plt -import numpy as np def test_QAOAtrain_func(): - giraf = nx.erdos_renyi_graph(9,0.7) - #draw graph - #nx.draw(giraf,with_labels = True) - #plt.show() + G = nx.erdos_renyi_graph(9, 0.7, seed = 133) + G_complement = nx.complement(G) + qarg = QuantumVariable(G.number_of_nodes()) + qaoa_max_clique = QAOAProblem(cost_operator=RZ_mixer, + mixer=create_max_indep_set_mixer(G_complement), + cl_cost_function=create_max_indep_set_cl_cost_function(G_complement), + init_function=max_indep_set_init_function) + training_func = qaoa_max_clique.train_function(qarg=qarg, depth=5) - #Instanciate QAOA - #mixer gets Graph as argument - #cost function gets graph as argument - QAOAinstance = QAOAProblem(cost_operator= maxCliqueCostOp(giraf), mixer= RX_mixer, cl_cost_function=maxCliqueCostfct(giraf)) - QAOAinstance.set_init_function(init_function=init_state) - qarg2 = QuantumVariable(giraf.number_of_nodes()) - training_func = QAOAinstance.train_function( qarg=qarg2, depth=5 ) - qarg3 = QuantumVariable(giraf.number_of_nodes()) - training_func(qarg3) - theNiceQAOA = qarg3.get_measurement() + qarg2 = QuantumVariable(G.number_of_nodes()) + training_func(qarg2) + results = qarg2.get_measurement() + cl_cost = create_max_indep_set_cl_cost_function(G_complement) - import itertools - def aClcostFct(state, G): - # we assume solution is right - temp = True - energy = 0 - intlist = [s for s in range(len(list(state))) if list(state)[s] == "1"] - # get all combinations of vertices in graph that are marked as |1> by the solution - combinations = list(itertools.combinations(intlist, 2)) - # if any combination is not found in the list of G.edges(), the solution is wrong, and energy == 0 - for combination in combinations: - if combination not in G.edges(): - temp = False - # else we just add the number of marked as |1> nodes - if temp: - energy = -len(intlist) - return(energy) + cliques = list(nx.find_cliques(G)) + + max = 0 + max_index = 0 + for index, clique in enumerate(cliques): + if len(clique) > max: + max = len(clique) + max_index = index + + optimal_sol = "".join(["1" if index in cliques[max_index] else "0" for index in range(G.number_of_nodes())]) + + # approximation ratio test + assert approximation_ratio(results, optimal_sol, cl_cost)>=0.5 - - # find the nx solutions - the_it = list(nx.find_cliques(giraf)) - the_itt = [set(it) for it in the_it] - print(the_itt) - - #print the ideal solutions - #print("5 most likely Solutions") - #check if condition is fullfilled for 5 best sols - maxfive = sorted(theNiceQAOA, key=theNiceQAOA.get, reverse=True)[:5] - print(maxfive) - for name in theNiceQAOA.keys(): # for name, age in dictionary.iteritems(): (for Python 2.x) - if name in maxfive: - print(name) - print(aClcostFct(name, giraf)) - if aClcostFct(name, giraf) < 0: - intlist = [s for s in range(len(list(name))) if list(name)[s] == "1"] - intlist2 = set(intlist) - set_g = False - for seto in the_itt: - if intlist2.issubset(seto): - set_g = True - break - assert set_g - #assert set(intlist) in the_itt - #assert aClcostFct(name, giraf) <= -1