From cced76a7266723439554b46350bb28cc11264782 Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Wed, 5 Jun 2024 17:40:24 +0100 Subject: [PATCH] feat: CircuitBuilder::add_constant --- hugr-core/src/builder.rs | 4 +++- hugr-core/src/builder/circuit.rs | 22 +++++++++++++++++++--- hugr-core/src/hugr.rs | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/hugr-core/src/builder.rs b/hugr-core/src/builder.rs index c186b57cd..787decc07 100644 --- a/hugr-core/src/builder.rs +++ b/hugr-core/src/builder.rs @@ -222,6 +222,7 @@ pub(crate) mod test { use crate::hugr::{views::HugrView, HugrMut, NodeType}; use crate::ops; + use crate::std_extensions::arithmetic::float_ops::FLOAT_OPS_REGISTRY; use crate::types::{FunctionType, PolyFuncType, Type}; use crate::{type_row, Hugr}; @@ -252,7 +253,8 @@ pub(crate) mod test { let f_builder = module_builder.define_function("main", signature)?; f(f_builder)?; - Ok(module_builder.finish_prelude_hugr()?) + + Ok(module_builder.finish_hugr(&FLOAT_OPS_REGISTRY)?) } #[fixture] diff --git a/hugr-core/src/builder/circuit.rs b/hugr-core/src/builder/circuit.rs index d59098776..2fecff49d 100644 --- a/hugr-core/src/builder/circuit.rs +++ b/hugr-core/src/builder/circuit.rs @@ -3,7 +3,7 @@ use std::mem; use thiserror::Error; -use crate::ops::{NamedOp, OpType}; +use crate::ops::{NamedOp, OpType, Value}; use crate::utils::collect_array; use super::{BuildError, Dataflow}; @@ -199,6 +199,11 @@ impl<'a, T: Dataflow + ?Sized> CircuitBuilder<'a, T> { Ok(collect_array(outputs)) } + /// Adds a constant value to the circuit and loads it into a wire. + pub fn add_constant(&mut self, value: impl Into) -> Wire { + self.builder.add_load_value(value) + } + /// Add a wire to the list of tracked wires. /// /// Returns the new unit index. @@ -237,7 +242,10 @@ mod test { use super::*; use cool_asserts::assert_matches; - use crate::utils::test_quantum_extension::{cx_gate, h_gate, measure, q_alloc, q_discard}; + use crate::std_extensions::arithmetic::float_types::{self, ConstF64}; + use crate::utils::test_quantum_extension::{ + cx_gate, h_gate, measure, q_alloc, q_discard, rz_f64, + }; use crate::{ builder::{ test::{build_main, NAT, QB}, @@ -252,7 +260,9 @@ mod test { #[test] fn simple_linear() { let build_res = build_main( - FunctionType::new(type_row![QB, QB], type_row![QB, QB]).into(), + FunctionType::new(type_row![QB, QB], type_row![QB, QB]) + .with_extension_delta(float_types::EXTENSION_ID) + .into(), |mut f_build| { let wires = f_build.input_wires().map(Some).collect(); @@ -268,6 +278,12 @@ mod test { .append(cx_gate(), [0, 1])? .append(cx_gate(), [1, 0])?; + let angle = linear.add_constant(ConstF64::new(0.5)); + linear.append_and_consume( + rz_f64(), + [CircuitUnit::Linear(0), CircuitUnit::Wire(angle)], + )?; + let outs = linear.finish(); f_build.finish_with_outputs(outs) }, diff --git a/hugr-core/src/hugr.rs b/hugr-core/src/hugr.rs index 875bd8ddc..d3e8f6959 100644 --- a/hugr-core/src/hugr.rs +++ b/hugr-core/src/hugr.rs @@ -201,7 +201,7 @@ impl Hugr { } /// Leaving this here as in the future we plan for it to infer deltas - /// of container nodes e.g. [DFG]. For the moment it does nothing. + /// of container nodes e.g. DFG. For the moment it does nothing. pub fn infer_extensions(&mut self) -> Result<(), ExtensionError> { Ok(()) }