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.

161 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. 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. #{"UUUU", "UU", "VVUU", "VV", #"VUU", "V", "VVV", "UUV",
  17. #"UVU", "UVUUU", "UVVVU", "UUUVU", #"UVV", "VVU", "UUU", "U",
  18. #"VVVU", "UUVU", "VU", "VUUU", #"UUUV", "UVVV", "UV", "UVUU"};
  19. #string LocCliffOp::get_name (void) const
  20. #{
  21. #static const char* paulinames[] = {"I", "X", "Y", "Z"};
  22. #return string (paulinames[op & 0x03]) + (char) ('A' + op / 4);
  23. #}
  24. def get_name(i):
  25. """ Get the human-readable name of this clifford """
  26. return "IXYZ"[i & 0x03] + "ABCDEF"[i / 4]
  27. def find_clifford(needle, haystack):
  28. """ Find the index of a given u within a list of unitaries, up to a global phase """
  29. needle = normalize_global_phase(needle)
  30. for i, t in enumerate(haystack):
  31. if np.allclose(t, needle):
  32. return i
  33. raise IndexError
  34. def normalize_global_phase(m):
  35. """ Normalize the global phase of a matrix """
  36. v = (x for x in m.flatten() if np.abs(x) > 0.001).next()
  37. phase = np.arctan2(v.imag, v.real) % np.pi
  38. rot = np.exp(-1j * phase)
  39. return rot * m if rot * v > 0 else -rot * m
  40. def find_cz(bond, c1, c2, commuters, state_table):
  41. """ Find the output of a CZ operation """
  42. # Figure out the target state
  43. target = qi.cz.dot(state_table[bond, c1, c2])
  44. target = normalize_global_phase(target)
  45. # Choose the sets to search over
  46. s1 = commuters if c1 in commuters else xrange(24)
  47. s2 = commuters if c2 in commuters else xrange(24)
  48. # Find a match
  49. for bond, c1p, c2p in it.product([0, 1], s1, s2):
  50. if np.allclose(target, state_table[bond, c1p, c2p]):
  51. return bond, c1p, c2p
  52. # Didn't find anything - this should never happen
  53. raise IndexError
  54. def compose_u(decomposition):
  55. """ Get the unitary representation of a particular decomposition """
  56. matrices = ({"x": qi.sqx, "z": qi.msqz}[c] for c in decomposition)
  57. output = reduce(np.dot, matrices, np.eye(2, dtype=complex))
  58. return normalize_global_phase(output)
  59. def get_unitaries():
  60. """ The Clifford group """
  61. return [compose_u(d) for d in decompositions]
  62. def get_by_name(unitaries):
  63. """ Get a lookup table of cliffords by name """
  64. return {name: find_clifford(u, unitaries)
  65. for name, u in qi.by_name.items()}
  66. def get_conjugation_table(unitaries):
  67. """ Construct the conjugation table """
  68. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries], dtype=int)
  69. def get_times_table(unitaries):
  70. """ Construct the times-table """
  71. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  72. for u in tqdm(unitaries, desc="Building times-table")], dtype=int)
  73. def get_state_table(unitaries):
  74. """ Cache a table of state to speed up a little bit """
  75. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  76. params = list(it.product([0, 1], range(24), range(24)))
  77. for bond, i, j in tqdm(params, desc="Building state table"):
  78. state = qi.bond if bond else qi.nobond
  79. kp = np.kron(unitaries[i], unitaries[j])
  80. state_table[bond, i, j, :] = normalize_global_phase(
  81. np.dot(kp, state).T)
  82. return state_table
  83. def get_cz_table(unitaries):
  84. """ Compute the lookup table for the CZ (A&B eq. 9) """
  85. # This is the set of Cliffords which commute with CZ
  86. commuters = (qi.id, qi.px, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  87. commuters = [find_clifford(u, unitaries) for u in commuters]
  88. # Get a cached state table
  89. state_table = get_state_table(unitaries)
  90. # And now build the CZ table
  91. cz_table = np.zeros((2, 24, 24, 3), dtype=int)
  92. rows = list(it.product([0, 1], it.combinations_with_replacement(range(24), 2)))
  93. # CZ is symmetric so we only need combinations
  94. for bond, (c1, c2) in tqdm(rows, desc="Building CZ table"):
  95. newbond, c1p, c2p = find_cz(bond, c1, c2, commuters, state_table)
  96. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  97. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  98. return cz_table
  99. # First try to load tables from cache. If that fails, build them from
  100. # scratch and store
  101. os.chdir(tempfile.gettempdir())
  102. try:
  103. unitaries = np.load("unitaries.npy")
  104. conjugation_table = np.load("conjugation_table.npy")
  105. times_table = np.load("times_table.npy")
  106. cz_table = np.load("cz_table.npy")
  107. with open("by_name.json") as f:
  108. by_name = json.load(f)
  109. print "Loaded tables from cache"
  110. except IOError:
  111. # Spend time building the tables
  112. unitaries = get_unitaries()
  113. by_name = get_by_name(unitaries)
  114. conjugation_table = get_conjugation_table(unitaries)
  115. times_table = get_times_table(unitaries)
  116. cz_table = get_cz_table(unitaries)
  117. # Write it all to disk
  118. np.save("unitaries.npy", unitaries)
  119. np.save("conjugation_table.npy", conjugation_table)
  120. np.save("times_table.npy", times_table)
  121. np.save("cz_table.npy", cz_table)
  122. with open("by_name.json", "wb") as f:
  123. json.dump(by_name, f)