-
Notifications
You must be signed in to change notification settings - Fork 3
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
fix: Allow borrowing inside comprehensions #723
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Error: Linearity violation (at $FILE:21:16) | ||
| | ||
19 | @guppy(module) | ||
20 | def foo(qs: list[qubit] @owned) -> list[int]: | ||
21 | return [baz(q) for q in qs if bar(q)] | ||
| ^ Variable `q` with linear type `qubit` cannot be borrowed | ||
| ... | ||
| | ||
21 | return [baz(q) for q in qs if bar(q)] | ||
| - since it was already consumed here | ||
|
||
Guppy compilation failed due to 1 previous error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit @owned) -> int: ... | ||
|
||
|
||
@guppy.declare(module) | ||
def baz(q: qubit) -> int: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[int]: | ||
return [baz(q) for q in qs if bar(q)] | ||
|
||
|
||
module.compile() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Error: Linearity violation (at $FILE:17:16) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(n: int, q: qubit @owned) -> list[int]: | ||
17 | return [bar(q) for _ in range(n)] | ||
| ^ Variable `q` with linear type `qubit` is leaked | ||
|
||
Help: Make sure that `q` is consumed or returned to avoid the leak | ||
|
||
Guppy compilation failed due to 1 previous error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> int: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(n: int, q: qubit @owned) -> list[int]: | ||
return [bar(q) for _ in range(n)] | ||
|
||
|
||
module.compile() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Error: Linearity violation (at $FILE:17:23) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(qs: list[qubit] @owned) -> list[int]: | ||
17 | return [bar(q) for q in qs] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. obvs not for this PR but it would be super sweet if a hint said "try removing owned" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created #726 |
||
| ^ Variable `q` with linear type `qubit` is leaked | ||
|
||
Guppy compilation failed due to 1 previous error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> int: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[int]: | ||
return [bar(q) for q in qs] | ||
|
||
|
||
module.compile() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Error: Linearity violation (at $FILE:17:18) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(qs: list[qubit] @owned) -> list[int]: | ||
17 | return [0 for q in qs if bar(q)] | ||
| ^ Variable `q` with linear type `qubit` may be leaked ... | ||
| | ||
17 | return [0 for q in qs if bar(q)] | ||
| ------ if this expression is `False` | ||
|
||
Guppy compilation failed due to 1 previous error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> bool: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[int]: | ||
return [0 for q in qs if bar(q)] | ||
|
||
|
||
module.compile() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Error: Linearity violation (at $FILE:17:18) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(qs: list[qubit] @owned) -> list[qubit]: | ||
17 | return [r for q in qs for r in bar(q)] | ||
| ^ Variable `q` with linear type `qubit` is leaked | ||
|
||
Guppy compilation failed due to 1 previous error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> list[qubit]: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[qubit]: | ||
return [r for q in qs for r in bar(q)] | ||
|
||
|
||
module.compile() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.builtins import array | ||
from guppylang.std.builtins import array, owned | ||
from guppylang.std.quantum import qubit | ||
|
||
import guppylang.std.quantum_functional as quantum | ||
|
@@ -88,3 +88,76 @@ def test(xs: array[int, n]) -> array[int, n]: | |
return array(x + 1 for x in xs) | ||
|
||
validate(module.compile()) | ||
|
||
|
||
def test_borrow(validate): | ||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
module.load(qubit) | ||
n = guppy.nat_var("n", module) | ||
|
||
@guppy.declare(module) | ||
def foo(q: qubit) -> int: ... | ||
|
||
@guppy(module) | ||
def test(q: qubit) -> array[int, n]: | ||
return array(foo(q) for _ in range(n)) | ||
|
||
validate(module.compile()) | ||
|
||
|
||
def test_borrow_twice(validate): | ||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
module.load(qubit) | ||
n = guppy.nat_var("n", module) | ||
|
||
@guppy.declare(module) | ||
def foo(q: qubit) -> int: ... | ||
|
||
@guppy(module) | ||
def test(q: qubit) -> array[int, n]: | ||
return array(foo(q) + foo(q) for _ in range(n)) | ||
|
||
validate(module.compile()) | ||
|
||
|
||
def test_borrow_struct(validate): | ||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
module.load(qubit) | ||
n = guppy.nat_var("n", module) | ||
|
||
@guppy.struct(module) | ||
class MyStruct: | ||
q1: qubit | ||
q2: qubit | ||
|
||
@guppy.declare(module) | ||
def foo(s: MyStruct) -> int: ... | ||
|
||
@guppy(module) | ||
def test(s: MyStruct) -> array[int, n]: | ||
return array(foo(s) for _ in range(n)) | ||
|
||
validate(module.compile()) | ||
|
||
|
||
def test_borrow_and_consume(validate): | ||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
module.load(qubit) | ||
n = guppy.nat_var("n", module) | ||
|
||
@guppy.declare(module) | ||
def foo(q: qubit) -> int: ... | ||
|
||
@guppy.declare(module) | ||
def bar(q: qubit @ owned) -> int: ... | ||
|
||
@guppy(module) | ||
def test(qs: array[qubit, n] @ owned) -> array[int, n]: | ||
return array(foo(q) + bar(q) for q in qs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flex |
||
|
||
validate(module.compile()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by: Improve leak errors by keeping track of the original place where a variable was defined if possible