test_qft.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from sympy.core.numbers import (I, pi)
  2. from sympy.core.symbol import Symbol
  3. from sympy.functions.elementary.exponential import exp
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.matrices.dense import Matrix
  6. from sympy.physics.quantum.qft import QFT, IQFT, RkGate
  7. from sympy.physics.quantum.gate import (ZGate, SwapGate, HadamardGate, CGate,
  8. PhaseGate, TGate)
  9. from sympy.physics.quantum.qubit import Qubit
  10. from sympy.physics.quantum.qapply import qapply
  11. from sympy.physics.quantum.represent import represent
  12. def test_RkGate():
  13. x = Symbol('x')
  14. assert RkGate(1, x).k == x
  15. assert RkGate(1, x).targets == (1,)
  16. assert RkGate(1, 1) == ZGate(1)
  17. assert RkGate(2, 2) == PhaseGate(2)
  18. assert RkGate(3, 3) == TGate(3)
  19. assert represent(
  20. RkGate(0, x), nqubits=1) == Matrix([[1, 0], [0, exp(2*I*pi/2**x)]])
  21. def test_quantum_fourier():
  22. assert QFT(0, 3).decompose() == \
  23. SwapGate(0, 2)*HadamardGate(0)*CGate((0,), PhaseGate(1)) * \
  24. HadamardGate(1)*CGate((0,), TGate(2))*CGate((1,), PhaseGate(2)) * \
  25. HadamardGate(2)
  26. assert IQFT(0, 3).decompose() == \
  27. HadamardGate(2)*CGate((1,), RkGate(2, -2))*CGate((0,), RkGate(2, -3)) * \
  28. HadamardGate(1)*CGate((0,), RkGate(1, -2))*HadamardGate(0)*SwapGate(0, 2)
  29. assert represent(QFT(0, 3), nqubits=3) == \
  30. Matrix([[exp(2*pi*I/8)**(i*j % 8)/sqrt(8) for i in range(8)] for j in range(8)])
  31. assert QFT(0, 4).decompose() # non-trivial decomposition
  32. assert qapply(QFT(0, 3).decompose()*Qubit(0, 0, 0)).expand() == qapply(
  33. HadamardGate(0)*HadamardGate(1)*HadamardGate(2)*Qubit(0, 0, 0)
  34. ).expand()
  35. def test_qft_represent():
  36. c = QFT(0, 3)
  37. a = represent(c, nqubits=3)
  38. b = represent(c.decompose(), nqubits=3)
  39. assert a.evalf(n=10) == b.evalf(n=10)