Anders and Briegel in Python
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

90 satır
2.7KB

  1. from abp import GraphState
  2. from abp import CircuitModel
  3. from abp import clifford
  4. import numpy as np
  5. import random
  6. from tqdm import tqdm
  7. REPEATS = 1
  8. def test_single_qubit():
  9. """ A multi qubit test with Hadamards only"""
  10. for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  11. g = GraphState([0])
  12. c = CircuitModel(1)
  13. for i in range(100):
  14. op = random.randint(0, 23)
  15. g.act_local_rotation(0, op)
  16. c.act_local_rotation(0, clifford.unitaries[op])
  17. assert g.to_state_vector() == c
  18. def test_hadamard_only_multiqubit(n=6):
  19. """ A multi qubit test with Hadamards only"""
  20. for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  21. g = GraphState(range(n))
  22. c = CircuitModel(n)
  23. for i in range(n):
  24. g.act_hadamard(i)
  25. c.act_hadamard(i)
  26. assert g.to_state_vector() == c
  27. for i in range(100):
  28. a, b = np.random.randint(0, n - 1, 2)
  29. if a != b:
  30. g.act_cz(a, b)
  31. c.act_cz(a, b)
  32. assert g.to_state_vector() == c
  33. def test_all_multiqubit(n=4):
  34. """ A multi qubit test with arbitrary local rotations """
  35. g = GraphState(range(n))
  36. c = CircuitModel(n)
  37. for i in range(10):
  38. qubit = np.random.randint(0, n - 1)
  39. rotation = np.random.randint(0, 24 - 1)
  40. g.act_local_rotation(qubit, rotation)
  41. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  42. assert g.to_state_vector() == c
  43. for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  44. a, b = np.random.randint(0, n - 1, 2)
  45. if a != b:
  46. g.act_cz(a, b)
  47. c.act_cz(a, b)
  48. assert np.allclose(np.sum(np.abs(c.state) ** 2), 1)
  49. assert np.allclose(
  50. np.sum(np.abs(g.to_state_vector().state) ** 2), 1)
  51. assert g.to_state_vector() == c
  52. assert g.to_state_vector() == c
  53. def test_all(n=4):
  54. """ A multi qubit test with arbitrary local rotations """
  55. g = GraphState(range(n))
  56. c = CircuitModel(n)
  57. depth = 100 # TODO: too small
  58. for step in tqdm(xrange(depth), "Testing a deep circuit against the circuit model"):
  59. if random.random()>0.5:
  60. qubit = np.random.randint(0, n - 1)
  61. rotation = np.random.randint(0, 24 - 1)
  62. g.act_local_rotation(qubit, rotation)
  63. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  64. else:
  65. a, b = np.random.randint(0, n - 1, 2)
  66. if a != b:
  67. g.act_cz(a, b)
  68. c.act_cz(a, b)
  69. assert g.to_state_vector() == c
  70. #print g.to_state_vector()
  71. #print c