QASM originated as a language for formally defining a quantum circuit to render images for visualization purposes. As quantum computation evolved, the language was adopted as a way to specify quantum circuits as input to a quantum computer.
A QASM program declares the classical bits and qubits, describes the operations (gates) on those qubits and the measurements needed to obtain the classical result by inspecting the qubits. Many variants of QASM have seen the light since its inception as a mark-up language for generating images. Quantum Inspire uses cQASM 3.0. Whenever this site mentions QASM or cQASM, it is referring to cQASM 3.0 unless explicitly stated otherwise.
cQASM is used to describe relatively simple circuits, which is fine for the current generation of quantum computers. In the future, a higher level of abstraction will be required to deal with the billions of qubits needed to make up a practical quantum computer.
cQASM 3.0 is extensively described here on github pages
Basic Example
Let's start with an example where we create a Bell state to get a feel for the language:
The file starts with the specification of the cQASM version on line 1. This line is always present.
Line 3 is a single-line comment. Single-line comments start with a (//
), and everything after it until the end of current line is ignored by the cQASM parser. Use comments to document your code, so that someone else can understand what you were trying to do (note: someone else could also be your future self).
Line 5 and 6 show a multi-line comment starting with (/*
) and ending in (*/
)
Line 8 defines the size of the qubit register. In this simple example, two qubits are defined. Each qubit in the register is identified by its index. The first qubit has index 0 and the second qubit has index 1. When displaying qubits or bits, the (qu)bit with index 0 is the right-most one.
Line 9 defines the size of the bit register.
Line 11 contains the first quantum instruction, to prepare both qubits in their ground state. The Single-Gate Multiple-Qubits (SGMQ) syntax is used to address both qubits at once. Alternatively, we could have written line 11 as two lines.
Line 13 and 14 describe the quantum gates that form the circuit. First, a Hadamard gate is applied to the qubit with index 0, followed by a CNOT where the qubit with index 0 is the control qubit and the qubit with index 1 is the target qubit.
Finally, at line 16 the state of all qubits is measured along the Z-axis to obtain the final result.
Note that cQASM 3.0 is case-sensitive, whereas cQASM 1.0 was case-insensitive.
Static loops
Repeating a sub-circuit is an essential part of certain algorithms, such as the Grover algorithm. The current implementation does not yet include loops. An extensive set of loop instructions will be released quickly.