Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_clifford.py 2.7KB

8 år sedan
8 år sedan
8 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from numpy import *
  2. from scipy.linalg import sqrtm
  3. from tqdm import tqdm
  4. import itertools as it
  5. from abp import clifford
  6. from abp import qi
  7. from nose.tools import raises
  8. def identify_pauli(m):
  9. """ Given a signed Pauli matrix, name it. """
  10. for sign in (+1, -1):
  11. for pauli_label, pauli in zip("xyz", qi.paulis):
  12. if allclose(sign * pauli, m):
  13. return sign, pauli_label
  14. def test_find_clifford():
  15. """ Test that slightly suspicious function """
  16. assert clifford.find_clifford(qi.id, clifford.unitaries) == 0
  17. assert clifford.find_clifford(qi.px, clifford.unitaries) == 1
  18. @raises(IndexError)
  19. def test_find_non_clifford():
  20. """ Test that looking for a non-Clifford gate fails """
  21. clifford.find_clifford(qi.t, clifford.unitaries)
  22. def get_action(u):
  23. """ What does this unitary operator do to the Paulis? """
  24. return [identify_pauli(u.dot(p.dot(qi.hermitian_conjugate(u)))) for p in qi.paulis]
  25. def format_action(action):
  26. return "".join("{}{}".format("+" if s >= 0 else "-", p) for s, p in action)
  27. def test_we_have_24_matrices():
  28. """ Check that we have 24 unique actions on the Bloch sphere """
  29. actions = set(tuple(get_action(u)) for u in clifford.unitaries)
  30. assert len(set(actions)) == 24
  31. def test_we_have_all_useful_gates():
  32. """ Check that all the interesting gates are included up to a global phase """
  33. for name, u in qi.by_name.items():
  34. clifford.find_clifford(u, clifford.unitaries)
  35. def test_group():
  36. """ Test we are really in a group """
  37. matches = set()
  38. for a, b in tqdm(it.combinations(clifford.unitaries, 2), "Testing this is a group"):
  39. i = clifford.find_clifford(a.dot(b), clifford.unitaries)
  40. matches.add(i)
  41. assert len(matches) == 24
  42. def test_conjugation_table():
  43. """ Check that the table of Hermitian conjugates is okay """
  44. assert len(set(clifford.conjugation_table)) == 24
  45. def test_times_table():
  46. """ Check the times table """
  47. assert clifford.times_table[0][4] == 4
  48. def _test_cz_table_is_symmetric():
  49. """ Test the CZ table is symmetric """
  50. for bond, (a, b) in it.product([0, 1], it.combinations(xrange(24), 2)):
  51. _, a1, a2 = clifford.cz_table[bond, a, b]
  52. _, b1, b2 = clifford.cz_table[bond, b, a]
  53. assert (a1, a2) == (b2, b1)
  54. def test_cz_table_makes_sense():
  55. """ Test the CZ table is symmetric """
  56. hadamard = clifford.by_name["hadamard"]
  57. assert all(clifford.cz_table[0, 0, 0] == [1, 0, 0])
  58. assert all(clifford.cz_table[1, 0, 0] == [0, 0, 0])
  59. assert all(
  60. clifford.cz_table[0, hadamard, hadamard] == [0, hadamard, hadamard])
  61. def test_commuters():
  62. """ Test that commutation is good """
  63. assert len(clifford.get_commuters(clifford.unitaries)) == 4