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(deps): Update hugr dependency to 0.3.0 #313

Merged
merged 7 commits into from
Apr 19, 2024
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 = "0.2"
portgraph = "0.11"
hugr = "0.3.0"
portgraph = "0.12"
pyo3 = "0.21.2"
itertools = "0.12.0"
tket-json-rs = "0.4.0"
Expand Down
2 changes: 1 addition & 1 deletion badger-optimiser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license-file = { workspace = true }
clap = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tket2 = { workspace = true, features = ["portmatching", "rewrite-tracing"] }
quantinuum-hugr = { workspace = true }
hugr = { workspace = true }
itertools = { workspace = true }
tket-json-rs = { workspace = true }
tracing = { workspace = true }
Expand Down
6 changes: 2 additions & 4 deletions compile-rewriter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
[package]
name = "compile-matcher"
name = "compile-rewriter"
edition = { workspace = true }
version = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
license-file = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { workspace = true, features = ["derive"] }
tket2 = { workspace = true, features = ["portmatching"] }
quantinuum-hugr = { workspace = true }
hugr = { workspace = true }
itertools = { workspace = true }
Binary file modified test_files/nam_6_3.rwr
Binary file not shown.
Binary file modified test_files/small_eccs.rwr
Binary file not shown.
2 changes: 1 addition & 1 deletion tket2-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tket2 = { workspace = true, features = ["pyo3", "portmatching"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tket-json-rs = { workspace = true, features = ["pyo3"] }
quantinuum-hugr = { workspace = true }
hugr = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
pyo3 = { workspace = true, features = ["extension-module"] }
num_cpus = { workspace = true }
Expand Down
Binary file modified tket2-py/tket2/data/nam_6_3.rwr
Binary file not shown.
2 changes: 1 addition & 1 deletion tket2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ petgraph = { workspace = true }
serde_yaml = { workspace = true }
portmatching = { workspace = true, optional = true, features = ["serde"] }
derive_more = { workspace = true }
quantinuum-hugr = { workspace = true }
hugr = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
pyo3 = { workspace = true, optional = true, features = ["multiple-pymethods"] }
strum_macros = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion tket2/benches/benchmarks/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn build_simple_circuit(

let qbs = h.input_wires();

let mut circ = h.as_circuit(qbs.into_iter().collect());
let mut circ = h.as_circuit(qbs);

f(&mut circ)?;

Expand Down
15 changes: 8 additions & 7 deletions tket2/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use hugr::hugr::hugrmut::HugrMut;
use hugr::hugr::NodeType;
use hugr::ops::dataflow::IOTrait;
use hugr::ops::{Input, Output, DFG};
use hugr::types::FunctionType;
use hugr::types::PolyFuncType;
use hugr::PortIndex;
use hugr::{HugrView, OutgoingPort};
use itertools::Itertools;
Expand Down Expand Up @@ -45,7 +45,7 @@ pub trait Circuit: HugrView {
///
/// Equivalent to [`HugrView::get_function_type`].
#[inline]
fn circuit_signature(&self) -> FunctionType {
fn circuit_signature(&self) -> PolyFuncType {
self.get_function_type()
.expect("Circuit has no function type")
}
Expand Down Expand Up @@ -183,7 +183,7 @@ pub(crate) fn remove_empty_wire(
return Err(CircuitMutError::DeleteNonEmptyWire(input_port.index()));
}
if link.is_some() {
circ.disconnect(inp, input_port)?;
circ.disconnect(inp, input_port);
}

// Shift ports at input
Expand Down Expand Up @@ -231,7 +231,7 @@ fn shift_ports<C: HugrMut + ?Sized>(
for port in port_range {
let links = circ.linked_ports(node, port).collect_vec();
if !links.is_empty() {
circ.disconnect(node, port)?;
circ.disconnect(node, port);
}
for (other_n, other_p) in links {
match other_p.as_directed() {
Expand All @@ -243,7 +243,7 @@ fn shift_ports<C: HugrMut + ?Sized>(
let src_port = free_port.as_outgoing().unwrap();
circ.connect(node, src_port, other_n, other_p)
}
}?;
};
}
free_port = port;
}
Expand Down Expand Up @@ -308,6 +308,7 @@ impl<T> Circuit for T where T: HugrView {}

#[cfg(test)]
mod tests {
use hugr::types::FunctionType;
use hugr::{
builder::{DFGBuilder, DataflowHugr},
extension::{prelude::BOOL_T, PRELUDE_REGISTRY},
Expand Down Expand Up @@ -338,8 +339,8 @@ mod tests {
let circ = test_circuit();

assert_eq!(circ.name(), None);
assert_eq!(circ.circuit_signature().input_count(), 3);
assert_eq!(circ.circuit_signature().output_count(), 3);
assert_eq!(circ.circuit_signature().body().input_count(), 3);
assert_eq!(circ.circuit_signature().body().output_count(), 3);
assert_eq!(circ.qubit_count(), 2);
assert_eq!(circ.num_gates(), 3);

Expand Down
4 changes: 2 additions & 2 deletions tket2/src/circuit/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ mod test {
let mut h = DFGBuilder::new(FunctionType::new(qb_row.clone(), qb_row)).unwrap();
let [q_in] = h.input_wires_arr();

let constant = h.add_constant(ConstF64::new(0.5)).unwrap();
let loaded_const = h.load_const(&constant).unwrap();
let constant = h.add_constant(ConstF64::new(0.5));
let loaded_const = h.load_const(&constant);
let rz = h
.add_dataflow_op(Tk2Op::RzF64, [q_in, loaded_const])
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tket2/src/circuit/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};

use fxhash::{FxHashMap, FxHasher64};
use hugr::hugr::views::{HierarchyView, SiblingGraph};
use hugr::ops::{LeafOp, OpName, OpType};
use hugr::ops::{OpName, OpType};
use hugr::{HugrView, Node};
use petgraph::visit::{self as pg, Walker};
use thiserror::Error;
Expand Down Expand Up @@ -78,7 +78,7 @@ impl HashState {
/// Returns a hashable representation of an operation.
fn hashable_op(op: &OpType) -> impl Hash {
match op {
OpType::LeafOp(LeafOp::CustomOp(op)) if !op.args().is_empty() => {
OpType::CustomOp(op) if !op.args().is_empty() => {
// TODO: Require hashing for TypeParams?
format!(
"{}[{}]",
Expand Down
6 changes: 4 additions & 2 deletions tket2/src/circuit/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,19 @@ where
//
// TODO: This is quite hacky, but we need it to accept Const static inputs.
// We should revisit it once this is reworked on the HUGR side.
//
// TODO: EdgeKind::Function is not currently supported.
fn init_types(circuit: &impl Circuit, node: Node, direction: Direction) -> TypeRow {
let optype = circuit.get_optype(node);
let sig = circuit.signature(node).unwrap_or_default();
let mut types = match direction {
Direction::Outgoing => sig.output,
Direction::Incoming => sig.input,
};
if let Some(EdgeKind::Static(static_type)) = optype.static_port_kind(direction) {
if let Some(EdgeKind::Const(static_type)) = optype.static_port_kind(direction) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you don't want to include EdgeKind::Function here - in that case I'd suggest a comment saying so, the "TODO: This is quite hacky, but we need it to accept Const static inputs" is a step in that suggestion but you might want to explicitly say "not Function" ;-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a TODO comment about it

types.to_mut().push(static_type);
};
if let Some(EdgeKind::Static(other)) = optype.other_port_kind(direction) {
if let Some(EdgeKind::Const(other)) = optype.other_port_kind(direction) {
types.to_mut().push(other);
}
types
Expand Down
7 changes: 4 additions & 3 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use hugr::extension::prelude::PRELUDE;
use hugr::extension::simple_op::MakeOpDef;
use hugr::extension::{CustomSignatureFunc, ExtensionId, ExtensionRegistry, SignatureError};
use hugr::hugr::IdentList;
use hugr::ops::custom::{ExternalOp, OpaqueOp};
use hugr::ops::custom::{CustomOp, OpaqueOp};
use hugr::ops::OpName;
use hugr::std_extensions::arithmetic::float_types::{EXTENSION as FLOAT_EXTENSION, FLOAT64_TYPE};
use hugr::types::type_param::{CustomTypeArg, TypeArg, TypeParam};
use hugr::types::{CustomType, FunctionType, PolyFuncType, Type, TypeBound};
Expand Down Expand Up @@ -73,7 +74,7 @@ pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::try_new([

}
/// Create a new opaque operation
pub(crate) fn wrap_json_op(op: &JsonOp) -> ExternalOp {
pub(crate) fn wrap_json_op(op: &JsonOp) -> CustomOp {
// TODO: This throws an error
//let op = serde_yaml::to_value(op).unwrap();
//let payload = TypeArg::Opaque(CustomTypeArg::new(TKET1_OP_PAYLOAD.clone(), op).unwrap());
Expand All @@ -100,7 +101,7 @@ pub(crate) fn wrap_json_op(op: &JsonOp) -> ExternalOp {

/// Extract a json-encoded TKET1 operation from an opaque operation, if
/// possible.
pub(crate) fn try_unwrap_json_op(ext: &ExternalOp) -> Option<JsonOp> {
pub(crate) fn try_unwrap_json_op(ext: &CustomOp) -> Option<JsonOp> {
// TODO: Check `extensions.contains(&TKET1_EXTENSION_ID)`
// (but the ext op extensions are an empty set?)
if ext.name() != format!("{TKET1_EXTENSION_ID}.{JSON_OP_NAME}") {
Expand Down
26 changes: 11 additions & 15 deletions tket2/src/extension/angle.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{cmp::max, num::NonZeroU64};

use hugr::extension::ExtensionSet;
use hugr::ops::constant::{downcast_equal_consts, CustomConst};
use hugr::{
extension::{prelude::ERROR_TYPE, SignatureError, SignatureFromArgs, TypeDef},
types::{
type_param::{TypeArgError, TypeParam},
ConstTypeError, CustomType, FunctionType, PolyFuncType, Type, TypeArg, TypeBound,
},
values::CustomConst,
Extension,
};
use itertools::Itertools;
Expand Down Expand Up @@ -116,11 +116,12 @@ impl CustomConst for ConstAngle {
format!("a(2π*{}/2^{})", self.value, self.log_denom).into()
}

fn custom_type(&self) -> CustomType {
super::angle_custom_type(self.log_denom)
fn get_type(&self) -> Type {
super::angle_custom_type(self.log_denom).into()
}

fn equal_consts(&self, other: &dyn CustomConst) -> bool {
hugr::values::downcast_equal_consts(self, other)
downcast_equal_consts(self, other)
}
fn extension_reqs(&self) -> ExtensionSet {
ExtensionSet::singleton(&TKET2_EXTENSION_ID)
Expand Down Expand Up @@ -205,9 +206,9 @@ pub(super) fn add_to_extension(extension: &mut Extension) {
vec![LOG_DENOM_TYPE_PARAM, LOG_DENOM_TYPE_PARAM],
FunctionType::new(
vec![generic_angle_type(0, &angle_type_def)],
vec![Type::new_sum(vec![
generic_angle_type(1, &angle_type_def),
ERROR_TYPE,
vec![Type::new_sum([
generic_angle_type(1, &angle_type_def).into(),
ERROR_TYPE.into(),
])],
),
),
Expand Down Expand Up @@ -241,6 +242,7 @@ pub(super) fn add_to_extension(extension: &mut Extension) {
#[cfg(test)]
mod test {
use super::*;
use crate::extension::angle_custom_type;
use hugr::types::TypeArg;

#[test]
Expand All @@ -264,14 +266,8 @@ mod test {
assert_ne!(const_a32_7, const_a32_8);
assert_eq!(const_a32_7, ConstAngle::new(5, 7).unwrap());

assert_eq!(
const_a32_7.custom_type(),
super::super::angle_custom_type(5)
);
assert_ne!(
const_a32_7.custom_type(),
super::super::angle_custom_type(6)
);
assert_eq!(const_a32_7.get_type(), angle_custom_type(5).into());
assert_ne!(const_a32_7.get_type(), angle_custom_type(6).into());
assert!(matches!(
ConstAngle::new(3, 256),
Err(ConstTypeError::CustomCheckFail(_))
Expand Down
5 changes: 2 additions & 3 deletions tket2/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use hugr::CircuitUnit;
use std::path::Path;
use std::{fs, io};

use hugr::ops::OpType;
use hugr::ops::{Const, OpType};
use hugr::std_extensions::arithmetic::float_types::{ConstF64, FLOAT64_TYPE};
use hugr::values::Value;
use hugr::Hugr;

use stringreader::StringReader;
Expand Down Expand Up @@ -194,7 +193,7 @@ fn parse_val(n: &str) -> Option<f64> {
}
/// Try to interpret a TKET1 parameter as a constant value.
#[inline]
fn try_param_to_constant(param: &str) -> Option<Value> {
fn try_param_to_constant(param: &str) -> Option<Const> {
if let Some(f) = parse_val(param) {
Some(ConstF64::new(f).into())
} else if param.split('/').count() == 2 {
Expand Down
8 changes: 1 addition & 7 deletions tket2/src/json/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use std::mem;
use hugr::builder::{CircuitBuilder, Container, DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::prelude::QB_T;

use hugr::ops::Const;
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::types::FunctionType;
use hugr::CircuitUnit;
use hugr::{Hugr, Wire};
Expand Down Expand Up @@ -143,11 +141,7 @@ impl JsonDecoder {
/// TODO: If the parameter is a variable, returns the corresponding wire from the input.
fn create_param_wire(&mut self, param: &str) -> Wire {
match try_param_to_constant(param) {
Some(c) => {
let const_type = FLOAT64_TYPE;
let const_op = Const::new(c, const_type).unwrap();
self.hugr.add_load_const(const_op).unwrap()
}
Some(const_op) => self.hugr.add_load_const(const_op),
None => {
// store string in custom op.
let symb_op = symbolic_constant_op(param);
Expand Down
27 changes: 8 additions & 19 deletions tket2/src/json/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::collections::HashMap;
use hugr::extension::prelude::QB_T;
use hugr::ops::{OpName, OpType};
use hugr::std_extensions::arithmetic::float_types::ConstF64;
use hugr::values::Value;
use hugr::Wire;
use itertools::{Either, Itertools};
use tket_json_rs::circuit_json::{self, Permutation, Register, SerialCircuit};
Expand Down Expand Up @@ -194,16 +193,10 @@ impl JsonEncoder {
let param = match optype {
OpType::Const(const_op) => {
// New constant, register it if it can be interpreted as a parameter.
match const_op.value() {
Value::Extension { c: (val,) } => {
if let Some(f) = val.downcast_ref::<ConstF64>() {
f.to_string()
} else {
return false;
}
}
_ => return false,
}
let Some(const_float) = const_op.get_custom_value::<ConstF64>() else {
return false;
};
const_float.to_string()
}
OpType::LoadConstant(_op_type) => {
// Re-use the parameter from the input.
Expand All @@ -212,15 +205,11 @@ impl JsonEncoder {
op if op_matches(op, Tk2Op::AngleAdd) => {
format!("{} + {}", inputs[0], inputs[1])
}
OpType::LeafOp(_) => {
if let Some(s) = match_symb_const_op(optype) {
s.to_string()
} else {
return false;
}
}
_ => {
return false;
let Some(s) = match_symb_const_op(optype) else {
return false;
};
s.to_string()
}
};

Expand Down
Loading
Loading