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.

45 lines
961B

  1. import chp
  2. from abp import qi
  3. import numpy as np
  4. n = 5
  5. def get_chp_state():
  6. """ Convert CHP to CircuitModel """
  7. output = qi.CircuitModel(n)
  8. ket = chp.get_ket()
  9. nonzero = np.sqrt(len(ket))
  10. output.state[0, 0]=0
  11. for key, phase in ket.items():
  12. output.state[key] = np.exp(1j*phase*np.pi/2)/nonzero
  13. return output
  14. def bell_test():
  15. chp.init(n)
  16. chp.act_hadamard(0)
  17. chp.act_cnot(0, 1)
  18. psi = qi.CircuitModel(n)
  19. psi.act_hadamard(0)
  20. psi.act_cnot(0, 1)
  21. assert psi == get_chp_state()
  22. def random_test():
  23. chp.init(n)
  24. psi = qi.CircuitModel(n)
  25. for i in range(1000):
  26. if np.random.rand()>.5:
  27. a = np.random.randint(0, n-1)
  28. chp.act_hadamard(a)
  29. psi.act_hadamard(a)
  30. else:
  31. a, b = np.random.randint(0, n-1, 2)
  32. if a!=b:
  33. chp.act_cnot(a, b)
  34. psi.act_cnot(a, b)
  35. assert psi == get_chp_state()