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.6KB

8 år sedan
8 år sedan
8 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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])