Solution (source code)

= Solution

Define $a=qB_0/(2L)>0$. Differentiating the <Lagrangian> with respect to each generalized <velocity> gives
$$
\boxed{p_x=m\dot x,\qquad p_y=m\dot y,\qquad p_z=m\dot z-a x^2.}
$$
The coordinates $y$ and $z$ are cyclic, so $p_y$ and $p_z$ are conserved. The <momentum> $p_x$ is generally not conserved: its equation is $\dot p_x=-2ax\dot z$. In particular, conserved canonical $p_z$ is not the same as conserved mechanical $m\dot z$, which changes as the particle moves across the magnetic-field gradient.