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 1.6KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_hadamard_only_multiqubit(n=6):
  8. """ A multi qubit test with Hadamards only"""
  9. for qqq in range(REPEATS):
  10. g = GraphState(range(n))
  11. c = CircuitModel(n)
  12. for i in range(n):
  13. g.act_hadamard(i)
  14. c.act_hadamard(i)
  15. assert g.to_state_vector() == c
  16. for i in range(100):
  17. a, b = np.random.randint(0, n - 1, 2)
  18. if a != b:
  19. g.act_cz(a, b)
  20. c.act_cz(a, b)
  21. assert g.to_state_vector() == c
  22. def test_all_multiqubit(n=4):
  23. """ A multi qubit test with arbitrary local rotations """
  24. g = GraphState(range(n))
  25. c = CircuitModel(n)
  26. for i in range(10):
  27. qubit = np.random.randint(0, n - 1)
  28. rotation = np.random.randint(0, 24 - 1)
  29. g.act_local_rotation(qubit, rotation)
  30. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  31. assert g.to_state_vector() == c
  32. for i in range(100):
  33. a, b = np.random.randint(0, n-1, 2)
  34. if a != b:
  35. g.act_cz(a, b)
  36. c.act_cz(a, b)
  37. assert np.allclose(np.sum(np.abs(c.state)**2), 1)
  38. assert np.allclose(np.sum(np.abs(g.to_state_vector().state)**2), 1)
  39. if not g.to_state_vector() == c:
  40. print g
  41. print a, b
  42. print "Circuit:"
  43. print g.to_state_vector()
  44. print "Graph:"
  45. print c
  46. raise ValueError
  47. assert g.to_state_vector() == c