In this tutorial, we’ll introduce OpenQASM, a key language for quantum programming and its use within IBM’s Qiskit framework.
Open Quantum Assembly Language
OpenQASM is an intermediate representation for quantum instructions. The language was first described in a paper published in July 2017, and a reference source code implementation was released as part of IBM’s Quantum Information Software Kit (Qiskit) for use with their IBM Q Experience cloud quantum computing platform.
OpenQASM defines its version at the head of a source file as a real number, as in the declaration:
OPENQASM 2.0;
The level of OpenQASM’s original published implementations (e.g., Qiskit, infra) is OpenQASM 2.0. The 3.0 level of the specification is currently work in progress and can be viewed at the OpenQASM repository on GitHub.
Tequila functions export_open_qasm, import_open_qasm, and import_open_qasm_from_file work with current OpenQASM version = 2.0
Example
The following is an example of OpenQASM source code from the official library. The program adds two four-bit numbers.
// quantum ripple-carry adder from Cuccaro et al, quant-ph/0410184
OPENQASM 2.0;
include "qelib1.inc";
gate majority a,b,c
{
cx c,b;
cx c,a;
ccx a,b,c;
}
gate unmaj a,b,c
{
ccx a,b,c;
cx c,a;
cx a,b;
}
qreg cin[1];
qreg a[4];
qreg b[4];
qreg cout[1];
creg ans[5];
// set input states
x a[0]; // a = 0001
x b; // b = 1111
// add a to b, storing result in b
majority cin[0],b[0],a[0];
majority a[0],b[1],a[1];
majority a[1],b[2],a[2];
majority a[2],b[3],a[3];
cx a[3],cout[0];
unmaj a[2],b[3],a[3];
unmaj a[1],b[2],a[2];
unmaj a[0],b[1],a[1];
unmaj cin[0],b[0],a[0];
measure b[0] -> ans[0];
measure b[1] -> ans[1];
measure b[2] -> ans[2];
measure b[3] -> ans[3];
measure cout[0] -> ans[4];
Export to OpenQASM
Once you have a circuit in Tequila, it is possible to generate its equivalent in OpenQASM code, using the export_open_qasm function, for example:
The corresponding qpic-file to this circuit is being created. For generating the corresponding png one has to write “qpic -f png C1_new.qpic” into the command line
It is possible to generate the OpenQASM code for ZX-Calculus, that is, without Y gates (Y, Ry, Cy, CRy), if you want to activate this option you need to use the zx_calculus flag. If enabled, the OpenQASM code will be generated without Y gates and will instead place their equivalents for each gate.
For convenience, it is possible to generate the code in OpenQASM and send the result to a file for external use. The name of the file must be indicated in the filename parameter:
You can import an OpenQASM code that is not written strictly, but only has the instructions of the gates without containing the definition lines, for this the rigorous flag is used, for example:
In the case of having the OpenQASM code in a file, it is possible to load that file to generate the Tequila circuit from there, for this the import_open_qasm_from_file function is used
If the OpenQASM code contains custom gates, importing into a Tequila circuit will generate the equivalent with the gates in the proper place with their corresponding parameters, for example:
It is possible to generate the OpenQASM code for ZX-Calculus, that is, without Y gates (Y, Ry, Cy, CRy), if you want to activate this option you need to use the zx_calculus flag. If enabled, the OpenQASM code will be generated without Y gates and will instead place their equivalents for each gate.
If the Tequila circuit is created with variables, the corresponding values must be indicated when exporting it to OpenQASM code:
For convenience, it is possible to generate the code in OpenQASM and send the result to a file for external use. The name of the file must be indicated in the filename parameter: