  The sensitivity and accessible mass range of magnetic resonance searches for
axionlike dark matter depends on the homogeneity of applied magnetic fields.
Optimizing homogeneity through shimming requires exploring a large parameter
space which can be prohibitively time consuming. We have automated the process
of tuning the shim-coil currents by employing an algorithm based on Bayesian
optimization. This method is especially suited for applications where the
duration of a single optimization step prohibits exploring the parameter space
extensively or when there is no prior information on the optimal operation
point. Using the Cosmic Axion Spin Precession Experiment (CASPEr)-gradient
low-field apparatus, we show that for our setup this method converges after
approximately 30 iterations to a sub-10 parts-per-million field homogeneity
which is desirable for our dark matter search.
