dwavesystems/dimod

frustrated loops code can't handle tuple labels

pau557 opened this issue · 1 comments

Description
If the graph has tuple labels (quite common), the numpy.random.choice(nodes) in frustrated_loops gets confused

Steps To Reproduce

[ins] In [1]: import dimod

[ins] In [2]: import networkx as nx

[ins] In [3]: g = nx.erdos_renyi_graph(100, 0.5)

[ins] In [4]: tuple_map = dict()

[ins] In [5]: for node in g.nodes:
         ...:     tuple_map[node]= (node, node, node)
         ...:

[ins] In [6]: g = nx.relabel_nodes(g, tuple_map)

[ins] In [7]: dimod.generators.frustrated_loop(g, R=3, num_cycles=100)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 dimod.generators.frustrated_loop(g, R=3, num_cycles=100)

File ~/Code/Tools/dimod/dimod/decorators.py:325, in graph_argument.<locals>._graph_arg.<locals>.new_f(*args, **kwargs)
    322 for name in arg_names:
    323     _enforce_single_arg(name, final_args, final_kwargs)
--> 325 return f(*final_args, **final_kwargs)

File ~/Code/Tools/dimod/dimod/generators/fcl.py:129, in frustrated_loop(graph, num_cycles, R, cycle_predicates, max_failed_cycles, plant_solution, planted_solution, seed)
    126 good_cycles = 0
    127 while good_cycles < num_cycles and failed_cycles < max_failed_cycles:
--> 129     cycle = _random_cycle(adj, r)
    131     # if the cycle failed or it is otherwise invalid, mark as failed and continue
    132     if cycle is None or not all(pred(cycle) for pred in cycle_predicates):

File ~/Code/Tools/dimod/dimod/generators/fcl.py:199, in _random_cycle(adj, random_state)
    196     return None
    198 # get a random neighbor
--> 199 u = random_state.choice(neighbors)
    200 if u in visited:
    201     # if we've seen this neighbour, then we have a cycle starting from it
    202     return walk[visited[u]:]

File mtrand.pyx:911, in numpy.random.mtrand.RandomState.choice()

ValueError: a must be 1-dimensional

Neat! It's because NumPy interprets [(0, 1), (0, 2)] as a 2d array implicitly. Should be simple to fix.