- A collective system of atoms in a high-quality cavity can be described by a nonlinear interaction which arises due to the Lamb shift of the energy levels due to the cavity vacuum [Agarwal, Phys. Rev. A 56, 2249 (1997)]. We show how this collective interaction can be used to perform quantum logic. In particular we produce schemes to realize controlled-NOT gates not only for two-qubit but also for three-qubit systems. We also discuss realizations of Toffoli gates. Our effective Hamiltonian is also realized in other systems such as trapped ions or magnetic molecules. 2005 The American Physical Society.