示例#1
0
 def test_topo_sort(self):
     dag = retworkx.PyDAG()
     node_a = dag.add_node('a')
     for i in range(5):
         dag.add_child(node_a, i, None)
     dag.add_parent(3, 'A parent', None)
     res = retworkx.topological_sort(dag)
     self.assertEqual([6, 0, 5, 4, 3, 2, 1], res)
示例#2
0
 def test_simple_dag_composition(self):
     dag = retworkx.PyDAG()
     dag.check_cycle = True
     node_a = dag.add_node('a')
     node_b = dag.add_child(node_a, 'b', {'a': 1})
     node_c = dag.add_child(node_b, 'c', {'a': 2})
     dag_other = retworkx.PyDAG()
     node_d = dag_other.add_node('d')
     dag_other.add_child(node_d, 'e', {'a': 3})
     res = dag.compose(dag_other, {node_c: (node_d, {'b': 1})})
     self.assertEqual({0: 3, 1: 4}, res)
     self.assertEqual([0, 1, 2, 3, 4], retworkx.topological_sort(dag))
示例#3
0
 def test_simple_dag_composition(self):
     dag = retworkx.PyDAG()
     dag.check_cycle = True
     node_a = dag.add_node("a")
     node_b = dag.add_child(node_a, "b", {"a": 1})
     node_c = dag.add_child(node_b, "c", {"a": 2})
     dag_other = retworkx.PyDAG()
     node_d = dag_other.add_node("d")
     dag_other.add_child(node_d, "e", {"a": 3})
     res = dag.compose(dag_other, {node_c: (node_d, {"b": 1})})
     self.assertEqual({0: 3, 1: 4}, res)
     self.assertEqual([0, 1, 2, 3, 4], retworkx.topological_sort(dag))
示例#4
0
    def _in_topological_order(self, ops):
        """Sorts a set of operators in the circuit in a topological order.

        Args:
            ops (Iterable[Operator]): set of operators in the circuit

        Returns:
            Iterable[Operator]: same set of operators, topologically ordered
        """
        G = self._graph.subgraph(
            list(self._graph.nodes().index(o) for o in ops))
        indexes = rx.topological_sort(G)
        return list(G[x] for x in indexes)
 def time_topological_sort(self, _, __):
     retworkx.topological_sort(self.graph)
示例#6
0
 def test_topo_sort_empty(self):
     dag = retworkx.PyDAG()
     self.assertEqual([], retworkx.topological_sort(dag))