test_qft.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. from sympy.functions.elementary.complexes import sign
  13. def test_RkGate():
  14. x = Symbol('x')
  15. assert RkGate(1, x).k == x
  16. assert RkGate(1, x).targets == (1,)
  17. assert RkGate(1, 1) == ZGate(1)
  18. assert RkGate(2, 2) == PhaseGate(2)
  19. assert RkGate(3, 3) == TGate(3)
  20. assert represent(
  21. RkGate(0, x), nqubits=1) == Matrix([[1, 0], [0, exp(sign(x)*2*pi*I/(2**abs(x)))]])
  22. def test_quantum_fourier():
  23. assert QFT(0, 3).decompose() == \
  24. SwapGate(0, 2)*HadamardGate(0)*CGate((0,), PhaseGate(1)) * \
  25. HadamardGate(1)*CGate((0,), TGate(2))*CGate((1,), PhaseGate(2)) * \
  26. HadamardGate(2)
  27. assert IQFT(0, 3).decompose() == \
  28. HadamardGate(2)*CGate((1,), RkGate(2, -2))*CGate((0,), RkGate(2, -3)) * \
  29. HadamardGate(1)*CGate((0,), RkGate(1, -2))*HadamardGate(0)*SwapGate(0, 2)
  30. assert represent(QFT(0, 3), nqubits=3) == \
  31. Matrix([[exp(2*pi*I/8)**(i*j % 8)/sqrt(8) for i in range(8)] for j in range(8)])
  32. assert QFT(0, 4).decompose() # non-trivial decomposition
  33. assert qapply(QFT(0, 3).decompose()*Qubit(0, 0, 0)).expand() == qapply(
  34. HadamardGate(0)*HadamardGate(1)*HadamardGate(2)*Qubit(0, 0, 0)
  35. ).expand()
  36. def test_qft_represent():
  37. c = QFT(0, 3)
  38. a = represent(c, nqubits=3)
  39. b = represent(c.decompose(), nqubits=3)
  40. assert a.evalf(n=10) == b.evalf(n=10)