paint-brush
Quantum Programming: Getting from 0 to 1 using Cirqby@saurabh-dubey
561 reads
561 reads

Quantum Programming: Getting from 0 to 1 using Cirq

by Neural MonkMarch 26th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Quantum Programming: Getting from 0 to 1 using Cirq485 reads.485 reads. We all know that regular computers use bits 0 and 1 for storing data and processing tasks so for example if I have four bits in a row I can represent a bunch of numbers.Quantum bits know as Qubits are either 0 or 1 but it may still be 0 & 1 at the same time. Instead of taking millions of years with a regular computer you can do it in seconds with quantum computers. We use a technique called a growver operator to sweep away all the wrong answers and what you're left with is the right answer.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Quantum Programming: Getting from 0 to 1 using Cirq
Neural Monk HackerNoon profile picture

We all know that regular computers use bits 0 and 1 for storing data and processing tasks so for example if I have four bits in a row I can represent a bunch of numbers.

Let's get Started!

So, what are Quantum computers?

Understanding Quantum computers are pretty complicated and quite confusing. So I'm going to break it down in an easy way to understand it.

We all know that regular computers use bits 0 and 1 for storing data and processing tasks so for example if I have four bits in a row I can represent a bunch of numbers.

Quantum bits know as Qubits are either 0 or 1 but it may still be 0 & 1 at the same time.

I know! I know! it sounds very strange and confusing because it's not really quite plain to grasp, for example, let's imagine that a bit is sort of like a coin, it can either be heads or tails just like 0 or 1.

it can only be either heads or tails right?

What is it right now?

while it's in the air Is it Heads or tails ?

it's heads or tails now in a strange manner, it's heads and tails at the same moment, that doesn't even make sense before it falls in my palm as I see the coin, so then I can see what it's like, and that's the theory behind the quantum bit so it should be a 0 and a 1 at the same moment.

In simple words qubits are bits with two state and each state have some probability just like in the case of coins

You get it right!

How it is going to change the world?

Well now is where the fun begins let's suppose we have 4 bits with this we have 16 possibilities( means we can have 16 numbers)

let's say that I'm trying to crack a password the password I'm trying to crack is one of the numbers we can get with this bits.

A normal computer will take one number at a time and put it in the normal machine one by one till we got the right answer

what if we use a quantum computer so instead of putting in these four regular bits, We put in four quantum bits, Now remember each bit is both a 0 and a 1 that means these quantum bits are all the numbers all at the same time

So, when I put the quantum bits into my machine to find the right password what comes off the other end of machine is saying that I'm both right and wrong because We gave it both right and wrong answers at the same time.

We still want to know what the correct password is? right?

Well there's a technique called a growver operator, this is a real thing where you can sweep away all the wrong answers and what you're left behind with is the right answer

So that's the beauty of Quantum computing

I heard some people say that it will take the age of the universe to try and crack these codes

That's how secure they are but with a quantum computer you can try them all at the same time use the growver operator to sweep away all the wrong answers and what you're left with is the right answer.

So instead of taking millions of years with a regular computer you can do it in seconds with quantum computers.

Are you excited to write you first quantum program

Lets get started

# install latest version
!pip install cirq
import cirq
import numpy as np
from cirq import Circuit
from cirq.devices import GridQubit
# creating circuit with 5 qubit
length = 5

qubits = [cirq.GridQubit(i, j) for i in range(length) for j in range(length)]
print(qubits)

Applying Hadamard operation on every qubit

circuit = cirq.Circuit()
H1 = cirq.H(qubits[0])
H2 = cirq.H(qubits[1])
H3 = cirq.H(qubits[2])
H4 = cirq.H(qubits[3])
H5 = cirq.H(qubits[4])

Apply CNOT operation on (0, 1), (1,2), (2,3), (3,4) , swap at (0,4) , rotating X by with pi/2

C1 = cirq.CNOT(qubits[0],qubits[1])
C2 = cirq.CNOT(qubits[1],qubits[2])
C3 = cirq.CNOT(qubits[2],qubits[3])
C4 = cirq.CNOT(qubits[3],qubits[4])

#swap
S1 = cirq.SWAP(qubits[0],qubits[4])

#Rotation
X1 = cirq.X(qubits[0])
X2 = cirq.X(qubits[1])
X3 = cirq.X(qubits[2])
X4 = cirq.X(qubits[3])
X5 = cirq.X(qubits[4])

Creating the moment and printing the circuit

moment1 = cirq.Moment([H1])
moment2 = cirq.Moment([H2])
moment3 = cirq.Moment([H3])
moment4 = cirq.Moment([H4])
moment5 = cirq.Moment([H5])
moment6 = cirq.Moment([C1])
moment7 = cirq.Moment([C2])
moment8 = cirq.Moment([C3])
moment9 = cirq.Moment([S1])
moment10 = cirq.Moment([X1])
moment11 = cirq.Moment([X2])
moment12 = cirq.Moment([X3])
moment13 = cirq.Moment([X4])
moment14 = cirq.Moment([X5])

#circuit
circuit = cirq.Circuit((moment1, moment2, moment3, moment4, moment5 ,moment6 ,moment7, moment8, moment9, moment10, moment11, moment12, moment13, moment14))
print(circuit)

This is the quantum circuit you get, I will recommend you to try and play with it.

I hope it's helped you in some way, Thanks for reading!