Skip to content

Fix subroutines with array arguments #77

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 3 commits into from
Oct 5, 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
11 changes: 10 additions & 1 deletion oqpy/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,16 @@ def increment_variable(int[32] i) {
for argname in argnames[1:]: # arg 0 should be program
if argname not in type_hints:
raise ValueError(f"No type hint provided for {argname} on subroutine {name}.")
elif not issubclass(type_hints[argname], (_ClassicalVar, Qubit)):

# ArrayVar[] returns a partial function instead of a type.
# The underlying function of that partial should be ArrayVar itself.
type_hint = (
type_hints[argname].func
if isinstance(type_hints[argname], functools.partial)
else type_hints[argname]
)

if not issubclass(type_hint, (_ClassicalVar, Qubit)):
raise ValueError(
f"Type hint for {argname} on subroutine {name} is not an oqpy variable type."
)
Expand Down
15 changes: 8 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,13 @@ def delay50ns(prog: Program, q: Qubit) -> None:
q = PhysicalQubits[0]
prog.do_expression(delay50ns(prog, q))

@subroutine
def get(prog: Program, arr: ArrayVar[IntVar, 3], i: IntVar) -> IntVar:
return arr[i]

arr = ArrayVar[IntVar, 3]([0, 1, 2], name="arr")
prog.set(y, get(prog, arr, y))

with pytest.raises(ValueError):

@subroutine
Expand Down Expand Up @@ -858,9 +865,14 @@ def multiply(int[32] x, int[32] y) -> int[32] {
def delay50ns(qubit q) {
delay[50.0ns] q;
}
def get(array[int[32], 3] arr, int[32] i) -> int {
return arr[i];
}
int[32] y = 2;
array[int[32], 3] arr = {0, 1, 2};
y = multiply(y, 3);
delay50ns($0);
y = get(arr, y);
"""
).strip()

Expand Down