Trace of an operator matrix (Quantum computation and quantum information) I'm reading the book Quantum computation and quantum information by Mike & Ike and I'm stuck at 2.60/2.61. There, the author says that, given the operator $A|ψ⟩⟨ψ|$, its trace is:
$${\rm tr}(A|\psi\rangle\langle\psi|) = \sum\limits_i\langle i|A|\psi\rangle\langle\psi|i\rangle$$
Why would that be true? Why can we rearrange the bras and kets like that?
 A: *

*Let $\{|i\rangle\}$ be an orthonormal basis for the Hilbert space of the system.  Then the trace of an operator $O$ is given by (See the Addendum below)
\begin{align}
  \mathrm {tr}(O) = \sum_i \langle i|O|i\rangle
\end{align}

*For a given state $|\psi\rangle$, we define an operator $P_\psi$ by
\begin{align}
  P_\psi|\phi\rangle = \langle\psi|\phi\rangle|\psi\rangle.
\end{align}
As a shorthand, we usually write $P_\psi = |\psi\rangle\langle\psi|$.

*Using steps 1 and 2, we compute:
\begin{align}
  \mathrm{tr}(A|\psi\rangle\langle\psi|)
  &= \mathrm{tr}(A P_\psi) \\
  &= \sum_i \langle i|AP_\psi|i\rangle\\
  &= \sum_i \langle i|A (\langle\psi|i\rangle|\psi\rangle)\\
  &= \sum_i \langle i|A|\psi\rangle\langle\psi|i\rangle
\end{align}
which is the desired result.
Addendum. (Formula for the trace)
For simplicity, I'll restrict the discussion to finite-dimensional vector spaces. Recall that if $O$ is a linear operator on a vector space $V$, and if $ \{|i\rangle\}$ is a basis for $V$, then the matrix elements $O_{ij}$ of $O$ with respect to this basis are defined by it's action on this basis as follows:
\begin{align}
  O|i\rangle = \sum_jO_{ji}|j\rangle. \tag{$\star$}
\end{align}
The trace of the linear operator with respect to this basis is then defined as the sum of its diagonal entries;
\begin{align}
  \mathrm{tr}(O) = \sum_i O_{ii}. \tag{$\star\star$}
\end{align}
Now it turns out that the trace is a basis-independent number, so we can simply refer to the trace of the the linear operator; it's just the trace with respect to any chosen basis.
Now, suppose that $V$ is equipped with an inner product, like in the case of Hilbert spaces, and let $\{|i\rangle\}$ be an orthonormal basis for $V$, then we can take the inner product of both sides of $(\star)$ with respect to an element $|k\rangle$ of the basis to obtain
\begin{align}
  \langle k|O|i\rangle = \sum_j \langle k|O_{ji}|j\rangle = \sum_j O_{ji}\langle k|j\rangle = \sum_jO_{ji}\delta_{jk} = O_{ki}
\end{align}
In other words, $\langle k|O|j\rangle$ gives precisely the matrix element $O_{kj}$ of $O$ in the given basis.  In particular, the diagonal entries are given by $\langle i|O|i\rangle$.  Plugging this into $(\star\star)$, we get
\begin{align}
  \mathrm{tr} (O) = \sum_i \langle i|O|i\rangle
\end{align}
as desired.
A: When we speak about traces we usually mean the linear function $tr : (\mathbb{H}\to_{lin}\mathbb{H})\to_{lin}\mathbb{C}$. Also we mean that in the space $\mathbb{H}\to_{lin}\mathbb{H}$ there are special functions $|i\rangle\langle j|$ which form an orthogonal basis, that is $\forall B\in \mathbb{H}\to_{lin}\mathbb{H} : \exists B_{ij}\in\mathbb{C}: B = \sum_{ij}B_{ij}|i\rangle\langle j|$ and that $|i\rangle$ and $|j\rangle$ are different names of the orthogonal basis vectors in $\mathbb{H}$.
Now we have a fact about inner product in $\mathbb{H}$ ($\delta_{ij}$ = 1 if $i=j$, 0 otherwise)
$$
\langle i|j\rangle =_1 \delta_{ij}
$$
We define our trace $tr$ for the basis functions like this:
$$
tr(|i\rangle\langle j|) =_2 \langle j|i\rangle 
$$
and automatically get from the one hand
$$
tr(B) = tr(\sum_{ij}B_{ij}|i\rangle\langle j|)=_{lin} \sum_{ij}B_{ij}tr(|i\rangle\langle j|) =_2 \sum_{ij}B_{ij} \langle j|i\rangle =_1 \sum_{ij}B_{ij}\delta_{ij} = \sum_{i}B_{ii}
$$
from the other hand (ignore differences in index names),
$$
\sum_{i}\langle i|B|i\rangle = \sum_{i}\sum_{mn}B_{mn}\langle i|m\rangle\langle n|i\rangle =_1 \sum_{i}\sum_{mn}B_{mn}\delta_{im}\delta_{ni} = \sum_{i}B_{ii}
$$
Finally, assume that $B = A|\psi\rangle\langle\psi|$ and get the desired $$
tr(A|\psi\rangle\langle\psi|) = \sum_{i}\langle i|A|\psi\rangle\langle\psi|i\rangle
$$
