Skip to content

Add .include method #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions oqpy/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,14 @@ def pragma(self, command: str) -> Program:
self._add_statement(ast.Pragma(command))
return self

def include(self, path: str) -> Program:
"""Add an include statement."""
if len(self.stack) != 1:
# cf. https://openqasm.com/language/comments.html#included-files
raise RuntimeError("Include statements must be global")
self._add_statement(ast.Include(path))
return self

def _do_assignment(self, var: AstConvertible, op: str, value: AstConvertible) -> None:
"""Helper function for variable assignment operations."""
if isinstance(var, classical_types.DurationVar):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -2249,3 +2249,20 @@ def test_gate_declarations():
).strip()

assert prog.to_qasm() == expected


def test_include():
prog = Program()
prog.include("foo.qasm")
prog.declare(IntVar(0, "i"))

expected = textwrap.dedent(
"""
OPENQASM 3.0;
include "foo.qasm";
int[32] i = 0;
"""
).strip()

assert prog.to_qasm() == expected