-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaxcut_optimization.py
More file actions
112 lines (72 loc) · 3.87 KB
/
Copy pathmaxcut_optimization.py
File metadata and controls
112 lines (72 loc) · 3.87 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
from qiskit.visualization import *
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as optimize
import networkx as nx
from optimizers import Optimizers
from problems import MaxCut
seed = 3
np.random.seed(seed)
#Problem properties
weighted = True
regularity = 3
number_of_qubits = 8
problem = MaxCut(number_of_qubits, regularity=regularity, weighted=weighted, seed=seed)
optimal_cost, optimal_strings = problem.optimal_cost_brute_force()
qubitOp, offset = problem.get_qubit_operator()
#Properties of the ansatz
layers = 3
maxiter = 500
eta = 0.05
single_qubit_gates = ['ry', 'rx']
entanglement_gates = ['cz']
entanglement = 'linear'
ansatz = 'HEA'
#Properties of the measurement
random_basis_layers = 1
single_qubit_gates_measurement = ['rz', 'ry']
entanglement_gates_measurement = ['cx']
entanglement = 'linear'
options = {'random_basis_layers':random_basis_layers, 'single_qubit_gates':single_qubit_gates_measurement, 'entanglement_gates':entanglement_gates_measurement, 'entanglement':entanglement}
#There are extra properties on how to calculate the expectation values (i.e. use_shots, number_of_shots) see optimizers.py
print(f'The optimal cost is {optimal_cost} and the strings corresponding to the optimal solution are {optimal_strings}')
number_of_parameters = 2*(layers+1)*number_of_qubits
reduced_parameters = int(number_of_parameters/2)
thetas = [np.random.uniform(0, 2*np.pi) for _ in range(number_of_parameters)]
optimizer_statevector = Optimizers(number_of_qubits, layers, thetas, maxiter, ansatz, single_qubit_gates, entanglement_gates, entanglement,
problem = {'type':'MaxCut', 'operator':qubitOp, 'offset':offset})
#Gradient Descent:
#vanilla_gradient_descent_exp_values = optimizer_statevector.gradient_descent(eta=eta)
#Random Natural Gradient:
random_natural_gradient_exp_values = optimizer_statevector.random_natural_gradient(eta=eta, basis='random', options=options)
#Quantum Natural Gradient:
#quantum_natural_gradient_exp_values = optimizer_statevector.quantum_natural_gradient(eta=eta)
#Stochastic-Coordinate Quantum Natural Gradient
#scqng_exp_values = optimizer_statevector.stochastic_quantum_natural_gradient(reduced_parameters, eta=eta)
optimal_exp_values = [-optimal_cost for _ in range(maxiter+1)]
#And below we can plot how the optimizers compare with each other.
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
for i in range(len(tableau20)):
r, g, b = tableau20[i]
tableau20[i] = (r / 255., g / 255., b / 255.)
plt.figure(figsize=(10, 7.5))
ax = plt.subplot(111)
x_axis = range(maxiter+1)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.xticks(fontsize=14)
plt.tick_params(axis="both", which="both", bottom="off", top="off",
labelbottom="on", left="off", right="off", labelleft="on")
optimal_exp_values = [-optimal_cost for _ in x_axis]
plt.plot(x_axis, vanilla_gradient_descent_exp_values, lw=2,linestyle='dashdot', color=tableau20[0], label='Vanilla Gradient Descent')
plt.plot(x_axis, random_natural_gradient_exp_values, lw=2,linestyle='dashdot', color=tableau20[2], label='Random Natural Gradient')
plt.plot(x_axis, quantum_natural_gradient_exp_values, lw=2,linestyle='dashdot', color=tableau20[4], label='Quantum Natural Gradient')
plt.plot(x_axis, optimal_exp_values, linestyle='-', color = tableau20[6], label='Optimal Expectation Value')
plt.xlabel('Optimization Iterations', fontsize = 12)
plt.ylabel('Expectation Value', fontsize=12)
plt.legend(fontsize=10, loc='upper right')
plt.show()