2
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$

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)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.