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.

120 lines
3.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. from tqdm import tqdm
  12. from clifford import decompositions
  13. def find_clifford(needle, haystack):
  14. """ Find the index of a given u within a list of unitaries, up to a global phase """
  15. for i, t in enumerate(haystack):
  16. for phase in range(8):
  17. if np.allclose(t, np.exp(1j * phase * np.pi / 4.) * needle):
  18. return i
  19. raise IndexError
  20. def find_cz(bond, c1, c2, commuters, state_table):
  21. """ Find the output of a CZ operation """
  22. # Figure out the target state
  23. state = qi.bond if bond else qi.nobond
  24. target = qi.cz.dot(state_table[bond, c1, c2])
  25. # Choose the sets to search over
  26. s1 = commuters if c1 in commuters else xrange(24)
  27. s2 = commuters if c2 in commuters else xrange(24)
  28. # Find a match
  29. for bond, c1p, c2p in it.product([0, 1], s1, s2):
  30. trial = state_table[bond, c1p, c2p]
  31. for phase in range(8):
  32. if np.allclose(target, np.exp(1j * phase * np.pi / 4.) * trial):
  33. return bond, c1p, c2p
  34. # Didn't find anything - this should never happen
  35. raise IndexError
  36. def compose_u(decomposition):
  37. """ Get the unitary representation of a particular decomposition """
  38. matrices = ({"x": qi.sqx, "z": qi.msqz}[c] for c in decomposition)
  39. return reduce(np.dot, matrices, np.matrix(np.eye(2, dtype=complex)))
  40. def get_unitaries():
  41. """ The Clifford group """
  42. return [compose_u(d) for d in decompositions]
  43. def get_by_name(unitaries):
  44. """ Get a lookup table of cliffords by name """
  45. return {name: find_clifford(u, unitaries)
  46. for name, u in qi.by_name.items()}
  47. def get_conjugation_table(unitaries):
  48. """ Construct the conjugation table """
  49. return np.array([find_clifford(qi.hermitian_conjugate(u), unitaries) for u in unitaries])
  50. def get_times_table(unitaries):
  51. """ Construct the times-table """
  52. return np.array([[find_clifford(u.dot(v), unitaries) for v in unitaries]
  53. for u in tqdm(unitaries, desc="Building times-table")])
  54. def get_state_table(unitaries):
  55. """ Cache a table of state to speed up a little bit """
  56. state_table = np.zeros((2, 24, 24, 4), dtype=complex)
  57. params = list(it.product([0, 1], range(24), range(24)))
  58. for bond, i, j in tqdm(params, desc="Building state table"):
  59. state = qi.bond if bond else qi.nobond
  60. kp = np.kron(unitaries[i], unitaries[j])
  61. state_table[bond, i, j, :] = np.dot(kp, state).T
  62. return state_table
  63. def get_cz_table(unitaries):
  64. """ Compute the lookup table for the CZ (A&B eq. 9) """
  65. commuters = (qi.id, qi.px, qi.pz, qi.ph, qi.hermitian_conjugate(qi.ph))
  66. commuters = [find_clifford(u, unitaries) for u in commuters]
  67. state_table = get_state_table(unitaries)
  68. # TODO: it's symmetric. this can be much faster
  69. cz_table = np.zeros((2, 24, 24, 3))
  70. rows = list(it.product([0, 1], range(24), range(24)))
  71. for bond, c1, c2 in tqdm(rows, desc="Building CZ table"):
  72. cz_table[bond, c1, c2] = find_cz(bond, c1, c2, commuters, state_table)
  73. return cz_table
  74. if __name__ == "__main__":
  75. # Spend time loading the stuff
  76. unitaries = get_unitaries()
  77. by_name = get_by_name(unitaries)
  78. conjugation_table = get_conjugation_table(unitaries)
  79. times_table = get_times_table(unitaries)
  80. #cz_table = get_cz_table(unitaries)
  81. # Write it all to disk
  82. directory = os.path.dirname(os.path.abspath(__file__))
  83. where = os.path.join(directory, "tables/")
  84. os.chdir(where)
  85. np.save("unitaries.npy", unitaries)
  86. np.save("conjugation_table.npy", conjugation_table)
  87. np.save("times_table.npy", times_table)
  88. #np.save("cz_table.npy", cz_table)
  89. with open("by_name.json", "wb") as f:
  90. json.dump(by_name, f)