-
Notifications
You must be signed in to change notification settings - Fork 209
/
max_cut_backprop.py
209 lines (174 loc) · 6.38 KB
/
max_cut_backprop.py
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
"""
MIT License
Copyright (c) 2020-present TorchQuantum Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import torch
import torchquantum as tq
import random
import numpy as np
import argparse
from torchquantum.functional import mat_dict
from torchquantum.measurement import expval_joint_analytical
seed = 0
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
class MAXCUT(tq.QuantumModule):
"""computes the optimal cut for a given graph.
outputs: the most probable bitstring decides the set {0 or 1} each
node belongs to.
"""
def __init__(self, n_wires, input_graph, n_layers):
super().__init__()
self.n_wires = n_wires
self.input_graph = input_graph # list of edges
self.n_layers = n_layers
self.betas = torch.nn.Parameter(0.01 * torch.rand(self.n_layers))
self.gammas = torch.nn.Parameter(0.01 * torch.rand(self.n_layers))
def mixer(self, qdev, beta):
"""
Apply the single rotation and entangling layer of the QAOA ansatz.
mixer = exp(-i * beta * sigma_x)
"""
for wire in range(self.n_wires):
qdev.rx(
wires=wire,
params=beta.unsqueeze(0),
) # type: ignore
def entangler(self, qdev, gamma):
"""
Apply the single rotation and entangling layer of the QAOA ansatz.
entangler = exp(-i * gamma * (1 - sigma_z * sigma_z)/2)
"""
for edge in self.input_graph:
qdev.cx(
[edge[0], edge[1]],
) # type: ignore
qdev.rz(
wires=edge[1],
params=gamma.unsqueeze(0),
) # type: ignore
qdev.cx(
[edge[0], edge[1]],
) # type: ignore
def edge_to_PauliString(self, edge):
# construct pauli string
pauli_string = ""
for wire in range(self.n_wires):
if wire in edge:
pauli_string += "Z"
else:
pauli_string += "I"
return pauli_string
def circuit(self, qdev):
"""
execute the quantum circuit
"""
# print(self.betas, self.gammas)
for wire in range(self.n_wires):
qdev.h(
wires=wire,
) # type: ignore
for i in range(self.n_layers):
self.mixer(qdev, self.betas[i])
self.entangler(qdev, self.gammas[i])
def forward(self, measure_all=False):
"""
Apply the QAOA ansatz and only measure the edge qubit on z-basis.
Args:
if edge is None
"""
qdev = tq.QuantumDevice(
n_wires=self.n_wires, device=self.betas.device, record_op=False
)
self.circuit(qdev)
# turn on the record_op above to print the circuit
# print(op_history2qiskit(self.n_wires, qdev.op_history))
# print(tq.measure(qdev, n_shots=1024))
# compute the expectation value
# print(qdev.get_states_1d())
if measure_all is False:
expVal = 0
for edge in self.input_graph:
pauli_string = self.edge_to_PauliString(edge)
expv = expval_joint_analytical(qdev, observable=pauli_string)
expVal += 0.5 * expv
# print(pauli_string, expv)
# print(expVal)
return expVal
else:
return tq.measure(qdev, n_shots=1024, draw_id=0)
def backprop_optimize(model, n_steps=100, lr=0.1):
"""
Optimize the QAOA ansatz over the parameters gamma and beta
Args:
betas (np.array): A list of beta parameters.
gammas (np.array): A list of gamma parameters.
n_steps (int): The number of steps to optimize, defaults to 10.
lr (float): The learning rate, defaults to 0.1.
"""
# measure all edges in the input_graph
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
print(
"The initial parameters are betas = {} and gammas = {}".format(
*model.parameters()
)
)
# optimize the parameters and return the optimal values
for step in range(n_steps):
optimizer.zero_grad()
loss = model()
loss.backward()
optimizer.step()
if step % 2 == 0:
print("Step: {}, Cost Objective: {}".format(step, loss.item()))
print(
"The optimal parameters are betas = {} and gammas = {}".format(
*model.parameters()
)
)
return model(measure_all=True)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--steps", type=int, default=300, help="number of steps"
)
args = parser.parse_args()
# create a input_graph
input_graph = [(0, 1), (0, 3), (1, 2), (2, 3)]
n_wires = 4
n_layers = 3
model = MAXCUT(n_wires=n_wires, input_graph=input_graph, n_layers=n_layers)
# model.to("cuda")
# model.to(torch.device("cuda"))
# circ = tq2qiskit(tq.QuantumDevice(n_wires=4), model)
# print(circ)
# print("The circuit is", circ.draw(output="mpl"))
# circ.draw(output="mpl")
# use backprop
backprop_optimize(model, n_steps=args.steps, lr=0.01)
# use parameter shift rule
# param_shift_optimize(model, n_steps=500, step_size=100000)
"""
Notes:
1. input_graph = [(0, 1), (3, 0), (1, 2), (2, 3)], mixer 1st & entangler 2nd, n_layers >= 2, answer is correct.
"""
if __name__ == "__main__":
# import pdb
# pdb.set_trace()
main()