Joining and splitting the quantum states of photons