Beginning Quantum Programming - The Hadamard Gate


When I took the Quantum Systems and Communications certification course with the University of Chicago in October of 2022, I was immediately immersed in graduate course-level quantum mechanics I had long forgotten. The last time I touched quantum mechanics was when I graduated in 1989. As a result, there was a lot of trepidation and anxiety on my part to quickly bring myself back up to speed for the course. From that experience, I have compassion for anyone here wanting to learn quantum computer programming or being out of touch with this math level for many decades like I was. I am from an era where I learned to program 6502 microprocessors from Jim Butterfield and Rodney Zaks. Back when learning to program computers and hardware was fun! If you're learning, I will sequentially lay it all out for you here. Our journey with quantum circuit implementation will start with one of the fundamental circuits - the Hadamard gate. I highly recommend getting a notebook and a pencil and following along with the computations that are presented here.

Quantum circuits, for the most part, are unitary operators on the qubit basis state vector. The Hadamard gate, in its linear algebraic form, looks like this. 

H=12[1111]H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}

The Hadamard accepts one qubit as input. We will do some basic linear algebra to understand the Hadamard gate, a unitary operator. Then, if you are unfamiliar with linear algebra, I'll do a quick refresher on multiplying matrix A by basis state vector |x>. Remember from our previous discussion on qubits that a qubit is a basis state vector. The qubit is represented using Dirac notation as a Ket vector, |0>, or |1>. The Ket vectors are column vectors.

|x>=[c01c11]|x>=\begin{bmatrix} c01 \\ c11 \end{bmatrix}

For the qubit basis state vector of state 0, we have,

|0>=[10]|0>=\begin{bmatrix} 1 \\ 0 \end{bmatrix}

And for a qubit basis state vector of state 1,

|1>=[01]|0>=\begin{bmatrix} 1 \\ 0 \end{bmatrix}

Here's a quick matrix multiplication refresher. We will use this method to multiply the Hadamard gate by the basis state vector.

Example: A|x>

Given matrix A:

A=[r01r02r11r12]A=\begin{bmatrix} r01 & r02 \\ r11 & r12 \end{bmatrix} 

 and state basis vector |x>,

|x>=[c01c11]|x>=\begin{bmatrix} c01 \\ c11 \end{bmatrix}

Multiplying, we have,

A|x>=[r01r02r11r12][c01c11]=[(r01*c01)+(r11*c11)(r11*c01)+(r12*c11)]A|x> = \begin{bmatrix} r01 & r02 \\ r11 & r12 \end{bmatrix} \begin{bmatrix} c01 \\ c11 \end{bmatrix} = \begin{bmatrix} (r01 * c01) + (r11 * c11) \\ (r11 * c01) + (r12 * c11) \end{bmatrix}

Just as we calculated A|x>, we will apply this same approach to determine H|0>.

H|0>=12[1111][10]=12[(1*1)+(1*0)(1*1)+(1*0)]=12[11]H|0> = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \frac{1}{\sqrt{2}} \begin{bmatrix} (1 * 1) + (1*0)\\ (1 * 1) + (-1 * 0) \end{bmatrix} = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix}

Now let's put our solution into Dirac notation.

=12(|0>+|1>)=12|0>+12|1>= \frac{1}{\sqrt{2}}(|0> + |1>) = \frac{1}{\sqrt{2}}|0> + \frac{1}{\sqrt{2}}|1>

You're probably wondering how we put the solution back into Dirac notation. From our matrix multiplication we have,

H|0>=12[11]H|0> = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix}

to get the Dirac form,

H|0>=12(|0>+|1>)=12|0>+12|1>H|0> = \frac{1}{\sqrt{2}}(|0> + |1>) = \frac{1}{\sqrt{2}}|0> + \frac{1}{\sqrt{2}}|1>

In my old days as a student in Physics, I had a professor who would do a mathematical derivation that would span over several chalkboards around the classroom. Then, he would complete his work and sarcastically proclaim, "the solution is intuitively obvious to even the most casual observer!"

This circumstance is one of those occasions. When I first encountered this in October of 2022, I was bewildered about how the instructor kept doing these matrix computations and doing all the Dirac notation in his head. It didn't help for those of us trying to follow along. My instructor thought the older guys in the class were still fully versed in Dirac notation. So let's look at the steps to put the matrix multiplication back into Dirac notation form.

|0>+|1>=[10]+[01]=[11]|0> + |1> = \begin{bmatrix} 1 \\ 0 \end{bmatrix} + \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}

Therefore, we have,

H|0>=12[11]=12(|0>+|1>)=12|0>+12|1>H|0> = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \frac{1}{\sqrt{2}}(|0> + |1>) = \frac{1}{\sqrt{2}}|0> + \frac{1}{\sqrt{2}}|1>

Mathematically, our qubit with state |0> is now in superposition. It has 1/2 probability of being measured in state 0, and 1/2 probability of being measured in state 1.

So what happens when we apply the Hadamard gate on the qubit with basis state vector |1>?

H|1>=12[1111][01]=12[11]H|1> = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ -1 \end{bmatrix}

Now express this in Dirac notation (read this as a reverse decomposition).

|0>|1>=[10][01]=[11]|0> - |1> = \begin{bmatrix} 1 \\ 0 \end{bmatrix} - \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 1 \\ -1 \end{bmatrix}

Now we have the Hadamard for |1>,

H|1>=12[11]=12(|0>|1>)=12|0>12|1>H|1> = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ -1 \end{bmatrix} = \frac{1}{\sqrt{2}}(|0> - |1>) = \frac{1}{\sqrt{2}}|0> - \frac{1}{\sqrt{2}}|1>


The qubit with state |1> is now in superposition in direction 1. It has 1/2 probability of being measured in state 0, and 1/2 probability of being measured in state 1.

For you pragmatists, here is a short and simple Youtube video on the linear algebra computations on the Hadamard Gate.

Superposition

The diagram below graphically depicts the computations we just performed with H|0> and H|1>.

Notice that the Hadamard gate is what we use to put our qubit into superposition. The figure above shows that the Hadamard rotates the qubit 180 degrees in the XZ plane. The Hadamard gate acts like an X-gate (Pauli gates), an operation we still need to discuss in a future class session. It works like an X-gate and then applies a 90-degree rotation around the Y-axis.

H|0> = |+>    called the plus state.

H|1> = |->   called the minus state.

So what is superposition? This mathematical operation is something you see used a lot in quantum mechanics. If we only operate on one qubit, we know the basis state is either |0> or |1>. However, if we work with two qubits, the basis states can vary from being in state |0>, or both being in state |1>, or one qubit being in state |0> with the other having a state of |1>. Two or more qubits having the ability to be in multiple states simultaneously is called superposition. In this case, we use superposition, a weighted sum of the states of two or more qubits.

The concept of superposition is where the qubit differs from the classical, digital logic bit. A "bit" can only be in one logical state of 0 or 1, but not both, whereas a qubit can be in a superposition of both states. With qubits, we are working with probabilities. Did we roll our qubit dice and measure a state of 0 occurring 50% of the time, or did we observe 1 in 50% of our measurements?

Mathematically, this means,

H|0>=12(|0>+|1>)H|0> = \frac{1}{\sqrt{2}}(|0> + |1>)

Calculating the probability,

(12)2=12(\frac{1}{\sqrt{2}})^2 = \frac{1}{2}

We have a 1/2 chance that the qubit is in the |0> state and a 1/2 chance the qubit is in the |1> state. 

So this is how the Hadamard gate creates a superposition state for our qubit.

If you're new to quantum computing and quantum mechanics, don't be discouraged by the technical complexity of the concept of superposition. The walkaway for right now is that the Hadamard gate puts the qubit into superposition.

Just make sure you understand the linear algebra concepts presented here. Then, as I mentioned earlier, get yourself a notebook, and follow the examples I showed above, so you know the methodology of the Hadamard gate. This practice will be beneficial when you start building circuits and understand what these unitary circuit operators are doing.

Reversible Computing and the Hadamard Gate

One of the fascinating features of the Hadamard gate is that applying a second Hadamard returns the initial input state. This behavior occurs because the HH computation results in an identity matrix.

For example, let's apply the Hadamard gate to our already familiar state vectors of |0> and |1>. We get the superposition vectors we expected,

H|0>=[1212]H|0> = \begin{bmatrix} \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} \end{bmatrix}

H|1>=[1212]H|1> = \begin{bmatrix} \frac{1}{\sqrt{2}} \\ -\frac{1}{\sqrt{2}} \end{bmatrix}

Let's apply the Hadamard gate a second time on the state vectors of H|0> and H|1>. This unitary operation returns the state vectors to their original state.

H(H|0>)=[12121212][1212]=[10]=|0>H(H|0>) = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \begin{bmatrix} \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} \end{bmatrix} = \begin{bmatrix} 1 \\ 0 \end{bmatrix} = |0>

H(H|1>)=[12121212][1212]=[01]=|1>H(H|1>) = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \begin{bmatrix} \frac{1}{\sqrt{2}} \\ -\frac{1}{\sqrt{2}} \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \end{bmatrix} = |1>

What is happening is that the -1 element in the lower right corner of the Hadamard matirx makes the computation reversible. This is a feature we will see a couple of more times in our quantum computing endeavors. Reversible computations are something unique that quantum computers bring to the table. We can have an answer of unknown input and reverse computation to determine the input value.

The Hadamard Gate Programming Labs and Exercises

Now let's have some fun and play with qubits. Load the following IBM Qiskit python project,

Entangled-03-The-Hadamard-Gate.ipynb

into your Google Colab environment. Let's make some observations about the behavior of the Hadamard gate. If you're new to the Entangled lab exercises, instructions on running the Qiskit project in the Google Colab browser environment. We will be running the code in a virtual machine.

Lab 1a. Create the basic quantum circuit of only one qubit with state |0>
Apply the Hadamard gate, H|0>.

Lab 1b. Examine the state vector H|0>.
Do you see the expected results for the state vector?

Note: the state vector is a complex number. 1 + 0j  1 is the real part, and the 0j represents the imaginary part.

Lab 2. Plot the Bloch sphere of H|0>.
Is the state vector |+> or |- >?

Lab 3. Examine the state vector of H|1>.
Do you see the expected results for the state vector?

Lab 4. Display the Bloch sphere for H|1>.
Is the state vector |+> or |- >?

Lab 5. Apply the Hadamard gate twice on the qubit with state vector |0>.
What happened? Did you get the state vector |0>?
Now try using the state vector of |1>. What did you get for your state vector?
Is the Hadamard gate a reversible function?

End of class for today.

If you're a high school or STEM program in the California bay area looking for an instructor in quantum computing send me an email. If you're a corporation looking for a trainer to explain quantum computing in simple English reach out to me I would love to come train! 

Enjoy!

Mike



Comments

Popular posts from this blog

Welcome to Entangled - A Leisure Discourse on Learning Quantum Computing

Coding Your First Quantum Computer Program