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
    statement1;
    statement2;
    

    And your main program file myprogram:

    include "headerfile" // Includes "headerfile" here
    statement3;
    statement4;
    

    After inclusion, myprogram will act as if it contains:

    // "headerfile" contents followed by "myprogram" contents
    statement1;
    statement2;
    statement3;
    statement4;
    

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

    Experiment: Importing qelib1.inc

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

    Diagram

    // PhotonQ Experiment: Importing qelib1.inc [^5] OPENQASM 2.0; // Declares the version of OpenQASM [^1] include "qelib1.inc"; // 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]

    Translation

    Powered by Perceval, Qiskit, PyZX

    Not run yet

    Simulation

    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 qelib1.inc file is available here:

    Footnotes

    1. Version String Documentation

    2. Include Documentation

    3. Quantum Registers Documentation

    4. Hadamard Gate Documentation

    5. Experiment: Importing qelib1.inc

    6. qelib1.inc Documentation

    Syntax
    Registers

    Exploring

    Quantum Frontiers

    Join us on our journey to push the boundaries of quantum computing and unlock the potential of this transformative technology, as we strive to make quantum accessible to all and shape the future of computing.

    Register Now
    University of Vienna Logo

    Links

    DocumentationExperimentsImprintContact

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