Skip to content

Commit

Permalink
mapping is not possible when a node has no neighbors (Qiskit#995)
Browse files Browse the repository at this point in the history
* mapping is not possible when a node has no neighbors

* suggested improvement

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
alexanderivrii and mergify[bot] authored Oct 6, 2023
1 parent c5b41fc commit 2d5694e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rustworkx-core/src/token_swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ where
}
let id_node = self.rev_node_map[&node];
let id_token = self.rev_node_map[&tokens[&node]];

if self.graph.neighbors(id_node).next().is_none() {
return Err(MapNotPossible {});
}

for id_neighbor in self.graph.neighbors(id_node) {
let neighbor = self.node_map[&id_neighbor];
let dist_neighbor: DictMap<G::NodeId, usize> = dijkstra(
Expand Down Expand Up @@ -705,4 +710,19 @@ mod test_token_swapper {
Err(_) => (),
};
}

#[test]
fn test_edgeless_graph_fails() {
let mut g = petgraph::graph::UnGraph::<(), ()>::new_undirected();
let a = g.add_node(());
let b = g.add_node(());
let c = g.add_node(());
let d = g.add_node(());
g.add_edge(c, d, ());
let mapping = HashMap::from([(a, b), (b, a)]);
match token_swapper(&g, mapping, Some(10), Some(4), Some(50)) {
Ok(_) => panic!("This should error"),
Err(_) => (),
};
}
}

0 comments on commit 2d5694e

Please sign in to comment.