Consider a quantum particle of mass $$m$$ moving in a $$D$$-dimensional space under the action of a Hamiltonian $H = T + V \,,$ where $T = \frac{\boldsymbol{p} \cdot \boldsymbol{p}}{2 m} = \frac{p_1^2 + p_2^2 + \ldots + p_D^2}{2 m}$ is the kinetic energy, and $V = V(\boldsymbol{x}) = V(x_1, x_2, \ldots, x_D)$ is the potential energy. Here, $$x_j$$ and $$p_j$$, with $$j = 1, 2, \ldots, D$$, are Cartesian components of the particle's position and momentum vectors, respectively, satisfying the standard commutation relation: $[x_j, p_k] = i \hbar \delta_{jk} \,.$ Suppose further that the potential $$V$$ is confining, and that the particle is in a bound state $$| \psi \rangle$$ of energy $$E$$, i.e. $H | \psi \rangle = E | \psi \rangle \,.$ The virial theorem states that the expectation value of the kinetic energy is given by $\boxed{ \langle T \rangle = \tfrac{1}{2} \langle \boldsymbol{x} \cdot \boldsymbol{\nabla} V \rangle }$ Here, $\langle \cdot \rangle \equiv \langle \psi | \cdot | \psi \rangle$ and $$\boldsymbol{\nabla} = (\partial / \partial x_1, \, \partial / \partial x_2, \, \ldots, \, \partial / \partial x_D)^{\text{T}}$$.
Proof: For any observable $$A$$, $\langle [H, A] \rangle = 0 \,.$ Indeed, $\langle [H, A] \rangle = \langle \psi | H A - A H | \psi \rangle = \langle \psi | E A - A E | \psi \rangle = 0 \,.$ Choosing $$A = \boldsymbol{x} \cdot \boldsymbol{p}$$, we get $\langle [H, \boldsymbol{x} \cdot \boldsymbol{p}] \rangle = 0 \,.$ Evaluating the commutator, \begin{align} [H, \boldsymbol{x} \cdot \boldsymbol{p}] &= \boldsymbol{x} \cdot [H, \boldsymbol{p}] + [H, \boldsymbol{x}] \cdot \boldsymbol{p} \\ &= \boldsymbol{x} \cdot [V, \boldsymbol{p}] + \tfrac{1}{2 m} [\boldsymbol{p} \cdot \boldsymbol{p}, \boldsymbol{x}] \cdot \boldsymbol{p} \\ &= \boldsymbol{x} \cdot (i \hbar \boldsymbol{\nabla} V) + \tfrac{1}{2 m} (-2 i \hbar \boldsymbol{p}) \cdot \boldsymbol{p} \\ &= i \hbar \left( \boldsymbol{x} \cdot \boldsymbol{\nabla} V - \tfrac{1}{m} \boldsymbol{p} \cdot \boldsymbol{p} \right) \,, \end{align} we obtain $\langle \boldsymbol{x} \cdot \boldsymbol{\nabla} V - \tfrac{1}{m} \boldsymbol{p} \cdot \boldsymbol{p} \rangle = 0 \,.$ This result is equivalent to the statement of the virial theorem.
Let $V(\boldsymbol{x}) = \alpha |\boldsymbol{x}|^{\beta} \,,$ where $$\alpha$$ and $$\beta$$ are some constants (whose values are such that $$V$$ supports at least one bound state). Then, $\boldsymbol{\nabla} V = \alpha \beta |\boldsymbol{x}|^{\beta-1} \boldsymbol{\nabla} |\boldsymbol{x}| = \alpha \beta |\boldsymbol{x}|^{\beta-1} \frac{\boldsymbol{x}}{|\boldsymbol{x}|} = \alpha \beta |\boldsymbol{x}|^{\beta-2} \boldsymbol{x} \,,$ $\boldsymbol{x} \cdot \boldsymbol{\nabla} V = \alpha \beta |\boldsymbol{x}|^{\beta-2} \boldsymbol{x} \cdot \boldsymbol{x} = \alpha \beta |\boldsymbol{x}|^{\beta} = \beta V \,,$ and the virial theorem reads $\langle T \rangle = \frac{\beta}{2} \langle V \rangle \,.$ For example, $\langle T \rangle = \langle V \rangle \qquad \text{if} \qquad \beta = 2 \quad (\text{harmonic trap}) \,,$ $\langle T \rangle = -\tfrac{1}{2} \langle V \rangle \qquad \text{if} \qquad \beta = -1 \quad (\text{Coulomb potential}) \,.$