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.

171 lines
5.5KB

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This program generates lookup tables
  5. """
  6. import os, json
  7. from functools import reduce
  8. import itertools as it
  9. import qi
  10. import numpy as np
  11. import tempfile
  12. from tqdm import tqdm
  13. import os, sys, json, string
  14. decompositions = ("xxxx", "xx", "zzxx", "zz", "zxx", "z", "zzz", "xxz",
  15. "xzx", "xzxxx", "xzzzx", "xxxzx", "xzz", "zzx", "xxx", "x",
  16. "zzzx", "xxzx", "zx", "zxxx", "xxxz", "xzzz", "xz", "xzxx")
  17. def get_name(i):
  18. """ Get the human-readable name of this clifford """
  19. return "IXYZ"[i & 0x03] + "ABCDEF"[i / 4]
  20. def find_clifford(needle, haystack):
  21. """ Find the index of a given u within a list of unitaries, up to a global phase """
  22. needle = normalize_global_phase(needle)
  23. for i, t in enumerate(haystack):
  24. if np.allclose(t, needle):
  25. return i
  26. raise IndexError
  27. def normalize_global_phase(m):
  28. """ Normalize the global phase of a matrix """
  29. v = (x for x in m.flatten() if np.abs(x) > 0.001).next()
  30. phase = np.arctan2(v.imag, v.real) % np.pi
  31. rot = np.exp(-1j * phase)
  32. return rot * m if rot * v > 0 else -rot * m
  33. def find_cz(bond, c1, c2, commuters, state_table, ab_cz_table):
  34. """ Find the output of a CZ operation """
  35. # Figure out the target state
  36. target = qi.cz.dot(state_table[bond, c1, c2])
  37. target = normalize_global_phase(target)
  38. # Choose the sets to search over
  39. s1 = commuters if c1 in commuters else xrange(24)
  40. s2 = commuters if c2 in commuters else xrange(24)
  41. # Find a match
  42. for bondp, c1p, c2p in it.product([0, 1], s1, s2):
  43. if np.allclose(target, state_table[bondp, c1p, c2p]):
  44. return bondp, c1p, c2p
  45. # Didn't find anything - this should never happen
  46. raise IndexError
  47. def compose_u(decomposition):
  48. """ Get the unitary representation of a particular decomposition """
  49. matrices = ({"x": qi.sqx, "z": qi.msqz}[c] for c in decomposition)
  50. output = reduce(np.dot, matrices, np.eye(2, dtype=complex))
  51. return normalize_global_phase(output)
  52. def get_unitaries():
  53. """ The Clifford group """
  54. return [compose_u(d) for d in decompositions]
  55. def get_by_name(unitaries):
  56. """ Get a lookup table of cliffords by name """
  57. return {name: find_clifford(u, unitaries)
  58. for name, u in qi.by_name.items()}
  59. def get_conjugation_table(unitaries):
  60. """ Construct the conjugation table """
  61. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries], dtype=int)
  62. def get_times_table(unitaries):
  63. """ Construct the times-table """
  64. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  65. for u in tqdm(unitaries, desc="Building times-table")], dtype=int)
  66. def get_state_table(unitaries):
  67. """ Cache a table of state to speed up a little bit """
  68. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  69. params = list(it.product([0, 1], range(24), range(24)))
  70. for bond, i, j in tqdm(params, desc="Building state table"):
  71. state = qi.bond if bond else qi.nobond
  72. kp = np.kron(unitaries[i], unitaries[j])
  73. state_table[bond, i, j, :] = normalize_global_phase(
  74. np.dot(kp, state).T)
  75. return state_table
  76. def get_commuters(unitaries):
  77. """ Get the indeces of gates which commute with CZ """
  78. commuters = (qi.id, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  79. return [find_clifford(u, unitaries) for u in commuters]
  80. def get_cz_table(unitaries):
  81. """ Compute the lookup table for the CZ (A&B eq. 9) """
  82. # Get a cached state table and a list of gates which commute with CZ
  83. commuters = get_commuters(unitaries)
  84. state_table = get_state_table(unitaries)
  85. ab_cz_table = get_ab_cz_table()
  86. # And now build the CZ table
  87. cz_table = np.zeros((2, 24, 24, 3), dtype=int)
  88. rows = list(
  89. it.product([0, 1], it.combinations_with_replacement(range(24), 2)))
  90. # CZ is symmetric so we only need combinations
  91. for bond, (c1, c2) in tqdm(rows, desc="Building CZ table"):
  92. newbond, c1p, c2p = find_cz(
  93. bond, c1, c2, commuters, state_table, ab_cz_table)
  94. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  95. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  96. return cz_table
  97. def get_ab_cz_table():
  98. """ Load anders and briegel's CZ table """
  99. filename = "anders_briegel/cphase.tbl"
  100. filename = os.path.join(os.path.dirname(sys.path[0]), filename)
  101. with open(filename) as f:
  102. s = f.read().translate(string.maketrans("{}", "[]"))
  103. return np.array(json.loads(s))
  104. # First try to load tables from cache. If that fails, build them from
  105. # scratch and store
  106. os.chdir(tempfile.gettempdir())
  107. try:
  108. if __name__ == "__main__":
  109. raise IOError
  110. unitaries = np.load("unitaries.npy")
  111. conjugation_table = np.load("conjugation_table.npy")
  112. times_table = np.load("times_table.npy")
  113. cz_table = np.load("cz_table.npy")
  114. # cz_table = get_ab_cz_table()
  115. with open("by_name.json") as f:
  116. by_name = json.load(f)
  117. print "Loaded tables from cache"
  118. except IOError:
  119. # Spend time building the tables
  120. unitaries = get_unitaries()
  121. by_name = get_by_name(unitaries)
  122. conjugation_table = get_conjugation_table(unitaries)
  123. times_table = get_times_table(unitaries)
  124. cz_table = get_cz_table(unitaries)
  125. # Write it all to disk
  126. np.save("unitaries.npy", unitaries)
  127. np.save("conjugation_table.npy", conjugation_table)
  128. np.save("times_table.npy", times_table)
  129. np.save("cz_table.npy", cz_table)
  130. with open("by_name.json", "wb") as f:
  131. json.dump(by_name, f)