OPENQASM: How to include my own libraries

I studied the OPENQASM 2.0 spec a few times but failed to find a way to include my own libraries. Have I missed anything?

Right now, the only way is to use include "qelib1.inc" mechanism to separate the library from my own code. When the circuit starts to grow, there is a real need to group various components to different files. How can that be achieved without contaminating the qelib1.inc?

• Hi, welcome to QC Stack Exchange. Do you want to include a library in IBM Q web interface? If yes, how do you edit the qelib1.inc library there? – Martin Vesely Jan 12 at 7:34
• The OpenQASM spec is supposed to be parser and platform independent. What I want is to have multiple include statements in the QASM file. I guess that is unsupported. Right now, I put various components in many .inc files and use a script to combine them into a single file. Then, the QASM file includes that file. – Emscripten Fan Jan 19 at 21:40

If you're using qiskit-terra as your parser this should work unless you name the local file "qelib1.inc". The parser included in the qiskit-terra parser uses the cwd unless the filename is present in the libs dir (the only current one being qelib1.inc: https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/qasm/qasmlexer.py#L131-L163 If you're using openqasm with another parser that parser should implement the same behavior (for example see: https://github.com/libtangle/qasm-rust/blob/master/src/lib.rs#L188-L244 for how it's implemented in qasm-rust).
• Are multiple include statements allowed? – Emscripten Fan Jan 19 at 21:34
• @emscripten-fan it should be, but it's up to the parser to implement that. It's not explicitly called out in the spec and from what I can tell multiple include statements is not part of the conformance tests. But it'd be kinda of odd to make a parser that only worked with one include statement. FWIW, the qiskit parser works fine with multiple includes. – Matthew Treinish Jan 21 at 9:32