Optimizers are classical algorithms that train ansatz parameters in variational algorithms. Given a cost function $f(\theta)$, an optimizer iteratively updates $\theta$ to minimize $f$. Qiskit provides gradient-free and gradient-based optimizers.
Gradient-free optimizers are noisy-friendly: they work well when cost function evaluations are noisy (as on real quantum hardware).
from qiskit.optimizers import COBYLA def cost_function(params): # Evaluate circuit, measure cost, return return ... optimizer = COBYLA(maxiter=100, rhobeg=1.0) result = optimizer.minimize(cost_function, x0=initial_params) print(result.fun) # Optimal cost print(result.x) # Optimal parameters
These require computing or estimating gradients, which is more expensive but can converge faster on smooth landscapes.
from qiskit.optimizers import SLSQP def cost_with_gradients(params): cost = cost_function(params) gradient = estimate_gradient(params, cost_function) return cost, gradient optimizer = SLSQP(maxiter=100) result = optimizer.minimize(cost_with_gradients, x0=initial_params)
Modern variational algorithms combine multiple optimizers: rough search with COBYLA, then fine-tuning with SLSQP.
For noisy hardware, gradient-free methods often work better because gradients are more sensitive to noise than function values.