What is $q$ in $\mathbb {E}_q$ in KL divergence? I am reading about $D_{KL}$ and I faced these equations which are confusing for me. Can someone explain what $q$ is doing as the index of expectation?
$$\log p(x)=\log \int_zp(x,z)=\log \int_z p(x,z) \frac{q(z)}{q(z)}=\log \Big( \mathbb {E}_q \frac{p(x,Z)}{q(z)} \Big)=\mathbb {E}_q[\log p(x,Z)]  - \mathbb {E}_q [\log q(Z)]$$
and also it is claimed that
$$D_{KL}\Big(q(z|x)||p(z|x)\Big)=\mathbb {E}_q[\log q(z|x)]-\mathbb {E}_q[\log p(x,z)]+\log p(x)$$
So, how this is related to the well-known definition of KL divergence?
$$D_{KL}(q||p)=H_p(q)-H(q)=\sum_xq(x)\log\Big(\frac{q(x)}{p(x)}\Big)$$
I see also this definiton but I cannot realize how these are related:
$$D_{KL}(q||p)=\mathbb {E}_q\Big[\log\frac{q(Z)}{p(Z|x)}\Big]$$
 A: q refers to the distribution of the random variable $Z$. When you take an expectation it is with respect to a random variable (Z) that must have a distribution, whether it be discrete or continuous.
Using the definition of KL-divergence, the second line says
$$\begin{split}D_{KL}\left(q(z|x)\|p(z|x)\right)&=\mathbb E_{q(z|x)}\left[\log \frac{q(z|x)}{p(z|x)}\right]\\
&=\mathbb E_{q(z|x)}\log q(z|x)-\mathbb E_{q(z|x)}\log p(z|x)\\
&=\mathbb E_{q(z|x)} \log q(z|x)-\mathbb E_{q(z|x)}\log\frac{p(z,x)}{p(x)}\\
&=\mathbb E_{q(z|x)}\log q(z|x)-\mathbb E_{q(z|x)}\log p(z, x)+\mathbb E_{q(z|x)} \log p(x)\\
&=\mathbb E_{q(z|x)}\log q(z|x)-\mathbb E_{q(z|x)}\log p(z, x)+\log p(x)\end{split}$$
where the last equality follows from the expectation of a constant is itself.
This relates to the "well-known" definition of KL-divergence if the distribution is discrete, in which case entropy is defined as $H(X)=\mathbb E_q[-\log (q(X))]=-\sum_{x\in\mathcal X}q(x)\log q(x)$ yielding
$$\begin{split}D_{KL}(q\|p)&=H_q(p)-H_q(q)\\
&=-\sum_{x\in\mathcal X}q(x)\log(p(x))+\sum_{x\in\mathcal X}q(x)\log(q(x))\\
&=\sum_{x\in\mathcal X}q(x)\log\frac{q(x)}{p(x)}\end{split}$$
