  We discuss quantum variational optimization of Ramsey interferometry with
ensembles of $N$ entangled atoms, and its application to atomic clocks based on
a Bayesian approach to phase estimation. We identify best input states and
generalized measurements within a variational approximation for the
corresponding entangling and decoding quantum circuits. These circuits are
built from basic quantum operations available for the particular sensor
platform, such as one-axis twisting, or finite range interactions. Optimization
is defined relative to a cost function, which in the present study is the
Bayesian mean square error of the estimated phase for a given prior
distribution, i.e. we optimize for a finite dynamic range of the
interferometer. In analogous variational optimizations of optical atomic
clocks, we use the Allan deviation for a given Ramsey interrogation time as the
relevant cost function for the long-term instability. Remarkably, even
low-depth quantum circuits yield excellent results that closely approach the
fundamental quantum limits for optimal Ramsey interferometry and atomic clocks.
The quantum metrological schemes identified here are readily applicable to
atomic clocks based on optical lattices, tweezer arrays, or trapped ions.
