Skip to content
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

Fix for nested subroutines and if statements in subroutines #53

Merged
merged 2 commits into from
Jul 10, 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
5 changes: 5 additions & 0 deletions oqpy/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def wrapper(
for input_val in inputs.values():
inner_prog._mark_var_declared(input_val)
output = func(inner_prog, **inputs)
inner_prog.autodeclare()
inner_prog._state.finalize_if_clause()
body = inner_prog._state.body
if isinstance(output, OQPyExpression):
return_type = output.type
Expand All @@ -115,6 +117,9 @@ def wrapper(
raise ValueError(
"Output type of subroutine {name} was neither oqpy expression nor None."
)
program.defcals.update(inner_prog.defcals)
program.subroutines.update(inner_prog.subroutines)
program.externs.update(inner_prog.externs)
stmt = ast.SubroutineDefinition(
identifier,
arguments=arguments,
Expand Down
38 changes: 38 additions & 0 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -1999,3 +1999,41 @@ def test_io_declaration():
).strip()
assert prog.to_qasm() == expected
_check_respects_type_hints(prog)


def test_nested_subroutines():
@oqpy.subroutine
def f(prog: oqpy.Program) -> oqpy.IntVar:
i = oqpy.IntVar(name="i", init_expression=1)
with oqpy.If(prog, i == 1):
prog.increment(i, 1)
return i

@oqpy.subroutine
def g(prog: oqpy.Program) -> oqpy.IntVar:
return f(prog)


prog = oqpy.Program()
x = oqpy.IntVar(name="x")
prog.set(x, g(prog))

expected = textwrap.dedent(
"""
OPENQASM 3.0;
def f() -> int[32] {
int[32] i = 1;
if (i == 1) {
i += 1;
}
return i;
}
def g() -> int[32] {
return f();
}
int[32] x;
x = g();
"""
).strip()

assert prog.to_qasm() == expected