  We lay down the {\em ab initio} many-body quantum theory of electrons and
phonons in- and out-of-equilibrium at any temperature. We begin by addressing a
fundamental issue concerning the {\em ab initio} Hamiltonian in the harmonic
approximation, which we show must be determined {\em self-consistently} to
avoid inconsistencies. After identifying the most suitable partitioning into a
``noninteracting'' and an ``interacting'' part we embark on the Green's
function diagrammatic analysis. We single out key diagrammatic structures to
carry on the expansion in terms of dressed propagators and screened
interaction. The final outcome is the finite-temperature nonequilibrium
extension of the Hedin equations, featuring the appearance of the time-local
Ehrenfest diagram in the electronic self-energy. The Hedin equations have
limited practical utility for real-time simulations of driven systems. We
leverage the versatility of diagrammatic expansion to generate a closed system
of integro-differential equations for the Green's functions and nuclear
displacements. These are the Kadanoff-Baym equations for electrons and phonons.
Another advantage of the diagrammatic derivation is the ability to use
conserving approximations, which ensure the satisfaction of all fundamental
conservation laws during the time evolution. As an example we show that the
adiabatic Born-Oppenheimer approximation is not conserving whereas its
dynamical extension is conserving provided that the electrons are treated in
the Fan-Migdal approximation with a dynamically screened electron-phonon
coupling. We also derive the formal solution of the Kadanoff-Baym equations in
the long time limit and at the steady state. The expansion of the phononic
Green's function around the quasi-phonon energies points to a possible
correlation-induced splitting of the phonon dispersion in materials with no
time-reversal invariance.
