# Where does the term $|\psi\rangle\langle\psi|$ come from while calculating the expectation value?

Suppose there're two systems $$A$$ and $$B$$, if we're in a pure state $$|\psi\rangle\in\mathbb{H}_a\otimes\mathbb{H}_b$$. Let $$\hat A$$ be an operator acting on $$\mathbb{H}_a$$ and $$|\psi\rangle=\sum_{i,j}\psi_{i,j}|a_i\rangle|b_j\rangle$$. The expectation value of A with respect to the state $$|\psi\rangle$$ can be calculated as $$\langle A\rangle_{\psi}= Tr_{\mathbb{H}_a\otimes\mathbb{H}_b}(A|\psi\rangle\langle\psi|)=\sum_{i,j}\langle a_i|\langle b_j|A\otimes\mathbb{1}_B|\psi\rangle\langle\psi|a_i\rangle|b_j\rangle=...$$

I'm very confused about where these two equalities coming from? In particular, where does the term $$|\psi\rangle\langle\psi|$$ in the sum come from? Can I understand its function as generating a $$\psi$$ matrix with different constants $$\psi_{i,j}$$?

You haven't really specified what you're happy with as a starting point. I suppose $$\langle A\rangle_\psi=\langle\psi|A\otimes 1_B|\psi\rangle?$$

To see that the other expressions are equivalent, I usually find it more helpful to work backwards. Let's say we start from $$\text{Tr}(A\otimes 1|\psi\rangle\langle \psi|),$$ well mathematically, we can take the trace by performing a sum over any orthonormal basis $$\{|u_i\rangle\}$$ in the Hilbert space, $$\text{Tr}(B)=\sum_i\langle u_i|B|u_i\rangle.$$ One such basis is the separable basis $$|a_i\rangle|b_j\rangle$$ which immediately gives you the second equality.

Another choice of basis has $$|u_1\rangle=|\psi\rangle$$, and all other states orthogonal. In this case $$\langle u_i|A\otimes 1|\psi\rangle\langle\psi|u_i\rangle=\delta_{i,1}\langle\psi|A\otimes 1|\psi\rangle,$$ as required to get the first equality, using the definition I gave above.

It is a variant of the formula for the average $$\langle A\rangle_\rho$$ of an operator $$A$$ measured on a state $$\rho$$

$$\langle A\rangle_\rho = \mathrm{tr}(A\rho).\tag1$$

In this case we are measuring an operator acting on the first subsystem, so $$\rho = \mathrm{tr}_B(|\psi\rangle\langle\psi|)$$. Substituting into $$(1)$$, we get

\begin{align} \langle A\rangle_\psi &= \mathrm{tr}(A \,\mathrm{tr}_B(|\psi\rangle\langle\psi|)) \\ &= \mathrm{tr}(\mathrm{tr}_B(A\otimes I |\psi\rangle\langle\psi|)) \\ &= \mathrm{tr}(A\otimes I |\psi\rangle\langle\psi|) \\ &= \mathrm{tr}(A|\psi\rangle\langle\psi|) \end{align}

where the first equality is the substitution, the second follows from the properties of partial trace, the third from the fact that tracing over subsystem $$B$$ followed by tracing over subsystem $$A$$ is equivalent to tracing over both of the subsystems and finally the fourth is a shortcut notation that allows to make identity implicit when composing operators defined on subsystems of a composite system.

The second equality follows from the fact that

$$\mathrm{tr} A = \sum_k \langle k|A|k\rangle$$

where $$|k\rangle$$ is any orthonormal basis. This can be proven by first choosing the eigenbasis of $$A$$ and then appealing to basis-independence of the trace.