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.

149 lines
4.8KB

  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. decompositions = ("xxxx", "xx", "zzxx", "zz", "zxx", "z", "zzz", "xxz",
  14. "xzx", "xzxxx", "xzzzx", "xxxzx", "xzz", "zzx", "xxx", "x",
  15. "zzzx", "xxzx", "zx", "zxxx", "xxxz", "xzzz", "xz", "xzxx")
  16. ab_names = {0: "IA"}
  17. def find_clifford(needle, haystack):
  18. """ Find the index of a given u within a list of unitaries, up to a global phase """
  19. needle = normalize_global_phase(needle)
  20. for i, t in enumerate(haystack):
  21. if np.allclose(t, needle):
  22. return i
  23. raise IndexError
  24. def normalize_global_phase(m):
  25. """ Normalize the global phase of a matrix """
  26. v = (x for x in m.flatten() if np.abs(x) > 0.001).next()
  27. phase = np.arctan2(v.imag, v.real) % np.pi
  28. rot = np.exp(-1j * phase)
  29. return rot * m if rot * v > 0 else -rot * m
  30. def find_cz(bond, c1, c2, commuters, state_table):
  31. """ Find the output of a CZ operation """
  32. # Figure out the target state
  33. target = qi.cz.dot(state_table[bond, c1, c2])
  34. target = normalize_global_phase(target)
  35. # Choose the sets to search over
  36. s1 = commuters if c1 in commuters else xrange(24)
  37. s2 = commuters if c2 in commuters else xrange(24)
  38. # Find a match
  39. for bond, c1p, c2p in it.product([0, 1], s1, s2):
  40. if np.allclose(target, state_table[bond, c1p, c2p]):
  41. return bond, c1p, c2p
  42. # Didn't find anything - this should never happen
  43. raise IndexError
  44. def compose_u(decomposition):
  45. """ Get the unitary representation of a particular decomposition """
  46. matrices = ({"x": qi.sqx, "z": qi.msqz}[c] for c in decomposition)
  47. output = reduce(np.dot, matrices, np.eye(2, dtype=complex))
  48. return normalize_global_phase(output)
  49. def get_unitaries():
  50. """ The Clifford group """
  51. return [compose_u(d) for d in decompositions]
  52. def get_by_name(unitaries):
  53. """ Get a lookup table of cliffords by name """
  54. return {name: find_clifford(u, unitaries)
  55. for name, u in qi.by_name.items()}
  56. def get_conjugation_table(unitaries):
  57. """ Construct the conjugation table """
  58. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries], dtype=int)
  59. def get_times_table(unitaries):
  60. """ Construct the times-table """
  61. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  62. for u in tqdm(unitaries, desc="Building times-table")], dtype=int)
  63. def get_state_table(unitaries):
  64. """ Cache a table of state to speed up a little bit """
  65. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  66. params = list(it.product([0, 1], range(24), range(24)))
  67. for bond, i, j in tqdm(params, desc="Building state table"):
  68. state = qi.bond if bond else qi.nobond
  69. kp = np.kron(unitaries[i], unitaries[j])
  70. state_table[bond, i, j, :] = normalize_global_phase(
  71. np.dot(kp, state).T)
  72. return state_table
  73. def get_cz_table(unitaries):
  74. """ Compute the lookup table for the CZ (A&B eq. 9) """
  75. # This is the set of Cliffords which commute with CZ
  76. commuters = (qi.id, qi.px, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  77. commuters = [find_clifford(u, unitaries) for u in commuters]
  78. # Get a cached state table
  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(it.product([0, 1], it.combinations_with_replacement(range(24), 2)))
  83. # CZ is symmetric so we only need combinations
  84. for bond, (c1, c2) in tqdm(rows, desc="Building CZ table"):
  85. newbond, c1p, c2p = find_cz(bond, c1, c2, commuters, state_table)
  86. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  87. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  88. return cz_table
  89. # First try to load tables from cache. If that fails, build them from
  90. # scratch and store
  91. os.chdir(tempfile.gettempdir())
  92. try:
  93. unitaries = np.load("unitaries.npy")
  94. conjugation_table = np.load("conjugation_table.npy")
  95. times_table = np.load("times_table.npy")
  96. cz_table = np.load("cz_table.npy")
  97. with open("by_name.json") as f:
  98. by_name = json.load(f)
  99. print "Loaded tables from cache"
  100. except IOError:
  101. # Spend time building the tables
  102. unitaries = get_unitaries()
  103. by_name = get_by_name(unitaries)
  104. conjugation_table = get_conjugation_table(unitaries)
  105. times_table = get_times_table(unitaries)
  106. cz_table = get_cz_table(unitaries)
  107. # Write it all to disk
  108. np.save("unitaries.npy", unitaries)
  109. np.save("conjugation_table.npy", conjugation_table)
  110. np.save("times_table.npy", times_table)
  111. np.save("cz_table.npy", cz_table)
  112. with open("by_name.json", "wb") as f:
  113. json.dump(by_name, f)