Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update hugr dep #268

Merged
merged 1 commit into from
Dec 14, 2023
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ missing_docs = "warn"
[workspace.dependencies]

tket2 = { path = "./tket2" }
quantinuum-hugr = { git = "https://github.com/CQCL/hugr", rev = "2efcfb3" }
portgraph = { version = "0.10" }
quantinuum-hugr = { git = "https://github.com/CQCL/hugr", rev = "4b11a24" }
portgraph = { version = "0.11" }
pyo3 = { version = "0.20" }
itertools = { version = "0.12.0" }
tket-json-rs = { version = "0.3.0" }
Expand Down
6 changes: 6 additions & 0 deletions tket2/src/extension/angle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cmp::max, num::NonZeroU64};

use hugr::extension::ExtensionSet;
use hugr::{
extension::{prelude::ERROR_TYPE, SignatureError, SignatureFromArgs, TypeDef},
types::{
Expand All @@ -14,6 +15,8 @@ use itertools::Itertools;
use smol_str::SmolStr;
use std::f64::consts::TAU;

use super::TKET2_EXTENSION_ID;

/// Identifier for the angle type.
const ANGLE_TYPE_ID: SmolStr = SmolStr::new_inline("angle");

Expand Down Expand Up @@ -125,6 +128,9 @@ impl CustomConst for ConstAngle {
fn equal_consts(&self, other: &dyn CustomConst) -> bool {
hugr::values::downcast_equal_consts(self, other)
}
fn extension_reqs(&self) -> ExtensionSet {
ExtensionSet::singleton(&TKET2_EXTENSION_ID)
}
}

/// Collect a vector into an array.
Expand Down
7 changes: 2 additions & 5 deletions tket2/src/json/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::hash::{Hash, Hasher};
use std::mem;

use hugr::builder::{CircuitBuilder, Container, DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::prelude::{PRELUDE_ID, QB_T};
use hugr::extension::ExtensionSet;
use hugr::extension::prelude::QB_T;

use hugr::ops::Const;
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
Expand Down Expand Up @@ -147,9 +146,7 @@ impl JsonDecoder {
Some(c) => {
let const_type = FLOAT64_TYPE;
let const_op = Const::new(c, const_type).unwrap();
self.hugr
.add_load_const(const_op, ExtensionSet::singleton(&PRELUDE_ID))
.unwrap()
self.hugr.add_load_const(const_op).unwrap()
}
None => {
// store string in custom op.
Expand Down
13 changes: 4 additions & 9 deletions tket2/src/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::BufReader;

use hugr::builder::{DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::prelude::QB_T;
use hugr::extension::ExtensionSet;

use hugr::std_extensions::arithmetic::float_types::{ConstF64, FLOAT64_TYPE};
use hugr::types::FunctionType;
use hugr::Hugr;
Expand Down Expand Up @@ -108,14 +108,9 @@ fn circ_add_angles_constants() -> Hugr {
let mut h = DFGBuilder::new(FunctionType::new(qb_row.clone(), qb_row)).unwrap();

let qb = h.input_wires().next().unwrap();
let f64_ext = hugr::std_extensions::arithmetic::float_types::EXTENSION_ID;

let point2 = h
.add_load_const(ConstF64::new(0.2).into(), ExtensionSet::singleton(&f64_ext))
.unwrap();
let point3 = h
.add_load_const(ConstF64::new(0.3).into(), ExtensionSet::singleton(&f64_ext))
.unwrap();

let point2 = h.add_load_const(ConstF64::new(0.2).into()).unwrap();
let point3 = h.add_load_const(ConstF64::new(0.3).into()).unwrap();
let point5 = h
.add_dataflow_op(Tk2Op::AngleAdd, [point2, point3])
.unwrap()
Expand Down
11 changes: 6 additions & 5 deletions tket2/src/passes/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use std::ops::{Index, IndexMut};
use derive_more::From;
use hugr::builder::{Container, FunctionBuilder};
use hugr::hugr::hugrmut::HugrMut;
use hugr::hugr::views::sibling_subgraph::ConvexChecker;
use hugr::hugr::views::sibling_subgraph::TopoConvexChecker;
use hugr::hugr::views::{HierarchyView, SiblingGraph, SiblingSubgraph};
use hugr::hugr::{HugrError, NodeMetadataMap};
use hugr::ops::handle::DataflowParentID;
use hugr::ops::OpType;
use hugr::types::FunctionType;
use hugr::{Hugr, HugrView, IncomingPort, Node, OutgoingPort, PortIndex, Wire};
use itertools::Itertools;
use portgraph::algorithms::ConvexChecker;

use crate::Circuit;

Expand Down Expand Up @@ -46,10 +47,10 @@ impl Chunk {
/// Extract a chunk from a circuit.
///
/// The chunk is extracted from the input wires to the output wires.
pub(self) fn extract<'h, H: HugrView>(
circ: &'h H,
pub(self) fn extract<H: HugrView>(
circ: &H,
nodes: impl IntoIterator<Item = Node>,
checker: &ConvexChecker<'h, H>,
checker: &impl ConvexChecker,
) -> Self {
let subgraph = SiblingSubgraph::try_from_nodes_with_checker(
nodes.into_iter().collect_vec(),
Expand Down Expand Up @@ -281,7 +282,7 @@ impl CircuitChunks {
.collect();

let mut chunks = Vec::new();
let convex_checker = ConvexChecker::new(circ);
let convex_checker = TopoConvexChecker::new(circ);
let mut running_cost = C::default();
let mut current_group = 0;
for (_, commands) in &circ.commands().map(|cmd| cmd.node()).group_by(|&node| {
Expand Down
27 changes: 14 additions & 13 deletions tket2/src/portmatching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use std::{

use super::{CircuitPattern, NodeID, PEdge, PNode};
use hugr::hugr::views::sibling_subgraph::{
ConvexChecker, InvalidReplacement, InvalidSubgraph, InvalidSubgraphBoundary,
InvalidReplacement, InvalidSubgraph, InvalidSubgraphBoundary, TopoConvexChecker,
};
use hugr::hugr::views::SiblingSubgraph;
use hugr::ops::OpType;
use hugr::{Hugr, IncomingPort, Node, OutgoingPort, Port, PortIndex};
use itertools::Itertools;
use portgraph::algorithms::ConvexChecker;
use portmatching::{
automaton::{LineBuilder, ScopeAutomaton},
EdgeProperty, PatternID,
Expand Down Expand Up @@ -108,7 +109,7 @@ impl PatternMatch {
circ: &impl Circuit,
matcher: &PatternMatcher,
) -> Result<Self, InvalidPatternMatch> {
let checker = ConvexChecker::new(circ);
let checker = TopoConvexChecker::new(circ);
Self::try_from_root_match_with_checker(root, pattern, circ, matcher, &checker)
}

Expand All @@ -118,12 +119,12 @@ impl PatternMatch {
/// checker object to speed up convexity checking.
///
/// See [`PatternMatch::try_from_root_match`] for more details.
pub fn try_from_root_match_with_checker<'c, C: Circuit>(
pub fn try_from_root_match_with_checker<C: Circuit>(
root: Node,
pattern: PatternID,
circ: &'c C,
circ: &C,
matcher: &PatternMatcher,
checker: &ConvexChecker<'c, C>,
checker: &impl ConvexChecker,
) -> Result<Self, InvalidPatternMatch> {
let pattern_ref = matcher
.get_pattern(pattern)
Expand Down Expand Up @@ -166,7 +167,7 @@ impl PatternMatch {
inputs: Vec<Vec<(Node, IncomingPort)>>,
outputs: Vec<(Node, OutgoingPort)>,
) -> Result<Self, InvalidPatternMatch> {
let checker = ConvexChecker::new(circ);
let checker = TopoConvexChecker::new(circ);
Self::try_from_io_with_checker(root, pattern, circ, inputs, outputs, &checker)
}

Expand All @@ -178,13 +179,13 @@ impl PatternMatch {
///
/// This checks at construction time that the match is convex. This will
/// have runtime linear in the size of the circuit.
pub fn try_from_io_with_checker<'c, C: Circuit>(
pub fn try_from_io_with_checker<C: Circuit>(
root: Node,
pattern: PatternID,
circ: &'c C,
circ: &C,
inputs: Vec<Vec<(Node, IncomingPort)>>,
outputs: Vec<(Node, OutgoingPort)>,
checker: &ConvexChecker<'c, C>,
checker: &impl ConvexChecker,
) -> Result<Self, InvalidPatternMatch> {
let subgraph = SiblingSubgraph::try_new_with_checker(inputs, outputs, circ, checker)?;
Ok(Self {
Expand Down Expand Up @@ -258,7 +259,7 @@ impl PatternMatcher {
&'a self,
circuit: &'c C,
) -> impl Iterator<Item = PatternMatch> + 'a {
let checker = ConvexChecker::new(circuit);
let checker = TopoConvexChecker::new(circuit);
circuit
.commands()
.flat_map(move |cmd| self.find_rooted_matches(circuit, cmd.node(), &checker))
Expand All @@ -270,11 +271,11 @@ impl PatternMatcher {
}

/// Find all convex pattern matches in a circuit rooted at a given node.
fn find_rooted_matches<'c, C: Circuit + Clone>(
fn find_rooted_matches<C: Circuit + Clone>(
&self,
circ: &'c C,
circ: &C,
root: Node,
checker: &ConvexChecker<'c, C>,
checker: &impl ConvexChecker,
) -> Vec<PatternMatch> {
self.automaton
.run(
Expand Down