paint-brush
Exploring Quantum Programming from "Hello World" to "Hello Quantum World"ā€‚by@hellorahulk
16,336 reads
16,336 reads

Exploring Quantum Programming from "Hello World" to "Hello Quantum World"

by Rahul Kumar
Rahul Kumar HackerNoon profile picture

Rahul Kumar

@hellorahulk

Chief AI Scientist

July 4th, 2018
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We have reached in an era where we can now implement basic AND, OR and XOR logics on quantum circuits similar to the classical computing and we call this era as <strong>Quantum Era</strong>.

Companies Mentioned

Mention Thumbnail
Discovery
Mention Thumbnail
Google
featured image - Exploring Quantum Programming from "Hello World" to "Hello Quantum World"
1x
Read by Dr. One voice-avatar

Listen to this story

Rahul Kumar HackerNoon profile picture
Rahul Kumar

Rahul Kumar

@hellorahulk

Chief AI Scientist

image

Introduction

We have reached in an era where we can now implement basic AND, OR and XOR logics on quantum circuits similar to the classical computing and we call this era as Quantum Era.

This article is a simple introduction to Applied Quantum Computing (AQC) where we will code a Hello World program on real quantum chip šŸ˜….

But wait ā€¦ I donā€™t have a quantum computer so how can I even do that ???

And there is a very simple solution for this, with companies like IBM and Google actually making quantum devices we can leverage them to build and test stuffs on real devices with ZERO setup of whatsoever.

So here is our tech stack :

  1. Google Colab : Cloud platform to run the code and execute the experiments.
  2. QISKit : A Python library for quantum programming.
  3. IBM Q: QuBits chipsets from IBM Q network .

image

So letā€™s get our hands entangled. šŸ˜‰

Background

What is Quantum Computer ?

Quantum computers are based on quantum bits, also known as qubits. They have two possible values (states) as 0 and 1, similar to the one we have in the classical computer. But the laws of quantum mechanics also allow other possibilities, which we call superposition states.

image

A Quantum Computer created by IBM combining the quantum registers.

Why Quantum Computing?

Quantum computing could enable exponential speedups for certain classes of problems by exploiting superposition and entanglement in the manipulation of quantum bits (qubits). One such example is using quantum computing in Artificial Intelligence space, we can implement an optimisation algorithm(s) which can use the properties of superposition and help in speeding up the optimisation problem which can eventually lead to better and faster learning algorithm(s).

image

Quantum Annealing Optimizer : Computing multiple local and global minima at a time.

What excites me about quantum computers?

Using quantum annealing in classical Boltzmann machine, the weight discovery process can be made quantum mechanical, which can help us to find pattern which we couldn't find using classical annealing process (SGD).

Which can be a key towards Artificial General Intelligence (AGI).

What is the current status in quantum landscape?

There have been lots of development made recently in the space of QC and all the major player in market are trying to make revolutionary break thru. Some of them are listed below:

image

Image Source : www.qilimanjaro.io

Implementing ā€œHello Quantum World !!!ā€

Letā€™s implement our first Hello World program using quantum registers.

Colab link : https://colab.research.google.com/drive/1gVet-CcDbsCgOjhtNg9gCvlE7PHIYuQP

Github link: https://github.com/goodrahstar/hello-quantum-world

To start with quantum programming we will use Open Source Quantum Information Science Kit (QISKit). Itā€™s a rapidly growing open source community making efforts to bring quantum computing easily accessible for larger audience.

For more: https://github.com/QISKit

pip install qiskit

image

Now that we have the package setupā€™ed lets get familiar with the programming paradigm in quantum world. There are 4 major components for quantum programming.

image

  1. Quantum Program : The environment to run the simulation/experiment.
  2. Quantum Circuit: The virtual circuit to setup the experiment.
  3. Quantum Registers: The register which consist of qubits.
  4. Classical Registers: Register containing bits.

We will initialize the quantum program, create a quantum register with 2 qubits and a classical register with 2 bits and set them into the circuit using below scripts:

from qiskit import QuantumProgram


**# Create a QuantumProgram object instance.**qp = QuantumProgram()


# Create a Quantum Register called "qr" with 2 qubits.qr = qp.create_quantum_register('qr',2)


# Create a Classical Register called "cr" with 2 bits.cr = qp.create_classical_register('cr',2)


**# Create a Quantum Circuit called "qc" involving qr and cr.**qc = qp.create_circuit('HelloWorldCircuit', [qr],[cr])

Once we have our circuit ready, we need to run it on a quantum computer. And here comes the real fun. šŸ˜ƒ

We will connect to a real quantum chipset and execute quantum operations on our HelloWorldCircuit. Here I am using IBM-Q deployed chipsets which has various options like:

They are still in development phase and by the time you are reading this article they may get deprecated. So to get updated list look here.

To access these chipsets you will need an account on IBM Quantum Experience. And generate the token from here.

image

Set the backend as ibmqx5, provide your token and setup the connection. If the token is valid and the backend is configured then you are all set to access the quantum power.

backend = 'ibmqx5'

token = 'a7dbfb3cfc1252c4a7555020c32808cff17102a467c595801371f7b7f1f7c3a3355d565469aa4a37564df269f3710f33d7d13ba3c900ca947c1513598b64c5e7'

qp.set_api(token,url='https://quantumexperience.ng.bluemix.net/api')

Now its time to compose our circuit and execute the experiment.

Steps to create our Hello World circuit:

  1. We will add the Hadamard gate (H) with 1 qubit for adding superposition property. H gate has the property to maps Xā†’Z, and Zā†’X.
  2. We add Controlled-NOT gate (CX) , a two-qubit gate that flips the target qubit if the control is in state 1. This gate is required to generate entanglement.
  3. A measurement gate to check the status.

image

Our Hello World circuit.


# Add the H gate in the Qubit 1, putting this qubit in superposition.qc.h(qr[1])


# Add the CX gate on control qubit 1 and target qubit 0, putting the qubits in a Bell state i.e entanglementqc.cx(qr[1], qr[0])



# Add a Measure gate to see the state.qc.measure(qr[0],cr[0])qc.measure(qr[1],cr[1])



# Compile and execute the Quantum Program in the ibmqx5results = qp.execute(['HelloWorldCircuit'] ,backend ,timeout=2400)print(results.get_counts('HelloWorldCircuit'))

Now when we examine the results you will see 4 quantum states 0000, 0001, 0010,0011 each having some probabilities associated with it.

So this depicts that all the four states co-exists at a given time.

{ā€˜00ā€™: 488, ā€˜01ā€™: 90, ā€˜10ā€™: 58, ā€˜11ā€™: 388}

image

Congratulations !!! šŸŽŠ šŸ™Œ You just experienced the 2 basic properties of quantum world i.e Superposition and Entanglement.

Future Reading

In this article we just touched the surface of quantum computing and there are lot more happening around. Here are some great links to keep up with the domain.

L O A D I N G
. . . comments & more!

About Author

Rahul Kumar HackerNoon profile picture
Rahul Kumar@hellorahulk
Chief AI Scientist

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Informburo
Primo

Mentioned in this story

X REMOVE AD