Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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