Solution (source code)

= Solution

The total excitation operator $\mathcal N=a^\dagger a+(\sigma_z+1)/2$ commutes with the <Hamiltonian>. Each sector with $\mathcal N=n+1\geq1$ is therefore spanned by $|e,n\rangle,|g,n+1\rangle$, and no term connects it to a sector of different <excitation number>.

In that ordered basis, with the source's <Rabi detuning> $\Delta=\omega-\omega_0$,
$$
\boxed{\mathcal H_n=\hbar\omega\left(n+\frac12\right)I
+\hbar\begin{pmatrix}-\Delta/2&g\sqrt{n+1}\\g\sqrt{n+1}&\Delta/2\end{pmatrix}.}
$$
The first term is the common sector <energy>; the remaining diagonal entries record the bare <Rabi detuning> and the off-diagonal terms exchange the excitation.

The <isolated ground sector of the Jaynes-Cummings model> is an important exception to a statement that the \i[entire] Hilbert space consists of such pairs: $|g,0\rangle$ has no partner and is annihilated by both interaction terms. It is a one-dimensional invariant sector with <energy> $-\hbar\omega_0/2$. The full decomposition is this one-dimensional sector plus all the two-dimensional sectors above.