Simulating Measurement in Python

In this lesson, we look at measuring qubits and reading the results of our quantum computations.

In this chapter, we started by creating quantum states and gates in NumPy. We then put those together and saw how one could apply gates to single and multi-qubit systems. Finally, in this lesson, we shall complete this computation puzzle and measure the state of our quantum system.

Measurement under the hood

Recall that measurement takes qubits to the computational basis states 0|0\rangle and 1|1\rangle. Intuitively, this meant that we were transferring information of the qubit state in a classical register whose value we could read. Mathematically, this process is defined as taking the inner product of an arbitrary state ϕ|\phi\rangle with the conjugate transpose of the x|x\rangle, which is the state we want to measure ψ|\psi\rangle in:

p(x)=xϕ2p(|x\rangle)=|\langle x|\phi\rangle|^2

This operation is easily implemented in NumPy. The code below shows the probability of measuring state ϕ=120i21|\phi\rangle=\frac{1}{\sqrt{2}}|0\rangle-\frac{i}{\sqrt{2}}|1\rangle in the state 1|1\rangle.

Get hands-on with 1200+ tech skills courses.