-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_maxcut_parameter_initialisation.py
More file actions
567 lines (495 loc) · 21.9 KB
/
run_maxcut_parameter_initialisation.py
File metadata and controls
567 lines (495 loc) · 21.9 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import os
import logging
logging.basicConfig(
level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s'
)
# Adjust Qiskit's logger to only display errors or critical messages
qiskit_logger = logging.getLogger('qiskit')
qiskit_logger.setLevel(logging.ERROR) # or use logging.CRITICAL
logging.info('Script started')
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import argparse
import time
import mlflow
import json
import pandas as pd
from qiskit import Aer
from qiskit.algorithms.optimizers import ADAM
from qiskit.algorithms import QAOA, NumPyMinimumEigensolver
from qiskit.utils import QuantumInstance
from qiskit_optimization.applications import Maxcut
# Custom imports
from src.haqc.generators.graph_instance import create_graphs_from_all_sources, GraphInstance
from src.haqc.exp_utils import (
str2bool,
to_snake_case,
make_temp_directory,
check_boto3_credentials,
)
from src.haqc.features.graph_features import get_graph_features
from src.haqc.generators.parameter import get_optimal_parameters
from src.haqc.solutions.solutions import compute_max_cut_brute_force, compute_distance
from src.haqc.parallel.landscape_parallel import parallel_computation
from src.haqc.initialisation.initialisation import Initialisation
from src.haqc.plot.utils import *
from haqc.initialisation.parameter_fixing import (
get_optimal_parameters_from_parameter_fixing,
)
# Theme plots to be seaborn style
plt.style.use('seaborn')
# Check that optimal parameters csv file exists
if not os.path.exists('data/optimal-parameters.csv'):
raise FileNotFoundError('Optimal parameters csv file not found.')
# Load the optimal parameters DataFrame from the csv file
df = pd.read_csv('data/optimal-parameters.csv')
def run_qaoa_script(track_mlflow, graph_type, node_size, quant_alg, n_layers=1):
if track_mlflow:
# Configure MLFlow Stuff
tracking_uri = os.environ["MLFLOW_TRACKING_URI"]
experiment_name = "QAOA-Instance-Based-Parameter-Optimization"
mlflow.set_tracking_uri(tracking_uri)
mlflow.set_experiment(experiment_name)
# if graph type is a `.pkl` file, load the graph from the file
if graph_type.endswith(".pkl"):
G = nx.read_gpickle(graph_type)
# Parse source from file name (keep everything before _target... non-inclusive of the '_target' part and after 'data/')
G.graph_type = graph_type.split('data/')[1].split('_target')[0]
# Append custom to the graph type
G.graph_type = f"{G.graph_type}"
logging.info(
f"\n{'-'*10} This run is for a custom graph with {len(G.nodes())} nodes of source {G.graph_type} {'-'*10}\n"
)
graph_instance = GraphInstance(G, G.graph_type)
if track_mlflow:
mlflow.log_param("custom_graph", True)
else:
# Generate all graph sources
G_instances = create_graphs_from_all_sources(instance_size=node_size, sources="ALL")
G_instances = [g for g in G_instances if g.graph_type == graph_type]
graph_instance = G_instances[0]
G = graph_instance.G
logging.info(
f"\n{'-'*10} This run is for a {graph_instance.graph_type} graph with {len(G.nodes())} nodes {'-'*10}\n"
)
# Show instance features
graph_features = get_graph_features(graph_instance.G)
instance_class = to_snake_case(graph_instance.graph_type)
graph_features = {str(key): val for key, val in graph_features.items()}
logging.info(f"Graph Features {json.dumps(graph_features, indent=2)}")
if track_mlflow:
mlflow.log_param("instance_class", instance_class)
mlflow.log_param("instance_size", node_size)
mlflow.log_param("quantum_algorithm", quant_alg)
mlflow.log_param("n_layers", n_layers)
mlflow.log_params(graph_features)
# Generate the adjacency matrix
adjacency_matrix = nx.adjacency_matrix(G)
max_cut = Maxcut(adjacency_matrix)
qubitOp, offset = max_cut.to_quadratic_program().to_ising()
# ### Brute Force Solution for the Max-Cut Problem
# A brute-force solution to the Max-Cut problem involves evaluating every possible partition of the graph's nodes into two sets.
# We calculate the 'cut' for each partition, which is the number of edges between the two sets. The goal is to maximize this cut.
# NOTE: This method is computationally intensive and not practical for large graphs,
# but it gives an exact solution for smaller ones.
logging.info(f"\n{'-'*10} Solving for Exact Ground State {'-'*10}\n")
# Apply the brute force solution to our graph
max_cut_partition, max_cut_value = compute_max_cut_brute_force(G)
# ### Visualizing the Brute Force Solution
with make_temp_directory() as tmp_dir:
# Define the colors for each node based on the brute force solution partition
node_colors = [
'pink' if node in max_cut_partition else 'lightblue' for node in G.nodes()
]
# Draw the graph with nodes colored based on the solution
nx.draw(
G,
with_labels=True,
node_color=node_colors,
edge_color='gray',
node_size=700,
font_size=10,
)
plt.savefig(os.path.join(tmp_dir, 'maxcut_solution_plot.png'))
if track_mlflow:
mlflow.log_artifact(os.path.join(tmp_dir, 'maxcut_solution_plot.png'))
# Clear plots
plt.clf()
logging.info(f"\n{'-'*10} Solving for Exact Ground State {'-'*10}\n")
exact_result = NumPyMinimumEigensolver().compute_minimum_eigenvalue(
operator=qubitOp
)
logging.info(f"Minimum Energy is {exact_result}")
if track_mlflow:
mlflow.log_metric("ground_state_energy", exact_result.eigenvalue.real)
logging.info(f"\n{'-'*10} Simulating Instance on Quantum{'-'*10}\n")
N_LAYERS = n_layers
# Initialize the optimizer and backend for the Quantum Algorithm
optimizer = ADAM()
backend = Aer.get_backend("aer_simulator_statevector")
quantum_instance = QuantumInstance(backend)
logging.info(f"Using Classical Optimizer {type(optimizer).__name__}")
# Initialise Quantum Algorithm list
algos_initializations = []
#### QAOA with Instance Based Initialization
# Get instance optimised paramters
optimal_params = get_optimal_parameters(instance_class, n_layers, df)
# Check if optimal parameters were found
if isinstance(optimal_params, str):
logging.warning(optimal_params)
else:
optimal_beta = np.array(optimal_params['beta'])
optimal_gamma = np.array(optimal_params['gamma'])
initial_point_optimal = np.concatenate([optimal_beta, optimal_gamma])
# Add QAOA with optimal initialization
algos_initializations.append(
('QAOA', initial_point_optimal, 'instance_class_optimsed')
)
# Add QAOA with random initialization as well
initial_point_random = np.concatenate(
[
np.random.uniform(-np.pi / 4, np.pi / 4, N_LAYERS),
np.random.uniform(-np.pi / 2, np.pi / 2, N_LAYERS),
]
)
#### QAOA with 3-regular graph initialization
# Add QAOA with 3-regular graph initialization
optimal_params = get_optimal_parameters('three_regular_graph', n_layers, df)
# Check if optimal parameters were found
if isinstance(optimal_params, str):
logging.warning(optimal_params)
else:
optimal_beta = np.array(optimal_params['beta'])
optimal_gamma = np.array(optimal_params['gamma'])
initial_point_optimal = np.concatenate([optimal_beta, optimal_gamma])
# Add QAOA with optimal initialization
algos_initializations.append(
('QAOA', initial_point_optimal, 'three_regular_graph_optimised')
)
#### QAOA with TQA (Trotterised Quantum Annealing) initialization
# Add QAOA with TQA initialization
initial_point_tqa = Initialisation().trotterized_quantum_annealing(n_layers)
algos_initializations.append(('QAOA', initial_point_tqa, 'tqa_initialisation'))
#### QAOA with random initialization
# Add QAOA with random initialization as well
algos_initializations.append(
('QAOA', initial_point_random, 'random_initialisation')
)
# Initialise empty dataframe to store results for each algorithm and init type (for evolution at each time step)
results_df = pd.DataFrame(
columns=['algo', 'init_type', 'eval_count', 'parameters', 'energy', 'std']
)
# Loop through each algorithm and initialization
for algo_name, initial_point, init_type in algos_initializations:
logging.info(f"Running {algo_name} with {init_type} Initialization")
# Print initial values from algorithm
logging.info(f"Initial point ({algo_name} - {init_type}): {initial_point}")
# Callback function to store intermediate values
intermediate_values = []
def store_intermediate_result(eval_count, parameters, mean, std):
if eval_count % 100 == 0:
logging.info(
f"{type(optimizer).__name__} iteration {eval_count} \t cost function {mean}"
)
betas = parameters[:N_LAYERS] # Extracting beta values
gammas = parameters[N_LAYERS:] # Extracting gamma values
intermediate_values.append(
{
'eval_count': eval_count,
'parameters': {'gammas': gammas, 'betas': betas},
'mean': mean,
'std': std,
}
)
# Initialize the algorithm based on its name (e.g., QAOA)
if algo_name == 'QAOA':
qaoa = QAOA(
optimizer=optimizer,
reps=N_LAYERS,
initial_point=initial_point,
callback=store_intermediate_result,
quantum_instance=quantum_instance,
include_custom=True,
)
algo_result = qaoa.compute_minimum_eigenvalue(qubitOp)
else:
# Add initialization for other algorithms here (could start off with VQE here too)
pass
# Compute performance metrics
eval_counts = [
intermediate_result['eval_count']
for intermediate_result in intermediate_values
]
most_likely_solution = max_cut.sample_most_likely(algo_result.eigenstate)
# Calculate the energy gap
energy_gap = exact_result.eigenvalue.real - algo_result.eigenvalue.real
# Convert exact result eigenstate to matrix form and get QAOA state
exact_result_vector = exact_result.eigenstate.to_matrix()
qaoa_state_vector = algo_result.eigenstate
# Compute inner product between exact result and QAOA state
inner_product = np.dot(exact_result_vector.conj(), qaoa_state_vector)
# Calculate the probability of success (adjusting for MAXCUT symmetry)
success_probability = (np.abs(inner_product) ** 2) * 2
# Calculate the approximation ratio
approximation_ratio = algo_result.eigenvalue.real / exact_result.eigenvalue.real
# Calculate Distance
distance = compute_distance(
N_LAYERS,
initial_point[:N_LAYERS],
algo_result.optimal_point[:N_LAYERS],
initial_point[N_LAYERS:],
algo_result.optimal_point[N_LAYERS:],
)
logging.info(f"Distance between initial point and optimal point: {distance}")
# Compile results into a dataframe from intermediate values
results_df = results_df.append(
pd.DataFrame(
{
'algo': [algo_name] * len(intermediate_values),
'init_type': [init_type] * len(intermediate_values),
'eval_count': [
intermediate_result['eval_count']
for intermediate_result in intermediate_values
],
'parameters': [
intermediate_result['parameters']
for intermediate_result in intermediate_values
],
'energy': [
intermediate_result['mean']
for intermediate_result in intermediate_values
],
'std': [
intermediate_result['std']
for intermediate_result in intermediate_values
],
}
)
)
# Log results to MLFlow
if track_mlflow:
mlflow.log_param(f"{algo_name}_{init_type}_initial_point", initial_point)
mlflow.log_metric(
f"{algo_name}_{init_type}_final_energy", algo_result.eigenvalue.real
)
# Convert array to string for logging
most_likely_solution = np.array2string(most_likely_solution)
mlflow.log_param(
f"{algo_name}_{init_type}_most_likely_solution", most_likely_solution
)
mlflow.log_metric(
f"{algo_name}_{init_type}_success_probability", success_probability
)
mlflow.log_metric(
f"{algo_name}_{init_type}_approximation_ratio", approximation_ratio
)
mlflow.log_metric(f"{algo_name}_{init_type}_energy_gap", energy_gap)
# log number of iterations
mlflow.log_metric(
f"{algo_name}_{init_type}_num_iterations", len(eval_counts)
)
# Log distance between initial point and optimal point
mlflow.log_metric(f"{algo_name}_{init_type}_distance", distance)
# Log each optimal parameter in MLFlow
for i, (beta, gamma) in enumerate(
zip(
algo_result.optimal_point[:N_LAYERS],
algo_result.optimal_point[N_LAYERS:],
)
):
mlflow.log_metric(f"{algo_name}_{init_type}_optimal_beta_{i}", beta)
mlflow.log_metric(f"{algo_name}_{init_type}_optimal_gamma_{i}", gamma)
logging.info(f"Results with {algo_name} {init_type} Initialization:")
logging.info(
f"Final energy for ({algo_name} {init_type})<C>: {algo_result.eigenvalue.real}"
)
logging.info(
f"Most likely solution ({algo_name} {init_type}): {most_likely_solution}"
)
logging.info(
f"Probability of success ({algo_name} {init_type}): {success_probability}"
)
logging.info(
f"Approximation ratio ({algo_name} {init_type}): {approximation_ratio}"
)
logging.info(f"Energy gap ({algo_name} {init_type}): {energy_gap}")
logging.info(
f"Number of iterations ({algo_name} {init_type}): {len(eval_counts)}"
)
logging.info(
f"Distance between initial point and optimal point ({algo_name} {init_type}): {distance}"
)
# Add column for approximation ratio
results_df['approximation_ratio'] = (
results_df['energy'] / exact_result.eigenvalue.real
)
# Save results dataframe to csv and log to mlflow (via tempdir)
with make_temp_directory() as tmp_dir:
results_df.to_csv(os.path.join(tmp_dir, 'results.csv'))
if track_mlflow:
mlflow.log_artifact(os.path.join(tmp_dir, 'results.csv'))
# Plot energy vs iterations for each algorithm and initialization on a single chart
plt.figure(figsize=(12, 8))
for algo_name, initial_point, init_type in algos_initializations:
# Filter results for specific algorithm and initialization
filtered_results_df = results_df[
(results_df['algo'] == algo_name)
& (results_df['init_type'] == init_type)
]
# Plot energy vs iterations
plt.plot(
filtered_results_df['eval_count'],
filtered_results_df['energy'],
label=f"{algo_name} {init_type}",
)
# Add dashed line for exact ground state energy
plt.axhline(
y=exact_result.eigenvalue.real,
color='r',
linestyle='--',
label='Exact Ground State Energy',
)
plt.xlabel('Iterations')
plt.ylabel('Energy')
plt.legend()
plt.savefig(os.path.join(tmp_dir, 'energy_vs_iterations.png'))
if track_mlflow:
mlflow.log_artifact(os.path.join(tmp_dir, 'energy_vs_iterations.png'))
# Clear plots
plt.clf()
# Plot approximation ratio vs iterations for each algorithm and initialization on a single chart
plt.figure(figsize=(12, 8))
for algo_name, initial_point, init_type in algos_initializations:
# Filter results for specific algorithm and initialization
filtered_results_df = results_df[
(results_df['algo'] == algo_name)
& (results_df['init_type'] == init_type)
]
# Plot approximation ratio vs iterations
plt.plot(
filtered_results_df['eval_count'],
filtered_results_df['approximation_ratio'],
label=f"{algo_name} {init_type}",
)
# Add dashed line for approximation ratio of 1
plt.axhline(y=1, color='r', linestyle='--', label='Approximation Ratio of 1')
plt.xlabel('Iterations')
plt.ylabel('Approximation Ratio')
plt.legend()
plt.savefig(os.path.join(tmp_dir, 'approximation_ratio_vs_iterations.png'))
if track_mlflow:
mlflow.log_artifact(
os.path.join(tmp_dir, 'approximation_ratio_vs_iterations.png')
)
# Logging the Parameter Landscape
logging.info(f"\n{'-'*10} Logging the Parameter Landscape {'-'*10}\n")
# Note this can take some time
logging.info(f"Logging the Parameter Landscape for {graph_type}")
# Landscape Analysis of Instance (at p=1)
qaoa = QAOA(optimizer=ADAM(), reps=1)
# Use constrained search space
beta = np.linspace(-np.pi / 2, np.pi / 2, 40)
gamma = np.linspace(-np.pi / 2, np.pi / 2, 40)
# Example usage
obj_vals = parallel_computation(beta, gamma, qubitOp, qaoa)
# ### Plotting the Parameter Landscape https://ar5iv.labs.arxiv.org/html/2209.01159#A5
# The heatmap below represents the landscape of the objective function across
# different values of \$\gamma\$ and \$\beta\$.
# The color intensity indicates the expectation value of the Hamiltonian,
# helping identify the regions where optimal parameters may lie.
with make_temp_directory() as tmp_dir:
Beta, Gamma = np.meshgrid(beta, gamma)
# Plotting
plt.figure(figsize=(10, 8))
cp = plt.contourf(
Beta, Gamma, obj_vals.T, cmap='viridis'
) # Transpose obj_vals if necessary
plt.colorbar(cp)
plt.title(f'QAOA Objective Function Landscape (p=1) for {graph_type}')
plt.xlabel('Beta')
plt.ylabel('Gamma')
# Adjust the x and y limits to show the new range
plt.xlim(-np.pi / 4, np.pi / 4)
plt.ylim(-np.pi / 2, np.pi / 2)
# Adjust the x and y labels to show the new pi values
plt.xticks([-np.pi / 2, 0, np.pi / 2], [r'$-\pi/2$', r'$0$', r'$\pi/2$'])
plt.yticks([-np.pi / 2, 0, np.pi / 2], [r'$-\pi/2$', r'$0$', r'$\pi/2$'])
# Add markers for the optimal parameters from each initialization point
for index, (algo_name, initial_point, init_type) in enumerate(
algos_initializations
):
# Get initialized parameters from the initial point
beta_init = initial_point[:N_LAYERS][0] # Only take first value for beta
gamma_init = initial_point[N_LAYERS:][0] # Only take first value for gamma
# Use a different marker and color for each initialization
plt.plot(
beta_init,
gamma_init,
color=colors[index],
marker=markers[index],
markersize=10,
label=f"{algo_name} {init_type}",
)
# Adjust legend position
plt.legend(
loc='upper center',
bbox_to_anchor=(0.5, -0.15),
ncol=len(algos_initializations),
fancybox=True,
shadow=True,
)
plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # Adjust the plot to fit the legend
plt.savefig(os.path.join(tmp_dir, 'landscape_plot.png'))
if track_mlflow:
mlflow.log_artifact(os.path.join(tmp_dir, 'landscape_plot.png'))
# Clear plots
plt.clf()
# Also save the graph instance as a .pkl file
if track_mlflow:
nx.write_gpickle(G, os.path.join(tmp_dir, 'graph_instance.pkl'))
mlflow.log_artifact(os.path.join(tmp_dir, 'graph_instance.pkl'))
if __name__ == "__main__":
check_boto3_credentials()
parser = argparse.ArgumentParser(
description="Run QAOA script with custom parameters."
)
parser.add_argument(
"-T",
"--track_mlflow",
type=str2bool,
nargs="?",
const=True,
default=False,
help="Activate MlFlow Tracking.",
)
parser.add_argument(
"-G",
"--graph_type",
type=str,
default="3-Regular Graph",
help="Type of Graph to test (based on qaoa_vrp/generators/graph_instance.py)",
)
parser.add_argument("-n", "--node_size", type=int, default=6, help="Size of Graph")
parser.add_argument(
"-q",
"--quantum_algorithm",
type=str,
default="QAOA",
help="Quantum Algorithm to test",
)
parser.add_argument(
"-l", "--n_layers", type=int, default=1, help="Number of layers for QAOA"
)
args = parser.parse_args()
print(vars(args))
start_time = time.time()
run_qaoa_script(
track_mlflow=args.track_mlflow,
graph_type=args.graph_type,
node_size=args.node_size,
quant_alg=args.quantum_algorithm,
n_layers=args.n_layers,
)
end_time = time.time()
print(f"Result found in: {end_time - start_time:.3f} seconds")