Open Quantum Assembly Language (OpenQASM, pronounced open kazm) is a custom programming language designed specifically to minimally describe quantum circuits. In this tutorial, we will learn how to translate OpenQASM programs into quantum scores with IBM QX. We will also look at representing quantum scores in OpenQASM 2.0 programs. You will need a modern web browser and the ability to sign into IBM QX This tutorial is an excerpt taken from the book ‘Mastering Quantum Computing with IBM QX’ written by Dr. Christine Corbett Moran. The book explores quantum computing by implementing quantum programs on IBM QX, helping you be at the forefront of the next revolution in computation. 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 into 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. 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; (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 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) CX, U We will now learn 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 a classical or quantum register at declaration. Translating OpenQASM programs into quantum scores In this tutorial, we will translate OpenQASM programs into quantum scores by hand to practice, reading OpenQASM code. OpenQASM to negate one qubit Consider the following program: OPENQASM 2.0;include “qelib1.inc”;qreg q[1];x q[0]; The following lines are the standard headers for working with IBM QX: OPENQASM 2.0;include “qelib1.inc”; Then the following line declares a quantum register of size one named q: qreg q[1]; Quantum registers are automatically initialized to contain |”0″>. Finally, the next line operates the X gate on the first (and only) qubit in the q quantum register: x q[0]; Putting this all together, we can create the following equivalent quantum score: OpenQASM to apply gates to two qubits, and measure the first qubit Next, consider the OpenQASM program: OPENQASM 2.0;include “qelib1.inc”;qreg q[2];creg c[1];x q[0];y q[0];z q[0];s q[1];measure q[0] -> c[0]; The first two preceding lines are the standard header to declare a program and OpenQASM program and the standard import header to interface with the IBM QX. The next two lines declare a quantum register of two qubits initialized to |”00″> and a classical register of one bit initialized to 0: qreg q[2];creg c[1]; The next three lines apply gates, in order, to the first qubit in the q quantum register: x q[0];y q[0];z q[0]; The next line applies a gate to the second qubit in the q quantum register: s q[1]; And the final line measures the first qubit in the q quantum register and places the result in the first (and only) bit in the c classical register: measure q[0] -> c[0]; Putting this all together, we can create the following equivalent quantum score: Representing quantum scores in OpenQASM 2.0 programs Here is an example of writing an OpenQASM 2.0 program from a quantum score. I have broken it down into columns from top to bottom of the score for clarity and annotated these in the diagram by indicating column numbers in orange. Here’s the circuit illustrating the reversibility of quantum computations: Let’s dissect the OpenQASM that generates this circuit. The first lines are, as usual, the headers, indicating the code is OpenQASM and that we will be using the standard IBM QX header: OPENQASM 2.0;include “qelib1.inc”; The next lines declare a quantum register named q of 5 qubits initialized to |”00000″> and a classical register name c of 5 bits initialized to 00000: // declare the quantum and classical registers we will useqreg q[5];creg c[5]; The next lines will go column by column in the circuit diagram, creating the code for each column in order. We will start with the first column: The first column we can see only contains a CNOT gate, with its control qubit being the third qubit in the q quantum register, q[2] and the target qubit being the second qubit in the q quantum register, q[1]. Looking up the OpenQASM syntax for the CNOT gate in the table in the previous section, we see that it is cnot control, target; which means that the first column will be coded as: //column 1cx q[2],q[1]; Next, we will move to the second column, which has a number of gates specified. The code for the second column is: //column2x q[1];h q[2];s q[3];y q[4]; Each successive column should now be straightforward to encode from looking at the quantum score in OpenQASM. The full program is as follows: OPENQASM 2.0;include “qelib1.inc”;// declare the quantum and classical registers we will useqreg q[5];creg c[5];//column 1cx q[2],q[1];//column2x q[1];h q[2];s q[3];y q[4];//column 3t q[2];z q[3];//column 4tdg q[2];z q[3];//column 5x q[1];h q[2];sdg q[3];y q[4];// column 6cx q[2],q[1];// column 7measure q[0] -> c[0];// column 8measure q[1] -> c[1];// column 9measure q[2] -> c[2];// column 10measure q[3] -> c[3];// column 11measure q[4] -> c[4]; The previous code exactly reproduced the quantum score as depicted, but we could make several quantum scores, which are equivalent (and thus several variations on the OpenQASM program that are equivalent), as we saw in previous sections. Here are a couple of things to keep in mind: Each column could be in any order, for example, column 3 could be: t q[2];z q[3]; Or it could be: z q[3];tdg q[2]; In addition, any gate operating on a qubit in any column where there is no gate in the previous column on the qubit can be moved to the previous column, without affecting the computation. In this article, we learned how to translate OpenQASM programs in IBX QX into quantum scores. We also looked at Representing quantum scores in OpenQASM 2.0 programs. If you want to learn other concepts and principles of Quantum Computing with IBM QX, be sure to check out the book ‘Mastering Quantum Computing with IBM QX’. Read Next Quantum computing, edge analytics, and meta-learning: key trends in data science and big data in 2019 Did quantum computing just take a quantum leap? A two-qubit chip by UK researchers makes quantum entanglement possible Quantum Computing is poised to take a quantum leap with industries and governments on its side