DEV Community

loading...
Cover image for Cirq and A Simple Quantum Circuit

Cirq and A Simple Quantum Circuit

dmarcr1997 profile image Damon Marc Rocha II ・3 min read

After discovering the world of quantum computers a few months ago I have been trying to take in as much information on them as possible. With the many free courses, Wikipedia pages, and dwave's leap program I can now say that I have a basic understanding of quantum computers.
However, this article is geared more towards using Cirq. In the future, I may compile all of my notes and write a series on quantum computers and circuits, but for now, this is it.

What is Cirq

So you may be asking what is cirq? Well, cirq is a software library used to create quantum circuits that you can then use to solve complex problems(I have not gotten this far yet but will have something on this in the near future).

Set Up

So first make sure you have python installed on your computer. If not then go to https://www.python.org/downloads/ and set it up for your system. Then once this is done go to your terminal and enter

pip install cirq
Enter fullscreen mode Exit fullscreen mode

I tried this with anaconda and pip3, but could only get it to work with pip. I did not research this issue, so if anyone was an idea as to why I am having this issue, please enlighten me.

Things You Should Know

Before we go any further let me explain a few things we will be using.
In a quantum circuit, there are various gates that increase or decrease the likelihood of the input(1 or 0) changing upon reaching its output. We are only going to use two in this article; the Hadamar Gate or H gate and Pauli's X Gate or just the X gate.
The H gate increases the probability of the input changing to fifty percent. So if a qubit's (don't worry I will get to what this is next) initial state is 1 then there is a fifty percent chance it will become a 0 once it reaches its output. The X gate just flips the state of the qubit(so 1 to 0, or 0 to 1).

At this point, you are probably wondering what a qubit is. Well to put it simply a qubit is short for a quantum bit, so like classical computer bits, it can have two states 1 or 0. However, since it is at the quantum level qubits have an advantage over classical bits; superposition. This means that unlike classical bits which must switch between 1 and 0 a qubit can be 1, 0, or both. If you want to read some more on this click here

Circuit Setup

Ok so with your new knowledge of qubits, and the two gates explained above lets set up a simple quantum circuit.

So first you need to import cirq, decide on the number of qubits, and create a new circuit.

import cirq

length = 3 #will produce length**2 qubits

qubits = [
   cirq.GridQubit(i, j) 
   for i in range(length) 
   for j in range(length)
] 
#This creates 9 grid qubits and gives 
#them positions (i, j) in the circuit
circuit = cirq.Circuit() #creates a blank circuit

Enter fullscreen mode Exit fullscreen mode

Then determine the gates that will be present with each qubit, and append them to the circuit

circuit.append(cirq.H(q) for q in qubits if (q.row + q.col) % 2 == 0) 
#append an H gate at qubit q if even position

circuit.append(cirq.X(q) for q in qubits if (q.row + q.col) % 2 != 0) 
#append a X gate at qubit q if odd position

print(circuit)
Enter fullscreen mode Exit fullscreen mode

Output:

(0, 0): ───H───

(0, 1): ───X───

(0, 2): ───H───

(1, 0): ───X───

(1, 1): ───H───

(1, 2): ───X───

(2, 0): ───H───

(2, 1): ───X───

(2, 2): ───H───

Enter fullscreen mode Exit fullscreen mode

There you go, the output above is a basic quantum circuit. As you can imagine this can be extended to much larger circuits. I recently created something like this and will go through its creation and purpose in a few weeks:

Alt Text

I hope you enjoyed this and I look forward to continuing writing on this subject in the future.

Discussion

pic
Editor guide