Cirq Programming 1: Single Qubit Gates

Richard Wang
3 min readAug 6, 2020

I used the Cirq programming language to program single qubit gates after understanding chapter 6, “Quantum Gates” of Quantum Computing as a High School Module.

While doing this, I was able to create a quantum circuit that implemented the X Gate, and Hadamard Gate using Cirq language. In this blog post, I will explain the functions of the Hadamard Gate, and the X Gate and how to program them using Cirq.

Qubits have two definite states, 0 or 1, as well as a superposition state. In superposition, qubits are neither 0 nor 1, but instead they are both. The superposition has a certain probability of being 0 or 1. When a qubit in superposition is measured, the superposition of the qubit will collapse, and the qubit will fall into one of the definite states based on the probabilities in the superposition.

Let’s look first at the Hadamard Gate.

The Hadamard Gate is used to put qubits into a superposition state. When a Hadamard Gate is applied to a qubit, the qubit is put into a superposition with a 50/50 chance of falling back into 0 or 1 when measured. Another thing to note about the Hadamard Gate is that by applying it twice to a qubit, you can recover the initial state of the qubit. So, if a qubit started in the 1 state, and 2 Hadamard Gates were applied in succession, then the qubit would be measured as 1 100% of the time.

Using Google’s Cirq language, I was able to produce the set of a 50/50 chance of being 0 or 1 by just applying one Hadamard gate, which puts the qubit into superposition, and then measuring the qubit. The code and a sample output are shown below:

Code
Sample Output

Next, I will talk about the X Gate.

The first, and probably main function of this gate is that it is used to flip the definite state of the qubit. So, if a qubit is initially 0, then it would turn to 1 after an X Gate is applied to the qubit. The second function of the X Gate is that it can flip the probabilities of a qubit falling into a certain definite state when the qubit is in superposition. For example, if a qubit was in a superposition with a 30% chance of falling into 0 and 70% chance of falling into 1, applying an X Gate to the qubit in superposition will change the probability of the qubit falling into 0 to 70%, and the probability of the qubit falling into 1 will be 30%.

With the X Gate, you can produce an output of only 0 or 1 100% of the time. I did this by applying an X Gate to the qubit which flips the state from 0 to 1 and vice versa. Alternatively, I could also apply two Hadamard Gates to a qubit which will always recover the initial state of the qubit, something I learned in the course. The code and a sample output are shown below:

Code
Sample Output

--

--