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

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

by Rahul KumarJuly 4th, 2018
Read on Terminal Reader
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
Mention Thumbnail
featured image - Exploring Quantum Programming from "Hello World" to "Hello Quantum World"
Rahul Kumar HackerNoon profile picture

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Ā .

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.

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).

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 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

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.

  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.

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.

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}

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.