Modularizing Code with Include

To avoid clutter and enhance maintainability, quantum programs can benefit from modularity. This is achieved by splitting a program into separate files. OpenQASM facilitates this process by providing the include keyword, allowing you to access the contents of external files seamlessly. This chapter will guide you through using the include keyword in OpenQASM.

The Include Statement

In OpenQASM, the include statement incorporates the contents of an external file, treating them as part of the main file. This approach is akin to copying and pasting the included file's content at the location of the include statement.

To include a file named "filename.ext":

include "filename.ext";

Example of Usage

Suppose you have a file called headerfile:

// Common definitions and setups

And your main program file myprogram:

include "headerfile" // Includes "headerfile" here

After inclusion, myprogram will act as if it contains:

// "headerfile" contents followed by "myprogram" contents

Note: If the included file is not in the same directory, the path must be relative to the current working directory.

Experiment: Importing

The file is essential to OpenQASM programming, as it contains definitions for many standard quantum gates. Including it in your program is straightforward. 12345


// PhotonQ Experiment: Importing [^5] OPENQASM 2.0; // Declares the version of OpenQASM [^1] include ""; // Brings in the standard gate definitions [^2] // Quantum register declaration qreg qubits[2]; // Defines a quantum register with two qubits [^3] // Quantum gate application h qubits[0]; // Applies a Hadamard gate to the first qubit [^4]


Powered by Perceval, Qiskit, PyZX

Not run yet


Not run yet

By including qelib1.inc6, you have access to a variety of quantum operations to use in your quantum circuits.

Further information on the file is available here:


  1. Version String Documentation

  2. Include Documentation

  3. Quantum Registers Documentation

  4. Hadamard Gate Documentation

  5. Experiment: Importing

  6. Documentation


Copyright © 2024 Walther Group, Faculty of Physics, University of Vienna. All rights reserved.