OpenQASM is a programming language to create Quantum Programs. Therefore, like any other programming language, OpenQASM has its rules and syntax. OpenQASM is inspired by the QASM or Quantum Assembly language and borrows concepts from C and Assembly language. In this chapter of the OpenQASM tutorial, you will be learning about the basic rules and syntax of OpenQASM programming language.
OpenQASM Statements
A statement expresses an action that is to be performed. Statements are the most fundamental unit of any program. A program can be thought of a sequence of one or many statements.
In OpenQASM, statements consist of the instruction to be performed and are always followed by a semicolon- ;
which marks the end of the statement.
The code below shows a sequence of abstract statements.
statement1;
statement2;
statement3;
The above sequence of statements can also be written as-
statement1; statement2; statement3;
Note– While both the methods of writing multiple statements is correct, the former method is preferred over the latter one.
Example
The code below consists of multiple statements each one containing an instruction to perform.
// First statement
qreg qubits[2];
// Second statement
h q[0]
// Third Statement
x q[1]
You will learn in later chapters about what these statements do.
OpenQASM Whitespaces
OpenQASM ignores whitespaces. Whitespaces as character that do not consist of any visible marks. Example of white space include spaces, horizontal tab, new line characters, etc.
Example
The following two code sections are the same. This is because OpenQASM ignores whitespaces, which in this case is extra spaces.
qreg qubits[2]; h q[0];
qreg qubits[2]; h q[0];
OpenQASM Identifiers
Identifiers are the names given to any user defined variable. There are different types of variables about which you will learn in later chapters of this tutorial.
All identifiers in the OpenQASM programming language must follow the following rules-
- All identifiers must start with a lower-case letter.
- Identifiers can contain alpha-numeric characters and underscore only.
- The name of an identifier cannot be a reserved keyword in OpenQASM.
For example, the following are valid identifier names- q
, c
, q0
, q_
, q_0
while the following are NOT valid identifier names- Q
, C
, Q0
, _Q
, 0_q
.
OpenQASM is a case-sensitive language. Therefore, for example, the identifier names q_a
, and q_A
are not the same.