...

/

Simulating Quantum Gates in Python

Simulating Quantum Gates in Python

In this lesson, we shall define the quantum gates using NumPy and Python.

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.

Press + to interact
import numpy as np
import 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 II 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:

I=[1001]I=\begin{bmatrix}1&0\\0&1\end{bmatrix}

We can use a 2x2 array in matrix to easily define the above gate.

Press + to interact
import numpy as np
identity = 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 XX can be considered the classical NOT gate, whereby it simply interchanges the amplitudes of 0|0\rangle and 1|1\rangle in an arbitrary state α0+β1\alpha|0\rangle+\beta|1\rangle. Visually, we can represent this process as rotating the qubit’s state vector by π\pi around the X-axis. The Pauli-X gate is defined as follows:

X=[0110]X=\begin{bmatrix}0&1\\1&0\end{bmatrix} ...

Access this course and 1400+ top-rated courses and projects.