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.

213 lines
7.4KB

  1. # -*- coding: utf-8 -*-
  2. """
  3. This program generates lookup tables and handles the Clifford group
  4. """
  5. import os, json, tempfile, os, sys, json, string
  6. from functools import reduce
  7. import itertools as it
  8. import numpy as np
  9. from tqdm import tqdm
  10. import qi
  11. decompositions = ("xxxx", "xx", "zzxx", "zz", "zxx", "z", "zzz", "xxz",
  12. "xzx", "xzxxx", "xzzzx", "xxxzx", "xzz", "zzx", "xxx", "x",
  13. "zzzx", "xxzx", "zx", "zxxx", "xxxz", "xzzz", "xz", "xzxx")
  14. def conjugate(vop, transform):
  15. """ Returns transform * vop * transform^dagger and a phase in {+1, -1} """
  16. assert vop in set(xrange(4))
  17. op = times_table[transform, vop]
  18. op = times_table[op, conjugation_table[transform]]
  19. is_id_or_vop = (transform % 4 == 0) or (transform % 4 == vop)
  20. is_non_pauli = (transform >= 4) and (transform <= 15)
  21. phase = ((-1, 1), (1, -1))[is_id_or_vop][is_non_pauli]
  22. if vop == 0:
  23. phase = 1
  24. return op, phase
  25. def get_name(i):
  26. """ Get the human-readable name of this clifford """
  27. return "IXYZ"[i & 0x03] + "ABCDEF"[i / 4]
  28. def find_clifford(needle, haystack):
  29. """ Find the index of a given u within a list of unitaries, up to a global phase """
  30. needle = qi.normalize_global_phase(needle)
  31. for i, t in enumerate(haystack):
  32. if np.allclose(t, needle):
  33. return i
  34. raise IndexError
  35. def find_cz(bond, c1, c2, commuters, state_table):
  36. """ Find the output of a CZ operation """
  37. # Figure out the target state
  38. target = qi.cz.dot(state_table[bond, c1, c2])
  39. target = qi.normalize_global_phase(target)
  40. # Choose the sets to search over
  41. s1 = commuters if c1 in commuters else xrange(24)
  42. s2 = commuters if c2 in commuters else xrange(24)
  43. # Find a match
  44. for bondp, c1p, c2p in it.product([0, 1], s1, s2):
  45. if np.allclose(target, state_table[bondp, c1p, c2p]):
  46. return bondp, c1p, c2p
  47. # Didn't find anything - this should never happen
  48. raise IndexError
  49. def compose_u(decomposition):
  50. """ Get the unitary representation of a particular decomposition """
  51. matrices = ({"x": qi.msqx, "z": qi.sqz}[c] for c in decomposition)
  52. output = reduce(np.dot, matrices, np.eye(2, dtype=complex))
  53. return qi.normalize_global_phase(output)
  54. def get_unitaries():
  55. """ The Clifford group """
  56. return [compose_u(d) for d in decompositions]
  57. def get_by_name(unitaries):
  58. """ Get a lookup table of cliffords by name """
  59. a = {name: find_clifford(u, unitaries)
  60. for name, u in qi.by_name.items()}
  61. a.update({get_name(i): i for i in range(24)})
  62. a.update({i: i for i in range(24)})
  63. return a
  64. def get_conjugation_table(unitaries):
  65. """ Construct the conjugation table """
  66. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries], dtype=int)
  67. def get_times_table(unitaries):
  68. """ Construct the times-table """
  69. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  70. for u in tqdm(unitaries, desc="Building times-table")], dtype=int)
  71. def get_state_table(unitaries):
  72. """ Cache a table of state to speed up a little bit """
  73. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  74. params = list(it.product([0, 1], range(24), range(24)))
  75. for bond, i, j in tqdm(params, desc="Building state table"):
  76. state = qi.bond if bond else qi.nobond
  77. kp = np.kron(unitaries[i], unitaries[j])
  78. state_table[bond, i, j, :] = qi.normalize_global_phase(
  79. np.dot(kp, state).T)
  80. return state_table
  81. def get_measurement_table():
  82. """
  83. Compute a table of transform * operation * transform^dagger
  84. This is pretty unintelligible right now, we should probably compute the phase from unitaries instead
  85. """
  86. for operation_index, transform_index in itertools.product(range(4), range(24)):
  87. assert vop in set(xrange(4))
  88. op = times_table[transform, vop]
  89. op = times_table[op, conjugation_table[transform]]
  90. is_id_or_vop = (transform % 4 == 0) or (transform % 4 == vop)
  91. is_non_pauli = (transform >= 4) and (transform <= 15)
  92. phase = ((-1, 1), (1, -1))[is_id_or_vop][is_non_pauli]
  93. if vop == 0:
  94. phase = 1
  95. return op, phase
  96. def get_commuters(unitaries):
  97. """ Get the indeces of gates which commute with CZ """
  98. commuters = (qi.id, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  99. return [find_clifford(u, unitaries) for u in commuters]
  100. def get_cz_table(unitaries):
  101. """ Compute the lookup table for the CZ (A&B eq. 9) """
  102. # Get a cached state table and a list of gates which commute with CZ
  103. commuters = get_commuters(unitaries)
  104. state_table = get_state_table(unitaries)
  105. # And now build the CZ table
  106. cz_table = np.zeros((2, 24, 24, 3), dtype=int)
  107. rows = list(
  108. it.product([0, 1], it.combinations_with_replacement(range(24), 2)))
  109. # CZ is symmetric so we only need combinations
  110. for bond, (c1, c2) in tqdm(rows, desc="Building CZ table"):
  111. newbond, c1p, c2p = find_cz(
  112. bond, c1, c2, commuters, state_table)
  113. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  114. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  115. return cz_table
  116. def write_javascript_tables():
  117. """ Write the tables to javascript files for consumption in the browser """
  118. path = os.path.dirname(sys.argv[0])
  119. path = os.path.split(path)[0]
  120. with open(os.path.join(path, "static/scripts/tables.js"), "w") as f:
  121. f.write("var tables = {\n");
  122. f.write("\tdecompositions : {},\n"\
  123. .format(json.dumps(decompositions)))
  124. f.write("\tconjugation_table : {},\n"\
  125. .format(json.dumps(conjugation_table.tolist())))
  126. f.write("\ttimes_table : {},\n"\
  127. .format(json.dumps(times_table.tolist())))
  128. f.write("\tcz_table : {},\n"\
  129. .format(json.dumps(cz_table.tolist())))
  130. f.write("\tclifford : {}\n"\
  131. .format(json.dumps(by_name)))
  132. f.write("};");
  133. # First try to load tables from cache. If that fails, build them from
  134. # scratch and store in /tmp/
  135. tempdir = tempfile.gettempdir()
  136. temp = lambda filename: os.path.join(tempdir, filename)
  137. try:
  138. if __name__ == "__main__":
  139. raise IOError
  140. # Parse command line args
  141. # parser = argparse.ArgumentParser()
  142. # parser.add_argument("-l", "--legacy", help="Use legacy CZ table", action="store_true", default=False)
  143. # args = parser.parse_args()
  144. legacy_cz = False
  145. unitaries = np.load(temp("unitaries.npy"))
  146. conjugation_table = np.load(temp("conjugation_table.npy"))
  147. times_table = np.load(temp("times_table.npy"))
  148. if legacy_cz:
  149. import anders_cz
  150. cz_table = anders_cz.cz_table
  151. else:
  152. cz_table = np.load(temp("cz_table.npy"))
  153. with open(temp("by_name.json")) as f:
  154. by_name = json.load(f)
  155. except IOError:
  156. # Spend time building the tables
  157. unitaries = get_unitaries()
  158. by_name = get_by_name(unitaries)
  159. conjugation_table = get_conjugation_table(unitaries)
  160. times_table = get_times_table(unitaries)
  161. cz_table = get_cz_table(unitaries)
  162. # Write it all to disk
  163. np.save(temp("unitaries.npy"), unitaries)
  164. np.save(temp("conjugation_table.npy"), conjugation_table)
  165. np.save(temp("times_table.npy"), times_table)
  166. np.save(temp("cz_table.npy"), cz_table)
  167. write_javascript_tables()
  168. with open(temp("by_name.json"), "wb") as f:
  169. json.dump(by_name, f)