Bug in gradient of the Hamiltonian