Optimally controlled quantum discrimination and estimation