Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

42 lines
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]