Skip to content

Commit

Permalink
feat: update to hugr 0.10 and tket2 0.6 (#725)
Browse files Browse the repository at this point in the history
Closes #720 

~Waiting for tket2 release~
  • Loading branch information
ss2165 authored Dec 17, 2024
1 parent 82dba49 commit 63ea7a7
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 673 deletions.
745 changes: 230 additions & 515 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ debug_assert_with_mut_call = "warn"
pyo3 = "0.19.0"
serde_json = "1.0.111"
cargo_toml = "0.20.4"
thiserror = "1.0.37"
hugr-llvm = "0.6.1"
hugr = "0.13.1"
inkwell = "0.4.0"
thiserror = "2.0.6"
hugr = "0.14.0"
hugr-cli = "0.14.0"
inkwell = "0.5.0"

[patch.crates-io]

# Uncomment these to test the latest dependency version during development
#hugr = { git = "https://github.com/CQCL/hugr", rev = "6bf6c82c9ec9d801ab43e311e5f815a3eea7d9c1" }
#hugr-cli = { git = "https://github.com/CQCL/hugr", rev = "6bf6c82c9ec9d801ab43e311e5f815a3eea7d9c1" }
#hugr-llvm = { git = "https://github.com/CQCL/hugr-llvm", rev = "c1e6407" }
# hugr = { git = "https://github.com/CQCL/hugr", rev = "861183e" }
# hugr-cli = { git = "https://github.com/CQCL/hugr", rev = "861183e" }
# hugr-llvm = { git = "https://github.com/CQCL/hugr", rev = "1091755" }
3 changes: 1 addition & 2 deletions execute_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ name = "execute_llvm"
crate-type = ["cdylib"]

[dependencies]
hugr-llvm.workspace = true
hugr.workspace = true
hugr = {workspace = true, features = ["llvm"]}
inkwell.workspace = true
pyo3.workspace = true
serde_json.workspace = true
38 changes: 11 additions & 27 deletions execute_llvm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! This module provides a Python interface to compile and execute a Hugr program to LLVM IR.
use hugr::{
self, extension::ExtensionRegistry, ops, ops::custom::resolve_extension_ops, std_extensions,
HugrView,
};
use hugr_llvm::utils::fat::FatExt;
use hugr::llvm::utils::fat::FatExt;
use hugr::Hugr;
use hugr::{self, ops, std_extensions, HugrView};
use inkwell::{context::Context, module::Module, values::GenericValue};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
Expand All @@ -13,22 +11,8 @@ macro_rules! pyerr {
}

fn parse_hugr(hugr_json: &str) -> PyResult<hugr::Hugr> {
// Deserializing should be a given if we validate before running
let mut hugr =
serde_json::from_str(hugr_json).map_err(|e| pyerr!("Couldn't deserialize hugr: {}", e))?;
let reg = ExtensionRegistry::try_new([
hugr::extension::PRELUDE.to_owned(),
std_extensions::arithmetic::int_ops::EXTENSION.to_owned(),
std_extensions::arithmetic::int_types::EXTENSION.to_owned(),
std_extensions::arithmetic::float_ops::EXTENSION.to_owned(),
std_extensions::arithmetic::float_types::EXTENSION.to_owned(),
std_extensions::arithmetic::conversions::EXTENSION.to_owned(),
std_extensions::collections::EXTENSION.to_owned(),
std_extensions::logic::EXTENSION.to_owned(),
])
.map_err(|e| pyerr!("Making extension registry: {}", e))?;
resolve_extension_ops(&mut hugr, &reg)
.map_err(|e| pyerr!("Instantiating extension ops: {}", e))?;
let hugr = Hugr::load_json(hugr_json.as_bytes(), &std_extensions::std_reg())
.map_err(|e| pyerr!("Couldn't deserialize hugr: {}", e))?;
Ok(hugr)
}

Expand Down Expand Up @@ -57,19 +41,19 @@ fn find_funcdef_node(hugr: impl HugrView, fn_name: &str) -> PyResult<hugr::Node>
fn compile_module<'a>(
hugr: &'a hugr::Hugr,
ctx: &'a Context,
namer: hugr_llvm::emit::Namer,
namer: hugr::llvm::emit::Namer,
) -> PyResult<Module<'a>> {
let llvm_module = ctx.create_module("guppy_llvm");
// TODO: Handle tket2 codegen extension
let extensions = hugr_llvm::custom::CodegenExtsBuilder::default()
let extensions = hugr::llvm::custom::CodegenExtsBuilder::default()
.add_int_extensions()
.add_default_prelude_extensions()
.add_default_array_extensions()
.add_float_extensions()
.add_conversion_extensions()
.add_default_rotation_extensions();
.add_conversion_extensions();

let emitter =
hugr_llvm::emit::EmitHugr::new(ctx, llvm_module, namer.into(), extensions.finish().into());
hugr::llvm::emit::EmitHugr::new(ctx, llvm_module, namer.into(), extensions.finish().into());
let hugr_module = hugr.fat_root().unwrap();
let emitter = emitter
.emit_module(hugr_module)
Expand All @@ -96,7 +80,7 @@ fn run_function<T>(
let hugr = parse_hugr(hugr_json)?;
let ctx = Context::create();

let namer = hugr_llvm::emit::Namer::default();
let namer = hugr::llvm::emit::Namer::default();
let funcdefn_node = find_funcdef_node(&hugr, fn_name)?;
let mangled_name = namer.name_func(fn_name, funcdefn_node);

Expand Down
6 changes: 3 additions & 3 deletions guppylang/std/_internal/compiler/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from __future__ import annotations

import hugr.std
from hugr import Wire, ops
from hugr import tys as ht
from hugr.std.collections.array import EXTENSION

from guppylang.compiler.hugr_extension import UnsupportedOp
from guppylang.definition.custom import CustomCallCompiler
Expand All @@ -31,15 +31,15 @@ def _instantiate_array_op(
inp: list[ht.Type],
out: list[ht.Type],
) -> ops.ExtOp:
return hugr.std.PRELUDE.get_op(name).instantiate(
return EXTENSION.get_op(name).instantiate(
[length, ht.TypeTypeArg(elem_ty)], ht.FunctionType(inp, out)
)


def array_type(elem_ty: ht.Type, length: ht.TypeArg) -> ht.ExtType:
"""Returns the hugr type of a fixed length array."""
elem_arg = ht.TypeTypeArg(elem_ty)
return hugr.std.PRELUDE.types["array"].instantiate([length, elem_arg])
return EXTENSION.types["array"].instantiate([length, elem_arg])


def array_new(elem_ty: ht.Type, length: int) -> ops.ExtOp:
Expand Down
18 changes: 9 additions & 9 deletions guppylang/std/_internal/compiler/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from typing import TYPE_CHECKING

import hugr.std.collections
import hugr.std.collections.list
from hugr import Wire, ops
from hugr import tys as ht
from hugr.std.collections import ListVal
from hugr.std.collections.list import List, ListVal

from guppylang.definition.custom import CustomCallCompiler
from guppylang.definition.value import CallReturnWires
Expand Down Expand Up @@ -37,7 +37,7 @@
def _instantiate_list_op(
name: str, elem_type: ht.Type, inp: list[ht.Type], out: list[ht.Type]
) -> ops.ExtOp:
op_def = hugr.std.collections.EXTENSION.get_op(name)
op_def = hugr.std.collections.list.EXTENSION.get_op(name)
return ops.ExtOp(
op_def,
ht.FunctionType(inp, out),
Expand All @@ -47,29 +47,29 @@ def _instantiate_list_op(

def list_pop(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `pop` operation."""
list_type = hugr.std.collections.list_type(elem_type)
list_type = List(elem_type)
return _instantiate_list_op(
"pop", elem_type, [list_type], [list_type, ht.Option(elem_type)]
)


def list_push(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `push` operation."""
list_type = hugr.std.collections.list_type(elem_type)
list_type = List(elem_type)
return _instantiate_list_op("push", elem_type, [list_type, elem_type], [list_type])


def list_get(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `get` operation."""
list_type = hugr.std.collections.list_type(elem_type)
list_type = List(elem_type)
return _instantiate_list_op(
"get", elem_type, [list_type, ht.USize()], [ht.Option(elem_type)]
)


def list_set(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `set` operation."""
list_type = hugr.std.collections.list_type(elem_type)
list_type = List(elem_type)
return _instantiate_list_op(
"set",
elem_type,
Expand All @@ -80,7 +80,7 @@ def list_set(elem_type: ht.Type) -> ops.ExtOp:

def list_insert(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `insert` operation."""
list_type = hugr.std.collections.list_type(elem_type)
list_type = List(elem_type)
return _instantiate_list_op(
"insert",
elem_type,
Expand All @@ -91,7 +91,7 @@ def list_insert(elem_type: ht.Type) -> ops.ExtOp:

def list_length(elem_type: ht.Type) -> ops.ExtOp:
"""Returns a list `length` operation."""
list_type = hugr.std.collections.list_type(elem_type)
list_type = List(elem_type)
return _instantiate_list_op(
"length", elem_type, [list_type], [list_type, ht.USize()]
)
Expand Down
10 changes: 3 additions & 7 deletions guppylang/std/_internal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections.abc import Callable
from typing import TYPE_CHECKING

import hugr.std.collections
import hugr.std.collections.list
import hugr.std.float
import hugr.std.int
import hugr.std.logic
Expand Down Expand Up @@ -79,11 +79,7 @@ def external_op(

def op(ty: ht.FunctionType, inst: Inst) -> ops.DataflowOp:
concrete_args = [make_concrete_arg(arg, inst, variable_remap) for arg in args]
return ops.ExtOp(
op_def,
ty,
concrete_args,
)
return op_def.instantiate(concrete_args, ty)

return op

Expand Down Expand Up @@ -171,7 +167,7 @@ def op(ty: ht.FunctionType, inst: Inst) -> ops.DataflowOp:

def list_op(
op_name: str,
ext: he.Extension = hugr.std.collections.EXTENSION,
ext: he.Extension = hugr.std.collections.list.EXTENSION,
) -> Callable[[ht.FunctionType, Inst], ops.DataflowOp]:
"""Utility method to create Hugr list ops.
Expand Down
10 changes: 6 additions & 4 deletions guppylang/tys/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from typing import TYPE_CHECKING, Literal, TypeGuard

import hugr.std
import hugr.std.collections
import hugr.std.collections.array
import hugr.std.collections.list
from hugr import tys as ht

from guppylang.ast_util import AstNode
Expand Down Expand Up @@ -122,7 +123,7 @@ def _list_to_hugr(args: Sequence[Argument]) -> ht.Type:
# Linear elements are turned into an optional to enable unsafe indexing.
# See `ListGetitemCompiler` for details.
elem_ty = ht.Option(arg.ty.to_hugr()) if arg.ty.linear else arg.ty.to_hugr()
return hugr.std.collections.list_type(elem_ty)
return hugr.std.collections.list.List(elem_ty)


def _array_to_hugr(args: Sequence[Argument]) -> ht.Type:
Expand All @@ -135,9 +136,10 @@ def _array_to_hugr(args: Sequence[Argument]) -> ht.Type:
# See `ArrayGetitemCompiler` for details.
# Same also for classical arrays, see https://github.com/CQCL/guppylang/issues/629
elem_ty = ht.Option(ty_arg.ty.to_hugr())
hugr_arg = len_arg.to_hugr()

array = hugr.std.PRELUDE.get_type("array")
return array.instantiate([len_arg.to_hugr(), ht.TypeTypeArg(elem_ty)])
# TODO remove type ignore after Array type annotation fixed to include VariableArg
return hugr.std.collections.array.Array(elem_ty, hugr_arg) # type:ignore[arg-type]


def _sized_iter_to_hugr(args: Sequence[Argument]) -> ht.Type:
Expand Down
20 changes: 11 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,24 @@ dependencies = [
"networkx >=3.2.1,<4",
"pydantic >=2.7.0b1,<3",
"typing-extensions >=4.9.0,<5",
"hugr >=0.9.0,<0.10",
"tket2-exts>=0.2.0,<0.3",
"hugr >=0.10.0,<0.11",
"tket2-exts>=0.3.0,<0.4",
]

[project.optional-dependencies]
# pytket = ["pytket >=1.30.0,<2", "tket2 >=0.4.1,<0.5"]
docs = ["sphinx >=7.2.6,<9", "sphinx-book-theme >=1.1.2,<2"]
execution = ["execute-llvm"]
pytket = ["pytket>=1.34"]

[project.urls]
homepage = "https://github.com/CQCL/guppylang"
repository = "https://github.com/CQCL/guppylang"

[dependency-groups]
docs = ["sphinx >=7.2.6,<9", "sphinx-book-theme >=1.1.2,<2"]
dev = [
{ include-group = "lint" },
{ include-group = "test" },
{ include-group = "llvm_integration" },
{ include-group = "execution" },
{ include-group = "pytket_integration" },
]
lint = ["pre-commit >=3.6.0,<4", "ruff >=0.6.2,<0.7", "mypy ==1.10.0"]
Expand All @@ -66,13 +65,14 @@ test = [
"pytest-notebook >=0.10.0,<0.11",
"pytest-snapshot >=0.9.0,<1",
"ipykernel >=6.29.5,<7",
"tket2>=0.5.0",
"tket2>=0.6.0",
]
llvm_integration = [
execution = [
{ include-group = "test" },
# Required to run the llvm integration tests
"maturin >=1.4.0,<2",
"pip >=24",
"execute-llvm",
]
pytket_integration = [{ include-group = "test" }, "pytket >=1.34.0,<2"]

Expand All @@ -83,8 +83,10 @@ members = ["execute_llvm"]
execute-llvm = { workspace = true }

# Uncomment these to test the latest dependency version during development
#hugr = { git = "https://github.com/CQCL/hugr", subdirectory = "hugr-py", rev = "4cbe890ab4e72090708ff83592c0771caf2335df" }
# tket2-exts = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-exts", branch = "ss/rename" }
# hugr = { git = "https://github.com/CQCL/hugr", subdirectory = "hugr-py", rev = "861183e" }
# tket2-exts = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-exts", rev = "eb7cc63"}
# tket2 = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-py", rev = "eb7cc63"}



[build-system]
Expand Down
Loading

0 comments on commit 63ea7a7

Please sign in to comment.