Energy as a functional of the density matrix