The Quantum Composer is a tool to specify quantum programs graphically, and many SDKs and APIs exist to write compute code to represent a quantum program in a modern language (Python being a common choice). Like the Quantum Composer, OpenQASM a higher-level language for specifying quantum programs than computer code, but unlike the Quantum Composer, it is neither graphical nor user interface specific, so it can be much easier to specify longer programs that can be directly copied in to the many quantum simulators or into IBM QX for use. The Quantum Composer can take as input, programs in OpenQASM, and translate them into the graphical view. Likewise, for every program specified in the Quantum Composer it is easy to access the equivalent in OpenQASM within the IBM QX user interface.
Just as a book on Python cannot be expected to outline every feature of Python, the scope of our description of OpenQASM will be limited and focused on OpenQASM 2.0. In this chapter, we will look in detail just at the portion of its syntax relevant to interfacing with IBM QX for the programs and algorithms described in this book. Other features, such as declaring custom gates, are beyond the scope of this book, but can be found in the full specifications of OpenQASM 2.0 (https://github.com/Qiskit/openqasm/blob/master/spec/qasm2.rst) or are briefly touched upon in the final section of this chapter.
OpenQASM is similar in syntax to C:
- Comments are one per line and begin with //
- White space isn't important
- Case is important
- Every line in the program must end in a semicolon ;
Additionally, the following points apply:
- Every program must begin with OPENQASM 2.0; (this book and IBM QX at the time of writing uses version 2.0, but this can be updated to whichever version of OpenQASM you are using).
- When working with IBM QX, the include "qelib1.inc"; header must be given. Any other file can be included with the same syntax; what OpenQASM does is simply copies the content of the file at the location of include. The path to the file is a relative path from the current program.
Reading and and writing OpenQASM 2.0 programs for the IBM QX will involve the following operations:
Include header |
include "qelib1.inc"; |
Declaring a quantum register (qregname is any name you choose for the quantum register) |
qreg qregname[k]; |
Referencing a quantum register |
qregname[i]; |
Declaring a classical register (cregname is any name you choose for the quantum register) |
creg cregname[k]; |
Referencing a classical register |
cregname[i]; |
One-qubit gate list, available with inclusion of qelib1.inc on IBM QX |
h, t, tdg, s, sdg, x, y, z, id |
One-qubit gate action syntax |
gate q[i]; |
Two-qubit CNOT gate list, available with inclusion of qelib1.inc on IBM QX |
cx |
Two-qubit CNOT gate action (control and target both qubits in a previously declared quantum register) |
cnot control, target; |
Measurement operations available |
measure, bloch |
Measurement operation action syntax |
measure q[i] -> c[j]; bloch q[i] -> c[j]; |
Barrier operation (args are a comma-separated list of qubits) |
barrier args; |
Primitive gates (OpenQASM standard but not used on IBM QX or within this book) |
CX, U |
I will not go over each operation in detail in this section. Rather, in the following sections, we will learn by doing and will practice reading OpenQASM programs and translating them into quantum scores as well as translating quantum scores to OpenQASM programs.