-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_counting.py
More file actions
104 lines (84 loc) · 2.26 KB
/
example_counting.py
File metadata and controls
104 lines (84 loc) · 2.26 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Example of counting the number of solutions of a NAE3SAT problem using the VQCount algorithm.
"""
import cotengra as ctg
from vqcount.counting.gen import generate_satisfiable_sat_problem
from vqcount.vqcount import VQCount
# PARAMETERS
# problem parameters
problem = "nae3sat"
qubit = 6
alpha = 2
# QAOA parameters
ini_method = "tqa"
qaoa_version = "vqcount-qaoa"
depth = 3
# optimization parameters
use_mps_contract = True
use_mps_sampling = True
optimizer = "SLSQP"
backend = "numpy"
# sampling parameters
reoptimize = False
num_sol_sampled_per_step = 50
# stats parameters
compute_stats = False
# 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 = generate_satisfiable_sat_problem(problem, qubit, alpha)
print("3-SAT formula:\n", graph.cnf_ini)
print()
# COMPUTATION
QAOA = VQCount(
graph,
depth,
qaoa_version=qaoa_version,
max_bond=None,
optimizer=optimizer,
backend=backend,
)
print("Instantiation is done.")
theta_ini = QAOA.initialize_qaoa(ini_method, opt=contract_opt, use_mps=use_mps_contract)
print("Initialization is done.")
energy_opt, theta_opt = QAOA.optimize_qaoa(opt=contract_opt, use_mps=use_mps_contract)
print("Optimization is done.")
approx_num_sol, stats = QAOA.counter(
num_sol_sampled_per_step=num_sol_sampled_per_step,
reoptimize=reoptimize,
compute_stats=compute_stats,
contract_opt=contract_opt,
use_mps_contract=use_mps_contract,
sampling_opt=sampling_opt,
use_mps_sampling=use_mps_sampling,
)
print("Counting is done.\n")
print("Approximate number of solutions:", approx_num_sol)
print("Exact number of solutions:", graph.exact_num_sol)
print()
if compute_stats:
for key, value in stats.items():
print(key)
print(value)
print()