17
17
"""
18
18
19
19
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
23
20
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
25
23
26
24
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 ():
46
26
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 )
50
30
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 )
53
35
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 )
58
37
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 ]
85
51
52
+ # approximation ratio test
53
+ assert approximation_ratio (results , optimal_sol , cl_cost )>= 0.5
0 commit comments