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.

61 lines
1.7KB

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