Skip to content
Draft
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
14 changes: 14 additions & 0 deletions guppylang-internals/src/guppylang_internals/tracing/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from guppylang_internals.checker.errors.type_errors import TypeMismatchError
from guppylang_internals.compiler.core import CompilerContext, DFContainer
from guppylang_internals.compiler.expr_compiler import ExprCompiler
from guppylang_internals.definition.custom import CustomFunctionDef
from guppylang_internals.definition.value import CallableDef
from guppylang_internals.diagnostic import Error
from guppylang_internals.error import GuppyComptimeError, GuppyError, exception_hook
Expand Down Expand Up @@ -44,6 +45,13 @@ class TracingReturnError(Error):
msg: str


@dataclass(frozen=True)
class VarArgsError(Error):
title: ClassVar[str] = "Varargs functions not supported in comptime"
message: ClassVar[str] = "{msg}"
msg: str


def trace_function(
python_func: Callable[..., Any],
ty: FunctionType,
Expand Down Expand Up @@ -173,6 +181,12 @@ def trace_call(func: CallableDef, *args: Any) -> Any:
# Update inouts
# If the input types of the function aren't known, we can't check this.
# This is the case for functions with a custom checker and no type annotations.
if isinstance(func, CustomFunctionDef) and not func.has_signature:
msg = f"Try wrapping `{func.name}` in a non-comptime @guppy function"
err = VarArgsError(call_node, msg)

raise GuppyError(err) from None

if len(func.ty.inputs) != 0:
for inp, arg, var in zip(func.ty.inputs, args, arg_vars, strict=True):
if InputFlags.Inout in inp.flags:
Expand Down
1 change: 0 additions & 1 deletion tests/emulator/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def test_emulator_builder_immutability():
assert builder.verbose is False



def test_emulator_builder_reuse():
"""Test that the same builder can be used multiple times."""
builder = EmulatorBuilder().with_name("reusable").with_verbose(True)
Expand Down
7 changes: 7 additions & 0 deletions tests/error/tracing_errors/varargs.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Traceback (most recent call last):
File "$FILE", line 12, in <module>
main.compile()
File "$FILE", line 8, in main
barrier(qs)
guppylang_internals.error.GuppyComptimeError: Varargs functions not supported in comptime
Try wrapping `barrier` in a non-comptime @guppy function
12 changes: 12 additions & 0 deletions tests/error/tracing_errors/varargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from guppylang import guppy
from guppylang.std.quantum import qubit, h, discard_array
from guppylang.std.builtins import array, barrier

@guppy.comptime
def main() -> None:
qs = array(qubit() for _ in range(5))
barrier(qs)
h(qs[0])
discard_array(qs)

main.compile()
21 changes: 19 additions & 2 deletions tests/integration/tracing/test_quantum.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from guppylang import qubit
from guppylang.decorator import guppy
from guppylang.std.angles import angle
from guppylang.std.builtins import array
from guppylang.std.quantum import h, measure, cx, rz
from guppylang.std.builtins import array, barrier
from guppylang.std.quantum import h, measure, cx, rz, discard_array
import itertools


Expand Down Expand Up @@ -40,3 +40,20 @@ def test(qs: array[qubit, 10], theta: angle) -> None:
theta /= 2

validate(test.compile())


def test_barrier_wrapper(validate):
"""Test workaround for https://github.com/CQCL/guppylang/issues/1189"""

@guppy
def barrier_wrapper(qs: array[qubit, 5]) -> None:
barrier(qs)

@guppy.comptime
def main() -> None:
qs = array(qubit() for _ in range(5))
barrier_wrapper(qs)
h(qs[0])
discard_array(qs)

validate(main.compile())
Loading