OpenQASM 3 Comments

OpenQASM comments are used to prevent an instructions or instructions from being executed. In this chapter of the OpenQASM 3 Tutorial, you will be learning about comments in OpenQASM 3 and using them. The use of comments for preventing execution of a certain part of the code can be for various reasons such as-

  • Enhancing the readability of the code by adding human readable logic inside the program file.
  • To stop the execution of a certain part of the code for testing or debugging purposes.

OpenQASM 3 supports both single and multi-line comments.

Single Line Comments

Single line comments start with the double-forward slash- // and end with the end of that respective line.

Example

Below is an example of a single line comment in OpenQASM 3.0

// An example of a single line comment

Single line comments can also occur after an instruction.

Example

Below is an example of a single line comments that comes after an instruction for initializing a Quantum Register of 4 Qubits.

qubit[4] qubits;    //This instruction declares a Quantum Register of 4 Qubits

Single line comments can be used multiple times to create multiple line comments.

Example

Below is an example in which several single line comments are used multiple times.

// This is a single line comment
// However multiple single lines comment can be quite painful
// Therefore, multiline comments also exist

Since it can be quite difficult to create multiple single line comments, there also exist multi-line comments in OpenQASM 3.0.

This feature of multi-line comments was added in OpenQASM 3.0, prior to that, several single line comments were created instead of a single multi-line comment.

These multi-line comments are also called Comment Blocks.

Multi Line Comments

Multi line comments start with a /* and end with a */

Example

Below is an example of a multi-line comment or a comment block in OpenQASM 3.0

/*
This is an example of a multi-line comment.
It is much more convenient to write multiple-line
comments in this way
*/

OpenQASM multi-line comments cannot be nested.

Example

In the example below, we have tried to create nested multi-line comments. Since multi-line comments cannot be nested, this is not a valid multi-line comment.

/* In this example, we are 
trying to /* next multi-line comments
However, nesting of multi-line comments is not allowed */
Therefore, this is not a valid comment. */

Note– Multi-line comments cannot be nested because the multi line comments ends with a */. Therefore, the first */ marks the end of the comment and anything following it is not a part of the comment.

Example

The example below is a valid multi-line comment. This is because it contains only a single */.

/* This is a valid multi-line comment
/* This is because irrespective of the
/* number of /* 's which are all a part of the comment
/* there is only one */