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.

  • 1
    $\begingroup$ How is the state presented to you? 2^5 amplitudes in the computational basis? As a circuit applied to a starting state? $\endgroup$
    – AHusain
    Commented Jul 10, 2019 at 21:58
  • $\begingroup$ @AHusain Presented as a 32 entry long vector tensor product of the 5 qubit states $\endgroup$
    – meelszz
    Commented Jul 10, 2019 at 22:26

3 Answers 3


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):
    for i in range(len(qubit2keep)):
    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))]
                        for q, m in zip(qubit_axis,minus_factor)]
    for i,j in minus_qubit_axis:
    if qubit_left>1:
    return rho_res

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.