Skip to content

Commit

Permalink
test: llvm_execution with array constants
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Dec 20, 2024
1 parent 647ad3d commit b48b858
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions tests/integration/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.std.builtins import array, owned, mem_swap
from guppylang.std.builtins import array, owned, mem_swap, py
from tests.util import compile_guppy

from guppylang.std.quantum import qubit, discard
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_linear_subscript(validate):
def foo(q: qubit) -> None: ...

@guppy(module)
def main(qs: array[qubit, 42] @owned, i: int) -> array[qubit, 42]:
def main(qs: array[qubit, 42] @ owned, i: int) -> array[qubit, 42]:
foo(qs[i])
return qs

Expand Down Expand Up @@ -142,7 +142,7 @@ def test_multi_subscripts(validate):
def foo(q1: qubit, q2: qubit) -> None: ...

@guppy(module)
def main(qs: array[qubit, 42] @owned) -> array[qubit, 42]:
def main(qs: array[qubit, 42] @ owned) -> array[qubit, 42]:
foo(qs[0], qs[1])
foo(qs[0], qs[0]) # Will panic at runtime
return qs
Expand All @@ -163,7 +163,7 @@ class S:
def foo(q1: qubit, q2: qubit) -> None: ...

@guppy(module)
def main(ss: array[S, 10] @owned) -> array[S, 10]:
def main(ss: array[S, 10] @ owned) -> array[S, 10]:
# This will panic at runtime :(
# To make this work, we would need to replace the qubits in the struct
# with `qubit | None` and write back `None` after `q1` has been extracted...
Expand All @@ -181,12 +181,10 @@ def test_nested_subscripts(validate):
def foo(q: qubit) -> None: ...

@guppy.declare(module)
def bar(
q1: qubit, q2: qubit, q3: qubit, q4: qubit
) -> None: ...
def bar(q1: qubit, q2: qubit, q3: qubit, q4: qubit) -> None: ...

@guppy(module)
def main(qs: array[array[qubit, 13], 42] @owned) -> array[array[qubit, 13], 42]:
def main(qs: array[array[qubit, 13], 42] @ owned) -> array[array[qubit, 13], 42]:
foo(qs[0][0])
# The following should work *without* panicking at runtime! Accessing `qs[0][0]`
# replaces one qubit with `None` but puts everything back into `qs` before
Expand Down Expand Up @@ -221,7 +219,7 @@ class A:
def foo(q1: qubit) -> None: ...

@guppy(module)
def main(a: A @owned, i: int, j: int, k: int) -> A:
def main(a: A @ owned, i: int, j: int, k: int) -> A:
foo(a.xs[i].ys[j][k].c)
return a

Expand All @@ -235,7 +233,7 @@ def test_generic_function(validate):
n = guppy.nat_var("n", module=module)

@guppy(module)
def foo(xs: array[T, n] @owned) -> array[T, n]:
def foo(xs: array[T, n] @ owned) -> array[T, n]:
return xs

@guppy(module)
Expand Down Expand Up @@ -265,7 +263,7 @@ def test_exec_array(validate, run_int_fn):

@guppy(module)
def main() -> int:
a = array(1,2,3)
a = array(1, 2, 3)
return a[0] + a[1] + a[2]

package = module.compile()
Expand Down Expand Up @@ -298,6 +296,7 @@ def test_mem_swap(validate):
module = GuppyModule("test")

module.load(qubit)

@guppy(module)
def foo(x: qubit, y: qubit) -> None:
mem_swap(x, y)
Expand All @@ -310,3 +309,21 @@ def main() -> array[qubit, 2]:

package = module.compile()
validate(package)


# https://github.com/CQCL/hugr/issues/1826
def test_array_const(validate, run_int_fn):
module = GuppyModule("test")
module.load_all(quantum)

bools = [True, False]

@guppy(module)
def main() -> int:
bs = py(bools)
return int(bs[0])

package = module.compile()
validate(package)

run_int_fn(package, expected=1)

0 comments on commit b48b858

Please sign in to comment.