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.

72 lines
2.2KB

  1. import numpy as np
  2. from abp import GraphState
  3. from abp import qi, clifford
  4. import random
  5. import itertools as it
  6. def test_single_qubit_measurements():
  7. """ Various simple tests of measurements """
  8. # Test that measuring |0> in Z gives 0
  9. g = GraphState([0], vop="hadamard")
  10. assert g.measure(0, "pz") == 0, "Measuring |0> in Z gives 0"
  11. # Test that measuring |1> in Z gives 1
  12. g = GraphState([0], vop="hadamard")
  13. g.act_local_rotation(0, "px")
  14. assert g.measure(0, "pz") == 1, "Measuring |1> in Z gives 1"
  15. # Test that measuring |+> in X gives 0
  16. g = GraphState([0], vop="hadamard")
  17. g.act_local_rotation(0, "hadamard")
  18. assert g.measure(0, "px") == 0
  19. assert g.measure(0, "px") == 0, "Measuring |+> in X gives 0"
  20. g.act_local_rotation(0, "pz")
  21. assert g.measure(0, "px") == 1, "Measuring |-> in X gives 1"
  22. def test_type():
  23. """ Test that the output is always an int """
  24. for r, m, f in it.product(list(range(24)), ("px", "py", "pz"), (0, 1)):
  25. g = GraphState([0], vop="hadamard")
  26. g.act_local_rotation(0, r)
  27. assert str(g.measure(0, m)) in "01"
  28. assert str(g.measure(0, m, f)) in "01"
  29. assert g.measure(0, m, f, detail=True)["determinate"] == True
  30. def test_random_outcomes():
  31. """ Testing random behaviour """
  32. ones = 0
  33. for i in range(1000):
  34. g = GraphState([0], vop="hadamard")
  35. g.act_local_rotation(0, "hadamard")
  36. ones += g.measure(0, "pz")
  37. assert 400 < ones < 600, "This is a probabilistic test!"
  38. def test_projection():
  39. """ Test that projection works correctly """
  40. g = GraphState([0], vop="hadamard")
  41. g.act_local_rotation(0, "hadamard")
  42. g.measure(0, "pz", 0)
  43. assert np.allclose(g.to_state_vector().state, qi.zero)
  44. # Now project onto |1>
  45. g = GraphState([0], vop="hadamard")
  46. g.act_local_rotation(0, "hadamard")
  47. g.measure(0, "pz", 1)
  48. assert np.allclose(g.to_state_vector().state, qi.one)
  49. def test_measure_sequence():
  50. """ Simple test of measurement sequences """
  51. g = GraphState(2, vop="identity")
  52. g.act_cz(0, 1)
  53. assert g.measure_sequence(((0, "px"), (1, "px")), forces=(0, 1)) == [0, 1]
  54. assert len(g.edgelist()) == 0
  55. assert g.node[1]["vop"] == clifford.pz