Optimal growth and transition to turbulence in channel flow with spanwise magnetic field