Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
172 changes: 61 additions & 111 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ large_enum_variant = "allow"
[workspace.dependencies]

# Make sure to run `just recompile-eccs` if the hugr serialisation format changes.
hugr = "0.25.7"
hugr-core = "0.25.7"
hugr-cli = "0.25.7"
hugr = "0.26.0"
hugr-core = "0.26.0"
hugr-passes = "0.26.0"
hugr-cli = "0.26.0"
portgraph = "0.15.3"
# There can only be one `pyo3` version in the whole workspace, so we use a
# loose version constraint to prevent breaking changes in dependent crates where possible.
pyo3 = ">= 0.27.2, < 0.29"
pyo3-build-config = ">= 0.27.2, < 0.29"
pyo3 = ">= 0.28.0, < 0.29"
pyo3-build-config = ">= 0.28.0, < 0.29"

bindgen = "0.72"
cbindgen = "0.29"
cc = "1.2.56"
cc = "1.2.57"
conan2 = "0.1.9"
itertools = "0.14.0"
tket-json-rs = "0.8.1"
tket-json-rs = "0.8.2"
portmatching = "0.3.3"
bytemuck = "1.25.0"
cgmath = "0.18.0"
Expand All @@ -92,7 +93,7 @@ rmp-serde = "1.3.1"
rstest = "0.26.1"
serde = "1.0.228"
serde_json = "1.0.149"
serde_with = "3.17.0"
serde_with = "3.18.0"
smol_str = "0.3.6"
strum = ">= 0.28.0"
thiserror = "2.0.18"
Expand Down
2 changes: 1 addition & 1 deletion tket-py/src/circuit/tk2circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::num::{NonZero, NonZeroU8};
use std::sync::LazyLock;

use anyhow::Context;
use hugr::algorithms::ComposablePass;
use hugr::builder::{CircuitBuilder, DFGBuilder, Dataflow, DataflowHugr};
use hugr::envelope::{EnvelopeConfig, EnvelopeFormat, ZstdConfig};
use hugr::extension::prelude::qb_t;
Expand All @@ -17,6 +16,7 @@ use hugr::ops::handle::NodeHandle;
use hugr::ops::{ExtensionOp, OpType};
use hugr::package::Package;
use hugr::types::Type;
use hugr_passes::ComposablePass;
use itertools::Itertools;
use pyo3::exceptions::{PyAttributeError, PyValueError};
use pyo3::types::{PyAnyMethods, PyModule, PyString, PyTypeMethods};
Expand Down
2 changes: 1 addition & 1 deletion tket-py/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod tket1;

use std::{cmp::min, convert::TryInto, fs, num::NonZeroUsize, path::PathBuf};

use hugr::algorithms::ComposablePass;
use hugr_passes::ComposablePass;
use pyo3::{prelude::*, types::IntoPyDict};
use tket::optimiser::badger::BadgerOptions;
use tket::passes;
Expand Down
1 change: 1 addition & 0 deletions tket-qsystem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ required-features = ["cli"]
[dependencies]
hugr.workspace = true
hugr-core.workspace = true
hugr-passes.workspace = true
tket = { path = "../tket", version = "0.17.0" }
tket-json-rs = { workspace = true }
lazy_static.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use hugr::std_extensions::collections::array::ArrayKind;
use hugr::types::{Signature, Type};
use hugr::{
Hugr, HugrView, IncomingPort, Node, OutgoingPort, Wire,
algorithms::replace_types::ReplaceTypes,
builder::{DFGBuilder, Dataflow},
extension::prelude::Barrier,
hugr::{
hugrmut::HugrMut,
patch::{PatchHugrMut, insert_cut::InsertCut},
},
};
use hugr_passes::replace_types::ReplaceTypes;
use tket::passes::unpack_container::type_unpack::{TypeUnpacker, is_array_of};

use crate::extension::qsystem::{
Expand Down
15 changes: 12 additions & 3 deletions tket-qsystem/src/extension/qsystem/lower.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use derive_more::{Display, Error, From};
use hugr::algorithms::replace_types::{NodeTemplate, ReplaceTypesError};
use hugr::algorithms::{ComposablePass, ReplaceTypes};
use hugr::builder::{Container, HugrBuilder};
use hugr::core::Visibility;
use hugr::extension::prelude::Barrier;
Expand All @@ -18,6 +16,9 @@ use hugr::{
std_extensions::arithmetic::{float_ops::FloatOps, float_types::ConstF64},
types::Signature,
};
use hugr_passes::composable::WithScope;
use hugr_passes::replace_types::{NodeTemplate, ReplaceTypesError};
use hugr_passes::{ComposablePass, ReplaceTypes};
use lazy_static::lazy_static;
use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
Expand Down Expand Up @@ -60,7 +61,7 @@ pub enum LowerTk2Error {
OpReplacement(HugrError),
/// An error raised when lowering operations.
#[display("Error when lowering ops: {_0}")]
CircuitReplacement(hugr::algorithms::lower::LowerError),
CircuitReplacement(hugr_passes::lower::LowerError),
/// TketOps were not lowered after the pass.
#[display("TketOps were not lowered: {missing_ops:?}")]
#[from(ignore)]
Expand Down Expand Up @@ -275,6 +276,14 @@ pub fn check_lowered<H: HugrView>(hugr: &H) -> Result<(), Vec<H::Node>> {
#[derive(Default, Debug, Clone)]
pub struct LowerTketToQSystemPass;

impl WithScope for LowerTketToQSystemPass {
fn with_scope(self, _scope: impl Into<hugr_passes::PassScope>) -> Self {
// TODO: Follow scope configuration
// <https://github.com/Quantinuum/tket2/pull/1429>
self
}
}

impl<H: HugrMut<Node = Node>> ComposablePass<H> for LowerTketToQSystemPass {
type Error = LowerTk2Error;
type Result = ();
Expand Down
81 changes: 10 additions & 71 deletions tket-qsystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ pub mod pytket;
pub mod replace_bools;

use derive_more::{Display, Error, From};
use hugr::algorithms::const_fold::{ConstFoldError, ConstantFoldPass};
use hugr::algorithms::{
ComposablePass as _, MonomorphizePass, RemoveDeadFuncsError, RemoveDeadFuncsPass, force_order,
replace_types::ReplaceTypesError,
};
use hugr::hugr::{HugrError, hugrmut::HugrMut};
use hugr::{Hugr, HugrView, Node, core::Visibility, ops::OpType};
use hugr_core::hugr::internal::HugrMutInternals;
use hugr_passes::PassScope;
use hugr_passes::composable::{Preserve, WithScope};
use hugr_passes::const_fold::{ConstFoldError, ConstantFoldPass};
use hugr_passes::{
ComposablePass as _, MonomorphizePass, RemoveDeadFuncsError, RemoveDeadFuncsPass, force_order,
replace_types::ReplaceTypesError,
};
use std::collections::HashSet;

use lower_drops::LowerDropsPass;
Expand All @@ -30,12 +32,6 @@ use extension::{
qsystem::{LowerTk2Error, LowerTketToQSystemPass, QSystemOp},
};

#[cfg(feature = "llvm")]
#[expect(deprecated)]
// TODO: We still want to run this as long as deserialized hugrs are allowed to contain Value::Function
// Once that variant is removed, we can remove this pass step.
use hugr::llvm::utils::inline_constant_functions;

/// Modify a [hugr::Hugr] into a form that is acceptable for ingress into a
/// Q-System. Returns an error if this cannot be done.
///
Expand Down Expand Up @@ -75,13 +71,10 @@ pub enum QSystemPassError<N = Node> {
ConstantFoldError(ConstFoldError),
/// An error from the component [LowerDropsPass] pass.
LinearizeArrayError(ReplaceTypesError),
#[cfg(feature = "llvm")]
/// An error from the component [inline_constant_functions()] pass.
InlineConstantFunctionsError(anyhow::Error),
/// An error when running [RemoveDeadFuncsPass] after the monomorphisation
/// pass.
///
/// [RemoveDeadFuncsPass]: hugr::algorithms::RemoveDeadFuncsError
/// [RemoveDeadFuncsPass]: hugr_passes::RemoveDeadFuncsError
DCEError(RemoveDeadFuncsError),
/// No [FuncDefn] named "main" in [Module].
///
Expand Down Expand Up @@ -117,7 +110,7 @@ impl QSystemPass {

#[expect(deprecated)]
// Will move to pass scopes in <https://github.com/Quantinuum/tket2/pull/1429>
let rdfp = RemoveDeadFuncsPass::default().with_module_entry_points([entrypoint]);
let rdfp = RemoveDeadFuncsPass::default_with_scope(PassScope::Global(Preserve::All));
rdfp.run(hugr)?
}

Expand Down Expand Up @@ -152,13 +145,6 @@ impl QSystemPass {
}
}

#[cfg(feature = "llvm")]
{
// TODO: We still want to run this as long as deserialized hugrs are allowed to contain Value::Function
// Once that variant is removed, we can remove this pass step.
#[expect(deprecated)]
inline_constant_functions(hugr)?;
}
if self.constant_fold {
self.constant_fold().run(hugr)?;
}
Expand Down Expand Up @@ -223,7 +209,7 @@ impl QSystemPass {
}

fn monomorphization(&self) -> MonomorphizePass {
MonomorphizePass
MonomorphizePass::default()
}

fn lower_drops(&self) -> LowerDropsPass {
Expand Down Expand Up @@ -429,51 +415,4 @@ mod test {
);
assert_eq!(hugr.num_nodes(), hugr_public.num_nodes());
}

#[cfg(feature = "llvm")]
#[test]
// TODO: We still want to test this as long as deserialized hugrs are allowed to contain Value::Function
// Once that variant is removed, we can remove this test.
#[expect(deprecated)]
fn const_function() {
use hugr::builder::{Container, DFGBuilder, DataflowHugr, ModuleBuilder};
use hugr::ops::{CallIndirect, Value};

let qb_sig: Signature = Signature::new_endo(qb_t());
let mut hugr = {
let mut builder = ModuleBuilder::new();
let val = Value::function({
let builder = DFGBuilder::new(Signature::new_endo(qb_t())).unwrap();
let [r] = builder.input_wires_arr();
builder.finish_hugr_with_outputs([r]).unwrap()
})
.unwrap();
let const_node = builder.add_constant(val);
{
let mut builder = builder.define_function("main", qb_sig.clone()).unwrap();
let [i] = builder.input_wires_arr();
let fun = builder.load_const(&const_node);
let [r] = builder
.add_dataflow_op(
CallIndirect {
signature: qb_sig.clone(),
},
[fun, i],
)
.unwrap()
.outputs_arr();
builder.finish_with_outputs([r]).unwrap();
};
builder.finish_hugr().unwrap()
};

QSystemPass::default().run(&mut hugr).unwrap();

// QSystemPass should have removed the const function
for n in hugr.descendants(hugr.module_root()) {
if hugr.get_optype(n).as_const().is_some() {
panic!("Const function is still there!");
}
}
}
}
2 changes: 1 addition & 1 deletion tket-qsystem/src/llvm/qsystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ mod test {
#[with(_i)] mut llvm_ctx: TestContext,
#[case] op: QSystemOp,
) {
use hugr::algorithms::ComposablePass;
use hugr_passes::ComposablePass;

use crate::llvm::{futures::FuturesCodegenExtension, prelude::QISPreludeCodegen};

Expand Down
13 changes: 11 additions & 2 deletions tket-qsystem/src/lower_drops.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Contains a pass to lower "drop" ops from the Guppy extension
use hugr::algorithms::replace_types::{Linearizer, NodeTemplate, ReplaceTypesError};
use hugr::algorithms::{ComposablePass, ReplaceTypes};
use hugr::extension::prelude::bool_t;
use hugr::extension::simple_op::MakeRegisteredOp;
use hugr::types::Term;
use hugr::{Node, hugr::hugrmut::HugrMut};
use hugr_passes::composable::WithScope;
use hugr_passes::replace_types::{Linearizer, NodeTemplate, ReplaceTypesError};
use hugr_passes::{ComposablePass, ReplaceTypes};
use tket::extension::guppy::{DROP_OP_NAME, GUPPY_EXTENSION};

use crate::extension::futures::{FutureOp, FutureOpDef, future_type};
Expand All @@ -13,6 +14,14 @@ use crate::extension::futures::{FutureOp, FutureOpDef, future_type};
#[derive(Default, Debug, Clone)]
pub struct LowerDropsPass;

impl WithScope for LowerDropsPass {
fn with_scope(self, _scope: impl Into<hugr_passes::PassScope>) -> Self {
// TODO: Follow scope configuration
// <https://github.com/Quantinuum/tket2/pull/1429>
self
}
}

impl<H: HugrMut<Node = Node>> ComposablePass<H> for LowerDropsPass {
type Error = ReplaceTypesError;

Expand Down
20 changes: 15 additions & 5 deletions tket-qsystem/src/replace_bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
mod static_array;

use derive_more::{Display, Error, From};
use hugr::algorithms::replace_types::{Linearizer, NodeTemplate, ReplaceTypesError};
use hugr::algorithms::{
ComposablePass, ReplaceTypes, ensure_no_nonlocal_edges, non_local::FindNonLocalEdgesError,
};
use hugr::builder::{
BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, SubContainer,
inout_sig,
Expand All @@ -24,6 +20,12 @@ use hugr::std_extensions::collections::{
use hugr::std_extensions::logic::LogicOp;
use hugr::types::{SumType, Term, Type};
use hugr::{Hugr, Node, Wire, hugr::hugrmut::HugrMut, type_row};
use hugr_passes::composable::WithScope;
use hugr_passes::non_local::LocalizeEdges;
use hugr_passes::replace_types::{Linearizer, NodeTemplate, ReplaceTypesError};
use hugr_passes::{
ComposablePass, ReplaceTypes, ensure_no_nonlocal_edges, non_local::FindNonLocalEdgesError,
};
use static_array::{ReplaceStaticArrayBoolPass, ReplaceStaticArrayBoolPassError};
use tket::TketOp;
use tket::extension::{
Expand Down Expand Up @@ -68,12 +70,20 @@ pub enum ReplaceBoolPassError<N> {
#[derive(Default, Debug, Clone)]
pub struct ReplaceBoolPass;

impl WithScope for ReplaceBoolPass {
fn with_scope(self, _scope: impl Into<hugr_passes::PassScope>) -> Self {
// TODO: Follow scope configuration
// <https://github.com/Quantinuum/tket2/pull/1429>
self
}
}

impl<H: HugrMut<Node = Node>> ComposablePass<H> for ReplaceBoolPass {
type Error = ReplaceBoolPassError<H::Node>;
type Result = ();

fn run(&self, hugr: &mut H) -> Result<(), Self::Error> {
ensure_no_nonlocal_edges(hugr)?;
LocalizeEdges::default().check_no_nonlocal_edges(hugr)?;
ReplaceStaticArrayBoolPass::default().run(hugr)?;
let lowerer = lowerer();
lowerer.run(hugr)?;
Expand Down
Loading
Loading