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.

69 lines
1.9KB

  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 = 100
  8. def test_single_qubit(n=1):
  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