VQE optimization history has unexpected plateaus
See original GitHub issueEnvironment
- Qiskit version: 0.37.2
- Qiskit Terra version: 0.21.2
- Qiskit Aer version: 0.10.4
- Python version: 3.9.0
- Operating system: macOS Big Sur 11.6.4
What is happening?
When using the statevector simulator, the ADAM optimizer, and a callback function in VQE, the energy stored by the callback function displays unexpected plateaus. In an example below, every 9 adjacent iterations have very similar energies.
How can we reproduce the issue?
The code is modified from https://qiskit.org/documentation/tutorials/algorithms/04_vqe_advanced.html
from qiskit import Aer
from qiskit.opflow import X, Z, I
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.algorithms import VQE
from qiskit.algorithms.optimizers import ADAM
from qiskit.circuit.library import TwoLocal
import matplotlib.pyplot as plt
H2_op = (-1.052373245772859 * I ^ I) + \
(0.39793742484318045 * I ^ Z) + \
(-0.39793742484318045 * Z ^ I) + \
(-0.01128010425623538 * Z ^ Z) + \
(0.18093119978423156 * X ^ X)
seed = 50
algorithm_globals.random_seed = seed
qi = QuantumInstance(Aer.get_backend('statevector_simulator'), seed_transpiler=seed, seed_simulator=seed)
ansatz = TwoLocal(rotation_blocks='ry', entanglement_blocks='cz')
optimizer = ADAM(maxiter=10)
intermediate_info = {
'nfev': [],
'parameters': [],
'energy': [],
'stddev': []
}
def callback(nfev, parameters, energy, stddev):
intermediate_info['nfev'].append(nfev)
intermediate_info['parameters'].append(parameters)
intermediate_info['energy'].append(energy)
intermediate_info['stddev'].append(stddev)
vqe = VQE(ansatz, optimizer=optimizer, quantum_instance=qi, callback=callback)
result = vqe.compute_minimum_eigenvalue(operator=H2_op)
Plotting intermediate_info['energy']
versus intermediate_info['nfev']
gives
Every 9 adjacent iterations have very similar energies, which seems unexpected with the Adam optimizer. When the number of qubits of the Hamiltonian changes, the length of each plateau can also change (from 9 in the current example).
What should happen?
The expected optimization history of energy with an Adam optimizer should be smoother, without artificial plateaus.
Any suggestions?
I guess that some repeated evaluations are done in optimization, or the callback function is not working properly.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
If you look at the plots in this tutorial https://qiskit.org/documentation/tutorials/algorithms/02_vqe_convergence.html you will see similar plateaus. These are where optimizers using gradients are computing say a finite diff gradient using the same objective function - the callback is from the objective function and cannot discriminate how the optimizer is using it. Since the delta around the point is so small when the gradient is computed at the scale of the plots they end up with the staircase like look. (The tutorial has a paragraph in it mentioning gradients and the staircase effect)
Adam uses a finite diff gradient by default. TwoLocal defaults to reps=3 and with 2 qubits that 2 parameters per layer (rep) plus 2 more in a final block so 8 params in total. So I believe the 9 is 1 + 8 i.e one eval at the current point and 8 around it to compute the gradient to go to the next point (the finite diff like scipy only does a small epsilon in one direction in each dimensions)
And the number of points will vary by size of the hamiltonian, number of qubits given the same ansatz structure. Hopefully you can see from my detailing where the 9 comes from in the above.
Thank you. There is actually a single point on that last plot. The plot does not set the marker, so it may not display. The center of the energy axis of the “empty” plot is at that final energy value.
I will raise another issue with more details.