Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

62 lines
1.8KB

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