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.

45 lines
1.4KB

  1. import numpy as np
  2. from tqdm import tqdm
  3. import os
  4. from qi import *
  5. import cPickle
  6. def find_up_to_phase(u):
  7. """ Find the index of a given u within a list of unitaries, up to a global phase """
  8. global unitaries
  9. for i, t in enumerate(unitaries):
  10. for phase in range(8):
  11. if np.allclose(t, np.exp(1j*phase*np.pi/4.)*u):
  12. return i, phase
  13. raise IndexError
  14. def compose_u(decomposition):
  15. """ Get the unitary representation of a particular decomposition """
  16. us = (elements[c] for c in decomposition)
  17. return np.matrix(reduce(np.dot, us), dtype=complex)
  18. def construct_tables():
  19. """ Constructs multiplication and conjugation tables """
  20. conjugation_table = [find_up_to_phase(u.H)[0] for i, u in enumerate(unitaries)]
  21. times_table = [[find_up_to_phase(u*v)[0] for v in unitaries]
  22. for u in tqdm(unitaries, "Building times-table")]
  23. with open("tables.pkl", "w") as f:
  24. cPickle.dump((conjugation_table, times_table), f)
  25. decompositions = \
  26. ("xxxx", "xx", "zzxx", "zz", "zxx", "z", "zzz", "xxz",
  27. "xzx", "xzxxx", "xzzzx", "xxxzx", "xzz", "zzx", "xxx", "x",
  28. "zzzx", "xxzx", "zx", "zxxx", "xxxz", "xzzz", "xz", "xzxx")
  29. elements = {"x": sqx, "z": msqz}
  30. unitaries = [compose_u(d) for d in decompositions]
  31. # Build / reload lookup tables
  32. if not os.path.exists("tables.pkl"):
  33. construct_tables()
  34. if __name__ == '__main__':
  35. pass