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

fix: Fix linearity checking bug #441

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions guppylang/checker/linearity_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ def check_cfg_linearity(
checked: dict[BB, CheckedBB[Place]] = {}

for bb, scope in scopes.items():
live_before_bb = live_before[bb]

# We have to check that used linear variables are not being outputted
for succ in bb.successors:
live = live_before[succ]
Expand Down Expand Up @@ -457,6 +459,12 @@ def check_cfg_linearity(
for place in scope.values():
for leaf in leaf_places(place):
x = leaf.id
# Some values are just in scope because the type checker determined
# them as live in the first (less precises) dataflow analysis. It
# might be the case that x is actually not live when considering
# the second, more fine-grained, analysis based on places.
if x not in live_before_bb and x not in scope.vars:
continue
used_later = x in live
if leaf.ty.linear and not scope.used(x) and not used_later:
# TODO: This should be "Variable x with linear type ty is not
Expand Down
1 change: 0 additions & 1 deletion tests/integration/test_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ def main(s: MyStruct, t: MyStruct) -> MyStruct:
validate(module.compile())


@pytest.mark.skip("Fails due to https://github.com/CQCL/guppylang/issues/337")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by: This test works now

def test_move_back_branch(validate):
module = GuppyModule("test")
module.load(quantum)
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,30 @@ def foo(i: bool) -> bool:
return b


def test_while_move_back(validate):
module = GuppyModule("test")
module.load(quantum)

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

@guppy.declare(module)
def use(q: qubit) -> None: ...

@guppy(module)
def test(s: MyStruct) -> MyStruct:
use(s.q)
while True:
s.q = qubit()
return s
# Guppy is not yet smart enough to detect that this code is unreachable
s.q = qubit()
return s

validate(module.compile())


def test_for(validate):
module = GuppyModule("test")
module.load(quantum)
Expand Down
Loading