  We show that the strong CP problem is solved in a large class of
compactifications of string theory. The Peccei-Quinn mechanism solves the
strong CP problem if the CP-breaking effects of the ultraviolet completion of
gravity and of QCD are small compared to the CP-preserving axion potential
generated by low-energy QCD instantons. We characterize both classes of
effects. To understand quantum gravitational effects, we consider an ensemble
of flux compactifications of type IIB string theory on orientifolds of
Calabi-Yau hypersurfaces in the geometric regime, taking a simple model of QCD
on D7-branes. We show that the D-brane instanton contribution to the neutron
electric dipole moment falls exponentially in $N^4$, with $N$ the number of
axions. In particular, this contribution is negligible in all models in our
ensemble with $N>17$. We interpret this result as a consequence of large $N$
effects in the geometry that create hierarchies in instanton actions and also
suppress the ultraviolet cutoff. We also compute the CP breaking due to
high-energy instantons in QCD. In the absence of vectorlike pairs, we find
contributions to the neutron electric dipole moment that are not excluded, but
that could be accessible to future experiments if the scale of supersymmetry
breaking is sufficiently low. The existence of vectorlike pairs can lead to a
larger dipole moment. Finally, we show that a significant fraction of models
are allowed by standard cosmological and astrophysical constraints.
