# Find probability of a single qubit's measurement results from a 5 qubit state

I have a tensor product of a 5 qubit state $$|h\rangle$$. From this I want to calculate the probability of the 2nd qubit being in state $$|1\rangle$$. Can someone show me how I can do this? I know I can use the Born rule but I am not sure how. For context I am using Python and NumPy.

• How is the state presented to you? 2^5 amplitudes in the computational basis? As a circuit applied to a starting state? Jul 10 '19 at 21:58
• @AHusain Presented as a 32 entry long vector tensor product of the 5 qubit states Jul 10 '19 at 22:26

So probability of the second qubit being in state $$|1\rangle$$ is the probability of the 5 qubit system being in a state that has $$|1\rangle$$ as the second qubit.

So among all the 32 states, find the ones that have $$|1\rangle$$ in the second qubit, which will be half of them, for example $$|01100\rangle$$ and $$|11111\rangle$$. Add up the corresponding probabilities, which is the absolute square of the amplitudes presented to you in vector form.

Here's an example for a 3 qubit state:

$$\begin{matrix} 000 \\ 001 \\ 010 \\ 011 \\100 \\ 101 \\ 110 \\ 111 \\ \end{matrix} \begin{bmatrix} 0 \\0.577 \\ 0 \\ 0.577\\ 0\\ 0\\ 0\\ 0.577\\ \end{bmatrix}$$

In the above case, the probability that the second qubit is $$|1\rangle$$ is probability that it will be in $$|010\rangle, |011\rangle, |110\rangle$$ or $$|111\rangle$$, which is $$|0.577|^2 + |0.577|^2$$ which is 0.666.

Since you can get the state vector of the 5-qubit state, you can use the technique of -----partial-trace to get the state of a specific qubit(This git-hub code implements partial trace using only NumPy).

Here comes an example. Suppose you have a two-qubit state $$|\psi\rangle=|0\rangle|0\rangle$$. Then you produced a state $$|\phi\rangle=CNOT(H|0\rangle|0\rangle)$$ where the control of CNOT is the first qubit. This produces one of the Bell states, $$|\Phi^+\rangle$$. Then you traced out the first qubit, the remaining matrix, I/2 is the density matrix of the second qubit, and you can read the probability of getting each result(50% for $$|0\rangle$$).

The code for the upper example:

from qiskit import QuantumCircuit,QuantumRegister
from qiskit.quantum_info import DensityMatrix
import numpy as np
def partial_trace(rho,qubit2keep):
#https://gist.github.com/neversakura/d6a60b4bb2990d252e9e89e5629d5553
num_qubit=int(np.log2(rho.shape[0]))
for i in range(len(qubit2keep)):
qubit2keep[i]=num_qubit-1-qubit2keep[i]
qubit_axis=[(i,num_qubit+i) for i in range(num_qubit)
if i not in qubit2keep]
minus_factor=[(i,2*i) for i in range(len(qubit_axis))]
minus_qubit_axis=[(q[0]-m[0],q[1]-m[1])
for q, m in zip(qubit_axis,minus_factor)]
rho_res=np.reshape(rho,[2,2]*num_qubit)
qubit_left=num_qubit-len(qubit_axis)
for i,j in minus_qubit_axis:
rho_res=np.trace(rho_res,axis1=i,axis2=j)
if qubit_left>1:
rho_res=np.reshape(rho_res,[2**qubit_left]*2)
return rho_res
qr=QuantumRegister(2)
circ=QuantumCircuit(qr)
circ.h(qr[0])
circ.cx(qr[0],qr[1])
DM=DensityMatrix.from_instruction(circ)
print(DM.data)
PT=partial_trace(DM.data,[0])
print(PT)


I used a different to generate the state vector and the density matrix, besides I have done tiny adjustment on the partial_trace' code to reverse it's qubit alignment to fit my own code. The two print` operation prints

$$\begin{pmatrix}0.5&0&0&0.5\\0&0&0&0\\0&0&0&0\\0.5&0&0&0.5&\end{pmatrix}$$ and $$\begin{pmatrix}0.5&0.5\\0.5&0.5\end{pmatrix}$$.

The formula should be:

$$\langle h |( \mathbb{I} \otimes |1\rangle\langle1| \otimes \mathbb{I}\otimes \mathbb{I}\otimes \mathbb{I})|h\rangle.$$ Now, you can use python or numpy to build the measurement operator in the middle and calculate the above inner product to find the probability of the second qubit being 1.