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

feat: CircuitBuilder::add_constant #1168

Merged
merged 2 commits into from
Jun 7, 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: 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
26 changes: 23 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 @@ -250,9 +258,15 @@ mod test {
};

#[test]
#[cfg_attr(
feature = "extension_inference",
ignore = "Extension validation fails when mixing in the float extension"
)]
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 +282,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