Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_against_circuit_model.py 2.0KB

8 年前
8 年前
8 年前
8 年前
8 年前
8 年前
8 年前
8 年前
8 年前
8 年前
8 年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from abp.graphstate import GraphState
  2. from abp.qi import CircuitModel
  3. from abp import clifford
  4. import numpy as np
  5. import random
  6. REPEATS = 10
  7. def test_single_qubit(n=1):
  8. """ A multi qubit test with Hadamards only"""
  9. for repeat in range(REPEATS):
  10. g = GraphState([0])
  11. c = CircuitModel(1)
  12. for i in range(100):
  13. op = random.randint(0, 23)
  14. g.act_local_rotation(0, op)
  15. c.act_local_rotation(0, clifford.unitaries[op])
  16. assert g.to_state_vector() == c
  17. def test_hadamard_only_multiqubit(n=6):
  18. """ A multi qubit test with Hadamards only"""
  19. for qqq in range(REPEATS):
  20. g = GraphState(range(n))
  21. c = CircuitModel(n)
  22. for i in range(n):
  23. g.act_hadamard(i)
  24. c.act_hadamard(i)
  25. assert g.to_state_vector() == c
  26. for i in range(100):
  27. a, b = np.random.randint(0, n - 1, 2)
  28. if a != b:
  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 i in range(100):
  43. a, b = np.random.randint(0, n-1, 2)
  44. if a != b:
  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(np.sum(np.abs(g.to_state_vector().state)**2), 1)
  49. if not g.to_state_vector() == c:
  50. print g
  51. print a, b
  52. print "Circuit:"
  53. print g.to_state_vector()
  54. print "Graph:"
  55. print c
  56. raise ValueError
  57. assert g.to_state_vector() == c