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.

98 lines
2.4KB

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