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.

163 lines
5.3KB

  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 = qi.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 find_cz(bond, c1, c2, commuters, state_table, ab_cz_table):
  28. """ Find the output of a CZ operation """
  29. # Figure out the target state
  30. target = qi.cz.dot(state_table[bond, c1, c2])
  31. target = qi.normalize_global_phase(target)
  32. # Choose the sets to search over
  33. s1 = commuters if c1 in commuters else xrange(24)
  34. s2 = commuters if c2 in commuters else xrange(24)
  35. # Find a match
  36. for bondp, c1p, c2p in it.product([0, 1], s1, s2):
  37. if np.allclose(target, state_table[bondp, c1p, c2p]):
  38. return bondp, c1p, c2p
  39. # Didn't find anything - this should never happen
  40. raise IndexError
  41. def compose_u(decomposition):
  42. """ Get the unitary representation of a particular decomposition """
  43. matrices = ({"x": qi.sqx, "z": qi.msqz}[c] for c in decomposition)
  44. output = reduce(np.dot, matrices, np.eye(2, dtype=complex))
  45. return qi.normalize_global_phase(output)
  46. def get_unitaries():
  47. """ The Clifford group """
  48. return [compose_u(d) for d in decompositions]
  49. def get_by_name(unitaries):
  50. """ Get a lookup table of cliffords by name """
  51. return {name: find_clifford(u, unitaries)
  52. for name, u in qi.by_name.items()}
  53. def get_conjugation_table(unitaries):
  54. """ Construct the conjugation table """
  55. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries], dtype=int)
  56. def get_times_table(unitaries):
  57. """ Construct the times-table """
  58. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  59. for u in tqdm(unitaries, desc="Building times-table")], dtype=int)
  60. def get_state_table(unitaries):
  61. """ Cache a table of state to speed up a little bit """
  62. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  63. params = list(it.product([0, 1], range(24), range(24)))
  64. for bond, i, j in tqdm(params, desc="Building state table"):
  65. state = qi.bond if bond else qi.nobond
  66. kp = np.kron(unitaries[i], unitaries[j])
  67. state_table[bond, i, j, :] = qi.normalize_global_phase(
  68. np.dot(kp, state).T)
  69. return state_table
  70. def get_commuters(unitaries):
  71. """ Get the indeces of gates which commute with CZ """
  72. commuters = (qi.id, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  73. return [find_clifford(u, unitaries) for u in commuters]
  74. def get_cz_table(unitaries):
  75. """ Compute the lookup table for the CZ (A&B eq. 9) """
  76. # Get a cached state table and a list of gates which commute with CZ
  77. commuters = get_commuters(unitaries)
  78. state_table = get_state_table(unitaries)
  79. ab_cz_table = get_ab_cz_table()
  80. # And now build the CZ table
  81. cz_table = np.zeros((2, 24, 24, 3), dtype=int)
  82. rows = list(
  83. it.product([0, 1], it.combinations_with_replacement(range(24), 2)))
  84. # CZ is symmetric so we only need combinations
  85. for bond, (c1, c2) in tqdm(rows, desc="Building CZ table"):
  86. newbond, c1p, c2p = find_cz(
  87. bond, c1, c2, commuters, state_table, ab_cz_table)
  88. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  89. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  90. return cz_table
  91. def get_ab_cz_table():
  92. """ Load anders and briegel's CZ table """
  93. filename = "anders_briegel/cphase.tbl"
  94. filename = os.path.join(os.path.dirname(sys.path[0]), filename)
  95. with open(filename) as f:
  96. s = f.read().translate(string.maketrans("{}", "[]"))
  97. return np.array(json.loads(s))
  98. # First try to load tables from cache. If that fails, build them from
  99. # scratch and store
  100. os.chdir(tempfile.gettempdir())
  101. try:
  102. if __name__ == "__main__":
  103. raise IOError
  104. unitaries = np.load("unitaries.npy")
  105. conjugation_table = np.load("conjugation_table.npy")
  106. times_table = np.load("times_table.npy")
  107. cz_table = np.load("cz_table.npy")
  108. # cz_table = get_ab_cz_table()
  109. with open("by_name.json") as f:
  110. by_name = json.load(f)
  111. print "Loaded tables from cache"
  112. except IOError:
  113. # Spend time building the tables
  114. unitaries = get_unitaries()
  115. by_name = get_by_name(unitaries)
  116. conjugation_table = get_conjugation_table(unitaries)
  117. times_table = get_times_table(unitaries)
  118. cz_table = get_cz_table(unitaries)
  119. #cz_table = get_ab_cz_table()
  120. # Write it all to disk
  121. np.save("unitaries.npy", unitaries)
  122. np.save("conjugation_table.npy", conjugation_table)
  123. np.save("times_table.npy", times_table)
  124. np.save("cz_table.npy", cz_table)
  125. with open("by_name.json", "wb") as f:
  126. json.dump(by_name, f)