A quantum mechanical description of electromagnetic and atom Mach-Zehnder interferometers