Computing partial trace Given the matrix representation of an operator $T:\mathcal H_A \otimes \mathcal H_B \to \mathcal H_A \otimes \mathcal H_B$ on the tensor product of two HIlbert spaces $\mathcal H_A$ and $\mathcal H_B$, how do we compute the partial trace of $T$ in general?
I was reading that the partial trace is the operator $\text{tr}_B: \mathcal H_A \otimes \mathcal H_B \to \mathcal H_A $ such that for any $A: \mathcal H_A \to \mathcal H_A$ and $B: \mathcal H_B \to \mathcal H_B$ $$\text{tr}_B(A\otimes B)=\text{tr}(B)A.$$
This definition makes for easy computations if you have $T$ represented as a linear combination of tensor products of matrices on the individual Hilbert spaces $T=\sum_{ij} A_{ij} \otimes B_{ij}$. Is there a way to compute the partial trace without this expansion? i.e. Given a matrix $T$ with respect to some basis, can we compute $\text{tr}_B(T)$ directly?
 A: Consider first how to compute the trace of an operator. Given an orthonormal basis $\{e_1,...,e_n\}$ of a finite dimensional Hilbert space $\mathcal H$, we can compute the trace of an operator $A: \mathcal H \to \mathcal H$ as $$\text{tr}(A)= \sum_{j=1}^n \langle e_j,Ae_j \rangle$$ where $\langle \cdot,\cdot \rangle$ is the inner product on $\mathcal H$. Next think about how we represent $A$ as a matrix. Really what we do is some coordinate isomorphism $\mathcal H \cong \mathbb C^n$ where 
$$e_1 \mapsto \begin{pmatrix} 1 \\0 \\ \vdots \\ 0\end{pmatrix} \hspace{1cm} e_2 \mapsto \begin{pmatrix} 0 \\1 \\ \vdots \\ 0\end{pmatrix} \hspace{0.5cm} ... \hspace{0.5cm} e_n \mapsto \begin{pmatrix} 0 \\0 \\ \vdots \\ n\end{pmatrix} \hspace{1cm}$$ and the columns of $A$ represent it's action on each of the basis vectors. This basis is then orthonormal with respect to the standard inner product on $\mathbb C^n$: $$\Bigg\langle \begin{pmatrix} x_1 \\x_2 \\ \vdots \\ x_n\end{pmatrix},\begin{pmatrix} y_1 \\y_2 \\ \vdots \\ y_n\end{pmatrix} \Bigg\rangle = \begin{pmatrix} \overline{x_1} & \overline{x_2} & ... & \overline{x_n} \end{pmatrix}\begin{pmatrix} y_1 \\y_2 \\ \vdots \\ y_n\end{pmatrix}= \sum_{j=1}^n \overline{x_j} y_j $$ which we can also write as $\langle x,y\rangle= x^\dagger y$ for any $x,y \in \mathbb C^n$. Next we use the fact that the trace is independent of which orthonormal basis was originally chosen. In this formulation of constructing the matrix $A$ we get that $$\text{tr}(A)= \sum_{j=1}^n e_j^\dagger A e_j$$ which exactly corresponds to the sum of the diagonal elements of $A$ in the reference basis. 
This idea can be generalized for the partial trace as follows. Let $\{a_1,...,a_m \}$ and $\{ b_1,...,b_n\}$ be orthonormal bases for two Hilbert spaces $\mathcal H_A$ and $\mathcal H_B$ respectively. We choose the reference basis of $\mathcal H_A \otimes \mathcal H_B$ to be the natural choice $\{ a_1 \otimes b_1,a_1 \otimes b_2,a_1 \otimes b_3,...,a_m \otimes b_n\}$ and represent each of these as a column vector $$a_1 \otimes b_1 \mapsto \begin{pmatrix} 1 \\0 \\ \vdots \\ 0\end{pmatrix} \hspace{1cm} a_1 \otimes b_2 \mapsto \begin{pmatrix} 0 \\1 \\ \vdots \\ 0\end{pmatrix} \hspace{0.5cm} ... \hspace{0.5cm} a_m \otimes b_n \mapsto \begin{pmatrix} 0 \\0 \\ \vdots \\ n\end{pmatrix} \hspace{1cm}$$ in $\mathbb C^{mn}$. Then any linear operator $T:\mathcal H_A \otimes \mathcal H_B \to \mathcal H_A \otimes \mathcal H_B$ has a matrix representation as $T: \mathbb C^{mn} \to \mathbb C^{mn}$ as we constructed above. The partial trace can then be computed by performing this sum of inner products with the basis of $\mathcal H_B$ only $$\text{tr}_B(T)= \sum_{j=1}^n (\text{id}_A \otimes b_j^\dagger)T (\text{id}_A \otimes b_j) $$ where $\text{id}_A$ is the $m \times m$ identity matrix and $b_j$ is $j^{th}$ coordinate vector in $\mathbb C^n$. Each $\text{id}_A \otimes b_j$ is therefore a $mn \times m$ matrix and each $\text{id}_A \otimes b_j^\dagger$ is an $m \times mn$ matrix resulting in an operator on $\mathbb C^m$ from which you can reverse the coordinate isomorphism to get back to $\mathcal H_A$. So in summary


*

*Fix orthonormal bases $\{a_1,..,a_m\}$ and $\{b_1,...,b_n\}$ of $\mathcal H_A$ and $\mathcal H_B$. Write as column vectors in $\mathbb C^m$ and $\mathbb C^n$ respectively.

*Compute $\text{id}_A \otimes b_j$ and $\text{id}_A \otimes b_j^\dagger$ for each $j$ where $\text{id}_A$ is the $m \times m$ identity matrix.

*$\text{tr}_B(T)= \sum_{j=1}^n (\text{id}_A \otimes b_j^\dagger)T (\text{id}_A \otimes b_j) $
