@@ -5,7 +5,12 @@ use rstest::*;
5
5
use crate :: {
6
6
builder:: { inout_sig, DFGBuilder , Dataflow , DataflowHugr } ,
7
7
extension:: prelude:: bool_t,
8
- hugr:: { patch:: Patch , persistent:: PatchNode , views:: SiblingSubgraph , Hugr , HugrView } ,
8
+ hugr:: {
9
+ patch:: { simple_replace:: DefaultInMap , OutputNodeBoundaryMap , Patch } ,
10
+ persistent:: PatchNode ,
11
+ views:: SiblingSubgraph ,
12
+ Hugr , HugrView ,
13
+ } ,
9
14
ops:: handle:: NodeHandle ,
10
15
std_extensions:: logic:: LogicOp ,
11
16
IncomingPort , Node , OutgoingPort , SimpleReplacement ,
@@ -48,7 +53,10 @@ fn simple_hugr() -> (Hugr, [Node; 3]) {
48
53
}
49
54
50
55
/// 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 {
56
+ fn create_double_not_replacement (
57
+ hugr : & Hugr ,
58
+ node_to_replace : Node ,
59
+ ) -> SimpleReplacement < Node , DefaultInMap < Node > , OutputNodeBoundaryMap < Node , OutgoingPort > > {
52
60
// Create a simple hugr with two NOT gates in sequence
53
61
let mut dfg_builder = DFGBuilder :: new ( inout_sig ( vec ! [ bool_t( ) ] , vec ! [ bool_t( ) ] ) ) . unwrap ( ) ;
54
62
let [ input_wire] = dfg_builder. input_wires_arr ( ) ;
@@ -67,11 +75,6 @@ fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleRe
67
75
68
76
let replacement_hugr = dfg_builder. finish_hugr_with_outputs ( [ not2_out] ) . unwrap ( ) ;
69
77
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
-
75
78
// Create the mappings
76
79
let mut nu_inp = HashMap :: new ( ) ;
77
80
nu_inp. insert (
@@ -80,18 +83,30 @@ fn create_double_not_replacement(hugr: &Hugr, node_to_replace: Node) -> SimpleRe
80
83
) ;
81
84
82
85
let mut nu_out = HashMap :: new ( ) ;
83
- nu_out. insert ( host_output_target, IncomingPort :: from ( 0 ) ) ;
86
+ nu_out. insert (
87
+ ( node_to_replace, OutgoingPort :: from ( 0 ) ) ,
88
+ IncomingPort :: from ( 0 ) ,
89
+ ) ;
84
90
85
91
// Create the subgraph with the single node
86
92
let subgraph = SiblingSubgraph :: try_from_nodes ( vec ! [ node_to_replace] , hugr) . unwrap ( ) ;
87
93
88
94
// Create the replacement
89
- SimpleReplacement :: new ( subgraph, replacement_hugr, nu_inp, nu_out)
95
+ SimpleReplacement :: try_new (
96
+ subgraph,
97
+ hugr,
98
+ replacement_hugr,
99
+ nu_inp,
100
+ OutputNodeBoundaryMap :: from ( nu_out) ,
101
+ )
102
+ . unwrap ( )
90
103
}
91
104
92
105
/// Creates a replacement that replaces the unique AND gate in `hugr` and its
93
106
/// predecessor NOT gate on 1st input with an XOR gate
94
- fn create_not_and_to_xor_replacement ( hugr : & Hugr ) -> SimpleReplacement {
107
+ fn create_not_and_to_xor_replacement (
108
+ hugr : & Hugr ,
109
+ ) -> SimpleReplacement < Node , DefaultInMap < Node > , OutputNodeBoundaryMap < Node , OutgoingPort > > {
95
110
// Create second replacement that replaces the second NOT gate from the first
96
111
// replacement
97
112
// Find the AND gate in the hugr
@@ -133,13 +148,19 @@ fn create_not_and_to_xor_replacement(hugr: &Hugr) -> SimpleReplacement {
133
148
134
149
// Output mapping - AND gate's output to XOR's output
135
150
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 ) ) ;
151
+ nu_out. insert ( ( and_gate, OutgoingPort :: from ( 0 ) ) , IncomingPort :: from ( 0 ) ) ;
138
152
139
153
// Create subgraph with both the AND gate and NOT0 node
140
154
let subgraph = SiblingSubgraph :: try_from_nodes ( vec ! [ not_node, and_gate] , & hugr) . unwrap ( ) ;
141
155
142
- SimpleReplacement :: new ( subgraph, replacement_hugr, nu_inp, nu_out)
156
+ SimpleReplacement :: try_new (
157
+ subgraph,
158
+ hugr,
159
+ replacement_hugr,
160
+ nu_inp,
161
+ OutputNodeBoundaryMap :: from ( nu_out) ,
162
+ )
163
+ . unwrap ( )
143
164
}
144
165
145
166
#[ fixture]
0 commit comments