7.2 KiB
Quantum Programming Demonstration Exercises (Demo Templates) These are the basic examples that are provided with qisket's notebook for instance. Also programming quantum computers will be a very useful resource as well.
We will be demonstrating with IBM's language qisket. There are many others that we can use including tket, cirq , pyquil , and more.
A Note on Programming languages
Generally technical people are familiar with the concept of HTML, that which most of webpages are based on. In order to begin the process of getting a hands on taste of developing quantum technology, I refer to the introduction of creating the basic HTML-like construction of how to build a quantum program. This will be discussed further in the hands-on programming portion of the paper. As a true quantum programming language has not yet globally decided upon there are several major languages being permeated in the race to dominate as the most popular language. Some of these include :
- Qisket: an IBM quantum language module that provides a set of packages that builds on top of the popular Python programming language for easy adoption. This runs on the IBM-Q quantum computer backends.
- to see how this language looks like take a look at this file or refer to the appendix at the end of the paper This is how the language looks like, and is an example of plotting a histogram of the superposition of a 2 qubit circuit, running on the IBM-Q backend.
from qiskit import QuantumCircuit,Aer,transpile
from qiskit.visualization import plot_histogram
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
backend = Aer.get_backend("aer_simulator")
tqc = transpile(qc, backend)
job = backend.run(tqc, shots=1000)
result = job.result()
counts = result.get_counts(tqc)
plot_histogram(counts)”
Many more tutorials and applications can be found here.
- Q#: a Microsoft quantum programming language that also provides several libraries for expressing quantum algorithms and gate logic. This runs on the Microsoft Azure quantum hardware backends.
- to see how this language looks like -
This is how the language looks like, and is an example of random number generator using n-qubits, and would run on the Azure Quantum backend.
operation RandomNumberFramework(bitCount : Int) : Int { use qubits = Qubit[bitCount];
ApplyToEach(H, qubits);
let register = LittleEndian(qubits);
let randomNumber = MeasureInteger(register); return randomNumber;
}
- OpenQASM: stands for Open Quantum Assembly Language and was originally released by IBM back in 2017 and provides a more fundamental set of instruction-based codes as a direct intermediary to a quantum machine. This assembly language is what is running directly on the IBM-Q machines.
- to see how this language looks like -
This is how the language looks like, and is an example of generating 5 random bits, and would allow it to run on a 5-qubit hardware.
OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
creg c[5];
h q[0];
h q[1];
h q[2];
h q[3];
h q[4];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
measure q[3] -> c[3];
measure q[4] -> c[4];
Further resources --> qisket notebook, programming with quantum computers
Template Codes to get started.
- Exercise 1 - Here is a general outline of qisket circuit template. Transpiled the quantum circuit on the IBM-Q backend and draws the optimized circuit. This is a great first step to visualize what's going on. Custom code is what you input in the commented area and I give you samples for that in the next section. The code is found here
Output:
- Exercise 2 - Putting qubits in superposition using a "hadamard" gate operation. For this we also still use python but instead we run it on a photonic quantum computer. We will also perform measurment here and then error correct. This is the general template for that and we use the Xanadu backend to draw the optimized circuit.
This is how the language looks like, and is an example of plotting a histogram of the superposition of a 2 qubit circuit, running on the IBM-Q backend.
from qiskit import QuantumCircuit,Aer,transpile
from qiskit.visualization import plot_histogram
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
backend = Aer.get_backend("aer_simulator")
tqc = transpile(qc, backend)
job = backend.run(tqc, shots=1000)
result = job.result()
counts = result.get_counts(tqc)
plot_histogram(counts)”
Many more tutorials and applications can be found here.
-
Exercise 3 - Here we demonstrate the template for Shor's Algorithm.
- can be found in this lab
-
Exercise 4 - Here we demo the template for the Grover's Search
-
Exercise 5 - Here we demo the template for QAOA
-
Exercise 6 - this is how you generate a single random qubit
The following is done in OpenQASM code
// Programming Quantum Computers
// by Eric Johnston, Nic Harrigan and Mercedes Gimeno-Segovia
// O'Reilly Media
//
// More samples like this can be found at http://oreilly-qc.github.io
// Run this sample in the IBM Q Experience Circuit Composer
// at https://quantum-computing.ibm.com
// This sample generates a single random bit.
OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
creg c[5];
h q[0];
measure q[0] -> c[0];
as well as in Qisket:
## Programming Quantum Computers
## by Eric Johnston, Nic Harrigan and Mercedes Gimeno-Segovia
## O'Reilly Media
##
## More samples like this can be found at http://oreilly-qc.github.io
## This sample generates a single random bit.
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer, IBMQ, BasicAer
import math
## Uncomment the next line to see diagrams when running in a notebook
#%matplotlib inline
## Example 2-1: Random bit
# Set up the program
reg = QuantumRegister(1, name='reg')
reg_c = ClassicalRegister(1, name='regc')
qc = QuantumCircuit(reg, reg_c)
qc.reset(reg) # write the value 0
qc.h(reg) # put it into a superposition of 0 and 1
qc.measure(reg, reg_c) # read the result as a digital bit
backend = BasicAer.get_backend('statevector_simulator')
job = execute(qc, backend)
result = job.result()
counts = result.get_counts(qc)
print('counts:',counts)
outputstate = result.get_statevector(qc, decimals=3)
print(outputstate)
qc.draw() # draw the circuit
- Add to appendices
References: