Skip to content

Commit ba8298d

Browse files
committed
further merge conflict fixes
1 parent 6377c5c commit ba8298d

File tree

2 files changed

+28
-118
lines changed

2 files changed

+28
-118
lines changed

tests/test_QAOAeTwoThrLin.py

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,37 @@
1717
"""
1818

1919

20-
from qrisp.qaoa import QAOAProblem
21-
from qrisp.qaoa.problems.eThrTwoLinInfrastr import eTwoThrLinclCostfct, eThrTwocostOp, init_state
22-
from qrisp.qaoa.mixers import RX_mixer
2320
from qrisp import QuantumVariable
24-
import numpy as np
21+
from qrisp.qaoa import QAOAProblem, RX_mixer, approximation_ratio, create_e3lin2_cl_cost_function, create_e3lin2_cost_operator
22+
import itertools
2523

2624

27-
def test_eTwoThrLinQAOA():
28-
# first three are the indices (a_1,j ; a_2,j ; a3,j), last one is b_L
29-
#clauses = [(1,3,4,5), (2,6,9,11), (1,5,8,33), (3,4,5,77), (2,4,6,7), (1,3,4,55),(1,3,6,49),(2,4,7,87), (1,3,4,55),(1,3,6,43),(2,4,7,89), (1,3,4,57),(1,3,6,44),(2,4,7,8)]
30-
for index in range(10):
31-
clauses = []
32-
for index2 in range(4):
33-
index2a = np.random.randint(0,1)
34-
if index2a % 2 == 0:
35-
int1 = np.random.randint(3,5)
36-
int2 = np.random.randint(0,3)
37-
int3 = np.random.randint(5,7)
38-
int4 = np.random.randint(30,39)
39-
clauses.append((int1,int2,int3,int4))
40-
else:
41-
int1 = np.random.randint(1,4)
42-
int2 = np.random.randint(0,1)
43-
int3 = np.random.randint(4,7)
44-
int4 = np.random.randint(31,40)
45-
clauses.append((int1,int2,int3,int4))
25+
def test_eThrLinTwoQAOA():
4626

47-
#assign cost function
48-
cl_cost_function=eTwoThrLinclCostfct(clauses)
49-
print(clauses)
27+
clauses = [[0,1,2,1],[1,2,3,0],[0,1,4,0],[0,2,4,1],[2,4,5,1],[1,3,5,1],[2,3,4,0]]
28+
num_variables = 6
29+
qarg = QuantumVariable(num_variables)
5030

51-
#assign qarg
52-
qarg = QuantumVariable(7)
31+
qaoa_e3lin2 = QAOAProblem(cost_operator=create_e3lin2_cost_operator(clauses),
32+
mixer=RX_mixer,
33+
cl_cost_function=create_e3lin2_cl_cost_function(clauses))
34+
results = qaoa_e3lin2.run(qarg=qarg, depth=5)
5335

54-
# run the qaoa
55-
QAOAinstance = QAOAProblem(cost_operator=eThrTwocostOp(clauses=clauses), mixer=RX_mixer, cl_cost_function=cl_cost_function)
56-
QAOAinstance.set_init_function(init_function=init_state)
57-
theNiceQAOA = QAOAinstance.run(qarg=qarg, depth=5, mes_kwargs = {"shots" : 100000})
36+
cl_cost = create_e3lin2_cl_cost_function(clauses)
5837

59-
import itertools
60-
def testCostFun(state):
61-
obj = 0
62-
#ljust do a minus 1 op if state is equiv to a given condition clause
63-
for index in range(len(clauses)):
64-
clause = clauses[index]
65-
sum = 0
66-
for index_aj in range(0,2):
67-
sum += int(state[clause[index_aj]])
68-
if sum == clause[3] % 2:
69-
obj -= 1
70-
return obj
71-
72-
73-
#maxfive = sorted(theNiceQAOA, key=theNiceQAOA.get, reverse=True)[:1]
74-
for name in theNiceQAOA.keys(): # for name, age in dictionary.iteritems(): (for Python 2.x)
75-
if testCostFun(name) < 0:
76-
temp = False
77-
78-
for clause in clauses:
79-
sum = 0
80-
for index_aj in range(0,2):
81-
sum += int(name[clause[index_aj]])
82-
if sum == clause[3] % 2:
83-
temp = True
84-
assert temp
38+
# find optimal solution by brute force
39+
temp_binStrings = list(itertools.product([1,0], repeat=num_variables))
40+
binStrings = ["".join(map(str, item)) for item in temp_binStrings]
41+
42+
min = 0
43+
min_index = 0
44+
for index in range(len(binStrings)):
45+
val = cl_cost({binStrings[index] : 1})
46+
if val < min:
47+
min = val
48+
min_index = index
49+
50+
optimal_sol = binStrings[min_index]
8551

52+
# approximation ratio test
53+
assert approximation_ratio(results, optimal_sol, cl_cost)>=0.5

tests/test_QAOAminSetCover.py

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,66 +36,8 @@ def test_QAOAminSetCover():
3636

3737
cl_cost = create_min_set_cover_cl_cost_function(sets, universe)
3838

39-
40-
"""
41-
42-
43-
# sets are given as list of lists
44-
examplesets = [[0,1,2,3],[1,5,6,4],[0,2,6,3,4,5],[3,4,0,1],[1,2,3,0],[1]]
45-
# full universe is given as a tuple
46-
sol = (0,1,2,3,4,5,6)
47-
48-
49-
# assign operators
50-
cost_fun = minSetCoverclCostfct(sets=examplesets,universe = sol)
51-
mixerOp = RZ_mixer
52-
costOp = minSetCoverCostOp(sets=examplesets, universe=sol)
53-
54-
#initialize variable
55-
qarg = QuantumVariable(len(examplesets))
56-
57-
#+run qaoa
58-
QAOAinstance = QAOAProblem(cost_operator=costOp ,mixer= mixerOp, cl_cost_function=cost_fun)
59-
QAOAinstance.set_init_function(init_function=init_state)
60-
InitTest = QAOAinstance.run(qarg=qarg, depth=5, mes_kwargs = {"shots" : 100000})
61-
62-
# create example cost_func
63-
def testCostFun(state,universe):
64-
obj = 0
65-
#literally just do a minus 1 op if state is equiv to a given condition
66-
intlist = [s for s in range(len(list(state))) if list(state)[s] == "1"]
67-
sol_sets = [examplesets[index] for index in intlist]
68-
res = ()
69-
for seto in sol_sets:
70-
res = tuple(set(res+ tuple(seto)))
71-
if res == universe:
72-
obj += len(intlist)
73-
74-
return obj
75-
76-
# test if cost_func functions for all items .
77-
for state in InitTest.keys():
78-
if testCostFun(state=state, universe = sol) > 0:
79-
intlist = [s for s in range(len(list(state))) if list(state)[s] == "1"]
80-
sol_sets = [examplesets[index] for index in intlist]
81-
res = ()
82-
for seto in sol_sets:
83-
res = tuple(set(res+ tuple(seto)))
84-
assert sol == res
85-
86-
87-
88-
#print the ideal solutions
89-
#print("5 most likely Solutions")
90-
maxfive = sorted(InitTest, key=InitTest.get, reverse=True)[:5]
91-
for name in InitTest.keys(): # for name, age in dictionary.iteritems(): (for Python 2.x)
92-
if name in maxfive:
93-
print(name)
94-
print(testCostFun(name, universe=sol))
95-
96-
#find ideal solution by brute force
97-
temp_binStrings = list(itertools.product([1,0], repeat=len(examplesets)))
98-
39+
# find optimal solution by brute force
40+
temp_binStrings = list(itertools.product([1,0], repeat=len(sets)))
9941
binStrings = ["".join(map(str, item)) for item in temp_binStrings]
10042

10143
min = len(sets)

0 commit comments

Comments
 (0)