@@ -172,7 +172,6 @@ def _check_cyclicity(indices: list[int]) -> bool:
172
172
# -> this results in any sequence of at least two numbers being detected
173
173
match = cycle_regex .search (" " .join (map (str , indices )))
174
174
logger .debug ("Cycle detected: %s" , match )
175
- logger .info ("Alternating sequence found. Finishing." )
176
175
# Additionally we also need to check whether the last two numbers are identical, because the
177
176
# reg-ex above will only find cycles of at least two consecutive numbers.
178
177
# It is sufficient to assert that the last two numbers are different due to the iterative
@@ -212,18 +211,23 @@ def compute_minimum_eigenvalue(
212
211
theta : list [float ] = []
213
212
max_grad : tuple [float , Optional [PauliSumOp ]] = (0.0 , None )
214
213
self ._excitation_list = []
214
+ history : list [float ] = []
215
215
iteration = 0
216
216
while self .max_iterations is None or iteration < self .max_iterations :
217
217
iteration += 1
218
218
logger .info ("--- Iteration #%s ---" , str (iteration ))
219
219
# compute gradients
220
+ logger .debug ("Computing gradients" )
220
221
cur_grads = self ._compute_gradients (theta , operator )
221
222
# pick maximum gradient
222
223
max_grad_index , max_grad = max (
223
224
enumerate (cur_grads ), key = lambda item : np .abs (item [1 ][0 ])
224
225
)
225
- # store maximum gradient's index for cycle detection
226
- prev_op_indices .append (max_grad_index )
226
+ logger .info (
227
+ "Found maximum gradient %s at index %s" ,
228
+ str (np .abs (max_grad [0 ])),
229
+ str (max_grad_index ),
230
+ )
227
231
# log gradients
228
232
if np .abs (max_grad [0 ]) < self .threshold :
229
233
if iteration == 1 :
@@ -238,12 +242,18 @@ def compute_minimum_eigenvalue(
238
242
)
239
243
termination_criterion = TerminationCriterion .CONVERGED
240
244
break
245
+ # store maximum gradient's index for cycle detection
246
+ prev_op_indices .append (max_grad_index )
241
247
# check indices of picked gradients for cycles
242
248
if self ._check_cyclicity (prev_op_indices ):
249
+ logger .info ("Alternating sequence found. Finishing." )
243
250
logger .info ("Final maximum gradient: %s" , str (np .abs (max_grad [0 ])))
244
251
termination_criterion = TerminationCriterion .CYCLICITY
245
252
break
246
253
# add new excitation to self._ansatz
254
+ logger .info (
255
+ "Adding new operator to the ansatz: %s" , str (self ._excitation_pool [max_grad_index ])
256
+ )
247
257
self ._excitation_list .append (self ._excitation_pool [max_grad_index ])
248
258
theta .append (0.0 )
249
259
# run VQE on current Ansatz
@@ -252,6 +262,8 @@ def compute_minimum_eigenvalue(
252
262
self .solver .initial_point = theta
253
263
raw_vqe_result = self .solver .compute_minimum_eigenvalue (operator )
254
264
theta = raw_vqe_result .optimal_point .tolist ()
265
+ history .append (raw_vqe_result .eigenvalue )
266
+ logger .info ("Current eigenvalue: %s" , str (raw_vqe_result .eigenvalue ))
255
267
else :
256
268
# reached maximum number of iterations
257
269
termination_criterion = TerminationCriterion .MAXIMUM
@@ -263,6 +275,7 @@ def compute_minimum_eigenvalue(
263
275
result .num_iterations = iteration
264
276
result .final_max_gradient = max_grad [0 ]
265
277
result .termination_criterion = termination_criterion
278
+ result .eigenvalue_history = history
266
279
267
280
# once finished evaluate auxiliary operators if any
268
281
if aux_operators is not None :
@@ -284,33 +297,47 @@ def __init__(self) -> None:
284
297
self ._num_iterations : int = None
285
298
self ._final_max_gradient : float = None
286
299
self ._termination_criterion : str = ""
300
+ self ._eigenvalue_history : list [float ] = None
287
301
288
302
@property
289
303
def num_iterations (self ) -> int :
290
- """Returns number of iterations"""
304
+ """Returns the number of iterations. """
291
305
return self ._num_iterations
292
306
293
307
@num_iterations .setter
294
308
def num_iterations (self , value : int ) -> None :
295
- """Sets number of iterations"""
309
+ """Sets the number of iterations. """
296
310
self ._num_iterations = value
297
311
298
312
@property
299
313
def final_max_gradient (self ) -> float :
300
- """Returns final maximum gradient"""
314
+ """Returns the final maximum gradient. """
301
315
return self ._final_max_gradient
302
316
303
317
@final_max_gradient .setter
304
318
def final_max_gradient (self , value : float ) -> None :
305
- """Sets final maximum gradient"""
319
+ """Sets the final maximum gradient. """
306
320
self ._final_max_gradient = value
307
321
308
322
@property
309
323
def termination_criterion (self ) -> str :
310
- """Returns termination criterion"""
324
+ """Returns the termination criterion. """
311
325
return self ._termination_criterion
312
326
313
327
@termination_criterion .setter
314
328
def termination_criterion (self , value : str ) -> None :
315
- """Sets termination criterion"""
329
+ """Sets the termination criterion. """
316
330
self ._termination_criterion = value
331
+
332
+ @property
333
+ def eigenvalue_history (self ) -> list [float ]:
334
+ """Returns the history of computed eigenvalues.
335
+
336
+ The history's length matches the number of iterations and includes the final computed value.
337
+ """
338
+ return self ._eigenvalue_history
339
+
340
+ @eigenvalue_history .setter
341
+ def eigenvalue_history (self , eigenvalue_history : list [float ]) -> None :
342
+ """Sets the history of computed eigenvalues."""
343
+ self ._eigenvalue_history = eigenvalue_history
0 commit comments