23
23
[OptimizableFunc , Optional [OptimizerInput ], Optional [OptimizerOptions ]],
24
24
tuple [float , OptimizerInput ],
25
25
]
26
+ OptimizerCallback = Optional [
27
+ Union [
28
+ Callable [[OptimizeResult ], None ],
29
+ Callable [
30
+ [Union [list [float ], npt .NDArray [np .float32 ], tuple [float , ...]]], None
31
+ ],
32
+ ]
33
+ ]
34
+
26
35
27
36
# TODO: all those functions with almost or exactly the same signature look like
28
37
# a code smell to me.
@@ -47,6 +56,7 @@ def minimize(
47
56
init_params : Optional [OptimizerInput ] = None ,
48
57
nb_params : Optional [int ] = None ,
49
58
optimizer_options : Optional [dict [str , Any ]] = None ,
59
+ callback : Optional [OptimizerCallback ] = None ,
50
60
) -> tuple [float , OptimizerInput ]:
51
61
"""This function runs an optimization on the parameters of the circuit, in order to
52
62
minimize the measured expectation value of observables associated with the given circuit.
@@ -71,6 +81,7 @@ def minimize(
71
81
optimizer_options: Options used to configure the VQA optimizer (maximum
72
82
iterations, convergence threshold, etc...). These options are passed
73
83
as is to the minimizer.
84
+ callback: A callable called after each iteration.
74
85
75
86
Returns:
76
87
The optimal value reached and the parameters corresponding to this value.
@@ -117,11 +128,25 @@ def minimize(
117
128
if device is None :
118
129
raise ValueError ("A device is needed to optimize a circuit" )
119
130
optimizer = _minimize_remote if device .is_remote () else _minimize_local
120
- return optimizer (optimizable , method , device , init_params , nb_params )
131
+ return optimizer (
132
+ optimizable ,
133
+ method ,
134
+ device ,
135
+ init_params ,
136
+ nb_params ,
137
+ optimizer_options ,
138
+ callback ,
139
+ )
121
140
else :
122
141
# TODO: find a way to know if the job is remote or local from the function
123
142
return _minimize_local (
124
- optimizable , method , device , init_params , nb_params , optimizer_options
143
+ optimizable ,
144
+ method ,
145
+ device ,
146
+ init_params ,
147
+ nb_params ,
148
+ optimizer_options ,
149
+ callback ,
125
150
)
126
151
127
152
@@ -133,6 +158,7 @@ def _minimize_remote(
133
158
init_params : Optional [OptimizerInput ] = None ,
134
159
nb_params : Optional [int ] = None ,
135
160
optimizer_options : Optional [dict [str , Any ]] = None ,
161
+ callback : Optional [OptimizerCallback ] = None ,
136
162
) -> tuple [float , OptimizerInput ]:
137
163
"""This function runs an optimization on the parameters of the circuit, to
138
164
minimize the expectation value of the measure of the circuit by it's
@@ -158,6 +184,7 @@ def _minimize_remote(
158
184
optimizer_options: Options used to configure the VQA optimizer (maximum
159
185
iterations, convergence threshold, etc...). These options are passed
160
186
as is to the minimizer.
187
+ callback: A callable called after each iteration.
161
188
162
189
Returns:
163
190
The optimal value reached and the parameters used to reach this value.
@@ -175,6 +202,7 @@ def _minimize_local(
175
202
init_params : Optional [OptimizerInput ] = None ,
176
203
nb_params : Optional [int ] = None ,
177
204
optimizer_options : Optional [dict [str , Any ]] = None ,
205
+ callback : Optional [OptimizerCallback ] = None ,
178
206
) -> tuple [float , OptimizerInput ]:
179
207
"""This function runs an optimization on the parameters of the circuit, to
180
208
minimize the expectation value of the measure of the circuit by it's
@@ -200,6 +228,7 @@ def _minimize_local(
200
228
optimizer_options: Options used to configure the VQA optimizer (maximum
201
229
iterations, convergence threshold, etc...). These options are passed
202
230
as is to the minimizer.
231
+ callback: A callable called after each iteration.
203
232
204
233
Returns:
205
234
the optimal value reached and the parameters used to reach this value.
@@ -208,11 +237,11 @@ def _minimize_local(
208
237
if device is None :
209
238
raise ValueError ("A device is needed to optimize a circuit" )
210
239
return _minimize_local_circ (
211
- optimizable , device , method , init_params , optimizer_options
240
+ optimizable , device , method , init_params , optimizer_options , callback
212
241
)
213
242
else :
214
243
return _minimize_local_func (
215
- optimizable , method , init_params , nb_params , optimizer_options
244
+ optimizable , method , init_params , nb_params , optimizer_options , callback
216
245
)
217
246
218
247
@@ -223,6 +252,7 @@ def _minimize_local_circ(
223
252
method : Optimizer | OptimizerCallable ,
224
253
init_params : Optional [OptimizerInput ] = None ,
225
254
optimizer_options : Optional [dict [str , Any ]] = None ,
255
+ callback : Optional [OptimizerCallback ] = None ,
226
256
) -> tuple [float , OptimizerInput ]:
227
257
"""This function runs an optimization on the parameters of the circuit, to
228
258
minimize the expectation value of the measure of the circuit by it's
@@ -244,6 +274,7 @@ def _minimize_local_circ(
244
274
optimizer_options: Options used to configure the VQA optimizer (maximum
245
275
iterations, convergence threshold, etc...). These options are passed
246
276
as is to the minimizer.
277
+ callback: A callable called after each iteration.
247
278
248
279
Returns:
249
280
The optimal value reached and the parameters used to reach this value.
@@ -264,7 +295,7 @@ def eval_circ(params: OptimizerInput):
264
295
).expectation_value
265
296
266
297
return _minimize_local_func (
267
- eval_circ , method , init_params , len (variables ), optimizer_options
298
+ eval_circ , method , init_params , len (variables ), optimizer_options , callback
268
299
)
269
300
270
301
@@ -275,6 +306,7 @@ def _minimize_local_func(
275
306
init_params : Optional [OptimizerInput ] = None ,
276
307
nb_params : Optional [int ] = None ,
277
308
optimizer_options : Optional [OptimizerOptions ] = None ,
309
+ callback : Optional [OptimizerCallback ] = None ,
278
310
) -> tuple [float , OptimizerInput ]:
279
311
"""This function runs an optimization on the parameters of the circuit, to
280
312
minimize the expectation value of the measure of the circuit by it's
@@ -298,6 +330,8 @@ def _minimize_local_func(
298
330
optimizer_options: Options used to configure the VQA optimizer (maximum
299
331
iterations, convergence threshold, etc...). These options are passed
300
332
as is to the minimizer.
333
+ callback: A callable called after each iteration.
334
+
301
335
302
336
Returns:
303
337
The optimal value reached and the parameters used to reach this value.
@@ -317,6 +351,7 @@ def _minimize_local_func(
317
351
x0 = np .array (init_params ),
318
352
method = method .name .lower (),
319
353
options = optimizer_options ,
354
+ callback = callback ,
320
355
)
321
356
return res .fun , res .x
322
357
else :
0 commit comments