Anders and Briegel in Python
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1KB

  1. """
  2. Provides an extremely basic graph structure, based on neighbour lists
  3. """
  4. def graph(n):
  5. """ Generate a graph with Hadamards on each qubit """
  6. graph = [set() for i in xrange(n)]
  7. vops = [0 for i in xrange(n)] # TODO: seems ugly
  8. return graph, vops
  9. def add_edge(graph, v1, v2):
  10. """ Add an edge between two vertices in the graph """
  11. graph[v1].add(v2)
  12. graph[v2].add(v1)
  13. def del_edge(graph, v1, v2):
  14. """ Delete an edge between two vertices in the graph """
  15. graph[v1].remove(v2)
  16. graph[v2].remove(v1)
  17. def has_edge(graph, v1, v2):
  18. """ Test existence of an edge between two vertices in the graph """
  19. return v2 in graph[v1]
  20. def toggle_edge(graph, v1, v2):
  21. """ Toggle an edge between two vertices in the graph """
  22. if has_edge(graph, v1, v2):
  23. del_edge(graph, v1, v2)
  24. else:
  25. add_edge(graph, v1, v2)
  26. def edgelist(graph):
  27. """ Describe a graph as an edgelist """
  28. edges = frozenset(frozenset((i, n))
  29. for i, v in enumerate(graph)
  30. for n in v)
  31. return [tuple(e) for e in edges]