Optimal control of spin dynamics in the presence of relaxation.