Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0KB

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