Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_against_circuit_model.py 2.6KB

8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.choice(range(n), 2, False)
  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.choice(range(n), 2, False)
  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(
  49. np.sum(np.abs(g.to_state_vector().state) ** 2), 1)
  50. assert g.to_state_vector() == c
  51. assert g.to_state_vector() == c
  52. def test_all(n=8):
  53. """ A multi qubit test with arbitrary local rotations """
  54. g = GraphState(range(n))
  55. c = CircuitModel(n)
  56. for repeat in tqdm(xrange(REPEATS), "Testing against circuit model"):
  57. for step in xrange(DEPTH):
  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.choice(range(n), 2, False)
  65. g.act_cz(a, b)
  66. c.act_cz(a, b)
  67. assert g.to_state_vector() == c