@@ -703,12 +703,27 @@ impl State {
703703 // If we apply a single swap it could be that we route 2 nodes; that is a setup like
704704 // A - B - A - B
705705 // and we swap the middle two qubits. This cannot happen if we apply 2 or more swaps.
706- match current_swaps. as_slice ( ) {
707- [ swap] => swap
706+ if current_swaps. len ( ) > 1 {
707+ smallvec ! [ closest_node]
708+ } else {
709+ // check if the closest node has neighbors that are now routable -- for that we get
710+ // the other physical qubit that was swapped and check whether the node on it
711+ // is now routable
712+ let mut possible_other_qubit = current_swaps[ 0 ]
708713 . iter ( )
709- . filter_map ( |q| self . routable_node_on_qubit ( problem, * q) )
710- . collect ( ) ,
711- _ => smallvec ! [ closest_node] ,
714+ // check if other nodes are in the front layer that are connected by this swap
715+ . filter_map ( |& swap_qubit| self . front_layer . qubits ( ) [ swap_qubit. index ( ) ] )
716+ // remove the closest_node, which we know we already routed
717+ . filter ( |( node_index, _other_qubit) | * node_index != closest_node)
718+ . map ( |( _node_index, other_qubit) | other_qubit) ;
719+
720+ // if there is indeed another candidate, check if that gate is routable
721+ if let Some ( other_qubit) = possible_other_qubit. next ( ) {
722+ if let Some ( also_routed) = self . routable_node_on_qubit ( problem, other_qubit) {
723+ return smallvec ! [ closest_node, also_routed] ;
724+ }
725+ }
726+ smallvec ! [ closest_node]
712727 }
713728 }
714729
0 commit comments