-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQGMVC_parallel.py
More file actions
284 lines (236 loc) · 7.67 KB
/
QGMVC_parallel.py
File metadata and controls
284 lines (236 loc) · 7.67 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from __future__ import annotations
# =======================
# Environment safeguards
# =======================
import os
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
# =======================
# Standard imports
# =======================
import sys
import random
import numpy as np
import networkx as nx
from multiprocessing import Pool, cpu_count
from datetime import datetime
# =======================
# Optional rustworkx
# =======================
try:
import rustworkx as rx
RxGraph = rx.PyGraph
except ImportError:
rx = None
RxGraph = tuple()
# =======================
# CPLEX + project imports
# =======================
from docplex.mp.model import Model
from quantum_greedy_util import (
mixer_from_graph,
expectation_value_cost_shifted,
greedy_optimize,
greedy_optimize_seq
)
from classical_runtime_guarantee_util import (
mvc_exact_cplex,
mvc_lp_relaxation,
greedy_degree_vertex_cover,
mvc_primal_dual_weighted,
)
# ============================================================
# STEP A — one independent (n, graph) experiment
# ============================================================
def run_single_graph(args):
(n, graph_seed, case, graph_type, degree, shots, n_stat) = args
rng = np.random.default_rng(graph_seed)
# ----- graph generation -----
if graph_type == "regular":
G = nx.random_regular_graph(
degree if n % 2 == 0 else degree + 1,
n,
seed=int(graph_seed),
)
elif graph_type == "erdos":
while True:
G = nx.erdos_renyi_graph(n=n, p=degree, seed=int(graph_seed))
if nx.is_connected(G):
break
else:
raise ValueError("Unknown graph type")
# ----- weights -----
if case == "unweighted":
c = {i: 1.0 for i in G.nodes()}
else:
c = {i: random.uniform(0.40, 0.70) for i in G.nodes()}
out = {}
# ----- exact MVC -----
C_opt = mvc_exact_cplex(G, c)
opt_cost = sum(c[i] for i in C_opt)
out["optimal"] = opt_cost
out["worst_case"] = sum(c.values()) / opt_cost
out["LP"] = sum(c[i] for i in mvc_lp_relaxation(G, c)) / opt_cost
out["Dual-Primal"] = sum(c[i] for i in mvc_primal_dual_weighted(G, c)) / opt_cost
out["Greedy vertex degree"] = (
sum(c[i] for i in greedy_degree_vertex_cover(G, c)) / opt_cost
)
# ----- quantum greedy -----
qc, betas, Gn = mixer_from_graph(G, c)
C_cost = {i: c[i] for i in Gn.nodes()}
beta_bias={i: np.pi/4+(i/len(C_cost))*0.1 for i in C_cost} #depth dependent
beta_unbias = {i: 0.5 * np.pi / 2 for i in C_cost}
E_bias, E_unbias = [], []
for _ in range(n_stat):
sol = greedy_optimize(qc, betas, C_cost, beta_bias, shots=shots)
E_bias.append(
expectation_value_cost_shifted(qc, betas, C_cost, sol, shots=shots)
)
sol = greedy_optimize(qc, betas, C_cost, beta_unbias, shots=shots)
E_unbias.append(
expectation_value_cost_shifted(qc, betas, C_cost, sol, shots=shots)
)
out["Quantum greedy bias"] = np.mean(E_bias) / opt_cost
out["Quantum greedy unbias"] = np.mean(E_unbias) / opt_cost
sol_bias = greedy_optimize_seq(qc, betas, C_cost, beta_bias, shots=shots)
E = expectation_value_cost_shifted(qc, betas, C_cost, sol_bias, shots=shots)
out["Quantum greedy seq bias"] = E / opt_cost
sol_unbias = greedy_optimize_seq(qc, betas, C_cost, beta_unbias, shots=shots)
E = expectation_value_cost_shifted(qc, betas, C_cost, sol_unbias, shots=shots)
out["Quantum greedy seq unbias"] = E / opt_cost
return n, out
# ============================================================
# STEP B — parallel driver over (n, graph_seed)
# ============================================================
def run_experiment_stats_weighted(
n_values,
N_graphs=10,
n_stat=1,
shots=None,
case="weighted",
graph_type="regular",
degree=3,
seed=None,
):
methods = [
"optimal",
"worst_case",
"LP",
"Dual-Primal",
"Greedy vertex degree",
"Quantum greedy bias",
"Quantum greedy unbias",
"Quantum greedy seq bias",
"Quantum greedy seq unbias",
]
results = {n: {m: [] for m in methods} for n in n_values}
rng = np.random.default_rng(seed)
# ----- build flat task list -----
tasks = []
for n in n_values:
for _ in range(N_graphs):
graph_seed = rng.integers(1e9)
tasks.append(
(n, graph_seed, case, graph_type, degree, shots, n_stat)
)
nproc = int(os.environ.get("SLURM_CPUS_PER_TASK", cpu_count()))
print(f"Using {nproc} worker processes")
with Pool(processes=nproc) as pool:
for n, out in pool.imap_unordered(run_single_graph, tasks):
for k, v in out.items():
results[n][k].append(v)
return results
# ============================================================
# Utilities: save + summarize
# ============================================================
def save_results(
results,
n_values,
N_graphs,
n_stat,
case,
graph_type,
degree,
shots,
seed,
prefix="mvc_results",
):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
fname = (
f"{prefix}_{case}_{graph_type}_deg{degree}_"
f"nG{N_graphs}_nStat{n_stat}_shots{shots}_seed{seed}_{timestamp}.npz"
)
out_dir = os.path.dirname(os.path.abspath(__file__))
out_path = os.path.join(out_dir, fname)
np.savez_compressed(
out_path,
results=results,
n_values=np.array(n_values),
N_graphs=N_graphs,
n_stat=n_stat,
case=case,
graph_type=graph_type,
degree=degree,
shots=shots,
seed=seed,
)
print(f"Results saved to:\n{out_path}")
# ============================================================
# Main
# ============================================================
def main(n_values, N_graphs, n_stat, case, graph_type, degree, shots, seed):
results = run_experiment_stats_weighted(
n_values=n_values,
N_graphs=N_graphs,
n_stat=n_stat,
case=case,
graph_type=graph_type,
degree=degree,
shots=shots,
seed=seed,
)
save_results(
results,
n_values,
N_graphs,
n_stat,
case,
graph_type,
degree,
shots,
seed,
)
if __name__ == "__main__":
# Usage:
# python QGMVC.py N_graphs n_stat case graph_type degree shots
N_graphs = int(sys.argv[1])
n_stat = int(sys.argv[2])
case = sys.argv[3]
graph_type = sys.argv[4]
degree = float(sys.argv[5])
shots = int(sys.argv[6])
if graph_type == "regular":
degree = int(degree)
elif graph_type == "erdos":
if not (0.0 <= degree <= 1.0):
raise ValueError("Erdos p must be in [0,1]")
else:
raise ValueError("graph_type must be 'regular' or 'erdos'")
n_values = [6, 8, 10, 12, 14, 16, 18, 20]
seed = 0
print("N_graphs =", N_graphs)
print("n_stat =", n_stat)
print("case =", case)
print("graph_type =", graph_type)
print("degree =", degree)
print("shots =", shots)
main(
n_values=n_values,
N_graphs=N_graphs,
n_stat=n_stat,
case=case,
graph_type=graph_type,
degree=degree,
shots=shots,
seed=seed,
)