paint-brush
Quantum Programming with Google Cirqby@Tanisha.Bassan
4,677 reads
4,677 reads

Quantum Programming with Google Cirq

by Tanisha BassanAugust 27th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The quantum computing research field has been booming for the past two years with an exponential growth in research papers, capital investment and growing number of startups. <em>Hint: great time to start learning about this technology and being prepared for the future.</em>

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Quantum Programming with Google Cirq
Tanisha Bassan HackerNoon profile picture

Using Cirq to create quantum circuits for NISQ quantum computers

The quantum computing research field has been booming for the past two years with an exponential growth in research papers, capital investment and growing number of startups. Hint: great time to start learning about this technology and being prepared for the future.

Google is taking initiative to join the “quantum race” and released a new software.

I will show a small demo of how it works and my thoughts near the end.

The software deveopled by Google is called Cirq, it was recently released this summer. It’s an open source platform to develop Noisy Intermediate-Scale Quantum (NISQ) algorithms. As coined by John Preskill, the NISQ era describes our current quantum computers that require error-correction and only have a small number of qubits. Cirq allows access for anyone to develop and conduct their own research into near-term applications for quantum computers.

Before we reach fault-tolerant quantum computers with millions of coherent qubits that can run Shor’s algorithm and DESTROY ALL PREMISES OF SECURITY AND BLOW THE WORLD UP IN FLAMES, (just kidding, luckily there is post-quantum cryptography) we need to work with noisy models. This means error-correction for qubits is a MUST or else many of our computations will output weird and strange answers under the affects of decoherence.

So it makes sense to do a small demo using Cirq on a error-correcting code. Specifically encoding the bit-flip code onto 3 qubits. This can be simulated on IBM’s Q experience but in this example I will use Cirq to re-create the same circuit. In the process you can learn the basic syntax for this specific software and make your own projects!

Unlike classical computers, quantum information cannot be copied and thus repetition methods of error-correction will not work on quantum states.

Instead information stored in a qubit is distributed among other qubits in an entangled state. This protects the superposition state and helps discover errors as qubits decohere over time.

In this case, the message stored in one qubit|ψ⟩=α|0⟩+β|1⟩ will be encoded onto 3 qubits such that |ψ⟩ = α|000⟩+β|111⟩. This means the message will be encoded in an entangled state and in equal superposition of 1√2(|0⟩+|1⟩). A deeper explanation can be found on IBM’s documentation. This tutorial just encodes the quantum states into the error correcting code however the project is still under construction as I learn how to decode errors after they have entered the system.

Step by step tutorial using Cirq:

This first step is to install Cirq either locally or in a python environment, can be easily done using pip install. See the full documentation.

Step #1:

To create our program we need to import the correct libraries.

Step #2:

After that define how many qubits your circuit will have, it is important to know how many qubits are needed for your computation. Always optimize the circuit because the bigger the number of qubits, the more susceptible they are to errors and quicker rate of decoherence.

We can visualize all the qubits using the GridQubit function so that it is easier to build our circuit.

This piece of codes checks for all different number of qubits and their position on grid and prints them out so I am aware of where I am placing all my gates on the circuit. It should print this:

Step #3:

After, define the circuit and all the gates which we are going to be applying.

For all my gates I defined an object and applied a certain gate on a specific qubit. For example, there are 4 different instances where the Hadamard gate is used and each one has different qubit on which its applied. There are shorter methods to combine all these operations however it was easier for me to understand my code by going step by step. This is not the only way to format the same gates.

Step #4:

As gates are applied they are stored on each circuits as moments that happen in sequence so the quantum computer knows which gates to apply first. I will define this below.

I created 7 different moments which are iterated through in sequence by a quantum computer. This essentially gives structure to how the circuit is built using different gates. A moment can have more than one gate applied but it cannot be on the same qubit since you can’t place more than one gate at the same place and time.

Step #5:

The gates and moments create our circuit, we can now print it out and visualize it.

The above code will print our circuit.

This is what the circuit looks like and you can see that it is essentially the exact same as IBM’s experiment except now we are coding the structure instead of using the drag and drop feature from Q experience.

My review:

This was my first attempt at using Cirq and building a circuit, so far it is a simple language and fairly easy to understand. Intuitively it makes sense and Cirq give you the ability to make all the code even simpler and write it in fewer lines of codes. Here is the link to my github repo if you would like to test the code as well.

Things to keep in mind:

  • Before coding your circuit, have a visual representation of what it will look like and where all the gates are applied
  • There are always different ways to do the same thing but more simplified, optimize your circuit and code for good practice, especially for our current fragile quantum computers
  • It’s helpful to print out your circuits as you build them to fix any errors right away

Hope this tutorial was helpful and now you can start creating your own quantum circuits. Reach out to me via LinkedIn, Twitter, or Personal Website and tell me about any interesting ideas/projects you are working on!