This is from classical optimization algo. used for VQE. May I know what is the difference between maxiter and the number of function evaluation difference?
1 Answer
$\begingroup$
$\endgroup$
For every iteration (step) of SPSA, the cost function is evaluated several times (similar principle to other global-optimization algorithms like Particle-Swarm), contributing to the next step.
Also, the details of SPSA can be read from:
You can see it yourself by running this code:
#!/usr/bin/env python3
import numpy as np
from qiskit.algorithms.optimizers import SPSA
from qiskit.circuit.library import PauliTwoDesign
from qiskit.opflow import Z, StateFn
ansatz = PauliTwoDesign(2, reps=1, seed=2)
observable = Z ^ Z
initial_point = np.random.random(ansatz.num_parameters)
iter = 0
def loss(x):
bound = ansatz.bind_parameters(x)
return np.real((StateFn(observable, is_measurement=True) @ StateFn(bound)).eval())
def callbackfun(nevals, params, fval, stepsize, acceptedstep):
global iter
print(f'Iteration: {iter} Number of evaluations: {nevals}')
iter += 1
spsa = SPSA(maxiter=300, callback=callbackfun)
result = spsa.minimize(loss, initial_point)
print(result)