Anders and Briegel in Python
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

56 linhas
1.5KB

  1. import numpy as np
  2. from abp import GraphState
  3. from abp import qi, clifford
  4. from anders_briegel import graphsim
  5. from tqdm import tqdm
  6. import random
  7. REPEATS = 100000
  8. LOCAL_ROTATION = 0
  9. CZ = 1
  10. MEASURE = 2
  11. def test_single_qubit_measurements():
  12. """ Various simple tests of measurements """
  13. # Test that measuring |0> in Z gives 0
  14. g = GraphState([0])
  15. assert g.measure(0, "pz") == 0, "Measuring |0> in Z gives 0"
  16. # Test that measuring |1> in Z gives 1
  17. g = GraphState([0])
  18. g.act_local_rotation(0, "px")
  19. assert g.measure(0, "pz") == 1, "Measuring |1> in Z gives 1"
  20. # Test that measuring |+> in X gives 0
  21. g = GraphState([0])
  22. g.act_local_rotation(0, "hadamard")
  23. assert g.measure(0, "px") == 0
  24. assert g.measure(0, "px") == 0, "Measuring |+> in X gives 0"
  25. g.act_local_rotation(0, "pz")
  26. assert g.measure(0, "px") == 1, "Measuring |-> in X gives 1"
  27. def test_random_outcomes():
  28. """ Testing random behaviour """
  29. ones = 0
  30. for i in range(1000):
  31. g = GraphState([0])
  32. g.act_local_rotation(0, "hadamard")
  33. ones += g.measure(0, "pz")
  34. assert 400 < ones < 600, "This is a probabilistic test!"
  35. def test_projection():
  36. """ Test that projection works correctly """
  37. g = GraphState([0])
  38. g.act_local_rotation(0, "hadamard")
  39. g.measure(0, "pz", 0)
  40. assert np.allclose(g.to_state_vector().state, qi.zero)
  41. # Now project onto |1>
  42. g = GraphState([0])
  43. g.act_local_rotation(0, "hadamard")
  44. g.measure(0, "pz", 1)
  45. assert np.allclose(g.to_state_vector().state, qi.one)