IBM Qiskit for beginners

What is IBM Qiskit?

IBM Qiskit is a software development kit for creating an application for quantum computing.

Install

It is very easy to install. Prepare the python language environment and,

pip3 install qiskit

The first tutorial

Let’s look at a simple tutorial to make superposition of qubit. Superposition is a state which the qubit can be 0 or 1 on 50% of possibility. The step to create a quantum circuit is,

  1. Initialization
  2. Make a quantum circuit
  3. Make a measurement

The Quantum Circuit

When we make an application on quantum computer, we have to make a quantum circuit.

All the qubits are initialized as 0 first.

|0> -----H-----

The upper circuit denote that the qubit start with value 0 and H gate applied for the qubit. Finally we need measurement which get the final result from the probability distribution.

The Sampling

First we see sampling. Sampling is to get the result repeating the circuit many times.

#Import the tools
from qiskit import *
from qiskit import Aer

#get the backend simulator
backend = Aer.get_backend('qasm_simulator')

#initialize the circuit
circ = QuantumCircuit(1,1)

#applying hadamard gate to the 0th qubit
circ.h(0)

#prepare the measurement
circ.measure(range(1),range(1))

#job execution, we do the same operation 1024 times
job = execute(circ, backend, shots=1024)

#get the result
result = job.result()

#get the total count of result from the result object
counts = result.get_counts(circ)

#see the counts
print(counts)

Now we have,

{'0': 492, '1': 532}

0 and 1 almost the same counts.

State Vector

Next we see at a specific function of simulator. The state vector is a function or vector to see how the probability of result is. This value can be get only on simulator and not from actual quantum computer.

from qiskit import *
from qiskit import Aer

#This time we prepare another statevector_simulator
backend = Aer.get_backend('statevector_simulator')

#initialize the circuit
circ = QuantumCircuit(1)

#hadamard gate on 0th qubit
circ.h(0)

#job execution
job = execute(circ, backend)

#get the result and show the state vector
result = job.result()
outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)

Now we have

[0.707+0.j 0.707+0.j]

The first column shows the possibility of value 0 and the second one as possibility of value 1. After the measurement we get 0.707*0.707 = 0.5 of value 0 and 0.707*0.707 = 0.5 of value 1.

Entanglement of qubits

The superposition is one specific character of quantum computer, and entanglement circuit is another specific character.

By using entanglement we can make a filter on the result and control the final result we want to get. Let’s see it.

from qiskit import *
from qiskit import Aer

backend = Aer.get_backend('qasm_simulator')

#Now we prepare 2 qubits
circ = QuantumCircuit(2,2)

#Applying hadamard gate on 0th qubit and cx gate on 0th and 1st qubit
circ.h(0)
circ.cx(0,1)

#measurement
circ.measure(range(2),range(2))
#job execution, get the result and show it
job = execute(circ, backend, shots=1024)
result = job.result()
counts = result.get_counts(circ)
print(counts)

By doing this circuit, we have the result as

{'00': 528, '11': 496}

Usually if we use 2qubits for the circuit, finally we have 00,01,10,11 of 4 possibility of final result.

Now we used entanglement of qubits and we get just 2 result from 4possibility of result. This is how to use entanglement of qubits.

Adder Circuit

Let’s see the basic adder circuit. Adder circuit is realize a simple adder on quantum circuit like 0+0=, 0+1=,…

The circuit is,

0-----*--*-----
0--X--*--|--*--
| | |
0-----|--X--X--M
0-----X--------M

All the qubits start from 0. X is X gate. * denote the controlled bit and M is measurement.

This circuit add upper 2qubits and get the result from lower 2 qubits. The circuit shows 0+1 = ??.

from qiskit import *
from qiskit import Aer

backend = Aer.get_backend('qasm_simulator')

circ = QuantumCircuit(4,4)

circ.x(1)
circ.ccx(0,1,3)
circ.cx(0,2)
circ.cx(1,2)

circ.measure(range(4),range(4))

job = execute(circ, backend, shots=1024)
result = job.result()
counts = result.get_counts(circ)
print(counts)

The result is,

{'0110': 1024}

This shows 0+1 = 01. And we see we can get the correct result. The ccx realize the carry of qubit.

Entangled adder circuit

Finally let’s see to use this adder circuit with hadamard gate together.

0--H--*--*-----
0--H--*--|--*--
| | |
0-----|--X--X--M
0-----X--------M

If we put hadamard gate on both of upper 2qubits, we get the final result as super position of all possibility of result of this adder circuit.

from qiskit import *
from qiskit import Aer

backend = Aer.get_backend('qasm_simulator')

circ = QuantumCircuit(4,4)

circ.h(0)
circ.h(1)
circ.ccx(0,1,3)
circ.cx(0,2)
circ.cx(1,2)

circ.measure(range(4),range(4))

job = execute(circ, backend, shots=1024)
result = job.result()
counts = result.get_counts(circ)
print(counts)

Now we have,

{'0000': 239, '0110': 259, '1011': 279, '0101': 247}

There is a little bit difficulty to read the result.

0+0 = 0 is very clear.

The next one is 0+1 = 1. The final number of the binary array is the 0th qubit. The order is reversed and we have to see the result from the end of the line.

The next one is 1+1 = 10. Reading the result from the end of the 1011.

The final one is 1+0 = 01.

As we use adder circuit and superposition together we can get a simple tutorial on quantum computer finally.

--

--