How can I find an operator originally expressed in terms of raising and lowering operators in terms of the field operators? I'm following this book on QFT called "Quantum Field Theory of Point Particles and Strings" by Brian Hatfield. After the end of the scalar field theory section on Exercise 3.6, it asks us to express the number operator
$$N=\int \frac{d^3k}{(2\pi)^3}\frac{1}{2\omega_k}a^\dagger(\vec{k}) a(\vec{k})$$
In terms of $\varphi(x,t)$ and $\pi(x,t)$.
My attempt
My first thought was to express the creation and annihilation operators in terms of the field and conjugate momentum
$$a(\vec{k})=\int d^3x \, e^{ik\cdot x}\,(\omega_k\varphi(x)+i\pi(x))$$
$$a^\dagger(\vec{k})=\int d^3x \, e^{-ik\cdot x}\,(\omega_k\varphi(x)-i\pi(x))$$
So substituting these definitions into the number operator we get the following
$$N=\int \frac{d^3k}{(2\pi)^3}\frac{1}{2\omega_k}\int d^3x \int d^3x' \, e^{i(x-x')\cdot k}\,(\omega_k\varphi(x')-i\pi(x'))(\omega_k\varphi(x)+i\pi(x))$$
So, I rearrange the expression in the following way
$$N=\int d^3x \int d^3x' \int \frac{d^3k}{(2\pi)^3}\frac{1}{2\omega_k} \, e^{i(x-x')\cdot k}\,(\omega_k\varphi(x')-i\pi(x'))(\omega_k\varphi(x)+i\pi(x))$$
However, I do not know how to do the $k$ integration over the exponential term to get the delta function, because we also have to integrate over the energy terms which may give a different result.
$$\int \frac{d^3 k}{(2\pi)^3} e^{i(\vec{x}-\vec{x}')\cdot \vec{k}} =\delta(\vec{x}-\vec{x}') $$
$$\int \frac{d^3 k}{(2\pi)^3} \frac{1}{2\omega_k} e^{i(\vec{x}-\vec{x}')\cdot \vec{k}} = \int \frac{d^3 k}{(2\pi)^3} \frac{1}{2\sqrt{|\vec{k}|^2 +m^2}} e^{i(\vec{x}-\vec{x}')\cdot \vec{k}} = ?$$
Also, I knew from inspection that if you expanded it out, there aren't any terms that you can combine into a commutator, so the commutation relations would not be useful.
I was thinking the number operator may take the form
$$N = \int d^3 x \varphi(x)\pi(x)$$
As in the complex case (judging by the charge operator), it takes
$$N_A - N_B = \int d^3 x( \varphi(x)\pi_\varphi(x) -  \varphi^*(x)\pi_{\varphi^*}(x))$$
How would you find an operator which is originally in terms of creation and annihilation operators to an operator that is dependent on fields?
Note:
This question is about how do you actually reverse the process to find an operator in terms of the field operators where using this computation as an example and may be useful to other users.
 A: I stress that your expression for the total charge is wrong. The total charge arising from the symmetry $U(1)$ by the Noether therem  reads $$Q=i \int d^3x \left(\varphi^*(x)\pi(x) - \pi^*(x) \varphi(x)\right)\:.$$
Notice that the two fields are mixed up and the factor $i$ takes place.
Let us start from your expression for $N$. I perform all computations at $t=0$, it would be easy to insert this variable, nothing would change, only all fiedls would be computed at that time
$\varphi(x)\to \varphi(x,t)$, $\varphi(x')\to \varphi(x',t)$,
$\pi(x)\to \pi(x,t)$, $\pi(x')\to \pi(x',t)$
You found
$$N=\int d^3x \int d^3x' \int \frac{d^3k}{(2\pi)^3}\frac{1}{2\omega_k} \, e^{i(x-x')\cdot k}\,(\omega_k\varphi(x')-i\pi(x'))(\omega_k\varphi(x)+i\pi(x)).$$
It can be expanded as
$$N=\frac{1}{2}\int d^3x \varphi(x)  \int \frac{d^3k}{(2\pi)^{3/2}}e^{ix\cdot k} \omega_k \, \int \frac{d^3x'}{(2\pi)^{3/2}} e^{-ix'\cdot k}\,\varphi(x') $$ $$ + \frac{1}{2}\int d^3x \pi(x) \int \frac{d^3k}{(2\pi)^{3/2}}e^{ix\cdot k}\frac{1}{\omega_k}  \int \frac{d^3x'}{(2\pi)^{3/2}}\, e^{-i-x'\cdot k}\,\pi(x')$$
$$+\frac{i}{2} \int d^3x \left(\varphi(x)\pi(x) - \pi(x) \varphi(x)\right)\:.$$
In the last term I used
$$ \int \frac{d^3k}{(2\pi)^{3}} \, e^{i(x-x')\cdot k}= \delta(x-x')\:.$$
The last term vanishes. The remaining ones can be re-written as follows.
$$N=\frac{1}{2}\int d^3x \;\varphi(x) \left(\sqrt{-\Delta +m^2}\varphi\right)(x) + \frac{1}{2}\int d^3x \:\pi(x)\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)\pi\right)(x)\tag{1}$$
where I used the fact that, in Fourier representation, $\sqrt{-\Delta +m^2}$ is just $\omega_k$.
What happens if we consider complex fields? The result is similar and one finds
$$N_a=\frac{1}{2}\int d^3x \;\varphi^*(x) \left(\sqrt{-\Delta +m^2}\varphi\right)(x) + \frac{1}{2}\int d^3x \:\pi^*(x)\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)\pi\right)(x)$$
$$+\frac{i}{2} \int d^3x \left(\varphi^*(x)\pi(x) - \pi^*(x) \varphi(x)\right)\:.$$
and (using also the fact that $\sqrt{-\Delta +m^2}$ and $1/\sqrt{-\Delta +m^2}$ are selfadjoint)
$$N_b=\frac{1}{2}\int d^3x \;\varphi^*(x) \left(\sqrt{-\Delta +m^2}\varphi\right)(x) + \frac{1}{2}\int d^3x \:\pi^*(x)\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)\pi\right)(x)$$
$$-\frac{i}{2} \int d^3x \left(\varphi^*(x)\pi(x) - \pi^*(x) \varphi(x)\right)\:.$$
Therefore, the total charge is the integral of a local object ($j^0$)
$$Q= N_a-N_b= i \int d^3x \left(\varphi^*(x)\pi(x) - \pi^*(x) \varphi(x)\right)$$
ADDENDUM. I would like to complete the solution of the proposed exercise with the formulae I introduced. Let us start from the equations arising from the Heisenberg evolution of the field $\varphi$ and taking the Klerin Gordon equation into account,
$$i[H, \varphi(x,t)]= \pi(x,t)\:\quad i[H, \pi(x,t)] = \Delta \varphi(x,t) - m^2\varphi(x,t)\:.$$
Using these equarions in (1) we find
$$i[H,N] = \frac{1}{2}\int d^3x \;i[H,\varphi(x)] \left(\sqrt{-\Delta +m^2}\varphi\right)(x) +  \frac{1}{2}\int d^3x \;\varphi(x) \left(\sqrt{-\Delta +m^2}i[H, \varphi]\right)(x)$$ $$+ \frac{1}{2}\int d^3x \:i[H,\pi(x)]\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)\pi\right)(x) +  \frac{1}{2}\int d^3x \:\pi(x)\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)i[H,\pi]\right)(x)$$
$$ = \frac{1}{2}\int d^3x \;\pi(x) \left(\sqrt{-\Delta +m^2}\varphi\right)(x) +  \frac{1}{2}\int d^3x \;\varphi(x) \left(\sqrt{-\Delta +m^2} \pi\right)(x)$$ $$- \frac{1}{2}\int d^3x \:((-\Delta +m^2)\varphi)(x)\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)\pi\right)(x) - \frac{1}{2}\int d^3x \:\pi(x)\left(\left(\frac{1}{\sqrt{-\Delta + m^2}}\right)(-\Delta+m^2)\varphi\right)(x)$$
$$= \frac{1}{2}\int d^3x \;\pi(x) \left(\sqrt{-\Delta +m^2}\varphi\right)(x) +  \frac{1}{2}\int d^3x \;\varphi(x) \left(\sqrt{-\Delta +m^2} \pi\right)(x)$$
$$- \frac{1}{2}\int d^3x \;\varphi(x) \left(\sqrt{-\Delta +m^2} \pi\right)(x)- \frac{1}{2}\int d^3x \;\pi(x) \left(\sqrt{-\Delta +m^2}\varphi\right)(x) =0\:.$$
Above I used the fac that $\sqrt{-\Delta+m^2}$ is selfadjoint and thus I can move it from one entry to the other one of the scalar product given by the variuos integrals appearing above.
