  In this paper we use ASPECS, the ALMA Spectroscopic Survey in the {\em
Hubble} Ultra Deep Field (UDF) in band 3 and band 6, to place blind constraints
on the CO luminosity function and the evolution of the cosmic molecular gas
density as a function of redshift up to $z\sim 4.5$. This study is based on
galaxies that have been solely selected through their CO emission and not
through any other property. In all of the redshift bins the ASPECS measurements
reach the predicted `knee' of the CO luminosity function (around
$5\times10^{9}$ K km/s pc$^2$). We find clear evidence of an evolution in the
CO luminosity function with respect to $z\sim 0$, with more CO luminous
galaxies present at $z\sim 2$. The observed galaxies at $z\sim 2$ also appear
more gas-rich than predicted by recent semi-analytical models. The comoving
cosmic molecular gas density within galaxies as a function of redshift shows a
factor 3-10 drop from $z \sim 2$ to $z \sim 0$ (with significant error bars),
and possibly a decline at $z>3$. This trend is similar to the observed
evolution of the cosmic star formation rate density. The latter therefore
appears to be at least partly driven by the increased availability of molecular
gas reservoirs at the peak of cosmic star formation ($z\sim2$).
