From 4f3cda8a70a6d4e6da246f16d4d5ede6cdd9b1c1 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 16 Jul 2024 14:23:27 +0100 Subject: [PATCH 01/14] test: Add failing test case for #1315 --- hugr-core/src/ops/custom.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 8f4e3f19c..42dbf99d1 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -429,7 +429,13 @@ pub enum CustomOpError { #[cfg(test)] mod test { - use crate::extension::prelude::{QB_T, USIZE_T}; + use crate::{ + extension::prelude::{BOOL_T, QB_T, USIZE_T}, + std_extensions::arithmetic::{ + int_ops::{self, INT_OPS_REGISTRY}, + int_types::INT_TYPES, + }, + }; use super::*; @@ -451,4 +457,23 @@ mod test { assert!(op.is_opaque()); assert!(!op.is_extension_op()); } + + #[test] + #[should_panic] + fn resolve_opaque_op() { + let registry = &INT_OPS_REGISTRY; + let i0 = &INT_TYPES[0]; + let opaque = OpaqueOp::new( + int_ops::EXTENSION_ID, + "itobool", + "description".into(), + vec![], + FunctionType::new(i0.clone(), BOOL_T), + ); + let resolved = + super::resolve_opaque_op(Node::from(portgraph::NodeIndex::new(1)), &opaque, registry) + .unwrap() + .unwrap(); + assert_eq!(resolved.def().name(), "itobool"); + } } From 794812911002305aee96b797b00bf4a91d2b69b0 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 16 Jul 2024 14:35:24 +0100 Subject: [PATCH 02/14] add comment --- hugr-core/src/ops/custom.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 42dbf99d1..0107296d3 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -460,6 +460,7 @@ mod test { #[test] #[should_panic] + /// This is a test for the yet-to-be-resolves Issue #1315 fn resolve_opaque_op() { let registry = &INT_OPS_REGISTRY; let i0 = &INT_TYPES[0]; From c520ece8ebe71ae896f64ef4952481bfd30fd190 Mon Sep 17 00:00:00 2001 From: Douglas Wilson <141026920+doug-q@users.noreply.github.com> Date: Tue, 16 Jul 2024 14:41:44 +0100 Subject: [PATCH 03/14] Update hugr-core/src/ops/custom.rs Co-authored-by: Alec Edgington <54802828+cqc-alec@users.noreply.github.com> --- hugr-core/src/ops/custom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 0107296d3..2cb15bdf1 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -459,7 +459,7 @@ mod test { } #[test] - #[should_panic] + #[should_panic] // https://github.com/CQCL/hugr/issues/1315 /// This is a test for the yet-to-be-resolves Issue #1315 fn resolve_opaque_op() { let registry = &INT_OPS_REGISTRY; From d03d99084a2d0eaa9b60bd8c09ebb8cd98f1178b Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 16 Jul 2024 14:42:09 +0100 Subject: [PATCH 04/14] revert dup comment --- hugr-core/src/ops/custom.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 2cb15bdf1..b85afdac6 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -460,7 +460,6 @@ mod test { #[test] #[should_panic] // https://github.com/CQCL/hugr/issues/1315 - /// This is a test for the yet-to-be-resolves Issue #1315 fn resolve_opaque_op() { let registry = &INT_OPS_REGISTRY; let i0 = &INT_TYPES[0]; From 143e507a9cc870890a8c7c3c86bb8281dd48969c Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 16 Jul 2024 14:40:34 +0100 Subject: [PATCH 05/14] fix: add op's extension to signature check in resolve_opaque_op --- hugr-core/src/ops/custom.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index b85afdac6..f45fb7132 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -393,8 +393,11 @@ pub fn resolve_opaque_op( )); }; let ext_op = - ExtensionOp::new(def.clone(), opaque.args.clone(), extension_registry).unwrap(); - if opaque.signature != ext_op.signature { + ExtensionOp::new(def.clone(), opaque.args.clone(), extension_registry)?; + // ops always require their own extensions, so we do not force OpaqueOps + // to add their extension to their signature + let opaque_sig = opaque.signature.clone().with_extension_delta(opaque.extension().clone()); + if opaque_sig != ext_op.signature { return Err(CustomOpError::SignatureMismatch { extension: opaque.extension.clone(), op: def.name().clone(), @@ -425,6 +428,10 @@ pub enum CustomOpError { stored: FunctionType, computed: FunctionType, }, + /// An error in computing the signature of the ExtensionOp + #[error(transparent)] + SignatureError(#[from] SignatureError), + } #[cfg(test)] From dd3bc4b7f36ed6183a2baa1cee1b30ef614e31a7 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 16 Jul 2024 14:45:55 +0100 Subject: [PATCH 06/14] fmt --- hugr-core/src/ops/custom.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index f45fb7132..b5772bede 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -392,11 +392,13 @@ pub fn resolve_opaque_op( r.name().clone(), )); }; - let ext_op = - ExtensionOp::new(def.clone(), opaque.args.clone(), extension_registry)?; + let ext_op = ExtensionOp::new(def.clone(), opaque.args.clone(), extension_registry)?; // ops always require their own extensions, so we do not force OpaqueOps // to add their extension to their signature - let opaque_sig = opaque.signature.clone().with_extension_delta(opaque.extension().clone()); + let opaque_sig = opaque + .signature + .clone() + .with_extension_delta(opaque.extension().clone()); if opaque_sig != ext_op.signature { return Err(CustomOpError::SignatureMismatch { extension: opaque.extension.clone(), @@ -431,7 +433,6 @@ pub enum CustomOpError { /// An error in computing the signature of the ExtensionOp #[error(transparent)] SignatureError(#[from] SignatureError), - } #[cfg(test)] From 865ecfbdb11b50953b6abe68b0004d09e12141f8 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 16 Jul 2024 14:46:40 +0100 Subject: [PATCH 07/14] un-should_panic test --- hugr-core/src/ops/custom.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index b5772bede..f0d57e7f3 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -467,7 +467,6 @@ mod test { } #[test] - #[should_panic] // https://github.com/CQCL/hugr/issues/1315 fn resolve_opaque_op() { let registry = &INT_OPS_REGISTRY; let i0 = &INT_TYPES[0]; From 3742dddd3fb4409aa9e187e9a7882d2396a5904c Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 17 Jul 2024 07:26:21 +0100 Subject: [PATCH 08/14] rework --- hugr-core/src/ops/custom.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index f0d57e7f3..cffe87750 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -134,7 +134,7 @@ impl DataflowOpTrait for CustomOp { /// The signature of the operation. fn signature(&self) -> FunctionType { match self { - Self::Opaque(op) => op.signature.clone(), + Self::Opaque(op) => op.signature(), Self::Extension(ext_op) => ext_op.signature(), } } @@ -276,7 +276,15 @@ impl DataflowOpTrait for ExtensionOp { } } -/// An opaquely-serialized op that refers to an as-yet-unresolved [`OpDef`] +/// An opaquely-serialized op that refers to an as-yet-unresolved [`OpDef`]. +/// +/// All [CustomOp]s are serialised as `OpaqueOp`s. +/// +/// The signature of a [CustomOp] always includes that op's extension. We do not +/// require that the `signature` field of [OpaqueOp] contains `extension`, +/// instead we are careful to add it whenever we look at the `signature` of an +/// `OpaqueOp`. This is a small efficiency in serialisation and allows us to +/// be more liberal in deserialisation. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[cfg_attr(test, derive(Arbitrary))] pub struct OpaqueOp { @@ -286,6 +294,9 @@ pub struct OpaqueOp { #[cfg_attr(test, proptest(strategy = "any_nonempty_string()"))] description: String, // cache in advance so description() can return &str args: Vec, + // note that `signature` may not include `extension`. Thus this field must + // remain private, and should be accessed through + // `DataflowOpTrait::signature`. signature: FunctionType, } @@ -343,7 +354,9 @@ impl DataflowOpTrait for OpaqueOp { } fn signature(&self) -> FunctionType { - self.signature.clone() + self.signature + .clone() + .with_extension_delta(self.extension().clone()) } } @@ -395,10 +408,7 @@ pub fn resolve_opaque_op( let ext_op = ExtensionOp::new(def.clone(), opaque.args.clone(), extension_registry)?; // ops always require their own extensions, so we do not force OpaqueOps // to add their extension to their signature - let opaque_sig = opaque - .signature - .clone() - .with_extension_delta(opaque.extension().clone()); + let opaque_sig = opaque.signature(); if opaque_sig != ext_op.signature { return Err(CustomOpError::SignatureMismatch { extension: opaque.extension.clone(), @@ -437,6 +447,8 @@ pub enum CustomOpError { #[cfg(test)] mod test { + + use crate::{ extension::prelude::{BOOL_T, QB_T, USIZE_T}, std_extensions::arithmetic::{ @@ -461,7 +473,10 @@ mod test { assert_eq!(op.name(), "res.op"); assert_eq!(DataflowOpTrait::description(&op), "desc"); assert_eq!(op.args(), &[TypeArg::Type { ty: USIZE_T }]); - assert_eq!(op.signature(), sig); + assert_eq!( + op.signature(), + sig.with_extension_delta(op.extension().clone()) + ); assert!(op.is_opaque()); assert!(!op.is_extension_op()); } From db6534b1b130f287a9b0d12e091c1b57ca5ae80c Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 17 Jul 2024 07:27:02 +0100 Subject: [PATCH 09/14] Revert "fix(hugr-py): ops require their own extensions (#1303)" This reverts commit 026bfcb0008ce55b102fffd21fb31f24aefe4a69. --- hugr-py/src/hugr/std/float.py | 3 +-- hugr-py/src/hugr/std/int.py | 4 +--- hugr-py/src/hugr/std/logic.py | 3 +-- hugr-py/src/hugr/tys.py | 8 ++------ hugr-py/tests/conftest.py | 20 +++++--------------- 5 files changed, 10 insertions(+), 28 deletions(-) diff --git a/hugr-py/src/hugr/std/float.py b/hugr-py/src/hugr/std/float.py index bf0cfff18..92cb5d79a 100644 --- a/hugr-py/src/hugr/std/float.py +++ b/hugr-py/src/hugr/std/float.py @@ -7,9 +7,8 @@ from hugr import tys, val #: HUGR 64-bit IEEE 754-2019 floating point type. -FLOAT_EXT_ID = "arithmetic.float.types" FLOAT_T = tys.Opaque( - extension=FLOAT_EXT_ID, + extension="arithmetic.float.types", id="float64", args=[], bound=tys.TypeBound.Copyable, diff --git a/hugr-py/src/hugr/std/int.py b/hugr-py/src/hugr/std/int.py index fa4483663..391ebc7eb 100644 --- a/hugr-py/src/hugr/std/int.py +++ b/hugr-py/src/hugr/std/int.py @@ -65,9 +65,7 @@ def to_custom(self) -> Custom: return Custom( "idivmod_u", tys.FunctionType( - input=[int_t(self.arg1)] * 2, - output=[int_t(self.arg2)] * 2, - extension_reqs=[OPS_EXTENSION], + input=[int_t(self.arg1)] * 2, output=[int_t(self.arg2)] * 2 ), extension=OPS_EXTENSION, args=[tys.BoundedNatArg(n=self.arg1), tys.BoundedNatArg(n=self.arg2)], diff --git a/hugr-py/src/hugr/std/logic.py b/hugr-py/src/hugr/std/logic.py index 873648a31..1291a61c5 100644 --- a/hugr-py/src/hugr/std/logic.py +++ b/hugr-py/src/hugr/std/logic.py @@ -20,8 +20,7 @@ class _NotDef(AsCustomOp): """Not operation.""" def to_custom(self) -> Custom: - sig = tys.FunctionType.endo([tys.Bool], [EXTENSION_ID]) - return Custom("Not", sig, extension=EXTENSION_ID) + return Custom("Not", tys.FunctionType.endo([tys.Bool]), extension=EXTENSION_ID) def __call__(self, a: ComWire) -> Command: return DataflowOp.__call__(self, a) diff --git a/hugr-py/src/hugr/tys.py b/hugr-py/src/hugr/tys.py index 965d0db9a..da346d3d2 100644 --- a/hugr-py/src/hugr/tys.py +++ b/hugr-py/src/hugr/tys.py @@ -330,18 +330,14 @@ def empty(cls) -> FunctionType: return cls(input=[], output=[]) @classmethod - def endo( - cls, tys: TypeRow, extension_reqs: ExtensionSet | None = None - ) -> FunctionType: + def endo(cls, tys: TypeRow) -> FunctionType: """Function type with the same input and output types. Example: >>> FunctionType.endo([Qubit]) FunctionType([Qubit], [Qubit]) """ - return cls( - input=tys, output=tys, extension_reqs=extension_reqs or ExtensionSet() - ) + return cls(input=tys, output=tys) def flip(self) -> FunctionType: """Return a new function type with input and output types swapped. diff --git a/hugr-py/tests/conftest.py b/hugr-py/tests/conftest.py index abdeb2354..a3d70a399 100644 --- a/hugr-py/tests/conftest.py +++ b/hugr-py/tests/conftest.py @@ -14,7 +14,7 @@ from hugr.hugr import Hugr from hugr.ops import AsCustomOp, Command, Custom, DataflowOp from hugr.serialization.serial_hugr import SerialHugr -from hugr.std.float import FLOAT_EXT_ID, FLOAT_T +from hugr.std.float import FLOAT_T if TYPE_CHECKING: from hugr.ops import ComWire @@ -48,7 +48,7 @@ def __call__(self, q: ComWire) -> Command: def to_custom(self) -> Custom: return Custom( self._enum.value, - tys.FunctionType.endo([tys.Qubit], extension_reqs=[QUANTUM_EXTENSION_ID]), + tys.FunctionType.endo([tys.Qubit]), extension=QUANTUM_EXTENSION_ID, ) @@ -70,9 +70,7 @@ class _Enum(Enum): def to_custom(self) -> Custom: return Custom( self._enum.value, - tys.FunctionType.endo( - [tys.Qubit] * 2, extension_reqs=[QUANTUM_EXTENSION_ID] - ), + tys.FunctionType.endo([tys.Qubit] * 2), extension=QUANTUM_EXTENSION_ID, ) @@ -92,11 +90,7 @@ class MeasureDef(AsCustomOp): def to_custom(self) -> Custom: return Custom( "Measure", - tys.FunctionType( - [tys.Qubit], - [tys.Qubit, tys.Bool], - extension_reqs=[QUANTUM_EXTENSION_ID], - ), + tys.FunctionType([tys.Qubit], [tys.Qubit, tys.Bool]), extension=QUANTUM_EXTENSION_ID, ) @@ -112,11 +106,7 @@ class RzDef(AsCustomOp): def to_custom(self) -> Custom: return Custom( "Rz", - tys.FunctionType( - [tys.Qubit, FLOAT_T], - [tys.Qubit], - extension_reqs=[QUANTUM_EXTENSION_ID, FLOAT_EXT_ID], - ), + tys.FunctionType([tys.Qubit, FLOAT_T], [tys.Qubit]), extension=QUANTUM_EXTENSION_ID, ) From e119e261e66b6ff2471b8b7cc7e886edcf678d8e Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 17 Jul 2024 07:56:03 +0100 Subject: [PATCH 10/14] fix with_nonlinear_and_outputs and test_invalid --- hugr-core/src/builder/circuit.rs | 9 +++- hugr-core/src/hugr/rewrite/replace.rs | 68 +++++++++++++++++---------- hugr-core/src/ops/custom.rs | 1 - 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/hugr-core/src/builder/circuit.rs b/hugr-core/src/builder/circuit.rs index 272ddead5..ea219fcfb 100644 --- a/hugr-core/src/builder/circuit.rs +++ b/hugr-core/src/builder/circuit.rs @@ -242,6 +242,7 @@ mod test { use super::*; use cool_asserts::assert_matches; + use crate::extension::{ExtensionId, ExtensionSet}; use crate::std_extensions::arithmetic::float_types::{self, ConstF64}; use crate::utils::test_quantum_extension::{ self, cx_gate, h_gate, measure, q_alloc, q_discard, rz_f64, @@ -295,8 +296,9 @@ mod test { #[test] fn with_nonlinear_and_outputs() { + let missing_ext: ExtensionId = "MissingExt".try_into().unwrap(); let my_custom_op = CustomOp::new_opaque(OpaqueOp::new( - "MissingRsrc".try_into().unwrap(), + missing_ext.clone(), "MyOp", "unknown op".to_string(), vec![], @@ -304,7 +306,10 @@ mod test { )); let build_res = build_main( FunctionType::new(type_row![QB, QB, NAT], type_row![QB, QB, BOOL_T]) - .with_extension_delta(test_quantum_extension::EXTENSION_ID) + .with_extension_delta(ExtensionSet::from_iter([ + test_quantum_extension::EXTENSION_ID, + missing_ext, + ])) .into(), |mut f_build| { let [q0, q1, angle]: [Wire; 3] = f_build.input_wires_arr(); diff --git a/hugr-core/src/hugr/rewrite/replace.rs b/hugr-core/src/hugr/rewrite/replace.rs index 72bd9e1b9..1271f52ae 100644 --- a/hugr-core/src/hugr/rewrite/replace.rs +++ b/hugr-core/src/hugr/rewrite/replace.rs @@ -643,38 +643,56 @@ mod test { } #[test] - fn test_invalid() -> Result<(), Box> { + fn test_invalid() { + let unknown_ext: ExtensionId = "unknown_ext".try_into().unwrap(); let utou = FunctionType::new_endo(vec![USIZE_T]); let mk_op = |s| { CustomOp::new_opaque(OpaqueOp::new( - ExtensionId::new("unknown_ext").unwrap(), + unknown_ext.clone(), s, String::new(), vec![], utou.clone(), )) }; - let mut h = DFGBuilder::new(FunctionType::new( - type_row![USIZE_T, BOOL_T], - type_row![USIZE_T], - ))?; + let mut h = DFGBuilder::new( + FunctionType::new(type_row![USIZE_T, BOOL_T], type_row![USIZE_T]) + .with_extension_delta(unknown_ext.clone()), + ) + .unwrap(); let [i, b] = h.input_wires_arr(); - let mut cond = h.conditional_builder( - (vec![type_row![]; 2], b), - [(USIZE_T, i)], - type_row![USIZE_T], - )?; - let mut case1 = cond.case_builder(0)?; - let foo = case1.add_dataflow_op(mk_op("foo"), case1.input_wires())?; - let case1 = case1.finish_with_outputs(foo.outputs())?.node(); - let mut case2 = cond.case_builder(1)?; - let bar = case2.add_dataflow_op(mk_op("bar"), case2.input_wires())?; - let mut baz_dfg = case2.dfg_builder(utou.clone(), bar.outputs())?; - let baz = baz_dfg.add_dataflow_op(mk_op("baz"), baz_dfg.input_wires())?; - let baz_dfg = baz_dfg.finish_with_outputs(baz.outputs())?; - let case2 = case2.finish_with_outputs(baz_dfg.outputs())?.node(); - let cond = cond.finish_sub_container()?; - let h = h.finish_hugr_with_outputs(cond.outputs(), &PRELUDE_REGISTRY)?; + let mut cond = h + .conditional_builder_exts( + (vec![type_row![]; 2], b), + [(USIZE_T, i)], + type_row![USIZE_T], + unknown_ext.clone(), + ) + .unwrap(); + let mut case1 = cond.case_builder(0).unwrap(); + let foo = case1 + .add_dataflow_op(mk_op("foo"), case1.input_wires()) + .unwrap(); + let case1 = case1.finish_with_outputs(foo.outputs()).unwrap().node(); + let mut case2 = cond.case_builder(1).unwrap(); + let bar = case2 + .add_dataflow_op(mk_op("bar"), case2.input_wires()) + .unwrap(); + let mut baz_dfg = case2 + .dfg_builder( + utou.clone().with_extension_delta(unknown_ext.clone()), + bar.outputs(), + ) + .unwrap(); + let baz = baz_dfg + .add_dataflow_op(mk_op("baz"), baz_dfg.input_wires()) + .unwrap(); + let baz_dfg = baz_dfg.finish_with_outputs(baz.outputs()).unwrap(); + let case2 = case2.finish_with_outputs(baz_dfg.outputs()).unwrap().node(); + let cond = cond.finish_sub_container().unwrap(); + let h = h + .finish_hugr_with_outputs(cond.outputs(), &PRELUDE_REGISTRY) + .unwrap(); let mut r_hugr = Hugr::new(h.get_optype(cond.node()).clone()); let r1 = r_hugr.add_node_with_parent( @@ -701,7 +719,7 @@ mod test { rep.verify(&h).unwrap(); { let mut target = h.clone(); - let node_map = rep.clone().apply(&mut target)?; + let node_map = rep.clone().apply(&mut target).unwrap(); let new_case2 = *node_map.get(&r2).unwrap(); assert_eq!(target.get_parent(baz.node()), Some(new_case2)); } @@ -716,7 +734,8 @@ mod test { // Root node type needs to be that of common parent of the removed nodes: let mut rep2 = rep.clone(); rep2.replacement - .replace_op(rep2.replacement.root(), h.root_type().clone())?; + .replace_op(rep2.replacement.root(), h.root_type().clone()) + .unwrap(); assert_eq!( check_same_errors(rep2), ReplaceError::WrongRootNodeTag { @@ -815,6 +834,5 @@ mod test { }), ReplaceError::BadEdgeKind(Direction::Outgoing, new_out_edge) ); - Ok(()) } } diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index cffe87750..e7ef786c7 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -447,7 +447,6 @@ pub enum CustomOpError { #[cfg(test)] mod test { - use crate::{ extension::prelude::{BOOL_T, QB_T, USIZE_T}, From b5008f0650fa8276e1d041cc1e1baadda29bb5ef Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 17 Jul 2024 08:03:19 +0100 Subject: [PATCH 11/14] tidy --- hugr-core/src/ops/custom.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index e7ef786c7..dc0a127d7 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -406,10 +406,7 @@ pub fn resolve_opaque_op( )); }; let ext_op = ExtensionOp::new(def.clone(), opaque.args.clone(), extension_registry)?; - // ops always require their own extensions, so we do not force OpaqueOps - // to add their extension to their signature - let opaque_sig = opaque.signature(); - if opaque_sig != ext_op.signature { + if opaque.signature() != ext_op.signature() { return Err(CustomOpError::SignatureMismatch { extension: opaque.extension.clone(), op: def.name().clone(), From f31989692998ea0d6406e5827529242810e3175c Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 17 Jul 2024 08:08:47 +0100 Subject: [PATCH 12/14] restore extension_reqs arg for endo --- hugr-py/src/hugr/tys.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hugr-py/src/hugr/tys.py b/hugr-py/src/hugr/tys.py index da346d3d2..965d0db9a 100644 --- a/hugr-py/src/hugr/tys.py +++ b/hugr-py/src/hugr/tys.py @@ -330,14 +330,18 @@ def empty(cls) -> FunctionType: return cls(input=[], output=[]) @classmethod - def endo(cls, tys: TypeRow) -> FunctionType: + def endo( + cls, tys: TypeRow, extension_reqs: ExtensionSet | None = None + ) -> FunctionType: """Function type with the same input and output types. Example: >>> FunctionType.endo([Qubit]) FunctionType([Qubit], [Qubit]) """ - return cls(input=tys, output=tys) + return cls( + input=tys, output=tys, extension_reqs=extension_reqs or ExtensionSet() + ) def flip(self) -> FunctionType: """Return a new function type with input and output types swapped. From 0aa0a307712a011a29e2e396f709dd5d3c465e53 Mon Sep 17 00:00:00 2001 From: Douglas Wilson <141026920+doug-q@users.noreply.github.com> Date: Wed, 17 Jul 2024 11:26:47 +0100 Subject: [PATCH 13/14] Update hugr-core/src/ops/custom.rs Co-authored-by: Alan Lawrence --- hugr-core/src/ops/custom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index dc0a127d7..f96f6ec9f 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -294,7 +294,7 @@ pub struct OpaqueOp { #[cfg_attr(test, proptest(strategy = "any_nonempty_string()"))] description: String, // cache in advance so description() can return &str args: Vec, - // note that `signature` may not include `extension`. Thus this field must + // note that the `signature` field might not include `extension`. Thus this must // remain private, and should be accessed through // `DataflowOpTrait::signature`. signature: FunctionType, From 1a5f3306c1244358d0939ea9834ebb6885360ccf Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 17 Jul 2024 11:28:54 +0100 Subject: [PATCH 14/14] put back improvement from reverted commit --- hugr-py/src/hugr/std/float.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hugr-py/src/hugr/std/float.py b/hugr-py/src/hugr/std/float.py index 92cb5d79a..bf0cfff18 100644 --- a/hugr-py/src/hugr/std/float.py +++ b/hugr-py/src/hugr/std/float.py @@ -7,8 +7,9 @@ from hugr import tys, val #: HUGR 64-bit IEEE 754-2019 floating point type. +FLOAT_EXT_ID = "arithmetic.float.types" FLOAT_T = tys.Opaque( - extension="arithmetic.float.types", + extension=FLOAT_EXT_ID, id="float64", args=[], bound=tys.TypeBound.Copyable,