Solution (source code)

= Solution

Use <unfolding a reflected interval trajectory>: reflect the interval across each wall so that a bouncing trajectory becomes a free trajectory in an image interval. A return path starting at $q$ ends at $q+2rL$ after an even number of reflections, or at $-q+2rL$ after an odd number. Each hard-wall reflection contributes phase $-1$, giving the positive direct images and negative reflected images of the <Dirichlet heat kernel on an interval>.

\Image[/past-exam-of-the-mathematics-course-of-the-university-of-cambridge/2015/iii/paper-81-unfolded-well-paths.png]
{title=Even and odd reflection paths unfolded across an infinite square well}
{height=600}

The <method of images> therefore gives
$$
Z=\int_0^L dq\sum_{r\in\mathbb Z}\left[\int_{q(0)=q}^{q(\beta)=q+2rL}\mathcal Dq\,e^{-\mathcal A[q]}-\int_{q(0)=q}^{q(\beta)=-q+2rL}\mathcal Dq\,e^{-\mathcal A[q]}\right],\qquad \mathcal A[q]=\frac m{2\hbar^2}\int_0^\beta(\partial_\tau q)^2d\tau.
$$
Here $\tau$ has inverse-energy units, since $\beta=1/(k_BT)$. In physical imaginary time $u=\hbar\tau$, the same dimensionless action is $\mathcal A=S_E/\hbar$, with $S_E=\int_0^{\hbar\beta}(m/2)(dq/du)^2du$. Thus the question's convention $S=(m/(2\hbar))\int_0^\beta(\partial_\tau q)^2d\tau$ has exactly the weight $e^{-S/\hbar}$.