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.

166 lines
5.3KB

  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 argparse
  11. import qi
  12. decompositions = ("xxxx", "xx", "zzxx", "zz", "zxx", "z", "zzz", "xxz",
  13. "xzx", "xzxxx", "xzzzx", "xxxzx", "xzz", "zzx", "xxx", "x",
  14. "zzzx", "xxzx", "zx", "zxxx", "xxxz", "xzzz", "xz", "xzxx")
  15. def get_name(i):
  16. """ Get the human-readable name of this clifford """
  17. return "IXYZ"[i & 0x03] + "ABCDEF"[i / 4]
  18. def find_clifford(needle, haystack):
  19. """ Find the index of a given u within a list of unitaries, up to a global phase """
  20. needle = qi.normalize_global_phase(needle)
  21. for i, t in enumerate(haystack):
  22. if np.allclose(t, needle):
  23. return i
  24. raise IndexError
  25. def find_cz(bond, c1, c2, commuters, state_table):
  26. """ Find the output of a CZ operation """
  27. # Figure out the target state
  28. target = qi.cz.dot(state_table[bond, c1, c2])
  29. target = qi.normalize_global_phase(target)
  30. # Choose the sets to search over
  31. s1 = commuters if c1 in commuters else xrange(24)
  32. s2 = commuters if c2 in commuters else xrange(24)
  33. # Find a match
  34. for bondp, c1p, c2p in it.product([0, 1], s1, s2):
  35. if np.allclose(target, state_table[bondp, c1p, c2p]):
  36. return bondp, c1p, c2p
  37. # Didn't find anything - this should never happen
  38. raise IndexError
  39. def compose_u(decomposition):
  40. """ Get the unitary representation of a particular decomposition """
  41. matrices = ({"x": qi.msqx, "z": qi.sqz}[c] for c in decomposition)
  42. output = reduce(np.dot, matrices, np.eye(2, dtype=complex))
  43. return qi.normalize_global_phase(output)
  44. def get_unitaries():
  45. """ The Clifford group """
  46. return [compose_u(d) for d in decompositions]
  47. def get_by_name(unitaries):
  48. """ Get a lookup table of cliffords by name """
  49. a = {name: find_clifford(u, unitaries)
  50. for name, u in qi.by_name.items()}
  51. b = {get_name(i): i for i in range(24)}
  52. a.update(b)
  53. return a
  54. def get_conjugation_table(unitaries):
  55. """ Construct the conjugation table """
  56. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries], dtype=int)
  57. def get_times_table(unitaries):
  58. """ Construct the times-table """
  59. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  60. for u in tqdm(unitaries, desc="Building times-table")], dtype=int)
  61. def get_state_table(unitaries):
  62. """ Cache a table of state to speed up a little bit """
  63. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  64. params = list(it.product([0, 1], range(24), range(24)))
  65. for bond, i, j in tqdm(params, desc="Building state table"):
  66. state = qi.bond if bond else qi.nobond
  67. kp = np.kron(unitaries[i], unitaries[j])
  68. state_table[bond, i, j, :] = qi.normalize_global_phase(
  69. np.dot(kp, state).T)
  70. return state_table
  71. def get_commuters(unitaries):
  72. """ Get the indeces of gates which commute with CZ """
  73. commuters = (qi.id, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  74. return [find_clifford(u, unitaries) for u in commuters]
  75. def get_cz_table(unitaries):
  76. """ Compute the lookup table for the CZ (A&B eq. 9) """
  77. # Get a cached state table and a list of gates which commute with CZ
  78. commuters = get_commuters(unitaries)
  79. state_table = get_state_table(unitaries)
  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)
  88. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  89. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  90. return cz_table
  91. # First try to load tables from cache. If that fails, build them from
  92. # scratch and store in /tmp/
  93. os.chdir(tempfile.gettempdir())
  94. try:
  95. if __name__ == "__main__":
  96. raise IOError
  97. # Parse command line args
  98. parser = argparse.ArgumentParser()
  99. parser.add_argument("-l", "--legacy", help="Use legacy CZ table", action="store_true", default=False)
  100. args = parser.parse_args()
  101. legacy_cz = args.legacy
  102. unitaries = np.load("unitaries.npy")
  103. conjugation_table = np.load("conjugation_table.npy")
  104. times_table = np.load("times_table.npy")
  105. if legacy_cz:
  106. import anders_cz
  107. cz_table = anders_cz.cz_table
  108. else:
  109. cz_table = np.load("cz_table.npy")
  110. with open("by_name.json") as f:
  111. by_name = json.load(f)
  112. print "Loaded tables from cache"
  113. except IOError:
  114. # Spend time building the tables
  115. unitaries = get_unitaries()
  116. by_name = get_by_name(unitaries)
  117. conjugation_table = get_conjugation_table(unitaries)
  118. times_table = get_times_table(unitaries)
  119. cz_table = get_cz_table(unitaries)
  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)