def test_tree_from_peo(filename='inst_2x2_7_0.txt'): import qtree.operators as ops nq, c = ops.read_circuit_file(filename) graph, *_ = circ2graph(nq, c, omit_terminals=False) peo, _ = get_peo(graph) tree = get_tree_from_peo(get_simple_graph(graph), peo) elements = list(range(1, graph.number_of_nodes() + 1)) for element in elements: nodes_containing_element = [] for node in tree.nodes(): if element in node: nodes_containing_element.append(node) subtree = nx.subgraph(tree, nodes_containing_element) if subtree.number_of_nodes() > 0: connected = nx.connected.is_connected(subtree) else: connected = True # Take empty graph as connected if not connected: # draw_graph(subtree, f"st_{element}") pass draw_graph(tree, f"tree")
def test_maximum_cardinality_search(): """Test maximum cardinality search algorithm""" # Read graph import qtree.operators as ops import os this_dir = os.path.dirname((os.path.abspath(__file__))) nq, c = ops.read_circuit_file(this_dir + '/../../inst_2x2_7_0.txt') old_g, *_ = circ2graph(nq, c) # Make random clique vertices = list(np.random.choice(old_g.nodes, 4, replace=False)) while is_clique(old_g, vertices): vertices = list(np.random.choice(old_g.nodes, 4, replace=False)) g = make_clique_on(old_g, vertices) # Make graph completion peo, tw = get_peo(g) g_chordal = get_fillin_graph2(g, peo) # MCS will produce alternative PEO with this clique at the end new_peo = maximum_cardinality_search(g_chordal, list(vertices)) # Test if new peo is correct assert is_peo_zero_fillin(g_chordal, peo) assert is_peo_zero_fillin(g_chordal, new_peo) new_tw = get_treewidth_from_peo(g, new_peo) assert tw == new_tw print('peo:', peo) print('new_peo:', new_peo)
def test_is_clique(): """Test is_clique""" import qtree.operators as ops import os this_dir = os.path.dirname((os.path.abspath(__file__))) nq, c = ops.read_circuit_file(this_dir + '/../../inst_2x2_7_0.txt') g, *_ = circ2graph(nq, c) # select some random vertices vertices = list(np.random.choice(g.nodes, 4, replace=False)) while is_clique(g, vertices): vertices = list(np.random.choice(g.nodes, 4, replace=False)) g_new = make_clique_on(g, vertices) assert is_clique(g_new, vertices)
def test_is_zero_fillin(): """ Test graph filling using the elimination order """ import time import qtree.operators as ops import os this_dir = os.path.dirname((os.path.abspath(__file__))) nq, c = ops.read_circuit_file( this_dir + '/../../test_circuits/inst/cz_v2/10x10/inst_10x10_60_1.txt') g, *_ = circ2graph(nq, c, omit_terminals=False) g1 = get_fillin_graph(g, list(range(g.number_of_nodes()))) tim1 = time.time() print(is_peo_zero_fillin(g1, list(range(g.number_of_nodes())))) tim2 = time.time() print(is_peo_zero_fillin2(g1, list(range(g.number_of_nodes())))) tim3 = time.time() print(tim2 - tim1, tim3 - tim2)
def test_get_fillin_graph(): """ Test graph filling using the elimination order """ import time import qtree.operators as ops import os this_dir = os.path.dirname((os.path.abspath(__file__))) nq, c = ops.read_circuit_file( this_dir + '/../../test_circuits/inst/cz_v2/10x10/inst_10x10_60_1.txt' # 'inst_2x2_7_1.txt' ) g, *_ = circ2graph(nq, c, omit_terminals=False) peo = np.random.permutation(g.nodes) tim1 = time.time() g1 = get_fillin_graph(g, list(peo)) tim2 = time.time() g2 = get_fillin_graph2(g, list(peo)) tim3 = time.time() assert nx.is_isomorphic(g1, g2) print(tim2 - tim1, tim3 - tim2)