  Physical implementations of quantum bits can contain coherent transitions to
energetically close non-qubit states. In particular, for anharmonic oscillator
systems such as the superconducting phase qubit and the transmon a two-level
approximation is insufficient. We apply optimal control theory to the envelope
of a resonant Rabi pulse in a qubit in the presence of a single, weakly
off-resonant leakage level. The gate error of a spin flip operation reduces by
orders of magnitude compared to simple pulse shapes. Near-perfect gates can be
achieved for any pulse duration longer than an intrinsic limit given by the
nonlinearity. The pulses can be understood as composite sequences that refocus
the leakage transition. We also discuss ways to improve the pulse shapes.
