Skip to content

Commit

Permalink
tests: Add various struct linearity and field access tests (#293)
Browse files Browse the repository at this point in the history
See #295 for context
  • Loading branch information
mark-koch authored Jul 23, 2024
1 parent 53fc333 commit d1adea5
Show file tree
Hide file tree
Showing 58 changed files with 994 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/error/comprehension_errors/capture3.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:18

16: @guppy(module)
17: def foo(xs: list[int], s: MyStruct) -> linst[MyStruct]:
18: return [s for x in xs]
^
GuppyTypeError: Field `s.q` with linear type `qubit` would be used multiple times when evaluating this comprehension
21 changes: 21 additions & 0 deletions tests/error/comprehension_errors/capture3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q: qubit


@guppy(module)
def foo(xs: list[int], s: MyStruct) -> linst[MyStruct]:
return [s for x in xs]


module.compile()
7 changes: 7 additions & 0 deletions tests/error/comprehension_errors/capture4.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:18

16: @guppy(module)
17: def foo(xs: list[int], s: MyStruct) -> linst[qubit]:
18: return [s.q for x in xs]
^^^
GuppyTypeError: Field `s.q` with linear type `qubit` would be used multiple times when evaluating this comprehension
21 changes: 21 additions & 0 deletions tests/error/comprehension_errors/capture4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q: qubit


@guppy(module)
def foo(xs: list[int], s: MyStruct) -> linst[qubit]:
return [s.q for x in xs]


module.compile()
7 changes: 7 additions & 0 deletions tests/error/comprehension_errors/guarded3.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:24

22: @guppy(module)
23: def foo(qs: linst[MyStruct]) -> linst[qubit]:
24: return [s.q2 for s in qs if bar(s.q1)]
^
GuppyTypeError: Field `s.q2` with linear type `qubit` is not used on all control-flow paths of the list comprehension
27 changes: 27 additions & 0 deletions tests/error/comprehension_errors/guarded3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q1: qubit
q2: qubit


@guppy.declare(module)
def bar(q: qubit) -> bool:
...


@guppy(module)
def foo(qs: linst[MyStruct]) -> linst[qubit]:
return [s.q2 for s in qs if bar(s.q1)]


module.compile()
7 changes: 7 additions & 0 deletions tests/error/comprehension_errors/not_used2.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:19

17: @guppy(module)
18: def foo(ss: linst[MyStruct]) -> linst[qubit]:
19: return [s.q1 for s in ss]
^
GuppyTypeError: Field `s.q2` with linear type `qubit` is not used
22 changes: 22 additions & 0 deletions tests/error/comprehension_errors/not_used2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit
from guppylang.prelude.builtins import linst

module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q1: qubit
q2: qubit


@guppy(module)
def foo(ss: linst[MyStruct]) -> linst[qubit]:
return [s.q1 for s in ss]


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/branch_use_field1.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:18

16: @guppy(module)
17: def foo(b: bool) -> bool:
18: s = MyStruct(qubit())
^
GuppyError: Field `s.q` with linear type `qubit` is not used on all control-flow paths
24 changes: 24 additions & 0 deletions tests/error/linear_errors/branch_use_field1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit, measure


module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q: qubit


@guppy(module)
def foo(b: bool) -> bool:
s = MyStruct(qubit())
if b:
return measure(s.q)
return False


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/branch_use_field2.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:21

19: if b:
20: measure(s.q2)
21: return s
^
GuppyError: Field `s.q2` with linear type `qubit` was already used (at 20:16)
24 changes: 24 additions & 0 deletions tests/error/linear_errors/branch_use_field2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit, measure


module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q1: qubit
q2: qubit


@guppy(module)
def foo(b: bool, s: MyStruct) -> MyStruct:
if b:
measure(s.q2)
return s


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/call_drop_field.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:24

22: @guppy(module)
23: def bar() -> qubit:
24: return foo().q1
^^^^^
GuppyTypeError: Linear field `q2` of expression with type `MyStruct` is not used
27 changes: 27 additions & 0 deletions tests/error/linear_errors/call_drop_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit


module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q1: qubit
q2: qubit


@guppy(module)
def foo() -> MyStruct:
return MyStruct(qubit(), qubit())


@guppy(module)
def bar() -> qubit:
return foo().q1


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/field_copy1.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:19

17: def foo(s: MyStruct) -> tuple[qubit, qubit]:
18: t = s
19: return s.q, t.q
^^^
GuppyError: Field `s.q` with linear type `qubit` was already used (at 18:8)
22 changes: 22 additions & 0 deletions tests/error/linear_errors/field_copy1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit


module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct:
q: qubit


@guppy(module)
def foo(s: MyStruct) -> tuple[qubit, qubit]:
t = s
return s.q, t.q


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/field_copy2.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:17

15: @guppy(module)
16: def foo(s: MyStruct) -> tuple[MyStruct, qubit]:
17: return s, s.q
^^^
GuppyError: Field `s.q` with linear type `qubit` was already used (at 17:11)
20 changes: 20 additions & 0 deletions tests/error/linear_errors/field_copy2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit


module = GuppyModule("test")
module.load(quantum)

@guppy.struct(module)
class MyStruct:
q: qubit


@guppy(module)
def foo(s: MyStruct) -> tuple[MyStruct, qubit]:
return s, s.q


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/field_copy3.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:17

15: @guppy(module)
16: def foo(s: MyStruct) -> tuple[qubit, MyStruct]:
17: return s.q, s
^
GuppyError: Field `s.q` with linear type `qubit` was already used (at 17:11)
20 changes: 20 additions & 0 deletions tests/error/linear_errors/field_copy3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit


module = GuppyModule("test")
module.load(quantum)

@guppy.struct(module)
class MyStruct:
q: qubit


@guppy(module)
def foo(s: MyStruct) -> tuple[qubit, MyStruct]:
return s.q, s


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/field_copy_nested1.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:26

24: measure(s.q)
25: s.q = s.x.q
26: return s
^
GuppyError: Field `s.x.q` with linear type `qubit` was already used (at 25:10)
29 changes: 29 additions & 0 deletions tests/error/linear_errors/field_copy_nested1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit, measure


module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct1:
q: qubit
x: "MyStruct2"


@guppy.struct(module)
class MyStruct2:
q: qubit


@guppy(module)
def foo(s: MyStruct1) -> MyStruct1:
measure(s.q)
s.q = s.x.q
return s


module.compile()
7 changes: 7 additions & 0 deletions tests/error/linear_errors/field_copy_nested2.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Guppy compilation failed. Error in file $FILE:25

23: def foo(s: MyStruct1) -> MyStruct1:
24: measure(s.x.q1)
25: return MyStruct1(s.x)
^^^
GuppyError: Field `s.x.q1` with linear type `qubit` was already used (at 24:12)
28 changes: 28 additions & 0 deletions tests/error/linear_errors/field_copy_nested2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import guppylang.prelude.quantum as quantum
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.prelude.quantum import qubit, measure


module = GuppyModule("test")
module.load(quantum)


@guppy.struct(module)
class MyStruct1:
x: "MyStruct2"


@guppy.struct(module)
class MyStruct2:
q1: qubit
q2: qubit


@guppy(module)
def foo(s: MyStruct1) -> MyStruct1:
measure(s.x.q1)
return MyStruct1(s.x)


module.compile()
Loading

0 comments on commit d1adea5

Please sign in to comment.