Solution (source code)

= Solution

Use the <canonical commutation relation> $[\widehat x,\widehat p]=i\hbar I$ on a common invariant domain of the operator products. Write $A=\widehat p\widehat x$, so $\widehat x\widehat p=A+i\hbar I$. Associativity then gives
$$
\widehat x\widehat p^2\widehat x
=(\widehat x\widehat p)(\widehat p\widehat x)
=(A+i\hbar I)A=A^2+i\hbar A,
$$
whereas
$$
\widehat p\widehat x^2\widehat p
=(\widehat p\widehat x)(\widehat x\widehat p)
=A(A+i\hbar I)=A^2+i\hbar A.
$$
Therefore
$$
\boxed{\widehat x\widehat p^2\widehat x=\widehat p\widehat x^2\widehat p.}
$$
As a direct position-representation check, the <momentum operator> is $\widehat p=-i\hbar\,d/dx$. Acting on a smooth function $f$ gives
$$
x(-\hbar^2)\frac{d^2(xf)}{dx^2}
=-\hbar^2(x^2f''+2xf')
=(-i\hbar)\frac d{dx}\left[x^2(-i\hbar f')\right].
$$
This is the <equality of two quadratic position-momentum orderings>; it does not make arbitrary rearrangements of noncommuting <linear operators> interchangeable.