Cirq for Quantum Computing

May 20, 2022

This tutorial will give an overview of the framework and demonstrate to readers how they can use this framework to learn about Quantum computing.


To follow along with this tutorial, you need to have:

  • Basic knowledge in Quantum Computing.
  • Google Colab or Jupyter notebook.

We will use Google Colab for this build.


A Brief History

In 2019, Google announced that it had reached a quantum advantage. Quantum advantage is the threshold where a quantum computer can outperform a classical computer for a certain problem. This was a huge achievement in the world of quantum computers.

There is several Google Quantum Hardware that has been used by the Google Research team to date. Foxtail is one of the hardware devices that was released in 2016, Bristlecone is a 72-qubit device and was released in 2018, and most recently, Sycamore released in 2019.

Sycamore is the 54-qubit quantum hardware device that was used to reach the quantum advantage mentioned above. These results are published in the, Quantum supremacy using a programmable superconducting processor research paper in the Nature journal.

Cirq is a quantum computing programming language. It is an open-source Python framework for writing, manipulating, and optimizing quantum circuits. It is used to run these programs on simulators and real quantum computers.

Implementing an example

To get Cirq installed in your notebook, we will perform a quick pip install.

!pip install cirq

The next step involves importing Cirq into our notebook. We import it by writing the following code:

import cirq

Next, we need to define the two qubits that we need to use for this circuit.

x = cirq.NamedQubit("x")
y = cirq.NamedQubit("y")

With classical computing, there is a set of commonly known gates; AND, OR, NAND, NOR, and XOR. These gates perform any form of computing in classical computers. But, they are irreversible because the information is lost.

What do we mean when we say it is irreversible?

For example, if we do an AND gate and we get an output of 0. For this case, we don’t know if the first gate was 1 and the second gate was 0. Or that both of the two inputs were 0.

We tend to lose that information in this process. In addition, fanout, a technique used in classical computing, requires the cloning of states. But, this technique is prohibited by the no-cloning theorem in quantum computing.

Since we cannot use these sets of classical gates, we’ll use Quantum gates.

Quantum computing has its own set of special gates. You can learn about them here. But, two popular gates that enhance the power of quantum are superposition and quantum entanglement.

Using a Hadamard (H) gate, we can put a qubit into a superposition state. Additionally, using a CNOT gate to entangle these two qubits.

Let’s build the circuit by adding these two gates to our circuit.

q_circuit = cirq.Circuit(
    cirq.CNOT(x, y),
    cirq.measure(x, y)
  • cirq.measure(x, y): We use the measure() method and pass the two qubits that we want to measure.
  • cirq.H(x): We apply the Hadamard gate onto the x qubit.
  • cirq.CNOT(x, y): Qubit x is the control qubit while qubit y is the target qubit.

We can now print the circuit and take a look. We use Python’s print() method for this task.




From the image, we can see the Hadamard gate on qubit x, and the CNOT gate on qubit x and y. The M on both qubits represents the measurements.

Now that we have built the circuit, let’s run it on the simulator. We perform this step by writing the following code:

simulator = cirq.Simulator()
output =, repetitions = 20)

We usually run circuits a lot of time to get the results. That is why we are running the simulator for 20 repetitions.

The results of this run is shown below:

x,y=11011000000111111111, 11011000000111111111

The Hadamard gate puts the first qubit in equal superposition of 0 and 1. This means that there is a 50% chance of a qubit being 0 upon measurement and a 50% chance of it being 1 upon measurement.

What would happen if we wanted to add an X (a bit-flip) gate?

Let’s add an X gate to qubit y, and see whether our result will be different.

q_circuit = cirq.Circuit(
    cirq.CNOT(x, y),
    cirq.measure(x, y)



After repeating the above simulator steps shown above, we get the results:

x,y=00111100001111101000, 11000011110000010111

We can see that if qubit x is in the 1 state, CNOT activates and the initial 1 state becomes a 0 state.

We have successfully implemented a circuit using Cirq and harnessed the power of superposition and entanglement. You can use this link to access the complete code for this tutorial.

Wrapping up

Currently, Cirq does not give the public access to the real quantum computer that Google has. Unless you are among the approved partners. Even though you might not have access to their machines now, you can still use the Cirq programming language, build some quantum circuits, and pass them along to the quantum simulator.

Further reading

Peer Review Contributions by: Wilkister Mumbi