Anders and Briegel in Python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

80 Zeilen
2.6KB

  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 tables as clifford
  7. class GraphState(object):
  8. def __init__(self):
  9. self.ngbh = defaultdict(set)
  10. self.vops = defaultdict(int)
  11. def add_edge(self, v1, v2):
  12. """ Add an edge between two vertices in the self """
  13. if not v1 in self.ngbh:
  14. self.vops[v1] = clifford.by_name["hadamard"]
  15. if not v2 in self.ngbh:
  16. self.vops[v2] = clifford.by_name["hadamard"]
  17. self.ngbh[v1].add(v2)
  18. self.ngbh[v2].add(v1)
  19. def del_edge(self, v1, v2):
  20. """ Delete an edge between two vertices in the self """
  21. self.ngbh[v1].remove(v2)
  22. self.ngbh[v2].remove(v1)
  23. def has_edge(self, v1, v2):
  24. """ Test existence of an edge between two vertices in the self """
  25. return v2 in self.ngbh[v1]
  26. def toggle_edge(self, v1, v2):
  27. """ Toggle an edge between two vertices in the self """
  28. if self.has_edge(v1, v2):
  29. self.del_edge(v1, v2)
  30. else:
  31. self.add_edge(v1, v2)
  32. def edgelist(self):
  33. """ Describe a graph as an edgelist """
  34. edges = frozenset(tuple(sorted((i, n)))
  35. for i, v in self.ngbh.items()
  36. for n in v)
  37. return [tuple(e) for e in edges]
  38. def remove_vop(self, a, avoid):
  39. """ Reduces VOP[a] to the identity """
  40. others = self.ngbh[a] - {avoid}
  41. swap_qubit = others.pop() if others else avoid
  42. for v in reversed(clifford.decompositions[self.vops[a]]):
  43. self.local_complementation(a if v == "x" else swap_qubit)
  44. def local_complementation(self, v):
  45. """ As defined in LISTING 1 of Anders & Briegel """
  46. for i, j in it.combinations(self.ngbh[v], 2):
  47. self.toggle_edge(i, j)
  48. # Update VOPs
  49. self.vops[v] = clifford.times_table[
  50. self.vops[v]][clifford.by_name["sqx"]]
  51. for i in self.ngbh[v]:
  52. self.vops[i] = clifford.times_table[
  53. self.vops[i]][clifford.by_name["msqz"]]
  54. def cphase(self, a, b):
  55. """ Act a controlled-phase gate on two qubits """
  56. if self.ngbh[a] - {b}:
  57. self.remove_vop(a, b)
  58. if self.ngbh[b] - {a}:
  59. self.remove_vop(b, a)
  60. if self.ngbh[a] - {b}:
  61. self.remove_vop(a, b)
  62. edge = self.has_edge(a, b)
  63. new_edge, vops[a], vops[b] = cphase_table[edge, vops[a], vops[b]]
  64. if new_edge != edge:
  65. self.toggle_edge(a, b)