Skip to content

Commit 0790911

Browse files
add .include method (#66)
1 parent b358610 commit 0790911

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

oqpy/program.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ def pragma(self, command: str) -> Program:
523523
self._add_statement(ast.Pragma(command))
524524
return self
525525

526+
def include(self, path: str) -> Program:
527+
"""Add an include statement."""
528+
if len(self.stack) != 1:
529+
# cf. https://openqasm.com/language/comments.html#included-files
530+
raise RuntimeError("Include statements must be global")
531+
self._add_statement(ast.Include(path))
532+
return self
533+
526534
def _do_assignment(self, var: AstConvertible, op: str, value: AstConvertible) -> None:
527535
"""Helper function for variable assignment operations."""
528536
if isinstance(var, classical_types.DurationVar):

tests/test_directives.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,3 +2249,20 @@ def test_gate_declarations():
22492249
).strip()
22502250

22512251
assert prog.to_qasm() == expected
2252+
2253+
2254+
def test_include():
2255+
prog = Program()
2256+
prog.include("foo.qasm")
2257+
prog.declare(IntVar(0, "i"))
2258+
2259+
expected = textwrap.dedent(
2260+
"""
2261+
OPENQASM 3.0;
2262+
include "foo.qasm";
2263+
int[32] i = 0;
2264+
"""
2265+
).strip()
2266+
2267+
assert prog.to_qasm() == expected
2268+

0 commit comments

Comments
 (0)