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.

72 lines
2.2KB

  1. """
  2. Provides an extremely basic graph structure, based on neighbour lists
  3. """
  4. from collections import defaultdict
  5. import itertools as it
  6. import clifford
  7. def graph():
  8. """ Generate a graph with Hadamards on each qubit """
  9. #return defaultdict(set), defaultdict(lambda: clifford.by_name["hadamard"])
  10. return [set() for i in range(100)], [clifford.by_name["hadamard"] for i in range(100)]
  11. def add_edge(graph, v1, v2):
  12. """ Add an edge between two vertices in the graph """
  13. graph[v1].add(v2)
  14. graph[v2].add(v1)
  15. def del_edge(graph, v1, v2):
  16. """ Delete an edge between two vertices in the graph """
  17. graph[v1].remove(v2)
  18. graph[v2].remove(v1)
  19. def has_edge(graph, v1, v2):
  20. """ Test existence of an edge between two vertices in the graph """
  21. return v2 in graph[v1]
  22. def toggle_edge(graph, v1, v2):
  23. """ Toggle an edge between two vertices in the graph """
  24. if has_edge(graph, v1, v2):
  25. del_edge(graph, v1, v2)
  26. else:
  27. add_edge(graph, v1, v2)
  28. def edgelist(g):
  29. """ Describe a graph as an edgelist """
  30. edges = frozenset(frozenset((i, n))
  31. for i, v in enumerate(g)
  32. for n in v)
  33. return [tuple(e) for e in edges]
  34. def cphase(g, vops, a, b):
  35. """ Act a controlled-phase gate on two qubits """
  36. if g[a]-{b}: remove_vop(g, vops, a, b)
  37. if g[b]-{a}: remove_vop(g, vops, b, a)
  38. if g[a]-{b}: remove_vop(g, vops, a, b)
  39. edge = has_edge(g, a, b)
  40. new_edge, vops[a], vops[b] = cphase_table[edge, vops[a], vops[b]]
  41. if new_edge != edge:
  42. toggle_edge(g, a, b)
  43. def remove_vop(g, vops, a, avoid):
  44. """ Reduces VOP[a] to the identity, avoiding (if possible) the use of vertex b as a swapping partner """
  45. others = g[a] - {avoid}
  46. swap_qubit = others.pop() if others else avoid
  47. for v in reversed(clifford.decompositions[vops[a]]):
  48. local_complementation(g, vops, a if v == "x" else swap_qubit)
  49. def local_complementation(g, vops, v):
  50. """ As defined in LISTING 1 of Anders & Briegel """
  51. for i, j in it.combinations(g[v], 2):
  52. toggle_edge(g, i, j)
  53. # Update VOPs
  54. vops[v] = clifford.times_table[vops[v]][clifford.by_name["sqx"]]
  55. for i in g[v]:
  56. vops[i] = clifford.times_table[vops[i]][clifford.by_name["msqz"]]