# Deriving the quantum Hamiltonian from the expression of classical energy

I am currently learning about the Dirac formalism in quantum mechanics, but don't quite understand how we derive the expression of the quantum Hamiltonian, given the value of energy in classical mechanics.

The specific example that came up in class was that of the harmonic oscillator, for which the classical energy is $$E = \frac{p^2}{2m} + \frac{1}{2}m\omega^2x^2$$

My teacher then concluded that

$$\hat{H} = \frac{\hat{p}^2}{2m} + \frac{1}{2}m\omega^2\hat{x}^2$$

Why is that? The only way I see to show this is by looking at a stationary wave function $$\psi (x)$$ and using the associated Schrödinger equation. We get get that, by writing $$V(x) = \frac{1}{2}m\omega^2x^2$$,

$$E\psi(x) = \frac{-\hbar^2}{2m}\frac{d^2\psi(x)}{dx^2} + V(x)\psi = \hat{H}\psi(x)$$

By identifying known expressions for $$\hat{p}$$ and $$\hat{x}$$, we can find the desired expression for the Hamiltonian. However, I do not feel like this method is very satisfying, as it requires to return to wave functions, and doesn't use the Schrödinger equation in the Dirac formalism.

I am getting a feeling that teachers will eagerly replace $$x$$ by $$\hat{x}$$ and p by $$\hat{p}$$ when going from classical mechanics to quantum mechanics.

Is there a more general result? Can it be said that if in classical mechanics $$E = f(x_1, \dots, x_n)$$ where $$x_1, \dots, x_n$$ are observables, then $$\hat{H} = f(\hat{x_1},\dots,\hat{x_n})$$? I cannot see why that would be true, so is it only a coincidence that it is true in the case of the harmonic oscillator?

To summarize, is there a rule for when such replacements are valid, and if so, for which observables and how can it be proven?

This caricature description of replacing every classical canonical variable (for example, $$x$$ and $$p$$) with a hat to get to the corresponding quantum operator is not idiot-proof. There are many subtleties involved. For example, the ordering ambiguities I mentioned. Classically, you have an observable $$xp$$. If you put on hats, you get an operator $$\hat{x}\hat{p}$$ which cannot be an observable because it's not Hermitian (as you can check). There is an issue with it to start with. Classically, $$xp$$ is the same as $$px$$, so which one do you choose to put on the hats? In quantum mechanics, since $$\hat{x}$$ and $$\hat{p}$$ don't commute, the two would give very different operators (and none of them will be Hermitian anyway, so none of them can be observables). We have adopted ordering procedures to deal with such issues, for example, if you say your classical observable is actually $$\frac{1}{2}(xp+px)$$ which is the same as $$xp$$ in classical mechanics, you get a Hermitian operator when you put on hats. See, for example, Weyl ordering. However, there can be multiple such ordering schemes. This comes back to the point that "quantization is not a functor" as the saying goes, the classical limit of a quantum theory doesn't uniquely determine the full quantum theory. Ultimately, we have to guess as to which quantum theory we think would reduce to the classical theory we're interested in in one of its limits.
Your suspicions, then, were largely right, but it is not quite right to call the success of $$x \rightarrow \hat{x},\; p \rightarrow \hat{p}$$ for a harmonic oscillator a mere coincidence. There is a bit more to it than that.