Anders and Briegel in Python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

89 Zeilen
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. 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. for step in tqdm(xrange(1000), "Testing a deep circuit against the circuit model"):
  58. if random.random()>0.5:
  59. qubit = np.random.randint(0, n - 1)
  60. rotation = np.random.randint(0, 24 - 1)
  61. g.act_local_rotation(qubit, rotation)
  62. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  63. else:
  64. a, b = np.random.randint(0, n - 1, 2)
  65. if a != b:
  66. g.act_cz(a, b)
  67. c.act_cz(a, b)
  68. assert g.to_state_vector() == c
  69. print g.to_state_vector()
  70. print c