Skip to content

Commit

Permalink
feat: CircuitBuilder::add_constant
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Jun 6, 2024
1 parent 19e64a9 commit cced76a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion hugr-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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]
Expand Down
22 changes: 19 additions & 3 deletions hugr-core/src/builder/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Value>) -> Wire {
self.builder.add_load_value(value)
}

/// Add a wire to the list of tracked wires.
///
/// Returns the new unit index.
Expand Down Expand Up @@ -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},
Expand All @@ -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();

Expand All @@ -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)
},
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down

0 comments on commit cced76a

Please sign in to comment.