-
Notifications
You must be signed in to change notification settings - Fork 3
/
qite.py
349 lines (309 loc) · 11.5 KB
/
qite.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
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
import cirq
import tensorflow as tf
import tensorflow_probability as tfp
import tensorflow_quantum as tfq
import numpy as np
from typing import Any, Dict, List, Optional, Tuple
from tqdm.autonotebook import tqdm, trange
from collections import defaultdict
from utils import (
circuit_to_state,
quantum_relative_entropy,
fidelity,
trace_distance,
get_two_variable_shift_matrix,
)
tf.get_logger().setLevel("ERROR")
class QITE:
def __init__(
self,
n_extra_timesteps: int = 0,
regularization: float = 1e-4,
spsa: Optional[int] = None,
spsa_perturbation: Optional[float] = 2 * np.pi / 100,
verbose: int = 1,
backend=None,
differentiator=None,
) -> None:
self.n_extra_timesteps = n_extra_timesteps
self.regularization = regularization
self.param_shift = 0.5
self.spsa = spsa
self.spsa_perturbation = spsa_perturbation
self.verbose = verbose
self.backend = backend
self.differentiator = differentiator
if spsa is not None and spsa > 0:
self.bernoulli = tfp.distributions.Bernoulli(
probs=0.5, dtype=tf.float32
)
def run(
self,
circuit: cirq.Circuit,
hamiltonian: cirq.PauliSum,
symbol_names: tf.Tensor,
initial_symbol_values: tf.Tensor,
evolution_time: float,
solution: Optional[tf.Tensor],
n_timesteps: int = 20,
) -> Tuple[tf.Tensor, tf.Tensor, Dict[str, List[Any]]]:
self.circuit = circuit
self.hamiltonian = hamiltonian
self.metrics = defaultdict(list)
self.qubits = sorted(circuit.all_qubits())
self.problem_qubits = hamiltonian.qubits
self.n_timesteps = n_timesteps
# Calculate the step size from evolution time and number of time steps
# In order to avoid going over the correct value +1 is used for mimicing as if there would be an extra timestep that is not run
step_size = tf.constant(evolution_time / (self.n_timesteps + 1))
# Initialize values
symbol_values = initial_symbol_values
state = circuit_to_state(
circuit,
symbol_names,
symbol_values,
list(range(len(self.problem_qubits))),
backend=self.backend,
)
# Avoid calculating shifts for each time step in exact mode
if self.spsa is None:
self.two_variable_shifts = get_two_variable_shift_matrix(
symbol_values.shape[0]
)
# Use tqdm if verbose
iterator = (
trange(
1,
self.n_timesteps + self.n_extra_timesteps + 1,
desc="QITE",
leave=False,
)
if self.verbose > 0
else range(1, self.n_timesteps + self.n_extra_timesteps + 1)
)
# Calculate initial metrics
self.metrics["time_step"].append(0)
self.calculate_initial_energy(symbol_names, symbol_values)
# If true solution is given calculate the initial metrics
if solution is not None:
if tf.math.reduce_any(
tf.math.logical_not(tf.math.is_finite(tf.math.real(solution)))
):
raise RuntimeError(f"Invalid solution")
self.calculate_metrics(state, solution)
# Print metrics if verbose
if self.verbose > 0:
self.print_step_metrics()
# Iterate through the time stpes
for time_step in iterator:
self.metrics["time_step"].append(time_step)
# Evolve symbol values
symbol_values -= step_size * self.step(symbol_names, symbol_values)
# Build density matrix from circuit with current values
state = circuit_to_state(
circuit,
symbol_names,
symbol_values,
list(range(len(self.problem_qubits))),
backend=self.backend,
)
# If true solution is given calculate the metrics
if solution is not None:
self.calculate_metrics(state, solution)
# Print metrics if verbose
if self.verbose > 0:
self.print_step_metrics()
# Return trained symbol values, final density matrix and metrics
return symbol_values, state, self.metrics
def step(
self,
symbol_names: tf.Tensor,
symbol_values: tf.Tensor,
tries: int = 10,
) -> tf.Tensor:
gradient = self.evaluate_circuit_gradient(symbol_names, symbol_values)
# In case information matrix is not invertable, looping and trying again
attempt = 0
while attempt < tries:
attempt += 1
if self.spsa is not None:
information_matrix = self.evaluate_information_matrix_spsa(
symbol_names, symbol_values
)
else:
information_matrix = self.evaluate_information_matrix_exact(
symbol_names, symbol_values
)
im_T = tf.transpose(information_matrix)
im_im = im_T @ information_matrix
im_g = im_T @ tf.expand_dims(gradient, 1)
regularizer = (
tf.eye(information_matrix.shape[0]) * self.regularization
)
try:
result = tf.linalg.solve(
(im_im + regularizer) / (1 + self.regularization), im_g
)
break
except:
tqdm.write("Failed to solve matrix.")
continue
else:
raise RuntimeError(f"Could not solve matrix in {tries} tries.")
return tf.squeeze(result)
def evaluate_circuit_gradient(
self, symbol_names: tf.Tensor, symbol_values: tf.Tensor
) -> tf.Tensor:
op = tfq.layers.Expectation(
backend=self.backend, differentiator=self.differentiator
)
with tf.GradientTape() as g:
g.watch(symbol_values)
energy = op(
self.circuit,
operators=self.hamiltonian,
symbol_names=symbol_names,
symbol_values=tf.expand_dims(symbol_values, 0),
)
gradient = g.gradient(energy, symbol_values)
self.metrics["energy"].append(tf.squeeze(energy).numpy().item())
return gradient
# https://arxiv.org/pdf/2008.06517.pdf
def evaluate_information_matrix_exact(
self, symbol_names: tf.Tensor, symbol_values: tf.Tensor
) -> tf.Tensor:
# Calculate inner products
product = self.calculate_overlap(
symbol_names,
symbol_values,
symbol_values + self.two_variable_shifts,
)
# Reshape according to parameter shifts
product = tf.reshape(
product, [4, symbol_values.shape[0], symbol_values.shape[0]]
)
# Sum over the differences
information_matrix = (
-tf.cast(
(product[0, :] + product[1, :] - product[2, :] - product[3, :]),
tf.float32,
)
* self.param_shift
)
# self.metrics["norm"].append(tf.norm(information_matrix).numpy().item())
return information_matrix
# https://arxiv.org/pdf/2103.09232.pdf
def evaluate_information_matrix_spsa(
self, symbol_names: tf.Tensor, symbol_values: tf.Tensor
) -> tf.Tensor:
# Sample two random directions [-1, 1]^s
directions = (
self.bernoulli.sample([self.spsa, 2, symbol_values.shape[0]]) * 2
- 1
)
# Build basis from the directions
basis = (
tf.matmul(
tf.transpose(directions, [0, 2, 1]),
tf.transpose(directions[:, ::-1], [0, 1, 2]),
)
/ 2
)
# Perturb the symbol values according to 2-spsa
symbol_values_perturbations = (
tf.concat(
[
directions[:, 0, :] + directions[:, 1, :],
-directions[:, 0, :],
-directions[:, 0, :] + directions[:, 1, :],
directions[:, 0, :],
],
0,
)
* self.spsa_perturbation
)
# Calculate inner products
product = self.calculate_overlap(
symbol_names,
symbol_values,
symbol_values + symbol_values_perturbations,
)
# Reshape according to perturbations shifts
product = tf.reshape(product, [4, self.spsa, -1])
# Sum over the differences
difference = (
-2
* tf.cast(
(product[0, :] - product[1, :] - product[2, :] + product[3, :]),
tf.float32,
)
/ (self.spsa_perturbation ** 2)
)
information_matrix = basis * tf.expand_dims(difference, 1)
# Reduce by mean over all SPSA estimates
information_matrix = tf.reduce_mean(information_matrix, 0)
information_matrix = tf.matrix_square_root(
tf.matmul(information_matrix, information_matrix)
)
# self.metrics["norm"].append(tf.norm(information_matrix).numpy().item())
return information_matrix
def calculate_overlap(
self,
symbol_names: tf.Tensor,
symbol_values: tf.Tensor,
symbol_values_overlap: tf.Tensor,
) -> tf.Tensor:
op = tfq.layers.State(backend=self.backend)
fixed_state = op(
self.circuit,
symbol_names=symbol_names,
symbol_values=tf.expand_dims(symbol_values, 0),
)
diff_states = op(
self.circuit,
symbol_names=symbol_names,
symbol_values=symbol_values_overlap,
)
# Calculate inner products
product = diff_states.to_tensor() @ tf.transpose(
fixed_state.to_tensor()
)
return tf.square(tf.abs(product))
def calculate_metrics(self, state: tf.Tensor, solution: tf.Tensor):
fidel = fidelity(solution, state)
relative_entropy = quantum_relative_entropy(solution, state)
trace_dist = trace_distance(solution, state)
self.metrics["fidelity"].append(fidel.numpy().item())
self.metrics["quantum_relative_entropy"].append(
relative_entropy.numpy().item()
)
self.metrics["trace_distance"].append(trace_dist.numpy().item())
def calculate_initial_energy(
self, symbol_names: tf.Tensor, symbol_values: tf.Tensor
):
op = tfq.layers.Expectation(
backend=self.backend, differentiator=self.differentiator
)
energy = op(
self.circuit,
operators=self.hamiltonian,
symbol_names=symbol_names,
symbol_values=tf.expand_dims(symbol_values, 0),
)
self.metrics["energy"].append(tf.squeeze(energy).numpy().item())
def print_step_metrics(self):
current_time_step = self.metrics["time_step"][-1]
list_metrics = [
f"{key} = {values[-1]:.3f}"
for key, values in filter(
lambda k: k[0] != "time_step", self.metrics.items()
)
]
string_metrics = ", ".join(list_metrics)
entry = f"QITE timestep {current_time_step:3}: [{string_metrics}]"
if self.verbose == 1 and current_time_step == self.n_timesteps:
tqdm.write(entry)
elif self.verbose == 2 and current_time_step % 5 == 0:
tqdm.write(entry)
elif self.verbose == 3:
tqdm.write(entry)