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.

63 linhas
1.9KB

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