OpenQASM Include

It is neither possible nor desirable to include all the contents and dependencies for running a program to be present in the same file. Dividing the contents of a program into files helps in achieving modularity in the source code. Therefore, many programming languages provide a feature for being able to access the contents of other files into a different file. OpenQASM also provides this feature through its include keyword. In this chapter of the OpenQASM tutorial, you will be learning about how you can access the contents of a different file in a OpenQASM file by using the include keyword.

Include Statement

The include statement parses the contents of a file as if they were the contents of the same file.

The following syntax is used for parsing a file filename.ext inside another file-

include "filename.ext";

Example

Consider the following file with the name headerfile

statement 1;
statement 2;

and another file myprogram

include "headerfile"
statement 3;
statement 4;

The include statement in myprogram file will parse the contents of headerfile as if they were they were copied in-place of the include statement.

Therefore, the statements will be executed in the following order-

statement 1;
statement 2;
statement 3;
statement 4;

Note– If the file to be included is not present in the same folder, then the complete path of the file to be specified relative to the current working directory.

qelib1.inc File

qelib1.inc is a OpenQASM file that contains definitions of many standard single and multiple Qubit Quantum Gates. It is therefore important to include this file before writing a Quantum Program. The qelib1.inc file can be included by using the following syntax-

include "qelib1.inc"

Learn More about qelib1.inc file