qiboteam / boostvqe

Using DBI top boost VQE optimization
3 stars 1 forks source link

Bug in calculating expectation values using eigenvectors #51

Closed marekgluza closed 2 months ago

marekgluza commented 2 months ago
hamiltonian = hamiltonians.XXZ(nqubits=9, delta=0.5)
eigenergies = hamiltonian.eigenvalues()
eigen = hamiltonian.eigenvectors()

plt.plot([e.T.conj()@hamiltonian.matrix @ e  for e in eigen])
plt.plot(eigenergies)
plt.plot([hamiltonian.expectation(e)  for e in eigen])

Produces unexpected results: obraz

andrea-pasquale commented 2 months ago

I think there is an error in the way you loop over the eigenvectors. I believe you should loop over eigen.T and not eigen. At least for numpy we are using np.linalg.eigvalsh that returns a (N,N) tensor where the eigenvectors are identified by the column and not by the row, this is why you need to transpose the tensor. See https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html. As a double check you can see that the ground state in qibo is implemented accordingly https://github.com/qiboteam/qibo/blob/1bb08d9dab45205875073c371ea4fcefa982221f/src/qibo/hamiltonians/hamiltonians.py#L457-L458

marekgluza commented 2 months ago

Thanks so much @andrea-pasquale !!