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.

test_clifford.py 3.7KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import numpy as np
  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. from anders_briegel import graphsim
  9. def identify_pauli(m):
  10. """ Given a signed Pauli matrix, name it. """
  11. for sign in (+1, -1):
  12. for pauli_label, pauli in zip("xyz", qi.paulis):
  13. if np.allclose(sign * pauli, m):
  14. return sign, pauli_label
  15. def test_find_clifford():
  16. """ Test that slightly suspicious function """
  17. assert build_tables.find_clifford(qi.id, clifford.unitaries) == 0
  18. assert build_tables.find_clifford(qi.px, clifford.unitaries) == 1
  19. @raises(IndexError)
  20. def test_find_non_clifford():
  21. """ Test that looking for a non-Clifford gate fails """
  22. build_tables.find_clifford(qi.t, clifford.unitaries)
  23. def get_action(u):
  24. """ What does this unitary operator do to the Paulis? """
  25. return [identify_pauli(u.dot(p.dot(qi.hermitian_conjugate(u)))) for p in qi.paulis]
  26. def format_action(action):
  27. return "".join("{}{}".format("+" if s >= 0 else "-", p) for s, p in action)
  28. def test_we_have_24_matrices():
  29. """ Check that we have 24 unique actions on the Bloch sphere """
  30. actions = set(tuple(get_action(u)) for u in clifford.unitaries)
  31. assert len(set(actions)) == 24
  32. def test_we_have_all_useful_gates():
  33. """ Check that all the interesting gates are included up to a global phase """
  34. for name, u in qi.by_name.items():
  35. build_tables.find_clifford(u, clifford.unitaries)
  36. def test_group():
  37. """ Test we are really in a group """
  38. matches = set()
  39. for a, b in tqdm(it.combinations(clifford.unitaries, 2), "Testing this is a group"):
  40. i = build_tables.find_clifford(a.dot(b), clifford.unitaries)
  41. matches.add(i)
  42. assert len(matches) == 24
  43. def test_conjugation_table():
  44. """ Check that the table of Hermitian conjugates is okay """
  45. assert len(set(clifford.conjugation_table)) == 24
  46. def test_cz_table_makes_sense():
  47. """ Test the CZ table is symmetric """
  48. hadamard = clifford.by_name["hadamard"]
  49. assert all(clifford.cz_table[0, 0, 0] == [1, 0, 0])
  50. assert all(clifford.cz_table[1, 0, 0] == [0, 0, 0])
  51. assert all(
  52. clifford.cz_table[0, hadamard, hadamard] == [0, hadamard, hadamard])
  53. def test_commuters():
  54. """ Test that commutation is good """
  55. assert len(build_tables.get_commuters(clifford.unitaries)) == 4
  56. def test_conjugation():
  57. """ Test that clifford.conugate() agrees with graphsim.LocCliffOp.conjugate """
  58. for operation_index, transform_index in it.product(range(4), range(24)):
  59. transform = graphsim.LocCliffOp(transform_index)
  60. operation = graphsim.LocCliffOp(operation_index)
  61. phase = operation.conjugate(transform).ph
  62. phase = [1, 0, -1][phase]
  63. new_operation = operation.op
  64. NEW_OPERATION, PHASE = clifford.conjugate(
  65. operation_index, transform_index)
  66. assert new_operation == NEW_OPERATION
  67. assert PHASE == phase
  68. def test_cz_table():
  69. """ Does the CZ code work good? """
  70. state_table = build_tables.get_state_table(clifford.unitaries)
  71. rows = it.product([0, 1], it.combinations_with_replacement(range(24), 2))
  72. for bond, (c1, c2) in rows:
  73. # Pick the input state
  74. input_state = state_table[bond, c1, c2]
  75. # Go and compute the output
  76. computed_output = np.dot(qi.cz, input_state)
  77. computed_output = qi.normalize_global_phase(computed_output)
  78. # Now look up the answer in the table
  79. bondp, c1p, c2p = clifford.cz_table[bond, c1, c2]
  80. table_output = state_table[bondp, c1p, c2p]
  81. assert np.allclose(computed_output, table_output)