High-fidelity control of spin ensemble dynamics is essential to many fields, spanning from quantum computing to optical, coherent, and nuclear magnetic resonance (NMR) spectroscopy and imaging (MRI). However, attaining robust and high-fidelity quantum spin operations remains an unmet challenge. Using a combination of an evolutionary algorithm and artificial intelligence, we designed time-optimal, radio frequency (RF) pulses with tunable spatial or temporal field inhomogeneity compensation and fidelity for unitary operations up to 0.9999. As a benchmark, we constructed a spin entanglement operator and a programmable quantum state creator for a weakly-coupled two-spin-1/2 system. We achieved high-fidelity transformations under multiple inhomogeneity sources. The newly designed RF pulses are more robust and less prone to imperfection than the commonly used shapes for basic liquid-state NMR experiments and reduce RF artifacts in MRI. This new strategy will enable the design of efficient quantum computing opera-tors as well as new spectroscopic and imaging techniques.