Anders and Briegel in Python
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

84 строки
2.6KB

  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. from config import *
  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.choice(range(n), 2, False)
  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 repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  43. a, b = np.random.choice(range(n), 2, False)
  44. g.act_cz(a, b)
  45. c.act_cz(a, b)
  46. assert np.allclose(np.sum(np.abs(c.state) ** 2), 1)
  47. assert np.allclose(
  48. np.sum(np.abs(g.to_state_vector().state) ** 2), 1)
  49. assert g.to_state_vector() == c
  50. assert g.to_state_vector() == c
  51. def test_all(n=8):
  52. """ A multi qubit test with arbitrary local rotations """
  53. g = GraphState(range(n))
  54. c = CircuitModel(n)
  55. for repeat in tqdm(xrange(REPEATS), "Testing against circuit model"):
  56. for step in xrange(DEPTH):
  57. if random.random()>0.5:
  58. qubit = np.random.randint(0, n - 1)
  59. rotation = np.random.randint(0, 24 - 1)
  60. g.act_local_rotation(qubit, rotation)
  61. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  62. else:
  63. a, b = np.random.choice(range(n), 2, False)
  64. g.act_cz(a, b)
  65. c.act_cz(a, b)
  66. assert g.to_state_vector() == c