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: add more command helper methods #125

Merged
merged 3 commits into from
Sep 21, 2023
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
2 changes: 1 addition & 1 deletion src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pub mod command;
mod hash;
mod units;
pub mod units;

pub use command::{Command, CommandIterator};
pub use hash::CircuitHash;
Expand Down
58 changes: 57 additions & 1 deletion src/circuit/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub use hugr::types::{EdgeKind, Signature, Type, TypeRow};
pub use hugr::{Direction, Node, Port, Wire};

/// An operation applied to specific wires.
#[derive(Eq, PartialOrd, Ord, Hash)]
pub struct Command<'circ, Circ> {
/// The circuit.
circ: &'circ Circ,
Expand Down Expand Up @@ -63,6 +62,24 @@ impl<'circ, Circ: Circuit> Command<'circ, Circ> {
Units::new(self.circ, self.node, direction, self).filter_units::<filter::Linear>()
}

/// Returns the linear units of this command in a given direction.
#[inline]
pub fn qubits(&self, direction: Direction) -> FilteredUnits<filter::Qubits, &Self> {
Units::new(self.circ, self.node, direction, self).filter_units::<filter::Qubits>()
}

/// Returns the linear units of this command in a given direction.
#[inline]
pub fn input_qubits(&self) -> FilteredUnits<filter::Qubits, &Self> {
self.qubits(Direction::Incoming)
}

/// Returns the linear units of this command in a given direction.
#[inline]
pub fn output_qubits(&self) -> FilteredUnits<filter::Qubits, &Self> {
self.qubits(Direction::Outgoing)
}

/// Returns the units and wires of this command in a given direction.
#[inline]
pub fn unit_wires(
Expand Down Expand Up @@ -121,6 +138,22 @@ impl<'circ, Circ: Circuit> Command<'circ, Circ> {
pub fn output_count(&self) -> usize {
self.optype().signature().output_count()
}

/// Returns the port in the command given a linear unit.
#[inline]
pub fn linear_unit_port(&self, unit: LinearUnit, direction: Direction) -> Option<Port> {
self.linear_units(direction)
.find(|(cu, _, _)| *cu == unit)
.map(|(_, port, _)| port)
}

/// Returns whether the port is a linear port.
#[inline]
pub fn is_linear_port(&self, port: Port) -> bool {
self.optype()
.port_kind(port)
.map_or(false, |kind| kind.is_linear())
}
}

impl<'a, 'circ, Circ: Circuit> UnitLabeller for &'a Command<'circ, Circ> {
Expand Down Expand Up @@ -162,6 +195,8 @@ impl<'circ, Circ> PartialEq for Command<'circ, Circ> {
}
}

impl<'circ, Circ> Eq for Command<'circ, Circ> {}

impl<'circ, Circ> Clone for Command<'circ, Circ> {
fn clone(&self) -> Self {
Self {
Expand All @@ -172,6 +207,27 @@ impl<'circ, Circ> Clone for Command<'circ, Circ> {
}
}

impl<'circ, Circ> std::hash::Hash for Command<'circ, Circ> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.node.hash(state);
self.linear_units.hash(state);
}
}

impl<'circ, Circ> PartialOrd for Command<'circ, Circ> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<'circ, Circ> Ord for Command<'circ, Circ> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.node
.cmp(&other.node)
.then(self.linear_units.cmp(&other.linear_units))
}
}

/// A non-borrowing topological walker over the nodes of a circuit.
type NodeWalker = pv::Topo<Node, HashSet<Node>>;

Expand Down
1 change: 1 addition & 0 deletions src/circuit/units/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub type FilteredUnits<F, UL = DefaultUnitLabeller> = std::iter::FilterMap<

/// A filter over a [`Units`] iterator.
pub trait UnitFilter {
/// The item yielded by the filtered iterator.
type Item;

/// Filter a [`Units`] iterator item, and unwrap it into a `Self::Item` if
Expand Down