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.

57 lines
1.8KB

  1. """
  2. Implements a simple Stabilizer object.
  3. """
  4. import itertools as it
  5. from abp import clifford
  6. I = clifford.identity
  7. X = clifford.px
  8. Z = clifford.pz
  9. class Stabilizer(object):
  10. def __init__(self, g):
  11. """ Construct a Stabilizer from a Graphstate """
  12. self.tableau = {i:{j: I for j in g.node} for i in g.node}
  13. self.phases = {i: 1 for i in g.node}
  14. for a, b in it.product(g.node, g.node):
  15. if a == b:
  16. self.tableau[a][b] = X
  17. elif a in g.adj[b]:
  18. self.tableau[a][b] = Z
  19. self.conjugate(a, b, g.node[b]["vop"])
  20. def conjugate(self, a, b, vop):
  21. """ Do a little conjugation """
  22. op, phase = clifford.conjugate(self.tableau[a][b], vop)
  23. self.tableau[a][b] = op
  24. self.phases[a] *= phase
  25. def to_dictionary(self):
  26. """ For comparison with old A&B code """
  27. m = {1: 0, 1j:1, -1: 2, -1j: 3}
  28. return {"paulis": self.tableau,
  29. "phases": {key: m[value] for key, value in list(self.phases.items())}}
  30. def __getitem__(self, xxx_todo_changeme):
  31. """" Pass straight through to the dictionary """
  32. (i, j) = xxx_todo_changeme
  33. return self.tableau[i][j]
  34. def __str__(self):
  35. """ Represent as a string """
  36. keys = list(map(str, list(self.tableau.keys())))
  37. w = max(len(k) for k in keys)
  38. keys = [k.ljust(w) for k in keys]
  39. s = " {}\n".format(" ".join(map(str, keys)))
  40. s += " " + "-"*len(keys)*(w+2) + "\n"
  41. for i in sorted(self.phases):
  42. sign = self.phases[i]
  43. sign = {1: " ", -1: " -", 1j: " i", -1j: "-i"}[sign]
  44. row = (self.tableau[i][j] for j in sorted(self.phases))
  45. row = (" XYZ"[i].ljust(w) for i in row)
  46. row = " ".join(row)
  47. s += "{} {}\n".format(sign, row)
  48. return s