Imperfect linear-optical photonic gates with number-resolving photodetection