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?
 A: Your teacher is being a bit sloppy in saying that you get the Hamiltonian for quantum mechanics from classical energy. You get the Hamiltonian for quantum mechanics by "quantizing" the classical Hamiltonian. OK, so what is this "quantizing"? 
As you point out, Dirac came up with a fairly generalized scheme of constructing quantum theories which correspond to a given classical theory in (one of) its classical limit(s). Now, keep in mind that we're guessing a quantum theory that we hope to reduce the classical theory at hand in some classical limit. Given that the quantum theory is the more basic theory, we cannot derive it generically from its classical limit. Anyway, so the idea is that a quantum system that respects the same symmetries as the classical system would be a good guess for the quantum version of the said classical system. In Hamiltonian mechanics, the Poisson brackets capture the symmetries of the system whereas in quantum mechanics, commutators do the same job. Thus, it'd make sense to make commutators of quantum operators to follow the same relations as the Poisson brackets of classical observables in Hamiltonian mechanics. I'm not aware if Dirac explicitly used the symmetry arguments but he did realize that Poisson brackets are the central objects of Hamiltonian formalism and thus set out to find their quantum analog which he found in commutators. See, the chapter titled "Quantum Conditions" from his excellent book Principles of Quantum Mechanics. Once we have done this for canonical coordinates and momenta, since all observables are functions of them, we can ensure the desired commutation relations for their quantum analogs by putting hats on the canonical coordinates and momenta in their classical expressions, barring unforeseen ordering ambiguities.
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. 
A: Dvij D. C. is correct. In a nutshell, the relationship between classical mechanics and quantum mechanics is that the former gives a lot of insight into the latter, but quantum cannot be derived from classical. Rather, classical mechanics gives hints as to what to try, and it gives insight into what quantum formulae are saying and what kind of behaviours will result in certain limits. 
So every time we say "here is something classical" and "here is something quantum" the move from classical to quantum is never a derivation. It might be clearer to say "here is something quantum" first, and then add "look, it has a similar overall structure to this classical equation, so the classical equation helps us on our journey into understanding the quantum one, and it can act as a mnemonic too."
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.
