Simulating Quantum Gates in Python
In this lesson, we shall define the quantum gates using NumPy and Python.
We'll cover the following...
In the last chapter, we saw the definitions of various quantum gates (shortened as ‘gates’ henceforth) and the operations they perform. As you would recall, gates are matrices. We can use NumPy to define multi-dimensional arrays
or lists
. In fact, we encourage you to try defining some basic gates studied earlier in the code editor below. Once you are confident, you can compare your implementations with ours and see how well you did.
import numpy as npimport math# Try defining the identity, pauli x/y/z, hadamard gates yourself# your code here.
Single-qubit gates
In this section, we will learn how to define the following single-qubit gates:
- Identity gate
- Pauli-X gate
- Pauli-Y gate
- Pauli-Z gate
- Hadamard gate
Identity gate
Starting with the simplest gate. We know the Identity Gate does not affect the state of a qubit. What goes in, however it goes in, comes out the same. Thus, on a single qubit, the Identity Gate is defined as follows:
We can use a 2x2
array in matrix
to easily define the above gate.
import numpy as npidentity = np.array([[1, 0],[0, 1]])'''another way of creating the gate above would be:identity = np.identity(2)here the 2 defines the dimensions we want'''print("Identity Gate: ")print(identity)
Pauli gates
We’ve studied three Pauli gates. First, the Pauli-X gate can be considered the classical NOT gate, whereby it simply interchanges the amplitudes of and in an arbitrary state . Visually, we can represent this process as rotating the qubit’s state vector by around the X-axis. The Pauli-X gate is defined as follows:
...