# Extract single qubit state from combined state in QuTiP

I would like to extract the state of single qubits from combined states, using QuTip (the Quantum Toolbox in Python. Is there some QuTiP function that will extract a single qubit state from a combined state?

### Example

For example, suppose I begin with two qubits, $|0\rangle$ and $|1\rangle$. I can create these in QuTiP (as q1 and q2) as follows:

q1 = basis(2,0)
q2 = basis(2,1)


This gives me:

$q1 = \left[\begin{array}{&} 1\\0\end{array}\right]$ and $q2 = \left[\begin{array}{&} 0\\1\end{array}\right]$.

I can then combine q1 and q2 using tensor multiplication:

q1q2 = tensor(q1, q2)


This gives me:

$q1 \otimes q2 = \left[\begin{array}{&} 0\\1\\0\\0 \end{array}\right]$

Now, suppose that I apply a Hadamard gate to q1, and Identity to q2. I can do this as follows:

H = hadamard_transform(1)
I = qeye(2)
HI = tensor(H, I)
result = HI * q1q2


This returns $H = \left[\begin{array}{&&}0.70710678&0.70710678\\0.70710678&-0.70710678\end{array}\right]$ and $I = \left[\begin{array}{&&}1&0\\0&1\end{array}\right]$,

with result:

\begin{align*}(H \otimes I)(q1 \otimes q2) &= \left[\begin{array}{&&&&} 0.70710678&0&0.70710678&0\\ 0&0.70710678&0&0.70710678\\ 0.70710678&0&-0.70710678&0\\ 0&0.70710678&0&-0.70710678\end{array}\right]\left[\begin{array}{&} 0\\1\\0\\0 \end{array}\right]\\ &= \left[\begin{array}{&} 0\\0.70710678\\0\\0.70710678 \end{array}\right]\end{align*}

By reading off the values from this matrix, q1 is in the state $\frac{|0\rangle + |1\rangle}{\sqrt{2}}$, and q2 is in the state $|1\rangle$. Put another way, if we write q1 in the form $\alpha|0\rangle + \beta|1\rangle$, we get $\alpha = \frac{1}{\sqrt{2}}$ and $\beta = \frac{1}{\sqrt{2}}$. And for q2 we get $\alpha = 0$ and $\beta = 1$.

### Extraction

Given a combined state, finding the values of $\alpha$ and $\beta$ for single qubits are what I mean by "extract a single qubit state from a combined state." I would like QuTiP to find those values for me. Of course in this simple case, I know I can do this by inspection; but in more complicated cases, I would like QuTiP to do it for me.

• I'd read qutip.org/docs/4.1/guide/guide-tensor.html . I think you can do what you want by using "ptrace" (partial trace, which basically gets rid of the degrees of freedom of the state that do not matter) to do what you want, but I'm not sure if that is really what you asked. Anyway, read that part of the documentation, and you might be good to go. You might have to fiddle with it a bit more tho. – Vendetta Nov 6 '17 at 17:36
• Note that the ptrace always return a density matrix even when the reduced state is pure. – Axel Dahlberg Nov 16 '17 at 7:16
• Indeed. I'd use the density matrix in both the pure and mixed cases, as it is just a matter of "getting used to it" and it is a simpler procedure. You can always calculate the purity (which also helps you check if the program is running correctly) tracing the square of the density matrix with, if I recall correctly, the ".tr()", or you can make another program to calculate that yourself without a lot of effort. – Vendetta Nov 16 '17 at 19:38

Before we talk about how to get QuTip to do what you're asking, we should make sure you understand what it means to "extract" one of the qubits" states. In general, such "extraction" is not possible.

## Combined quantum systems

The question, we have four dimensional vectors representing the states of the combined two-qubit system. Note that when you write a vector in terms of components, there's an underlying assumed basis for the vector space. For example, when the question writes $$\frac{1}{\sqrt{2}}\left[ \begin{array}{c} 0\\1\\0\\1 \end{array} \right] \, ,$$ this really means $$\frac{1}{\sqrt{2}} \left( 0 |00 \rangle + 1|01 \rangle + 0 | 10 \rangle + 1 |11 \rangle\right) \tag{\star}$$ where in each ket the first number refers to the state of $q_1$ and the second number refers to the state of $q_2$. The notation used here has the tensor multiplication implicitly, but if we write it explicitly we get \begin{align} & \frac{1}{\sqrt{2}} \left( 0 \left( |0 \rangle \otimes |0 \rangle \right) + 1 \left( |0 \rangle \otimes |1 \rangle \right) + 0 \left( |1 \rangle \otimes |0 \rangle \right) + 1 \left( |1 \rangle \otimes |1 \rangle \right) \right)\\ =& \frac{1}{\sqrt{2}} \left( \left |0 \rangle \otimes |1\rangle \right) + \left( |1\rangle \otimes |1\rangle \right) \right) \tag{$\star \star$} \end{align} where here in each term the first ket refers to $q_1$ and the second ket refers to $q_2$. anyway, the point is that we're using a basis set $\{|00\rangle, |01\rangle, |10\rangle, |11\rangle \}$. This is a very common choice of basis, but note that others are possible. The state $(\star \star)$ can be factored into $$\frac{1}{\sqrt{2}} \left(|0\rangle + |1\rangle \right) \otimes |1\rangle \, .$$ Because we can factor the combined state into a product of single individual qubit states, in this case it is meaningful to talk about the individual states. However, this is not generally the case. Consider, for example, a Bell state $$\frac{1}{2} \left( |0\rangle \otimes |0\rangle + |1\rangle \otimes |1\rangle \right) \, .$$ There is no way to factor this into a product of individual qubit states. States that can't be factored are called "entangled".$^{[a]}$

## How describe a sub-part of a combined system

Of course, if you make an entangled state, you can make measurements on just one of the qubits, so there must be some way to talk about the information contained in a single qubit without the second one even if the state can't be factored. To do this, we have to go beyond the usual state vector description of quantum systems and use a density matrix. Given a regular state $|\Psi\rangle$, the associated density matrix $\rho$ is $$\rho \equiv |\Psi \rangle \langle \Psi | \, .$$ If you have a regular (i.e. classical) probability distribution over a bunch of quantum states $\{|\Psi_i \rangle \}$ with probability $p_i$ for each one, then the density matrix is $$\sum_i p_i |\Psi_i \rangle \langle \Psi_i | \,.$$

What's the density matrix for the Bell state $(1/\sqrt{2})(|00\rangle + |11\rangle$)? Well, following the recipe, it's \begin{align} \rho_\text{Bell} =& \frac{1}{2} \left( |00\rangle \langle 00| +|00\rangle \langle 11| +|11\rangle \langle 00| +|11\rangle \langle 11| \right) \, . \end{align} Great, but what do we do with it? The Bell state is not factorable, which means that we cannot find a state vector for each qubit individually. However, we can find a density matrix for each qubit. This is done by taking the trace over the qubit you don't care about. A trace of a matrix is where you sum the diagonal elements, i.e. $$\text{Tr}M = \sum_n \langle n | M | n \rangle \, .$$ In our case, tracing over the second qubit picks out elements of $\rho_\text{Bell}$ where the state of the second qubit is the same (try it yourself to see why). The result is $$\rho_\text{Bell, first qubit only} = \frac{1}{2} \left( |0\rangle \langle 0| + |1 \rangle \langle 1 | \right) \, .$$ Remember when we said that a density matrix can be a sum of probabilistically weighted quantum states? This is a case of that: we have a (1/2) probability that the first qubit is in state $|0\rangle$ and a (1/2) probability that the first qubit is in state $|1\rangle$. These are not probability amplitudes. These are normal probabilities. This is an example where when you lose part of an entangled system (i.e. by ignoring the second qubit), you lose quantum coherence and wind up with classical probability distributions. This is intimately related to what happens when you measure a quantum system and get so-called "wavefunction collapse".

$[a]$: Note that there are bases in which the Bell state factors, but the states in those bases mix the two qubits. For example, a Bell state is factorable if you use the Bell states $$\{|00\rangle + |11\rangle, |00\rangle - |11\rangle, |01\rangle + |10\rangle, |01\rangle - |10\rangle \}$$ themselves as your basis.