Optimal quantum measurements in gravitational-wave detectors