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.

83 line
2.1KB

  1. from clifford import *
  2. """
  3. Porting Anders and Briegel to Python
  4. """
  5. stab_rep = {None: "-", 0: "X", 1: "Y", 2: "Z"}
  6. def rightphase(n):
  7. """ This is dumb. TODO: get rid """
  8. return n % 4
  9. class Stabilizer(object):
  10. def __init__(self, graph):
  11. n = graph.nqubits
  12. self.paulis = [[None for i in range(n)] for j in range(n)]
  13. self.signs = [None for i in range(n)]
  14. for i in range(n):
  15. signs[i] = 0
  16. for j in range(n):
  17. if i == j:
  18. self.paulis[i][j] = lco_x
  19. elif j in g.vertices[i].neighbors:
  20. self.paulis[i][j] = lco_z
  21. else:
  22. self.paulis[i][j] = lco_id
  23. def __str__(self):
  24. return "\n".join(" ".join(stab_rep[x] for x in row) for row in self.paulis)
  25. class Vertex(object):
  26. def __init__(self, index):
  27. self.index = index
  28. self.vertex_operator = lco_h
  29. self.neighbors = set()
  30. def edgelist(self):
  31. return [set((self.index, n)) for n in self.neighbors]
  32. def __str__(self):
  33. return "{}".format(", ".join(map(str, self.neighbors)))
  34. class GraphRegister(object):
  35. def __init__(self, n):
  36. self.nqubits = n
  37. self.vertices = [Vertex(i) for i in xrange(n)]
  38. def add_edge(self, v1, v2):
  39. self.vertices[v1].neighbors.add(v2)
  40. self.vertices[v2].neighbors.add(v1)
  41. def del_edge(self, v1, v2):
  42. self.vertices[v1].neighbors.remove(v2)
  43. self.vertices[v2].neighbors.remove(v1)
  44. def toggle_edge(self, v1, v2):
  45. if v2 in self.vertices[v1].neighbors:
  46. self.del_edge(v1, v2)
  47. else:
  48. self.add_edge(v1, v2)
  49. def edgelist(self):
  50. return map(tuple, frozenset(frozenset((v.index, n))
  51. for v in self.vertices
  52. for n in v.neighbors))
  53. def __str__(self, ):
  54. return "\n".join(str(v) for v in self.vertices
  55. if len(v.neighbors) > 0)
  56. if __name__ == '__main__':
  57. g = GraphRegister(10)
  58. g.toggle_edge(0, 1)
  59. print g