Skip to content

Commit 14f969a

Browse files
committed
switched to outgoing ports
1 parent 8ee0ab7 commit 14f969a

File tree

3 files changed

+56
-51
lines changed

3 files changed

+56
-51
lines changed

hugr-core/src/hugr/patch/simple_replace.rs

+47-35
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ impl<N: HugrNode> OutputBoundaryMap<N> {
147147
}
148148
}
149149
}
150+
151+
fn map_nodes<M: HugrNode>(&self, node_map: impl Fn(N) -> M) -> OutputBoundaryMap<M> {
152+
match self {
153+
OutputBoundaryMap::ByIncoming(map) => OutputBoundaryMap::ByIncoming(
154+
map.iter()
155+
.map(|(&(node, port), &val)| ((node_map(node), port), val))
156+
.collect(),
157+
),
158+
OutputBoundaryMap::ByOutgoing(map) => OutputBoundaryMap::ByOutgoing(
159+
map.iter()
160+
.map(|(&(node, port), &val)| ((node_map(node), port), val))
161+
.collect(),
162+
),
163+
}
164+
}
150165
}
151166

152167
impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
@@ -361,41 +376,6 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
361376
self.nu_inp.get(&(node, port)).copied().map(Into::into)
362377
}
363378

364-
/// Map the host nodes in `self` according to `node_map`.
365-
///
366-
/// `node_map` must map nodes in the current HUGR of the subgraph to
367-
/// its equivalent nodes in some `new_hugr`.
368-
///
369-
/// This converts a replacement that acts on nodes of type `HostNode` to
370-
/// a replacement that acts on `new_hugr`, with nodes of type `N`.
371-
///
372-
/// This does not check convexity. It is up to the caller to ensure that
373-
/// the mapped replacement obtained from this applies on a convex subgraph
374-
/// of the new HUGR.
375-
pub(crate) fn map_host_nodes<N: HugrNode>(
376-
&self,
377-
node_map: impl Fn(HostNode) -> N,
378-
) -> SimpleReplacement<N> {
379-
let Self {
380-
subgraph,
381-
replacement,
382-
nu_inp,
383-
nu_out,
384-
} = self;
385-
let nu_inp = nu_inp
386-
.iter()
387-
.map(|(&(node, port), &(host_node, host_port))| {
388-
((node, port), (node_map(host_node), host_port))
389-
})
390-
.collect();
391-
let nu_out = nu_out
392-
.iter()
393-
.map(|(&(host_node, host_port), &port)| ((node_map(host_node), host_port), port))
394-
.collect();
395-
let subgraph = subgraph.map_nodes(node_map);
396-
SimpleReplacement::new(subgraph, replacement.clone(), nu_inp, nu_out)
397-
}
398-
399379
/// Get all edges that the replacement would add between `host` and
400380
/// `self.replacement`.
401381
///
@@ -427,6 +407,38 @@ impl<HostNode: HugrNode> SimpleReplacement<HostNode> {
427407
.chain(outgoing_boundary)
428408
.chain(host_to_host_boundary)
429409
}
410+
411+
/// Map the host nodes in `self` according to `node_map`.
412+
///
413+
/// `node_map` must map nodes in the current HUGR of the subgraph to
414+
/// its equivalent nodes in some `new_hugr`.
415+
///
416+
/// This converts a replacement that acts on nodes of type `HostNode` to
417+
/// a replacement that acts on `new_hugr`, with nodes of type `N`.
418+
///
419+
/// This does not check convexity. It is up to the caller to ensure that
420+
/// the mapped replacement obtained from this applies on a convex subgraph
421+
/// of the new HUGR.
422+
pub(crate) fn map_host_nodes<N: HugrNode>(
423+
&self,
424+
node_map: impl Fn(HostNode) -> N,
425+
) -> SimpleReplacement<N> {
426+
let Self {
427+
subgraph,
428+
replacement,
429+
nu_inp,
430+
nu_out,
431+
} = self;
432+
let nu_inp = nu_inp
433+
.iter()
434+
.map(|(&repl_node_port, &(host_node, host_port))| {
435+
(repl_node_port, (node_map(host_node), host_port))
436+
})
437+
.collect();
438+
let nu_out = nu_out.map_nodes(&node_map);
439+
let subgraph = subgraph.map_nodes(node_map);
440+
SimpleReplacement::new(subgraph, replacement.clone(), nu_inp, nu_out)
441+
}
430442
}
431443

432444
impl<HostNode: HugrNode> PatchVerification for SimpleReplacement<HostNode> {

hugr-core/src/hugr/persistent.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ impl Commit {
139139
&self.0
140140
}
141141

142-
fn all_parents(&self) -> impl Iterator<Item = &Commit> + '_ {
143-
self.0.all_parents().map_into()
144-
}
145-
146-
fn replacement(&self) -> Option<&SimpleReplacement<PatchNode>> {
142+
fn replacement(&self) -> Option<&PersistentReplacement> {
147143
match self.0.value() {
148144
CommitData::Base(_) => None,
149145
CommitData::Replacement(replacement) => Some(replacement),
@@ -278,7 +274,7 @@ impl PersistentHugr {
278274
let new_invalid_nodes = replacement
279275
.subgraph()
280276
.nodes()
281-
.into_iter()
277+
.iter()
282278
.map(|&PatchNode(id, node)| (id, node))
283279
.into_grouping_map()
284280
.collect::<BTreeSet<_>>();

hugr-core/src/hugr/persistent/tests.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn simple_hugr() -> (Hugr, [Node; 3]) {
4848
}
4949

5050
/// Creates a replacement that replaces a node with a sequence of two NOT gates
51-
fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleReplacement {
51+
fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleReplacement<Node> {
5252
// Create a simple hugr with two NOT gates in sequence
5353
let mut dfg_builder = DFGBuilder::new(inout_sig(vec![bool_t()], vec![bool_t()])).unwrap();
5454
let [input_wire] = dfg_builder.input_wires_arr();
@@ -67,11 +67,6 @@ fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleRe
6767

6868
let replacement_hugr = dfg_builder.finish_hugr_with_outputs([not2_out]).unwrap();
6969

70-
// Find the input and output of the node to replace
71-
let host_output_target = hugr
72-
.single_linked_input(node_to_replace, OutgoingPort::from(0))
73-
.unwrap();
74-
7570
// Create the mappings
7671
let mut nu_inp = HashMap::new();
7772
nu_inp.insert(
@@ -80,7 +75,10 @@ fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleRe
8075
);
8176

8277
let mut nu_out = HashMap::new();
83-
nu_out.insert(host_output_target, IncomingPort::from(0));
78+
nu_out.insert(
79+
(node_to_replace, OutgoingPort::from(0)),
80+
IncomingPort::from(0),
81+
);
8482

8583
// Create the subgraph with the single node
8684
let subgraph = SiblingSubgraph::try_from_nodes(vec![node_to_replace], hugr).unwrap();
@@ -91,7 +89,7 @@ fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleRe
9189

9290
/// Creates a replacement that replaces the unique AND gate in `hugr` and its
9391
/// predecessor NOT gate on 1st input with an XOR gate
94-
fn create_not_and_to_xor_replacement(hugr: &Hugr) -> SimpleReplacement {
92+
fn create_not_and_to_xor_replacement(hugr: &Hugr) -> SimpleReplacement<Node> {
9593
// Create second replacement that replaces the second NOT gate from the first
9694
// replacement
9795
// Find the AND gate in the hugr
@@ -133,8 +131,7 @@ fn create_not_and_to_xor_replacement(hugr: &Hugr) -> SimpleReplacement {
133131

134132
// Output mapping - AND gate's output to XOR's output
135133
let mut nu_out = HashMap::new();
136-
let and_output_port = hugr.single_linked_input(and_gate, 0).unwrap();
137-
nu_out.insert(and_output_port, IncomingPort::from(0));
134+
nu_out.insert((and_gate, OutgoingPort::from(0)), IncomingPort::from(0));
138135

139136
// Create subgraph with both the AND gate and NOT0 node
140137
let subgraph = SiblingSubgraph::try_from_nodes(vec![not_node, and_gate], &hugr).unwrap();

0 commit comments

Comments
 (0)