  We study theoretically the interaction between two photons in a nonlinear
cavity. The photons are loaded into the cavity via a method we propose here, in
which the input/output coupling of the cavity is effectively controlled via a
tunable coupling to a second cavity mode that is itself strongly
output-coupled. Incoming photon wave packets can be loaded into the cavity with
high fidelity when the timescale of the control is smaller than the duration of
the wave packets. Dynamically coupled cavities can be used to avoid limitations
in the photon-photon interaction time set by the delay-bandwidth product of
passive cavities. Additionally, they enable the elimination of wave packet
distortions caused by dispersive cavity transmission and reflection. We
consider three kinds of nonlinearities, those arising from
$\chi^{\scriptscriptstyle(2)}$ and $\chi^{\scriptscriptstyle(3)}$ materials and
that due to an interaction with a two-level emitter. To analyze the input and
output of few-photon wave packets we use a Schr\"odinger-picture formalism in
which travelling-wave fields are discretized into infinitesimal time-bins. We
suggest that dynamically coupled cavities provide a very useful tool for
improving the performance of quantum devices relying on cavity-enhanced
light-matter interactions such as single-photon sources and atom-like quantum
memories with photon interfaces. As an example, we present simulation results
showing that high fidelity two-qubit entangling gates may be constructed using
any of the considered nonlinear interactions.
