Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion guppylang/src/guppylang/std/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from guppylang.std.array import ArrayIter, FrozenarrayIter, array, frozenarray
from guppylang.std.bool import bool
from guppylang.std.iter import Range, SizedIter, range
from guppylang.std.lang import comptime, owned, py
from guppylang.std.lang import comptime, control, dagger, owned, power, py
from guppylang.std.list import list
from guppylang.std.mem import mem_swap
from guppylang.std.num import (
Expand Down Expand Up @@ -102,6 +102,8 @@
"compile",
"complex",
"comptime",
"control",
"dagger",
"delattr",
"dict",
"dir",
Expand Down Expand Up @@ -145,6 +147,7 @@
"owned",
"panic",
"pow",
"power",
"print",
"property",
"py",
Expand Down
20 changes: 20 additions & 0 deletions guppylang/src/guppylang/std/lang.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Provides Python objects for builtin language keywords."""

from collections.abc import Generator
from contextlib import contextmanager
from typing import Any, Protocol, TypeVar

T = TypeVar("T")
Expand Down Expand Up @@ -42,3 +44,21 @@ class Copy(Protocol):

class Drop(Protocol):
"""Bound to mark generic type parameters as being implicitly droppable."""


@contextmanager
def control() -> Generator[None]:
"""Dummy context manager to support `with control(...):` blocks in Guppy code."""
yield


@contextmanager
def dagger() -> Generator[None]:
"""Dummy context manager to support `with dagger:` blocks in Guppy code."""
yield


@contextmanager
def power() -> Generator[None]:
"""Dummy context manager to support `with power(...):` blocks in Guppy code."""
yield
12 changes: 6 additions & 6 deletions tests/error/modifier_errors/captured_var_inout_own.err
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Error: Not owned (at $FILE:14:16)
Error: Not owned (at $FILE:16:16)
|
12 | a = qubit()
13 | with dagger:
14 | discard(a)
14 | a = qubit()
15 | with dagger:
16 | discard(a)
| ^ Function `discard` wants to take ownership of this argument,
| but `__modified__()` doesn't own `a`

Help:
|
11 | def test() -> None:
12 | a = qubit()
13 | def test() -> None:
14 | a = qubit()
| - Argument `a` is only borrowed. Consider taking ownership:
| `a: qubit @owned`

Expand Down
2 changes: 2 additions & 0 deletions tests/error/modifier_errors/captured_var_inout_own.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, owned
from guppylang.std.builtins import dagger



@guppy.declare(dagger=True)
Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/captured_var_inout_reassign.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Drop violation (at $FILE:11:4)
Error: Drop violation (at $FILE:13:4)
|
9 | @guppy
10 | def test() -> None:
11 | a = qubit()
11 | @guppy
12 | def test() -> None:
13 | a = qubit()
| ^ Variable `a` with non-droppable type `qubit` is leaked

Help: Make sure that `a` is consumed or returned to avoid the leak
Expand Down
2 changes: 2 additions & 0 deletions tests/error/modifier_errors/captured_var_inout_reassign.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit
from guppylang.std.builtins import dagger



@guppy.declare(dagger=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/error/modifier_errors/ctrl_arg_array_many_arg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from guppylang.decorator import guppy
from guppylang.std.array import array
from guppylang.std.builtins import owned
from guppylang.std.builtins import owned, control
from guppylang.std.quantum import qubit


Expand Down
12 changes: 6 additions & 6 deletions tests/error/modifier_errors/ctrl_arg_body_copy.err
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Error: Copy violation (at $FILE:12:20)
Error: Copy violation (at $FILE:13:20)
|
10 | def test() -> None:
11 | q = qubit()
12 | with control(q, q):
11 | def test() -> None:
12 | q = qubit()
13 | with control(q, q):
| ^ Variable `q` with non-copyable type `qubit` cannot be
| borrowed ...

Note:
|
11 | q = qubit()
12 | with control(q, q):
12 | q = qubit()
13 | with control(q, q):
| - Variable `q` already borrowed here

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/ctrl_arg_body_copy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import control
from guppylang.std.quantum import qubit, owned


Expand Down
12 changes: 6 additions & 6 deletions tests/error/modifier_errors/ctrl_arg_copy.err
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Error: Copy violation (at $FILE:17:12)
Error: Copy violation (at $FILE:18:12)
|
15 | q = qubit()
16 | with control(q):
17 | use(q)
16 | q = qubit()
17 | with control(q):
18 | use(q)
| ^ Variable `q` with non-copyable type `qubit` cannot be
| borrowed ...

Note:
|
15 | q = qubit()
16 | with control(q):
16 | q = qubit()
17 | with control(q):
| - Variable `q` already borrowed here

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/ctrl_arg_copy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import control
from guppylang.std.quantum import qubit, owned


Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/ctrl_arg_not_assigned.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Drop violation (at $FILE:7:17)
Error: Drop violation (at $FILE:8:17)
|
5 | @guppy
6 | def test() -> None:
7 | with control(qubit()):
6 | @guppy
7 | def test() -> None:
8 | with control(qubit()):
| ^^^^^^^ Expression with non-droppable type `qubit` is leaked

Help: Consider assigning this value to a local variable
Expand Down
1 change: 1 addition & 0 deletions tests/error/modifier_errors/ctrl_arg_not_assigned.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import control
from guppylang.std.quantum import qubit


Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/ctrl_arg_type.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Type mismatch (at $FILE:7:20)
Error: Type mismatch (at $FILE:9:20)
|
5 | def test() -> None:
6 | x = qubit()
7 | with control(x, True):
7 | def test() -> None:
8 | x = qubit()
9 | with control(x, True):
| ^^^^ Expected expression of type `qubit`, got `bool`

Guppy compilation failed due to 1 previous error
2 changes: 2 additions & 0 deletions tests/error/modifier_errors/ctrl_arg_type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit
from guppylang.std.builtins import control


@guppy
Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/ctrl_arg_type_arr.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Type mismatch (at $FILE:7:17)
Error: Type mismatch (at $FILE:9:17)
|
5 | def test() -> None:
6 | x = array(1, 2, 3)
7 | with control(x):
7 | def test() -> None:
8 | x = array(1, 2, 3)
9 | with control(x):
| ^ Expected expression of type `array[qubit, ?n]`, got
| `array[int, 3]`

Expand Down
2 changes: 2 additions & 0 deletions tests/error/modifier_errors/ctrl_arg_type_arr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import array
from guppylang.std.builtins import control


@guppy
Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/ctrl_no_arg.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Not enough arguments (at $FILE:8:16)
Error: Not enough arguments (at $FILE:9:16)
|
6 | @guppy
7 | def test() -> None:
8 | with control():
7 | @guppy
8 | def test() -> None:
9 | with control():
| ^^ Missing argument (expected 1, got 0)

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/ctrl_no_arg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import control


# `control` is overloaded, so the number of arguments need not be fixed to 1.
Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/dagger_arg.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Too many arguments (at $FILE:6:16)
Error: Too many arguments (at $FILE:7:16)
|
4 | @guppy
5 | def test() -> None:
6 | with dagger(1):
5 | @guppy
6 | def test() -> None:
7 | with dagger(1):
| ^ Unexpected argument (expected 0, got 1)

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/dagger_arg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import dagger


@guppy
Expand Down
12 changes: 6 additions & 6 deletions tests/error/modifier_errors/dagger_assign.err
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Error: Invalid expression in dagger (at $FILE:7:8)
Error: Invalid expression in dagger (at $FILE:8:8)
|
5 | def test() -> None:
6 | with dagger:
7 | a = 3
6 | def test() -> None:
7 | with dagger:
8 | a = 3
| ^^^^^ Assignment found in a dagger context

Note:
|
5 | def test() -> None:
6 | with dagger:
6 | def test() -> None:
7 | with dagger:
| ------ dagger modifier is used here

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/dagger_assign.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import dagger


@guppy
Expand Down
14 changes: 7 additions & 7 deletions tests/error/modifier_errors/dagger_loop.err
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Error: Invalid expression in dagger (at $FILE:7:8)
Error: Invalid expression in dagger (at $FILE:8:8)
|
5 | def test() -> None:
6 | with dagger:
7 | for _ in range(46):
6 | def test() -> None:
7 | with dagger:
8 | for _ in range(46):
| ^^^^^^^^^^^^^^^^^^^
8 | pass
9 | pass
| ^^^^^^^^^^^^^^^^ Loop found in a dagger context

Note:
|
5 | def test() -> None:
6 | with dagger:
6 | def test() -> None:
7 | with dagger:
| ------ dagger modifier is used here

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/dagger_loop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import dagger


@guppy
Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/flags_nested.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Unitary constraint violation (at $FILE:15:12)
Error: Unitary constraint violation (at $FILE:16:12)
|
13 | with dagger:
14 | with power(2):
15 | foo(q)
14 | with dagger:
15 | with power(2):
16 | foo(q)
| ^^^^^^ This function cannot be called in a dagger context

Guppy compilation failed due to 1 previous error
1 change: 1 addition & 0 deletions tests/error/modifier_errors/flags_nested.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import dagger, power
from guppylang.std.quantum import qubit


Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/flags_nested_combined_outer.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Unitary constraint violation (at $FILE:15:12)
Error: Unitary constraint violation (at $FILE:16:12)
|
13 | with control(ctrl), dagger:
14 | with power(2):
15 | foo(q)
14 | with control(ctrl), dagger:
15 | with power(2):
16 | foo(q)
| ^^^^^^ This function cannot be called in a unitary context

Guppy compilation failed due to 1 previous error
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import control, dagger, power
from guppylang.std.quantum import qubit


Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/flags_nested_control_dagger.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Unitary constraint violation (at $FILE:15:12)
Error: Unitary constraint violation (at $FILE:16:12)
|
13 | with control(ctrl):
14 | with dagger:
15 | foo(q)
14 | with control(ctrl):
15 | with dagger:
16 | foo(q)
| ^^^^^^ This function cannot be called in a dagger context

Guppy compilation failed due to 1 previous error
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppylang.decorator import guppy
from guppylang.std.builtins import control, dagger
from guppylang.std.quantum import qubit


Expand Down
8 changes: 4 additions & 4 deletions tests/error/modifier_errors/flags_nested_missing_power.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Unitary constraint violation (at $FILE:15:12)
Error: Unitary constraint violation (at $FILE:16:12)
|
13 | with dagger:
14 | with power(2):
15 | foo(q)
14 | with dagger:
15 | with power(2):
16 | foo(q)
| ^^^^^^ This function cannot be called in a power context

Guppy compilation failed due to 1 previous error
Loading
Loading