🌱Aadam's Garden

Search

Search IconIcon to open search

OpenQASM

Last updated Jun 23, 2022

OpenQASM, where Open refers to the specification being open or freely available and QASM (pronounced kazm) stands for quantum assembly language, is a programming language that we can use to write quantum circuits. It look similar to a hardware description language like Verilog.

# Sources

# Examples

Source:: 2022 Introduction to Classical and Quantum Computing

Many companies are building NISQ device. In this chapter, we will lean to program IBM’s Quantum Computers because they are freely available to the public. Lessons learned from this toolkit can easily be used to learn other toolkits as well.

# IBM Quantum Experience

# Services

  • The arrangement or topology of qubits in a quantum processor can affect which Quantum Gates can be naturally applied.

# Circuit Composer

  • How to use Circuit Composer to design and run your circuits?

# Quantum Processor

  • It might not be possible to run our originally designed quantum circuit on an available quantum device due to the topology or gate set available to the processor, so the software will transpile or convert our circuit to an equivalent one that can be physically run.

# Simulator

  • If there are long queue times or the quantum processors available have too few qubits, we can the simulator ibm_qasm_simulator to run our circuit.
  • Results might not be 100% accurate, but they’ll be pretty close.

# Quantum Assembly Language

# OpenQASM

  • OpenQASM is the language that we can use to write our circuits programmatically.

# A simple program:

1
2
3
4
5
6
7
8
9
OPENQASM 2.0;

qreg q[3];
creg c[3];

U(pi, 0, pi) q[0];
CX [q0], q[1];

measure q -> c;
  • The first line specifies that it is an OpenQASM program, version 2.0.
  • Then we define a quantum register or array named $q$ consisting of $3$ qubits, $|q2⟩|q1⟩|q0⟩$, and a classical register named $c$ consisting of 3 bits, $c_2c_1c_0$, all of which are initially $\ket{0}$ and $0$ respectively.
  • After that, we apply a One-Qubit Quantum Gate $U(pi,0,pi)$ to qubit $q[0]$, where the one-qubit gate is parameterized as $$U(\theta, \phi, \lambda) = \begin{pmatrix}e^{-i(\phi + \lambda)/2}\cos(\theta/2) & -e^{-i(\phi - \lambda)/2}\sin(\theta/2) \\ e^{i(\phi - \lambda)/2}\sin(\theta/2) & e^{i(\phi + \lambda)/2}\cos(\theta/2) \end{pmatrix}.$$ Any one-qubit gate can be written this way, up to a global phase. In this example, when $(\theta, \phi, \lambda) = (\pi, 0, \pi)$, we get $$
    \begin{aligned} U(\pi, 0, \pi)
    &= \begin{pmatrix}e^{-i\pi/2}\cos(\pi/2) & -e^{-i\pi/2}\sin(\pi/2) \\ e^{i\pi/2}\sin(\pi/2) & e^{i\pi/2}\cos(\pi/2) \end{pmatrix} \newline
    &= \begin{pmatrix}0 & -i \\ -i & 0 \end{pmatrix}
    = -i \begin{pmatrix}0 & 1 \\ 1 & 0 \end{pmatrix} \newline
    &= -iX \equiv X.
    \end{aligned}$$ So, this gate transforms $q$ from $\ket{000}$ to $\ket{001}$.
  • Next, CNOT Gate ($CX$) is applied with $q[0]$ as the control and $q[1]$ as the target, transforming the state from $|001⟩$ to $|011⟩$.
  • Finally, $q$ is measured, and the resulting bits are placed in the classical register $c$.
  • $U(θ , φ , λ )$ and $CX$ are the only two gates that OpenQASM has built-in because they form a Universal Gate Set > Quantum Gate Sets.
  • We can define our own gates so that they are easier to use.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
OPENQASM 2.0;

// Define the Pauli X gate. 
gate x a 
{ 
	U(pi,0,pi) a; 
}


qreg q[3];
creg c[3];

x q[0];
CX [q0], q[1];

measure q -> c;

# Quantum Experience Standard Header

  • To have access to the commonly used quantum gates ( X, Y, Z, H) in OpenQASM, we can use/include a library called qelib1.inc (IBM Quantum Experience Standard Header).

# OpenQASM in IBM Quantum Experience

  • We can use both, drag-and-drop as well as code, to write quantum circuits in IBM Quantum Experience.

# Quantum Adder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
OPENQASM 2.0; 

// Include standard gates from IBM Quantum Experience. 
include "qelib1.inc"; 

// Define the quantum sum gate. 
gate sum cin, a, b 
{ 
    cx a, b; 
    cx cin, b; 
} 

// Define the quantum carry gate. 
gate carry cin, a, b, cout 
{ 
    ccx a, b, cout; 
    cx a, b; 
    ccx cin, b, cout; 
} 

// Define the inverse of the quantum carry gate. 
gate carrydg cin, a, b, cout 
{ 
    ccx cin, b, cout; 
    cx a, b; 
    ccx a, b, cout; 
} 

// Declare the quantum registers. 
qreg c[4]; 
qreg a[4]; 
qreg b[5]; 

// Declare the classical registers. 
creg bc[5]; 

// Set the input states by applying X gates. 
x a[1]; 
x a[2]; 
x a[3]; // a = 1110 
x b[0]; 
x b[1]; 
x b[3]; // b = 1011 

// Add the numbers so that |a>|b> becomes |a>|a+b>. 
carry c[0], a[0], b[0], c[1]; 
carry c[1], a[1], b[1], c[2]; 
carry c[2], a[2], b[2], c[3]; 
carry c[3], a[3], b[3], b[4]; 
cx a[3], b[3]; 
sum c[3], a[3], b[3];
carrydg c[2], a[2], b[2], c[3]; 
sum c[2], a[2], b[2]; 
carrydg c[1], a[1], b[1], c[2]; 
sum c[1], a[1], b[1]; 
carrydg c[0], a[0], b[0], c[1]; 
sum c[0], a[0], b[0]; 

// Measure the sum and put it in the classical register. 
measure b -> bc;
  • Because of the lack of error correction, this circuit/program is very noisy and doesn’t result in correct output.

# Qiskit

# Circuit Composer

  • Besides the Circuit Composer and OpenQASM, IBM has provided another way to program their quantum processors, called Qiskit.

# Quantum Lab

  • Quantum Lab gives us an interactive Jupyter Notebook environment to modify and run circuits according to our own specifications.

# Simulator

  • Aer is Qiskit library that contains different simulators.

  • To see the list of simulators, we can use the following command:

    1
    2
    
    from qiskit import Aer
    Aer.backends()
    

# Quantum Processor

  • To check which quantum processors are available, we can execute the following commands:

    1
    2
    3
    
    from qiskit import IBMQ
    provider = IBMQ.load_account()
    provider.backends()
    

# Other Quantum Programming Languages

  • Braket is Amazon‘s quantum computing platform, and they have their own Python SDK.
  • Cirq is Google‘s Python library.
  • Q-sharp, written Q# is Microsoft’s programming language for quantum computers.
  • Quil is a quantum “instruction set architecture” by Rigetti, similar to OpenQASM. PyQuil is a library for programming Quil in Python.

# Summary

  • Quantum computing is progressing from an academic research interest to a nascent industry, evident from the actual devices being made, and the tools being developed.

# Quantum Adder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
OPENQASM 2.0; 

// Include standard gates from IBM Quantum Experience. 
include "qelib1.inc"; 

// Define the quantum sum gate. 
gate sum cin, a, b 
{ 
    cx a, b; 
    cx cin, b; 
} 

// Define the quantum carry gate. 
gate carry cin, a, b, cout 
{ 
    ccx a, b, cout; 
    cx a, b; 
    ccx cin, b, cout; 
} 

// Define the inverse of the quantum carry gate. 
gate carrydg cin, a, b, cout 
{ 
    ccx cin, b, cout; 
    cx a, b; 
    ccx a, b, cout; 
} 

// Declare the quantum registers. 
qreg c[4]; 
qreg a[4]; 
qreg b[5]; 

// Declare the classical registers. 
creg bc[5]; 

// Set the input states by applying X gates. 
x a[1]; 
x a[2]; 
x a[3]; // a = 1110 
x b[0]; 
x b[1]; 
x b[3]; // b = 1011 

// Add the numbers so that |a>|b> becomes |a>|a+b>. 
carry c[0], a[0], b[0], c[1]; 
carry c[1], a[1], b[1], c[2]; 
carry c[2], a[2], b[2], c[3]; 
carry c[3], a[3], b[3], b[4]; 
cx a[3], b[3]; 
sum c[3], a[3], b[3];
carrydg c[2], a[2], b[2], c[3]; 
sum c[2], a[2], b[2]; 
carrydg c[1], a[1], b[1], c[2]; 
sum c[1], a[1], b[1]; 
carrydg c[0], a[0], b[0], c[1]; 
sum c[0], a[0], b[0]; 

// Measure the sum and put it in the classical register. 
measure b -> bc;
  • Because of the lack of error correction, this circuit/program is very noisy and doesn’t result in correct output.

# Syntax