The project includes a stand-alone parser for QASM files. The parser is a part of the compiler pipeline of the PhotonQ compiler project.
This repository contains the source files of a parser built with Flex and Bison that translates standard QASM 2.0 files consisting of
The first test versions of the parser were based on examples taken from Levine's book and then adapted accordingly for use in translating QASM files.
The lex file parser.l
contains Flex code that defines the regular expressions and character tokens for which the input file is scanned.
The yacc file parser.y
contains Bison code that defines the relationship between the input tokens as recursive grammar rules.
To try the parser yourself you can clone the repo with
git clone https://github.com/zilkf92/qasm-parser.git
The repository contains 2 branches:
Here, the string "pi" is interpreted as a mathematical constant from the <math.h>
header and all other numbers are interpreted as integers or floats. E.g.:
rz(pi/4) q[8];
cx q[8],q[7];
rx(2*pi) q[7];
is converted to
rz(0.785398) q[8];
h q[8];
rz(0) q[8];
h q[8];
rz(0) q[7];
h q[7];
cz q[8], q[7];
rz(0) q[7];
h q[7];
rz(0) q[7];
h q[7];
rz(6.283185) q[7];
h q[7];
Here, each argument of a QASM gate (parameter and qubit assignment) is interpreted and parsed as a string. E.g.:
rz(pi/4) q[8];
cx q[8],q[7];
is converted to
rz(pi/4) q[8];
h q[8];
rz(0) q[8];
h q[8];
rz(0) q[7];
h q[7];
cz q[8],q[7];
rz(0) q[7];
h q[7];
After copying the repository, you can use, e.g., Qiskit to transpile an arbitrary quantum circuit into the gate set input.qasm
file with your generated QASM file and run the ./parser
file in the "parser" folder. This will create an output.qasm
file according to the following translation rules: