Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ jobs:
- name: Run tests
run: uv run pytest

- name: Install tket2 dependencies
- name: Install tket dependencies
run: uv sync --extra pytket

- name: Rerun comptime expression tests and pytket lowering with tket2 installed
- name: Rerun comptime expression tests and pytket lowering with tket installed
run: uv run pytest tests/integration/test_comptime_expr.py tests/error/test_comptime_expr_errors.py tests/integration/test_pytket_circuits.py

test-coverage:
Expand Down
14 changes: 0 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,3 @@ debug_assert_with_mut_call = "warn"

[workspace.dependencies]
pyo3 = "0.24.1"
serde_json = "1.0.111"
cargo_toml = "0.20.4"
thiserror = "2.0.6"
hugr = "0.20.2"
hugr-cli = "0.20.2"
tket2 = { version = "0.12.0" }

[patch.crates-io]

# Uncomment these to test the latest dependency version during development
# hugr = { git = "https://github.com/CQCL/hugr", rev = "42ce05d" }
# hugr-cli = { git = "https://github.com/CQCL/hugr", rev = "42ce05d" }
# hugr-llvm = { git = "https://github.com/CQCL/hugr", rev = "42ce05d" }
# hugr-passes = { git = "https://github.com/CQCL/hugr", rev = "42ce05d" }
2 changes: 0 additions & 2 deletions devenv.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{ pkgs, lib, ... }:

{
# for building optional tket2 dependency
# see https://github.com/CQCL/tket2/blob/main/devenv.nix
packages = [
pkgs.just
pkgs.graphviz
Expand Down
7 changes: 3 additions & 4 deletions examples/t_factory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[0.2967-0.j 0.8094-0.5068j]\n"
"True\n"
]
}
],
"source": [
"runner = build(compiled)\n",
"\n",
"shots = QsysResult(\n",
" runner.run_shots(\n",
" simulator=Quest(random_seed=1),\n",
Expand All @@ -224,7 +223,7 @@
" states = Quest.extract_states_dict(shot.entries)\n",
" if \"t_state\" in states:\n",
" dist = states[\"t_state\"].get_state_vector_distribution()\n",
" print(f\"{dist[0].state}\")"
" print(np.allclose(dist[0].state, np.array([0.2967+0.j, 0.8094-0.5068j]), rtol=1e-4))"
]
},
{
Expand Down Expand Up @@ -339,7 +338,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "guppylang (3.13.4)",
"display_name": "venv",
"language": "python",
"name": "python3"
},
Expand Down
8 changes: 4 additions & 4 deletions guppylang/checker/errors/comptime_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class ComptimeExprIncoherentListError(Error):


@dataclass(frozen=True)
class Tket2NotInstalled(Error):
title: ClassVar[str] = "Tket2 not installed"
class TketNotInstalled(Error):
title: ClassVar[str] = "Tket not installed"
span_label: ClassVar[str] = (
"Experimental pytket compatibility requires `tket2` to be installed"
"Experimental pytket compatibility requires `tket` to be installed"
)

@dataclass(frozen=True)
class InstallInstruction(Help):
message: ClassVar[str] = "Install tket2: `pip install tket2`"
message: ClassVar[str] = "Install tket: `pip install tket`"


@dataclass(frozen=True)
Expand Down
2 changes: 1 addition & 1 deletion guppylang/compiler/cfg_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from guppylang.compiler.expr_compiler import ExprCompiler
from guppylang.compiler.stmt_compiler import StmtCompiler
from guppylang.std._internal.compiler.tket2_bool import OpaqueBool, read_bool
from guppylang.std._internal.compiler.tket_bool import OpaqueBool, read_bool
from guppylang.tys.ty import SumType, row_to_type, type_to_row


Expand Down
10 changes: 5 additions & 5 deletions guppylang/compiler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, ClassVar, cast

import tket2_exts
import tket_exts
from hugr import Hugr, Node, Wire, ops
from hugr import tys as ht
from hugr.build import function as hf
Expand Down Expand Up @@ -272,7 +272,7 @@ def declare_global_func(
"""
if (const_id, mono_args) in self.global_funcs:
return self.global_funcs[const_id, mono_args], True
func = self.module.define_function(
func = self.module.module_root_builder().define_function(
name=const_id.name,
input_types=func_ty.body.input,
output_types=func_ty.body.output,
Expand Down Expand Up @@ -514,9 +514,9 @@ def compile_variable_idx(idx: int, mono_args: PartiallyMonomorphizedArgs) -> int
return sum(1 for arg in mono_args[:idx] if arg is None)


QUANTUM_EXTENSION = tket2_exts.quantum()
RESULT_EXTENSION = tket2_exts.result()
DEBUG_EXTENSION = tket2_exts.debug()
QUANTUM_EXTENSION = tket_exts.quantum()
RESULT_EXTENSION = tket_exts.result()
DEBUG_EXTENSION = tket_exts.debug()

#: List of extension ops that have side-effects, identified by their qualified name
EXTENSION_OPS_WITH_SIDE_EFFECTS: list[str] = [
Expand Down
16 changes: 8 additions & 8 deletions guppylang/compiler/expr_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
build_unwrap,
panic,
)
from guppylang.std._internal.compiler.tket2_bool import (
from guppylang.std._internal.compiler.tket_bool import (
OpaqueBool,
OpaqueBoolVal,
make_opaque,
Expand Down Expand Up @@ -606,7 +606,7 @@ def visit_PanicExpr(self, node: PanicExpr) -> Wire:
def visit_BarrierExpr(self, node: BarrierExpr) -> Wire:
hugr_tys = [get_type(e).to_hugr(self.ctx) for e in node.args]
op = hugr.std.prelude.PRELUDE_EXTENSION.get_op("Barrier").instantiate(
[ht.SequenceArg([ht.TypeTypeArg(ty) for ty in hugr_tys])],
[ht.ListArg([ht.TypeTypeArg(ty) for ty in hugr_tys])],
ht.FunctionType.endo(hugr_tys),
)

Expand Down Expand Up @@ -854,9 +854,9 @@ def array_comprehension_init_func(ctx: CompilerContext) -> hf.Function:

See https://github.com/CQCL/guppylang/issues/629
"""
v = ht.Variable(0, ht.TypeBound(ht.TypeBound.Any))
v = ht.Variable(0, ht.TypeBound(ht.TypeBound.Linear))
sig = ht.PolyFuncType(
params=[ht.TypeTypeParam(ht.TypeBound.Any)],
params=[ht.TypeTypeParam(ht.TypeBound.Linear)],
body=ht.FunctionType([], [ht.Option(v)]),
)
func, already_defined = ctx.declare_global_func(ARRAY_COMPREHENSION_INIT, sig)
Expand All @@ -868,9 +868,9 @@ def array_comprehension_init_func(ctx: CompilerContext) -> hf.Function:
def array_unwrap_elem(ctx: CompilerContext) -> hf.Function:
"""Returns the Hugr function that is used to unwrap the elements in an option array
to turn it into a regular array."""
v = ht.Variable(0, ht.TypeBound(ht.TypeBound.Any))
v = ht.Variable(0, ht.TypeBound(ht.TypeBound.Linear))
sig = ht.PolyFuncType(
params=[ht.TypeTypeParam(ht.TypeBound.Any)],
params=[ht.TypeTypeParam(ht.TypeBound.Linear)],
body=ht.FunctionType([ht.Option(v)], [v]),
)
func, already_defined = ctx.declare_global_func(ARRAY_UNWRAP_ELEM, sig)
Expand All @@ -883,9 +883,9 @@ def array_unwrap_elem(ctx: CompilerContext) -> hf.Function:
def array_wrap_elem(ctx: CompilerContext) -> hf.Function:
"""Returns the Hugr function that is used to wrap the elements in an regular array
to turn it into a option array."""
v = ht.Variable(0, ht.TypeBound(ht.TypeBound.Any))
v = ht.Variable(0, ht.TypeBound(ht.TypeBound.Linear))
sig = ht.PolyFuncType(
params=[ht.TypeTypeParam(ht.TypeBound.Any)],
params=[ht.TypeTypeParam(ht.TypeBound.Linear)],
body=ht.FunctionType([v], [ht.Option(v)]),
)
func, already_defined = ctx.declare_global_func(ARRAY_WRAP_ELEM, sig)
Expand Down
2 changes: 1 addition & 1 deletion guppylang/compiler/func_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def compile_local_func_def(
# Prepend captured variables to the function arguments
func_ty = func.ty.to_hugr(ctx)
closure_ty = ht.FunctionType([*captured_types, *func_ty.input], func_ty.output)
func_builder = dfg.builder.define_function(
func_builder = dfg.builder.module_root_builder().define_function(
func.name, closure_ty.input, closure_ty.output
)

Expand Down
40 changes: 20 additions & 20 deletions guppylang/compiler/hugr_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
poly_func=ht.PolyFuncType(
params=[
# Captured input types
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Any)),
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Linear)),
# Non-captured input types
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Any)),
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Linear)),
# Output types
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Any)),
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Linear)),
],
body=ht.FunctionType(
input=[
ht.FunctionType(
input=[
ht.RowVariable(0, ht.TypeBound.Any),
ht.RowVariable(1, ht.TypeBound.Any),
ht.RowVariable(0, ht.TypeBound.Linear),
ht.RowVariable(1, ht.TypeBound.Linear),
],
output=[ht.RowVariable(2, ht.TypeBound.Any)],
output=[ht.RowVariable(2, ht.TypeBound.Linear)],
),
ht.RowVariable(0, ht.TypeBound.Any),
ht.RowVariable(0, ht.TypeBound.Linear),
],
output=[
ht.FunctionType(
input=[ht.RowVariable(1, ht.TypeBound.Any)],
output=[ht.RowVariable(2, ht.TypeBound.Any)],
input=[ht.RowVariable(1, ht.TypeBound.Linear)],
output=[ht.RowVariable(2, ht.TypeBound.Linear)],
),
],
),
Expand Down Expand Up @@ -109,9 +109,9 @@ def type_args(self) -> list[ht.TypeArg]:
other_args: list[ht.TypeArg] = [ht.TypeTypeArg(ty) for ty in self.other_inputs]
output_args: list[ht.TypeArg] = [ht.TypeTypeArg(ty) for ty in self.outputs]
return [
ht.SequenceArg(captured_args),
ht.SequenceArg(other_args),
ht.SequenceArg(output_args),
ht.ListArg(captured_args),
ht.ListArg(other_args),
ht.ListArg(output_args),
]

def cached_signature(self) -> ht.FunctionType | None:
Expand Down Expand Up @@ -151,13 +151,13 @@ def num_out(self) -> int:
# Name of the operation
ht.StringParam(),
# Input types
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Any)),
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Linear)),
# Output types
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Any)),
ht.ListParam(ht.TypeTypeParam(ht.TypeBound.Linear)),
],
body=ht.FunctionType(
input=[ht.RowVariable(1, ht.TypeBound.Any)],
output=[ht.RowVariable(2, ht.TypeBound.Any)],
input=[ht.RowVariable(1, ht.TypeBound.Linear)],
output=[ht.RowVariable(2, ht.TypeBound.Linear)],
),
)
),
Expand Down Expand Up @@ -185,8 +185,8 @@ def op_def(self) -> he.OpDef:

def type_args(self) -> list[ht.TypeArg]:
op_name = ht.StringArg(self.op_name)
input_args = ht.SequenceArg([ht.TypeTypeArg(ty) for ty in self.inputs])
output_args = ht.SequenceArg([ht.TypeTypeArg(ty) for ty in self.outputs])
input_args = ht.ListArg([ht.TypeTypeArg(ty) for ty in self.inputs])
output_args = ht.ListArg([ht.TypeTypeArg(ty) for ty in self.outputs])
return [op_name, input_args, output_args]

def cached_signature(self) -> ht.FunctionType | None:
Expand Down Expand Up @@ -217,8 +217,8 @@ def num_out(self) -> int:


def _arg_seq_to_types(args: ht.TypeArg) -> Iterator[ht.Type]:
"""Converts a SequenceArg of type arguments into a sequence of types."""
assert isinstance(args, ht.SequenceArg)
"""Converts a ListArg of type arguments into a sequence of types."""
assert isinstance(args, ht.ListArg)
for arg in args.elems:
assert isinstance(arg, ht.TypeTypeArg)
yield arg.ty
2 changes: 1 addition & 1 deletion guppylang/definition/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from guppylang.error import GuppyError, InternalGuppyError
from guppylang.nodes import GlobalCall
from guppylang.span import SourceMap
from guppylang.std._internal.compiler.tket2_bool import (
from guppylang.std._internal.compiler.tket_bool import (
OpaqueBool,
make_opaque,
read_bool,
Expand Down
2 changes: 1 addition & 1 deletion guppylang/definition/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def monomorphize(
"""
mono_ty = self.ty.instantiate_partial(mono_args)
hugr_ty = mono_ty.to_hugr_poly(ctx)
func_def = module.define_function(
func_def = module.module_root_builder().define_function(
self.name, hugr_ty.body.input, hugr_ty.body.output, hugr_ty.params
)
return CompiledFunctionDef(
Expand Down
14 changes: 7 additions & 7 deletions guppylang/definition/pytket_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from guppylang.checker.core import Context, Globals
from guppylang.checker.errors.comptime_errors import (
PytketSignatureMismatch,
Tket2NotInstalled,
TketNotInstalled,
)
from guppylang.checker.expr_checker import check_call, synthesize_call
from guppylang.checker.func_checker import (
Expand Down Expand Up @@ -47,7 +47,7 @@
array_pop,
)
from guppylang.std._internal.compiler.prelude import build_unwrap
from guppylang.std._internal.compiler.tket2_bool import OpaqueBool, make_opaque
from guppylang.std._internal.compiler.tket_bool import OpaqueBool, make_opaque
from guppylang.tys.builtin import array_type, bool_type
from guppylang.tys.subst import Inst, Subst
from guppylang.tys.ty import (
Expand Down Expand Up @@ -167,7 +167,7 @@ def compile_outer(
import pytket

if isinstance(self.input_circuit, pytket.circuit.Circuit):
from tket2.circuit import ( # type: ignore[import-untyped, import-not-found, unused-ignore]
from tket.circuit import ( # type: ignore[import-untyped, import-not-found, unused-ignore]
Tk2Circuit,
)

Expand All @@ -179,7 +179,7 @@ def compile_outer(
hugr_func = mapping[circ.entrypoint]

func_type = self.ty.to_hugr_poly(ctx)
outer_func = module.define_function(
outer_func = module.module_root_builder().define_function(
self.name, func_type.body.input, func_type.body.output
)

Expand Down Expand Up @@ -365,7 +365,7 @@ def _signature_from_circuit(

if isinstance(input_circuit, pytket.circuit.Circuit):
try:
import tket2 # type: ignore[import-untyped, import-not-found, unused-ignore] # noqa: F401
import tket # type: ignore[import-untyped, import-not-found, unused-ignore] # noqa: F401

from guppylang.std.quantum import qubit

Expand All @@ -392,8 +392,8 @@ def _signature_from_circuit(
row_to_type([bool_type()] * input_circuit.n_bits),
)
except ImportError:
err = Tket2NotInstalled(defined_at)
err.add_sub_diagnostic(Tket2NotInstalled.InstallInstruction(None))
err = TketNotInstalled(defined_at)
err.add_sub_diagnostic(TketNotInstalled.InstallInstruction(None))
raise GuppyError(err) from None
else:
pass
Expand Down
2 changes: 1 addition & 1 deletion guppylang/definition/traced.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def compile_outer(
`CompiledFunctionDef.compile_inner()`.
"""
func_type = self.ty.to_hugr_poly(ctx)
func_def = module.define_function(
func_def = module.module_root_builder().define_function(
self.name, func_type.body.input, func_type.body.output, func_type.params
)
return CompiledTracedFunctionDef(
Expand Down
4 changes: 2 additions & 2 deletions guppylang/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ def compile(self, id: DefId) -> ModulePointer:
# We should compute this dynamically from the imported dependencies instead.
#
# The hugr prelude and std_extensions are implicit.
from guppylang.std._internal.compiler.tket2_exts import TKET2_EXTENSIONS
from guppylang.std._internal.compiler.tket_exts import TKET_EXTENSIONS

extensions = [
*TKET2_EXTENSIONS,
*TKET_EXTENSIONS,
guppylang.compiler.hugr_extension.EXTENSION,
*self.additional_extensions,
]
Expand Down
Loading
Loading