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.

95 linhas
2.8KB

  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. REPEATS = 100000
  8. LOCAL_ROTATION = 0
  9. CZ = 1
  10. MEASURE = 2
  11. def test_single_qubit_measurements():
  12. """ Various simple tests of measurements """
  13. # Test that measuring |0> in Z gives 0
  14. g = GraphState([0])
  15. assert g.measure(0, "pz") == 0, "Measuring |0> in Z gives 0"
  16. # Test that measuring |1> in Z gives 1
  17. g = GraphState([0])
  18. g.act_local_rotation(0, "px")
  19. assert g.measure(0, "pz") == 1, "Measuring |1> in Z gives 1"
  20. # Test that measuring |+> in X gives 0
  21. g = GraphState([0])
  22. g.act_local_rotation(0, "hadamard")
  23. assert g.measure(0, "px") == 0
  24. assert g.measure(0, "px") == 0, "Measuring |+> in X gives 0"
  25. g.act_local_rotation(0, "pz")
  26. assert g.measure(0, "px") == 1, "Measuring |-> in X gives 1"
  27. def test_random_outcomes():
  28. """ Testing random behaviour """
  29. ones = 0
  30. for i in range(1000):
  31. g = GraphState([0])
  32. g.act_local_rotation(0, "hadamard")
  33. ones += g.measure(0, "pz")
  34. assert 400 < ones < 600, "This is a probabilistic test!"
  35. def test_projection():
  36. """ Test that projection works correctly """
  37. g = GraphState([0])
  38. g.act_local_rotation(0, "hadamard")
  39. g.measure(0, "pz", 0)
  40. assert np.allclose(g.to_state_vector().state, qi.zero)
  41. def test_another_projection():
  42. """ This one fails at the moment """
  43. g = GraphState([0])
  44. g.act_local_rotation(0, "hadamard")
  45. g.measure(0, "pz", 1)
  46. assert np.allclose(g.to_state_vector().state, qi.one)
  47. def test_z_measurement_against_ab():
  48. for i in range(10):
  49. a = graphsim.GraphRegister(1)
  50. b = GraphState()
  51. b.add_node(0)
  52. #print a.measure(0, graphsim.lco_Z)
  53. #print b.measure(0, "pz")
  54. def test_all(N=20):
  55. """ Test everything"""
  56. #clifford.use_old_cz()
  57. #a = graphsim.GraphRegister(N)
  58. #b = GraphState(range(N))
  59. #previous_state, previous_cz = None, None
  60. #for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"):
  61. #which = random.choice([LOCAL_ROTATION, CZ, MEASURE])
  62. #if which == LOCAL_ROTATION:
  63. #j = random.randint(0, N-1)
  64. #u = random.randint(0, 23)
  65. #a.local_op(j, graphsim.LocCliffOp(u))
  66. #b.act_local_rotation(j, u)
  67. #elif which == CZ:
  68. #q = random.randint(0, N-2)
  69. #if a!=b:
  70. #a.cphase(q, q+1)
  71. #b.act_cz(q, q+1)
  72. #else:
  73. #q = random.randint(0, N-2)
  74. #m = random.choice([1,2,3])
  75. #force = random.choice([0, 1])
  76. #thing=3
  77. #ma = a.measure(q, graphsim.LocCliffOp(m))
  78. #mb = b.measure(q, str(m), force)
  79. #print ma, mb
  80. #assert ma == mb, i