Well it depends on what you mean by p and q, I suppose. I'm going to assume you mean to ask about the commutation for regular position and momentum.
It has to do with the basic construction of quantum mechanics along with
momentum [itex]\Leftrightarrow \hbar \mathbf{k}[/itex] (wavevector)
Energy [itex]\Leftrightarrow \hbar \omega[/itex] (frequency)
This leads us to the Schrödinger equation. We simply define the operator [itex]\mathbf{p}[/itex] so that it extracts the momentum from a plane wave [itex]e^{ik x - i\omega t}[/itex]
So we want the operator [itex]p[/itex] to extract the k as a coefficient like so:
[itex]p\, e^{ikx - i\omega t} = \hbar k \,e^{ikx - i\omega t}[/itex]
This can be done if we define p as
[itex]p \equiv -i\hbar \frac{\partial}{\partial x}[/itex]
Since the derivative brings down a factor of [itex]ik[/itex], and[itex]-i\hbar \, ik = \hbar k[/itex].
This results in the commutation relations for position and momentum. Note that [itex]\frac{\partial}{\partial x} (x f(x)) \neq x \frac{\partial}{\partial x} f(x)[/itex].
\begin{align*} (xp - px) f(x) &= -i\hbar\left( x \frac{\partial}{\partial x} f(x) - \frac{\partial}{\partial x} (x f(x)) \right) \\ &=-i\hbar \left( x \frac{\partial}{\partial x} f(x) - \left[f(x) + x \frac{\partial}{\partial x} f(x)\right] \right) \\
& = -i\hbar\left(-f(x) \right) \\ &= i\hbar f(x)\end{align*}
So we conclude then that [itex]\left[x,\, p\right] = i\hbar[/itex]. The product rule was used to get from the first line to the second line.