Skip to content

Commit d102a5b

Browse files
Fix subroutines with array arguments (#77)
* update urllib3 to 2.0.6 * fix subroutines with array arguments * add comments Co-authored-by: Phil Reinhold <[email protected]> --------- Co-authored-by: Phil Reinhold <[email protected]>
1 parent 97da776 commit d102a5b

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

oqpy/subroutines.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,16 @@ def increment_variable(int[32] i) {
100100
for argname in argnames[1:]: # arg 0 should be program
101101
if argname not in type_hints:
102102
raise ValueError(f"No type hint provided for {argname} on subroutine {name}.")
103-
elif not issubclass(type_hints[argname], (_ClassicalVar, Qubit)):
103+
104+
# ArrayVar[] returns a partial function instead of a type.
105+
# The underlying function of that partial should be ArrayVar itself.
106+
type_hint = (
107+
type_hints[argname].func
108+
if isinstance(type_hints[argname], functools.partial)
109+
else type_hints[argname]
110+
)
111+
112+
if not issubclass(type_hint, (_ClassicalVar, Qubit)):
104113
raise ValueError(
105114
f"Type hint for {argname} on subroutine {name} is not an oqpy variable type."
106115
)

poetry.lock

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_directives.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,13 @@ def delay50ns(prog: Program, q: Qubit) -> None:
817817
q = PhysicalQubits[0]
818818
prog.do_expression(delay50ns(prog, q))
819819

820+
@subroutine
821+
def get(prog: Program, arr: ArrayVar[IntVar, 3], i: IntVar) -> IntVar:
822+
return arr[i]
823+
824+
arr = ArrayVar[IntVar, 3]([0, 1, 2], name="arr")
825+
prog.set(y, get(prog, arr, y))
826+
820827
with pytest.raises(ValueError):
821828

822829
@subroutine
@@ -858,9 +865,14 @@ def multiply(int[32] x, int[32] y) -> int[32] {
858865
def delay50ns(qubit q) {
859866
delay[50.0ns] q;
860867
}
868+
def get(array[int[32], 3] arr, int[32] i) -> int {
869+
return arr[i];
870+
}
861871
int[32] y = 2;
872+
array[int[32], 3] arr = {0, 1, 2};
862873
y = multiply(y, 3);
863874
delay50ns($0);
875+
y = get(arr, y);
864876
"""
865877
).strip()
866878

0 commit comments

Comments
 (0)