QASM 2.0 : Programming Language for Quantum Computers

Introduction

QASM (pronounced kazm), or “Quantum Assembly Language”, is a programming language designed for describing quantum circuits.1 While traditional computers use bits (0 or 1) as their basic unit of information, quantum computers use quantum bits (qubits) that can exist in multiple states simultaneously. QASM provides a structured way to write instructions for manipulating these qubits and implementing quantum algorithms.

QASM has evolved through two major versions: 2.0 and 3.0. While QASM 3.0 introduces sophisticated features for developing complex quantum algorithms,2 QASM 2.0 maintains a straightforward and accessible structure.

This note explores QASM 2.0, focusing on its fundamental quantum operations and how they can be used to create quantum circuits.

Basic Structure of QASM 2.0

The basic structure of a QASM 2.0 program is as follows:

OPENQASM 2.0;
include "qelib1.inc";

// Declaration of quantum and classical registers
qreg q[n];
creg c[m];

// Quantum operations
// Measurements

Preamble

Every QASM 2.0 program must begin with two mandatory statements known as the “standard preamble”. These statements initialize the programming environment and provide access to essential quantum operations:

OPENQASM 2.0;

This mandatory declaration specifies that we’re using version 2.0 of QASM. It must be the first line of every program, serving as a version identifier that enables the compiler to properly interpret the code that follows.

include "qelib1.inc";

This statement imports the standard quantum gate library (qelib1), which provides commonly used quantum gates and operations. This library includes fundamental gates like X, H, CNOT, and others that we’ll explore later.

Register Declaration

QASM 2.0 uses two types of registers: quantum registers (qreg) for storing qubits and classical registers (creg) for storing classical bits. These registers are declared at the beginning of the program and specify how many bits they can store. For example:

qreg q[2];  // Register that can store 2 qubits
creg c[2];  // Register that can store 2 classical bits

In this example, we declare two registers: a quantum register q that can store 2 qubits and a classical register c that can store 2 classical bits. The quantum register will be used for quantum operations, while the classical register will store measurement results.

Basic Quantum Gates

Quantum gates are fundamental operations that manipulate qubits. Here are some of the most important quantum gates used in QASM:

X Gate (NOT Gate)

This gate flips the state of a qubit from |0⟩ to |1⟩ or vice versa, similar to a classical NOT gate. In QASM syntax:

x q[0]

H Gate (Hadamard)

Creates an equal superposition of |0⟩ and |1⟩ states. This gate is essential for many quantum algorithms as it enables quantum parallelism. In QASM syntax:

h q[0]

CNOT Gate (Controlled-X)

A two-qubit gate where the target qubit is flipped only if the control qubit is |1⟩. This gate is fundamental for creating entanglement between qubits. In QASM syntax:

cx q[0],q[1]

Measurement

Measurement is a crucial quantum operation that collapses a qubit’s quantum state and records the classical outcome (0 or 1) in a classical bit. When measuring a qubit in the computational basis, a qubit in superposition will randomly collapse to either |0⟩ or |1⟩ according to the probability amplitudes of the quantum state. In QASM, measurement is expressed as follows:

measure q[0] -> c[0];

This instruction measures the quantum state of qubit q[0] and stores the resulting classical outcome (0 or 1) in classical bit c[0].

Custom Gates

QASM 2.0 provides a powerful feature that allows you to define custom gates by combining multiple elementary quantum gates. These custom gates serve as reusable building blocks, making your quantum circuits more modular and easier to understand. By encapsulating common sequences of operations into named gates, you can express complex quantum transformations more elegantly and reduce repetition in your code:

gate mygate a,b {
  h a;
  cx a,b;
}

mygate q[0],q[1];

This example defines a custom gate called mygate that takes two qubit parameters (a and b). The gate implementation combines a Hadamard gate (h) applied to the first qubit (a) followed by a CNOT gate (cx) with a as the control and b as the target. This sequence of operations is encapsulated into a reusable quantum operation that can be applied to any pair of qubits. In the example usage, the custom gate is applied to qubits q[0] and q[1], which will create entanglement between them.

A Simple Example of QASM 2.0

Let’s explore a simple example that demonstrates one of the most fundamental quantum phenomena: the creation and measurement of a Bell state. A Bell state represents a maximally entangled pair of qubits, named after physicist John Stewart Bell, and serves as a cornerstone for many quantum computing applications, including quantum teleportation and superdense coding. Here’s how we can create one:

OPENQASM 2.0;
include "qelib1.inc";

qreg q[2];
creg c[2];

h q[0];
cx q[0],q[1];
measure q -> c;

Let’s analyze this example line by line:

  • The first two lines are the standard preamble that imports necessary QASM definitions:
    • OPENQASM 2.0; declares we’re using QASM version 2.0
    • include "qelib1.inc"; imports the standard quantum gate library
  • qreg q[2] and creg c[2] create our quantum and classical registers:
    • A 2-qubit quantum register q for our quantum operations
    • A 2-bit classical register c to store measurement results
  • h q[0] applies a Hadamard gate to the first qubit:
    • Transforms |0⟩ into (|0⟩ + |1⟩)/√2, creating an equal superposition
  • cx q[0],q[1] performs a controlled-NOT operation:
    • Uses q[0] as the control qubit and q[1] as the target
    • Creates the Bell state (|00⟩ + |11⟩)/√2, where both qubits are maximally entangled
  • measure q -> c performs a measurement on both qubits:
    • Collapses the quantum state
    • Records either 00 or 11 in the classical register with equal probability

This circuit creates and measures a Bell state, one of the most fundamental entangled quantum states. When measured, both qubits will always yield the same result (either both 0 or both 1), demonstrating quantum correlation that surpasses what’s possible with classical systems. This property, known as quantum entanglement, is a key resource for quantum computing applications like quantum teleportation and quantum cryptography.

Appendix: Quantum Gate Operations in QASM 2.0

QASM 2.0 provides a comprehensive set of quantum gate operations that form the building blocks of quantum circuits. The following sections detail the fundamental gates, their effects on quantum states, and their QASM syntax. Understanding these operations is crucial for constructing meaningful quantum algorithms and protocols.

Single Qubit Operations

X Gate (Bit Flip Gate)

Performs a 180-degree rotation around the X-axis of the Bloch sphere, transforming |0⟩ to |1⟩ and |1⟩ to |0⟩. This operation is analogous to a classical NOT gate and is represented by the Pauli-X matrix. The X gate is one of the fundamental Pauli gates and is essential for quantum state manipulation, particularly in quantum error correction and quantum algorithms requiring bit flips.

x q[0]

Y Gate (Pauli-Y Gate)

Performs a π rotation around the Y-axis of the Bloch sphere, transforming |0⟩ to i|1⟩ and |1⟩ to -i|0⟩. This operation is represented by the Pauli-Y matrix [[0,-i],[i,0]], which combines both a bit flip and a phase change. The Y gate complements the X and Z Pauli gates by providing rotations in a different basis, making it essential for quantum algorithms that require complete control over qubit states.

y q[0]

Z Gate (Phase Flip Gate)

Executes a π rotation around the Z-axis of the Bloch sphere. When applied to basis states, it leaves |0⟩ unchanged but adds a negative phase to |1⟩, transforming |0⟩ to |0⟩ and |1⟩ to -|1⟩. This operation is represented by the Pauli-Z matrix and is crucial for quantum phase manipulation. Unlike the X gate which flips bits, the Z gate only affects the quantum phase, making it essential for algorithms requiring phase interference.

z q[0]

H Gate (Hadamard Gate)

A fundamental quantum operation that creates equal superposition states. When applied to basis states, it transforms |0⟩ into (|0⟩ + |1⟩)/√2 and |1⟩ into (|0⟩ - |1⟩)/√2. The Hadamard gate is represented by a unitary matrix H = (1/√2)[[1,1],[1,-1]] and is essential for many quantum algorithms, including quantum Fourier transform and quantum phase estimation. It effectively rotates states between the Z and X bases, making it crucial for creating quantum interference effects and initializing qubits for quantum parallelism.

h q[0]

T Gate (π/4 Phase Gate)

Applies a π/4 phase rotation around the Z-axis of the Bloch sphere. When applied to basis states, it leaves |0⟩ unchanged and adds a phase of e^(iπ/4) to |1⟩. The T gate is represented by the matrix [[1,0],[0,e^(iπ/4)]] and is particularly important for universal quantum computation, as it enables the implementation of non-Clifford operations necessary for quantum error correction and fault-tolerant quantum computing.

t q[0]

S Gate (Phase Gate)

Implements a π/2 phase rotation around the Z-axis of the Bloch sphere. When applied to basis states, it leaves |0⟩ unchanged and adds a phase of i to |1⟩, transforming |0⟩ to |0⟩ and |1⟩ to i|1⟩. The S gate is represented by the matrix [[1,0],[0,i]] and is equivalent to the square root of the Z gate, making it useful for implementing phase-based quantum operations and constructing more complex quantum gates.

s q[0]

RX, RY, RZ Gates (Rotation Gates)

Perform arbitrary rotations around the X, Y, or Z axes of the Bloch sphere. These gates enable precise control over qubit states by rotating them by any specified angle θ. The RX gate rotates around the X-axis and is useful for bit flips with arbitrary angles, the RY gate rotates around the Y-axis enabling superposition with arbitrary amplitudes, and the RZ gate rotates around the Z-axis providing arbitrary phase shifts. These rotation gates are essential for implementing quantum algorithms that require fine-grained control over qubit transformations.

rx(θ) q[0]

U1, U2, U3 Gates (Universal Gates)

These gates form a complete set of single-qubit operations, allowing for arbitrary unitary transformations on a qubit. The U1 gate performs a phase rotation, U2 creates superposition states with relative phases, and U3 is the most general single-qubit gate that can implement any rotation in the Bloch sphere. Together, they provide a universal framework for single-qubit quantum control, with U3(θ,φ,λ) being able to reproduce any other single-qubit gate through appropriate parameter choices. The U1 and U2 gates are special cases of U3, optimized for specific types of transformations.

u3(θ,φ,λ) q[0]

Two Qubit Operations

CNOT (CX) Gate

A fundamental two-qubit quantum operation that performs a controlled-NOT operation. It flips the state of the target qubit (applies X gate) if and only if the control qubit is in state |1⟩, leaving it unchanged if the control qubit is |0⟩. The CNOT gate is represented by a 4x4 matrix and is essential for creating quantum entanglement between qubits. It serves as a building block for quantum error correction, quantum arithmetic, and many other quantum algorithms. Along with single-qubit gates, the CNOT gate forms a universal set for quantum computation.

cx q[0],q[1];

CZ Gate (Controlled-Z Gate)

Performs a controlled phase flip operation where the target qubit undergoes a Z gate operation (phase flip) only when the control qubit is in state |1⟩. When applied, it adds a phase of -1 if both qubits are in state |1⟩, leaving all other basis states unchanged. The CZ gate is symmetric, meaning either qubit can act as the control or target, and is particularly useful in quantum error correction protocols and creating specific types of quantum entanglement. It can be constructed from a CNOT gate sandwiched between two Hadamard gates on the target qubit.

cz q[0],q[1];

CY Gate (Controlled-Y Gate)

Performs a controlled-Y operation where the target qubit undergoes a Y gate operation (bit and phase flip) only when the control qubit is in state |1⟩. The Y gate component introduces both a bit flip and a phase change of i, making this gate useful for quantum algorithms requiring controlled rotations in multiple bases. Like other controlled operations, it’s essential for creating specific types of quantum entanglement and implementing complex quantum transformations.

cy q[0],q[1];

CH Gate (Controlled-Hadamard Gate)

Performs a controlled-Hadamard operation where the target qubit undergoes a Hadamard transformation only when the control qubit is in state |1⟩. This gate combines the properties of the Hadamard gate (creating superposition) with conditional execution, making it useful for quantum algorithms requiring controlled superposition states. Like the Hadamard gate, when activated, it transforms the target qubit’s basis states, creating equal superpositions with appropriate phase relationships.

ch q[0],q[1];

CU1 Gate (Controlled-U1 Gate)

Performs a controlled phase rotation where the target qubit undergoes a U1 phase rotation only when the control qubit is in state |1⟩. This gate applies a phase factor of e^(iλ) to the state |11⟩ while leaving all other computational basis states unchanged. The CU1 gate is useful for implementing controlled phase operations in quantum algorithms and can be used to construct more complex controlled unitary operations. Like other controlled gates, it plays an important role in quantum error correction and quantum simulation protocols.

cu1(λ) q[0],q[1];

CU3 Gate (Controlled-U3 Gate)

Performs a controlled version of the U3 gate, applying a general single-qubit unitary operation on the target qubit only when the control qubit is in state |1⟩. The U3 gate component allows for arbitrary rotations in the Bloch sphere through its three parameters (θ,φ,λ), making the CU3 gate one of the most versatile controlled operations. This gate can be used to implement any controlled single-qubit operation and is particularly valuable in quantum algorithms requiring precise controlled transformations.

cu3(θ,φ,λ) q[0],q[1];

CRZ Gate (Controlled-RZ Gate)

Performs a controlled Z-axis rotation where the target qubit undergoes an RZ rotation by angle θ only when the control qubit is in state |1⟩. This gate applies a phase rotation that varies with the rotation angle parameter, making it useful for implementing controlled phase operations with arbitrary angles. The CRZ gate is particularly valuable in quantum algorithms requiring precise controlled phase transformations and can be used as a building block for more complex quantum operations.

crz(θ) q[0],q[1];

Three Qubit Operation

CCX Gate (Toffoli Gate)

A fundamental three-qubit quantum operation that performs a controlled-controlled-NOT operation. The target qubit is flipped (X gate applied) if and only if both control qubits are in state |1⟩. This gate is universal for classical computation and, combined with single-qubit gates, can be used to construct any reversible Boolean function. The Toffoli gate is essential for quantum arithmetic operations, quantum error correction, and serves as a building block for more complex quantum algorithms. It can be decomposed into a sequence of one- and two-qubit gates.

ccx q[0],q[1],q[2];

Other Operations

Barrier

Creates an optimization boundary in the quantum circuit that prevents the compiler from reordering quantum operations across this boundary. This is useful for maintaining specific operation sequences, debugging quantum circuits, and ensuring certain quantum operations remain separated during circuit optimization. The barrier command does not perform any quantum operations itself but acts as a structural element in the circuit design.

barrier q[0],q[1];

Measure

Performs a quantum measurement operation that collapses a qubit’s quantum state into a classical basis state (|0⟩ or |1⟩) and stores the result in a classical bit. This operation is irreversible and essential for extracting information from quantum computations. The measurement follows the rules of quantum mechanics, where the probability of measuring |0⟩ or |1⟩ depends on the qubit’s quantum state before measurement. This operation is typically performed at the end of quantum algorithms to obtain classical results.

measure q[0] -> c[0];

  1. Cross, A. W., et al., (2017). Open Quantum Assembly Language. arXiv:1707.03429 ↩︎

  2. Cross, A. W., et al., (2022). OpenQASM 3: A broader and deeper quantum assembly language. arXiv:2104.14722 ↩︎

Posted :