Skip to content

Commit

Permalink
Nicer typeRow definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Mar 29, 2023
1 parent b9bbbe4 commit 067e4c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
8 changes: 7 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ pub(crate) use impl_box_clone;

/// Creates a [`TypeRow`] backed by statically defined data, avoiding allocations.
///
/// The parameters must be constants of type [`SimpleType`].
///
/// For type rows that cannot be statically defined, use a vector or slice instead.
///
/// Example:
/// ```
/// # use hugr::macros::type_row;
/// # use hugr::types::{ClassicType, SimpleType, Signature, TypeRow};
/// const B: SimpleType = SimpleType::Classic(ClassicType::Bit);
/// let sig: Signature = Signature::new_df(type_row![B, B], type_row![B]);
/// let static_row: TypeRow = type_row![B, B];
/// let dynamic_row: TypeRow = vec![B, B, B].into();
/// let sig: Signature = Signature::new_df(static_row, dynamic_row);
/// ```
#[allow(unused_macros)]
#[macro_export]
Expand Down
18 changes: 8 additions & 10 deletions src/ops/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use smol_str::SmolStr;

use super::{Op, OpaqueOp};
use crate::types::{ClassicType, QuantumType, Signature, SimpleType};
use crate::{
type_row,
types::{ClassicType, QuantumType, Signature, SimpleType},
};

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
Expand Down Expand Up @@ -72,11 +75,6 @@ impl Op for LeafOp {
// copy-on-write strategy, so we can avoid unnecessary allocations.
const Q: SimpleType = SimpleType::Quantum(QuantumType::Qubit);
const B: SimpleType = SimpleType::Classic(ClassicType::Bit);
static ROW_QUBIT: &[SimpleType] = &[Q];
static ROW_2QUBIT: &[SimpleType] = &[Q, Q];
static ROW_BIT: &[SimpleType] = &[B];
static ROW_2BIT: &[SimpleType] = &[B, B];
static ROW_QUBIT_BIT: &[SimpleType] = &[Q, B];

match self {
LeafOp::Noop(typ) => Signature::new_df(vec![typ.clone()], vec![typ.clone()]),
Expand All @@ -88,14 +86,14 @@ impl Op for LeafOp {
| LeafOp::Sadj
| LeafOp::X
| LeafOp::Y
| LeafOp::Z => Signature::new_linear(ROW_QUBIT),
LeafOp::CX | LeafOp::ZZMax => Signature::new_linear(ROW_2QUBIT),
LeafOp::Measure => Signature::new_linear(ROW_QUBIT_BIT),
| LeafOp::Z => Signature::new_linear(type_row![Q]),
LeafOp::CX | LeafOp::ZZMax => Signature::new_linear(type_row![Q, Q]),
LeafOp::Measure => Signature::new_linear(type_row![Q, B]),
LeafOp::Copy { n_copies, typ } => {
let typ: SimpleType = typ.clone().into();
Signature::new_df(vec![typ.clone()], vec![typ; *n_copies as usize])
}
LeafOp::Xor => Signature::new_df(ROW_2BIT, ROW_BIT),
LeafOp::Xor => Signature::new_df(type_row![B, B], type_row![B]),
LeafOp::CustomOp(opaque) => opaque.signature(),
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/ops/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::any::Any;
use super::Op;
use crate::{
macros::impl_box_clone,
type_row,
types::{ClassicType, Signature, SignatureDescription, SimpleType, TypeRow},
};

Expand Down Expand Up @@ -90,11 +91,11 @@ impl Default for ConstValue {
impl ConstValue {
/// Returns the datatype of the constant
pub fn type_row(&self) -> TypeRow {
static BIT_SIG: &[SimpleType] = &[SimpleType::Classic(ClassicType::Bit)];
static INT_SIG: &[SimpleType] = &[SimpleType::Classic(ClassicType::Int)];
const BIT: SimpleType = SimpleType::Classic(ClassicType::Bit);
const INT: SimpleType = SimpleType::Classic(ClassicType::Int);
match self {
Self::Bit(_) => BIT_SIG.into(),
Self::Int(_) => INT_SIG.into(),
Self::Bit(_) => type_row![BIT],
Self::Int(_) => type_row![INT],
Self::Opaque(row, _) => TypeRow::from(vec![row.clone()]),
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/types/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ impl TypeRow {
}
}
impl TypeRow {
/// Create a new empty row.
pub const fn new() -> Self {
Self { types: None }
}

/// Create a new row from a Cow slice of types.
///
/// See [`type_row!`] for a more ergonomic way to create a statically allocated rows.
///
/// [`type_row!`]: crate::macros::type_row
pub fn from(types: impl Into<Cow<'static, [SimpleType]>>) -> Self {
let types = types.into();
Self {
Expand Down

0 comments on commit 067e4c5

Please sign in to comment.