Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test_measurement.py 1.9KB

8年前
8年前
8年前
8年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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], vop="hadamard")
  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], vop="hadamard")
  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], vop="hadamard")
  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], vop="hadamard")
  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], vop="hadamard")
  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], vop="hadamard")
  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], vop="hadamard")
  47. g.act_local_rotation(0, "hadamard")
  48. g.measure(0, "pz", 1)
  49. assert np.allclose(g.to_state_vector().state, qi.one)