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.

76 lines
2.3KB

  1. from numpy import *
  2. from tqdm import tqdm
  3. import itertools as it
  4. from abp import clifford
  5. from abp import build_tables
  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 build_tables.find_clifford(qi.id, clifford.unitaries) == 0
  17. assert build_tables.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. build_tables.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. build_tables.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 = build_tables.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_cz_table_makes_sense():
  46. """ Test the CZ table is symmetric """
  47. hadamard = clifford.by_name["hadamard"]
  48. assert all(clifford.cz_table[0, 0, 0] == [1, 0, 0])
  49. assert all(clifford.cz_table[1, 0, 0] == [0, 0, 0])
  50. assert all(
  51. clifford.cz_table[0, hadamard, hadamard] == [0, hadamard, hadamard])
  52. def test_commuters():
  53. """ Test that commutation is good """
  54. assert len(build_tables.get_commuters(clifford.unitaries)) == 4