/
parameter_shift.py
341 lines (293 loc) · 15.9 KB
/
parameter_shift.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
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Compute analytic gradients by using general parameter-shift rule. """
import tensorflow as tf
from tensorflow_quantum.python.differentiators import differentiator
from tensorflow_quantum.python.differentiators import parameter_shift_util
class ParameterShift(differentiator.Differentiator):
"""Calculate the general version of parameter-shift rule based gradients.
This ParameterShift is the gradient estimator of the following paper:
[arXiv:1905.13311](https://arxiv.org/abs/1905.13311), Gavin E. Crooks.
This ParameterShift is used for any programs with parameterized gates.
It internally decomposes any programs into array of gates with at most
two distinct eigenvalues.
>>> non_diff_op = tfq.get_expectation_op()
>>> linear_differentiator = tfq.differentiators.ParameterShift()
>>> # Get an expectation op, with this differentiator attached.
>>> op = linear_differentiator.generate_differentiable_op(
... analytic_op=non_diff_op
... )
>>> qubit = cirq.GridQubit(0, 0)
>>> circuit = tfq.convert_to_tensor([
... cirq.Circuit(cirq.X(qubit) ** sympy.Symbol('alpha'))
... ])
>>> psums = tfq.convert_to_tensor([[cirq.Z(qubit)]])
>>> symbol_values_array = np.array([[0.123]], dtype=np.float32)
>>> # Calculate tfq gradient.
>>> symbol_values_tensor = tf.convert_to_tensor(symbol_values_array)
>>> with tf.GradientTape() as g:
... g.watch(symbol_values_tensor)
... expectations = op(circuit, ['alpha'], symbol_values_tensor, psums)
>>> # This value is now computed via the ParameterShift rule.
>>> # https://arxiv.org/abs/1905.13311
>>> grads = g.gradient(expectations, symbol_values_tensor)
>>> grads
tf.Tensor([[-1.1839752]], shape=(1, 1), dtype=float32)
"""
@tf.function
def differentiate_analytic(self, programs, symbol_names, symbol_values,
pauli_sums, forward_pass_vals, grad):
"""Calculate the gradient.
The gradient calculations follows the following steps:
1. Compute the decomposition of the incoming circuits so that we have
their generator information (done using cirq in a tf.py_function)
2. Use formula (31) from paper inside of TensorFlow to calculate
gradients from all the decomposed circuits.
3. Sum up terms and reshape for the total gradient that is compatible
with TensorFlow.
**CAUTION**
Analytic gradient measurements based on this ParameterShift generally
run at least K(=2) times SLOWER than the original circuit.
On top of it, since all parameters of gates are shifted individually,
the time complexity is linear in the number of parameterized gates L.
So, you will see O(KL) slower time & space complexity than the original
forward pass measurements.
Args:
programs: `tf.Tensor` of strings with shape [batch_size] containing
the string representations of the circuits to be executed.
symbol_names: `tf.Tensor` of strings with shape [n_params], which
is used to specify the order in which the values in
`symbol_values` should be placed inside of the circuits in
`programs`.
symbol_values: `tf.Tensor` of real numbers with shape
[batch_size, n_params] specifying parameter values to resolve
into the circuits specified by programs, following the ordering
dictated by `symbol_names`.
pauli_sums: `tf.Tensor` of strings with shape [batch_size, n_ops]
containing the string representation of the operators that will
be used on all of the circuits in the expectation calculations.
forward_pass_vals: `tf.Tensor` of real numbers with shape
[batch_size, n_ops] containing the output of the forward pass
through the op you are differentiating.
grad: `tf.Tensor` of real numbers with shape [batch_size, n_ops]
representing the gradient backpropagated to the output of the
op you are differentiating through.
Returns:
Backward gradient values for each program & each pauli sum. It has
the shape of [batch_size, n_symbols].
"""
# these get used a lot
n_symbols = tf.gather(tf.shape(symbol_names), 0)
n_programs = tf.gather(tf.shape(programs), 0)
n_ops = tf.gather(tf.shape(pauli_sums), 1)
# Assume cirq.decompose() generates gates with at most two distinct
# eigenvalues, which results in two parameter shifts.
n_shifts = 2
# STEP 1: Generate required inputs for executor
# Deserialize programs and parse the whole parameterized gates
# new_programs has [n_symbols, n_param_gates, n_shifts, n_programs].
# These new_programs has programs that parameter-shift rule is applied,
# so those programs has
(new_programs, weights, shifts,
n_param_gates) = parameter_shift_util.parse_programs(
programs, symbol_names, symbol_values, n_symbols)
# Reshape & transpose new_programs, weights and shifts to fit into
# the input format of tensorflow_quantum simulator.
# [n_symbols, n_param_gates, n_shifts, n_programs]
new_programs = tf.transpose(new_programs, [0, 2, 3, 1])
weights = tf.transpose(weights, [0, 2, 3, 1])
shifts = tf.transpose(shifts, [0, 2, 3, 1])
# reshape everything to fit into expectation op correctly
total_programs = n_programs * n_shifts * n_param_gates * n_symbols
# tile up and then reshape to order programs correctly
flat_programs = tf.reshape(new_programs, [total_programs])
flat_shifts = tf.reshape(shifts, [total_programs])
# tile up and then reshape to order ops correctly
n_tile = n_shifts * n_param_gates * n_symbols
flat_perturbations = tf.concat([
tf.reshape(
tf.tile(tf.expand_dims(symbol_values, 0),
tf.stack([n_tile, 1, 1])), [total_programs, n_symbols]),
tf.expand_dims(flat_shifts, axis=1)
],
axis=1)
flat_ops = tf.reshape(
tf.tile(tf.expand_dims(pauli_sums, 0), tf.stack([n_tile, 1, 1])),
[total_programs, n_ops])
# Append impurity symbol into symbol name
new_symbol_names = tf.concat([
symbol_names,
tf.expand_dims(tf.constant(
parameter_shift_util._PARAMETER_IMPURITY_NAME),
axis=0)
],
axis=0)
# STEP 2: calculate the required expectation values
expectations = self.expectation_op(flat_programs, new_symbol_names,
flat_perturbations, flat_ops)
# STEP 3: generate gradients according to the results
# we know the rows are grouped according to which parameter
# was perturbed, so reshape to reflect that
grouped_expectations = tf.reshape(
expectations,
[n_symbols, n_shifts * n_programs * n_param_gates, -1])
# now we can calculate the partial of the circuit output with
# respect to each perturbed parameter
def rearrange_expectations(grouped):
def split_vertically(i):
return tf.slice(grouped, [i * n_programs, 0],
[n_programs, n_ops])
return tf.map_fn(split_vertically,
tf.range(n_param_gates * n_shifts),
dtype=tf.float32)
# reshape so that expectations calculated on different programs are
# separated by a dimension
rearranged_expectations = tf.map_fn(rearrange_expectations,
grouped_expectations)
# now we will calculate all of the partial derivatives
partials = tf.einsum(
'spco,spc->sco', rearranged_expectations,
tf.cast(
tf.reshape(weights,
[n_symbols, n_param_gates * n_shifts, n_programs]),
rearranged_expectations.dtype))
# now apply the chain rule
return tf.einsum('sco,co -> cs', partials, grad)
@tf.function
def differentiate_sampled(self, programs, symbol_names, symbol_values,
pauli_sums, num_samples, forward_pass_vals, grad):
"""Calculate the gradient.
The gradient calculations follows the following steps:
1. Compute the decomposition of the incoming circuits so that we have
their generator information (done using cirq in a tf.py_function)
2. Use formula (31) from paper inside of TensorFlow to calculate
gradients from all the decomposed circuits.
3. Sum up terms and reshape for the total gradient that is compatible
with TensorFlow.
**CAUTION**
Analytic gradient measurements based on this ParameterShift generally
run at least K(=2) times SLOW than the original circuit.
On top of it, since all parameters of gates are shifted individually,
the time complexity is linear in the number of parameterized gates L.
So, you will see O(KL) slower time & space complexity than the original
forward pass measurements.
Args:
programs: `tf.Tensor` of strings with shape [batch_size] containing
the string representations of the circuits to be executed.
symbol_names: `tf.Tensor` of strings with shape [n_params], which
is used to specify the order in which the values in
`symbol_values` should be placed inside of the circuits in
`programs`.
symbol_values: `tf.Tensor` of real numbers with shape
[batch_size, n_params] specifying parameter values to resolve
into the circuits specified by programs, following the ordering
dictated by `symbol_names`.
pauli_sums: `tf.Tensor` of strings with shape [batch_size, n_ops]
containing the string representation of the operators that will
be used on all of the circuits in the expectation calculations.
num_samples: `tf.Tensor` of positiver integers indicating the number
of samples used per term to calculate the expectation value
in the forward pass.
forward_pass_vals: `tf.Tensor` of real numbers with shape
[batch_size, n_ops] containing the output of the forward pass
through the op you are differentiating.
grad: `tf.Tensor` of real numbers with shape [batch_size, n_ops]
representing the gradient backpropagated to the output of the
op you are differentiating through.
Returns:
Backward gradient values for each program & each pauli sum. It has
the shape of [batch_size, n_symbols].
"""
# these get used a lot
n_symbols = tf.gather(tf.shape(symbol_names), 0)
n_programs = tf.gather(tf.shape(programs), 0)
n_ops = tf.gather(tf.shape(pauli_sums), 1)
# Assume cirq.decompose() generates gates with at most two distinct
# eigenvalues, which results in two parameter shifts.
n_shifts = 2
# STEP 1: Generate required inputs for executor
# Deserialize programs and parse the whole parameterized gates
# new_programs has [n_symbols, n_param_gates, n_shifts, n_programs].
# These new_programs has programs that parameter-shift rule is applied,
# so those programs has
(new_programs, weights, shifts,
n_param_gates) = parameter_shift_util.parse_programs(
programs, symbol_names, symbol_values, n_symbols)
# Reshape & transpose new_programs, weights and shifts to fit into
# the input format of tensorflow_quantum simulator.
# [n_symbols, n_param_gates, n_shifts, n_programs]
new_programs = tf.transpose(new_programs, [0, 2, 3, 1])
weights = tf.transpose(weights, [0, 2, 3, 1])
shifts = tf.transpose(shifts, [0, 2, 3, 1])
# reshape everything to fit into expectation op correctly
total_programs = n_programs * n_shifts * n_param_gates * n_symbols
# tile up and then reshape to order programs correctly
flat_programs = tf.reshape(new_programs, [total_programs])
flat_shifts = tf.reshape(shifts, [total_programs])
# tile up and then reshape to order ops correctly
n_tile = n_shifts * n_param_gates * n_symbols
flat_perturbations = tf.concat([
tf.reshape(
tf.tile(tf.expand_dims(symbol_values, 0),
tf.stack([n_tile, 1, 1])), [total_programs, n_symbols]),
tf.expand_dims(flat_shifts, axis=1)
],
axis=1)
flat_ops = tf.reshape(
tf.tile(tf.expand_dims(pauli_sums, 0), tf.stack([n_tile, 1, 1])),
[total_programs, n_ops])
flat_num_samples = tf.reshape(
tf.tile(tf.expand_dims(num_samples, 0), tf.stack([n_tile, 1, 1])),
[total_programs, n_ops])
# Append impurity symbol into symbol name
new_symbol_names = tf.concat([
symbol_names,
tf.expand_dims(tf.constant(
parameter_shift_util._PARAMETER_IMPURITY_NAME),
axis=0)
],
axis=0)
# STEP 2: calculate the required expectation values
expectations = self.expectation_op(flat_programs, new_symbol_names,
flat_perturbations, flat_ops,
flat_num_samples)
# STEP 3: generate gradients according to the results
# we know the rows are grouped according to which parameter
# was perturbed, so reshape to reflect that
grouped_expectations = tf.reshape(
expectations,
[n_symbols, n_shifts * n_programs * n_param_gates, -1])
# now we can calculate the partial of the circuit output with
# respect to each perturbed parameter
def rearrange_expectations(grouped):
def split_vertically(i):
return tf.slice(grouped, [i * n_programs, 0],
[n_programs, n_ops])
return tf.map_fn(split_vertically,
tf.range(n_param_gates * n_shifts),
dtype=tf.float32)
# reshape so that expectations calculated on different programs are
# separated by a dimension
rearranged_expectations = tf.map_fn(rearrange_expectations,
grouped_expectations)
# now we will calculate all of the partial derivatives
partials = tf.einsum(
'spco,spc->sco', rearranged_expectations,
tf.cast(
tf.reshape(weights,
[n_symbols, n_param_gates * n_shifts, n_programs]),
rearranged_expectations.dtype))
# now apply the chain rule
return tf.einsum('sco,co -> cs', partials, grad)