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.

127 lines
4.1KB

  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. from clifford import decompositions
  14. def find_clifford(needle, haystack):
  15. """ Find the index of a given u within a list of unitaries, up to a global phase """
  16. needle = normalize_global_phase(needle)
  17. for i, t in enumerate(haystack):
  18. if np.allclose(t, needle):
  19. return i
  20. raise IndexError
  21. def normalize_global_phase(m):
  22. """ Normalize the global phase of a matrix """
  23. v = (x for x in m.flatten() if np.abs(x)>0.001).next()
  24. phase = np.arctan2(v.imag, v.real) % np.pi
  25. rot = np.exp(-1j*phase)
  26. return rot * m if rot * v > 0 else -rot*m
  27. def find_cz(bond, c1, c2, commuters, state_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 = 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 bond, c1p, c2p in it.product([0, 1], s1, s2):
  37. if np.allclose(target, state_table[bond, c1p, c2p]):
  38. return bond, 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 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])
  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")])
  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, :] = normalize_global_phase(np.dot(kp, state).T)
  68. return state_table
  69. def get_cz_table(unitaries):
  70. """ Compute the lookup table for the CZ (A&B eq. 9) """
  71. commuters = (qi.id, qi.px, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  72. commuters = [find_clifford(u, unitaries) for u in commuters]
  73. state_table = get_state_table(unitaries)
  74. # TODO: it's symmetric. this can be much faster
  75. cz_table = np.zeros((2, 24, 24, 3))
  76. rows = list(it.product([0, 1], it.combinations(range(24), 2)))
  77. for bond, (c1, c2) in tqdm(rows, desc="Building CZ table"):
  78. newbond, c1p, c2p = find_cz(bond, c1, c2, commuters, state_table)
  79. cz_table[bond, c1, c2] = [newbond, c1p, c2p]
  80. cz_table[bond, c2, c1] = [newbond, c2p, c1p]
  81. return cz_table
  82. if __name__ == "__main__":
  83. # Spend time loading the stuff
  84. unitaries = get_unitaries()
  85. by_name = get_by_name(unitaries)
  86. conjugation_table = get_conjugation_table(unitaries)
  87. times_table = get_times_table(unitaries)
  88. cz_table = get_cz_table(unitaries)
  89. # Write it all to disk
  90. where = tempfile.gettempdir()
  91. np.save("unitaries.npy", unitaries)
  92. np.save("conjugation_table.npy", conjugation_table)
  93. np.save("times_table.npy", times_table)
  94. np.save("cz_table.npy", cz_table)
  95. with open("by_name.json", "wb") as f:
  96. json.dump(by_name, f)