test_gate_application.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import qcgpu
  2. from qcgpu import State
  3. import pytest
  4. import numpy as np
  5. def test_application_x():
  6. s = State(3)
  7. x = qcgpu.gate.x()
  8. s.apply_gate(x, 0)
  9. res = np.array([0,1,0,0,0,0,0,0]).astype(np.complex64).transpose()
  10. amps = s.amplitudes()
  11. assert np.allclose(res, amps)
  12. def test_apply_all_x():
  13. s = State(3)
  14. x = qcgpu.gate.x()
  15. s.apply_all(x)
  16. res = np.array([0,0,0,0,0,0,0,1]).astype(np.complex64).transpose()
  17. amps = s.amplitudes()
  18. assert np.allclose(res, amps)
  19. def test_application_h():
  20. s = State(3)
  21. h = qcgpu.gate.h()
  22. s.apply_gate(h, 1)
  23. res = (1/np.sqrt(2)) * np.array([1,0,1,0,0,0,0,0]).astype(np.complex64).transpose()
  24. amps = s.amplitudes()
  25. assert np.allclose(res, amps)
  26. def test_apply_all_h():
  27. s = State(8)
  28. h = qcgpu.gate.h()
  29. s.apply_all(h)
  30. res = (1 / np.sqrt(2**8)) * np.ones((1, 2**8), dtype=np.complex64)
  31. amps = s.amplitudes()
  32. assert np.allclose(res, amps)
  33. def test_apply_cnot_1():
  34. s = State(2)
  35. x = qcgpu.gate.x()
  36. s.apply_controlled_gate(x, 0, 1)
  37. res = np.array([1,0,0,0]).astype(np.complex64).transpose()
  38. amps = s.amplitudes()
  39. assert np.allclose(res, amps)
  40. def test_apply_cnot_2():
  41. s = State(2)
  42. x = qcgpu.gate.x()
  43. s.apply_gate(x, 0)
  44. s.apply_controlled_gate(x, 0, 1)
  45. res = np.array([0,0,0,1]).astype(np.complex64).transpose()
  46. amps = s.amplitudes()
  47. assert np.allclose(res, amps)