-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_optimization.py
More file actions
79 lines (64 loc) · 1.63 KB
/
example_optimization.py
File metadata and controls
79 lines (64 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Example of solving the optimization version of the NAE3SAT problem using QAOA.
"""
import cotengra as ctg
from vqcount.counting.problem import PositiveNaeThreeSatGraph
from vqcount.qaoa.launcher import QAOALauncher
# PARAMETERS
# problem parameters
problem = "nae3sat"
qubit = 6
alpha = 1
# QAOA parameters
ini_method = "tqa"
qaoa_version = "qaoa"
depth = 3
# optimization parameters
use_mps_contract = True
use_mps_sampling = True
optimizer = "SLSQP"
backend = "numpy"
# sampling parameters
num_samples = 1000
# COTENGRA PARAMETERS
# contraction parameters
contract_kwargs = {
"minimize": "size",
"methods": ["greedy", "kahypar"],
"reconf_opts": {},
"optlib": "random",
"max_repeats": 64,
"parallel": True,
"max_time": "rate:1e6",
}
# sampling parameters
sampling_kwargs = {
"minimize": "flops",
"methods": ["greedy"],
"reconf_opts": {},
"optlib": "random",
"max_repeats": 32,
"parallel": True,
"max_time": "rate:1e6",
}
contract_opt = ctg.ReusableHyperOptimizer(**contract_kwargs)
sampling_opt = ctg.ReusableHyperOptimizer(**sampling_kwargs)
# GENERATION
graph = PositiveNaeThreeSatGraph(qubit, alpha * qubit, int(alpha * 3), 3)
print("3-SAT formula:\n", graph.cnf_ini)
print()
# COMPUTATION
QAOA = QAOALauncher(
graph,
depth,
qaoa_version=qaoa_version,
optimizer=optimizer,
backend=backend,
)
theta_ini = QAOA.initialize_qaoa(
ini_method=ini_method, opt=contract_opt, use_mps=use_mps_contract
)
print("Initialization is done!")
energy, theta = QAOA.optimize_qaoa(opt=contract_opt, use_mps=use_mps_contract)
print("Optimization is done!\n")
print("Energy:", energy)