Anders and Briegel in Python
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0KB

  1. from abp.graphstate import GraphState
  2. from abp.qi import CircuitModel
  3. from abp import clifford
  4. import numpy as np
  5. import random
  6. REPEATS = 10
  7. def test_single_qubit(n=1):
  8. """ A multi qubit test with Hadamards only"""
  9. for repeat in range(REPEATS):
  10. g = GraphState([0])
  11. c = CircuitModel(1)
  12. for i in range(100):
  13. op = random.randint(0, 23)
  14. g.act_local_rotation(0, op)
  15. c.act_local_rotation(0, clifford.unitaries[op])
  16. assert g.to_state_vector() == c
  17. def test_hadamard_only_multiqubit(n=6):
  18. """ A multi qubit test with Hadamards only"""
  19. for qqq in range(REPEATS):
  20. g = GraphState(range(n))
  21. c = CircuitModel(n)
  22. for i in range(n):
  23. g.act_hadamard(i)
  24. c.act_hadamard(i)
  25. assert g.to_state_vector() == c
  26. for i in range(100):
  27. a, b = np.random.randint(0, n - 1, 2)
  28. if a != b:
  29. g.act_cz(a, b)
  30. c.act_cz(a, b)
  31. assert g.to_state_vector() == c
  32. def test_all_multiqubit(n=4):
  33. """ A multi qubit test with arbitrary local rotations """
  34. g = GraphState(range(n))
  35. c = CircuitModel(n)
  36. for i in range(10):
  37. qubit = np.random.randint(0, n - 1)
  38. rotation = np.random.randint(0, 24 - 1)
  39. g.act_local_rotation(qubit, rotation)
  40. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  41. assert g.to_state_vector() == c
  42. for i in range(100):
  43. a, b = np.random.randint(0, n-1, 2)
  44. if a != b:
  45. g.act_cz(a, b)
  46. c.act_cz(a, b)
  47. assert np.allclose(np.sum(np.abs(c.state)**2), 1)
  48. assert np.allclose(np.sum(np.abs(g.to_state_vector().state)**2), 1)
  49. if not g.to_state_vector() == c:
  50. print g
  51. print a, b
  52. print "Circuit:"
  53. print g.to_state_vector()
  54. print "Graph:"
  55. print c
  56. raise ValueError
  57. assert g.to_state_vector() == c