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

test: llvm_execution with array constants #741

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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)
Loading