Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

42 行
1.2KB

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