Variational approach to learning photonic unitary operators