Skip to content

Commit

Permalink
Updated test for QAOAtrain_func
Browse files Browse the repository at this point in the history
  • Loading branch information
renezander90 committed Oct 17, 2024
1 parent 61eda52 commit 20d6426
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 68 deletions.
14 changes: 7 additions & 7 deletions tests/test_QAOAmaxClique.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

Expand All @@ -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


87 changes: 26 additions & 61 deletions tests/test_QAOAtrain_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 20d6426

Please sign in to comment.