Selecting the correct OpenQASM version string is one of the first and most important steps in quantum programming. The version string tells the quantum execution environment which set of syntax rules and features to apply to your code. This chapter provides a comprehensive understanding of the OpenQASM version string and its correct usage with practical examples.
The version string is an essential directive at the beginning of an OpenQASM file that specifies the version of the language used. This directive ensures compatibility and proper interpretation across different quantum processing environments.
The version string is formatted as "OPENQASM V.v;", where "V" is the major version and "v" is the minor version number.
For example, to specify that your program is written in OpenQASM version 2.01:
OPENQASM 2.0; // Indicates we are using OpenQASM version 2.0 [^1]
There are two fundamental rules for properly declaring the version string:
Here's a valid OpenQASM program with a correctly positioned version string. 12345
Diagram
// PhotonQ Experiment: Version String Declaration [^5] OPENQASM 2.0; // Correctly specifies OpenQASM version [^1] include "qelib1.inc"; // Including the standard library for convenience [^2] // Quantum gate application qreg qubits[2]; // Declaring a quantum register with two qubits [^3] cx qubits[0], qubits[1]; // Applying a CNOT gate to the qubits [^4]
Translation
Powered by Perceval, Qiskit, PyZX
Not run yet
Simulation
Not run yet
Multiple declarations of the version string are not allowed and will result in an error:
OPENQASM 2.0; // Redundant declarations cause conflicts and errors
OPENQASM 3.0;
Placing the version string after executable lines is incorrect and should be avoided:
// Declaration before the version string results in an invalid program structure
qreg qubits[4]; // This will cause an error
OPENQASM 2.0; // Incorrectly placed after an executable line
Understanding and using the OpenQASM version string correctly is foundational for constructing valid and functional quantum programs. In our upcoming chapters, we'll delve into more exciting aspects of quantum computing using OpenQASM, like working with quantum registers and implementing quantum gates!