OpenQASM

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:

Additionally, the following points apply:

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.

Note that i and j are integer counters, starting at 0, which specifies which qubit/bit in the quantum or classical register the program would like to reference; k is an integer counter greater than 0 which specifies the size of of a classical or quantum register at declaration