Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

39 行
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]