Simulating Quantum States in Python
In this chapter, we'll see how we can represent and simulate quantum states in Python.
For this chapter, we shall use NumPy to create and manipulate arrays and matrices. While it certainly helps to be familiar with NumPy, you should be able to follow these tutorials even without much experience with the library. We shall soon shift to proper libraries dedicated to quantum computing, but it helps to familiarize yourself with what goes on under the hood before using library functions. This chapter and its code examples will also form the basis of creating our very own quantum computing simulator at the end.
Representing the computational basis states
Recall that a quantum state is a vector in 2-D complex space. A vector can be easily represented in Python using lists. As stated earlier, we’ll be using NumPy for this.
Below is an example of the states , and .
import numpy as npzero = np.array([[1, 0]]).Tone = np.array([[0, 1]]).Tprint("State |0> is:")print(zero)print("State |1> is:")print(one)
In the code example above, you would notice that we are providing the complex entries of a state vector that is generalized as . We define and with [1,0] and [0,1] in the declarations above.
Notice that we create a two-dimensional column vector (since kets are column vectors), that’s the reason we needed to take the transpose while initializing the and . To verify that these are indeed column vectors, you can print the np.array.shape
attribute, ...