gates.rst 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Quantum Gates
  2. =============
  3. In quantum computing, gates are used to manipulate quantum registers and
  4. to implement quantum algorithms.
  5. Built-In Gates
  6. --------------
  7. There are a number of gates built into QCGPU. They can all be applied
  8. the same way:
  9. .. code:: python
  10. import qcgpu
  11. register = qcgpu.State(2)
  12. state.h(0) # Applies the Hadamard (H) gate to the first qubit.
  13. state.x(1) # Applies a pauli-x (NOT) gate to the second qubit.
  14. ``h`` and ``x`` can be replaced with any of the following:
  15. - The Hadamard gate: **h** - ``state.h(0)``
  16. - The S gate: **s** - ``state.s(0)``
  17. - The T gate: **t** - ``state.t(0)``
  18. - The Pauli-X / NOT gate: **x** - ``state.x(0)``
  19. - The Pauli-Y gate: **y** - ``state.y(0)``
  20. - The Pauli-Z gate: **z** - ``state.z(0)``
  21. - The CNOT gate: **cx** -
  22. ``state.cx(0, 1) # CNOT with control = 0, target = 1``
  23. - The SWAP gate: **swap** -
  24. ``state.swap(0,1) # Swaps the 0th and 1st qubit``
  25. - The Toffoli gate: **toffoli** -
  26. ``state.toffoli(0, 1, 2) # Toffoli with controls = (0, 1), target = 2``
  27. These are all shorthand methods for the application of arbitrary gates.
  28. For example, the application of a Hadamard gate above is shorthand for
  29. .. code:: python
  30. import qcgpu
  31. h = qcgpu.gate.h()
  32. register = qcgpu.State(5)
  33. register.apply_gate(h, 0)
  34. You can also use any of the gates as controlled gates. For example, the
  35. application of the CNOT gate above is shorthand for
  36. .. code:: python
  37. import qcgpu
  38. x = qcgpu.gate.x()
  39. register = qcgpu.State(5)
  40. register.apply_controlled_gate(x, 0, 1)
  41. Applying A Gate To A Whole Register
  42. ----------------------------------
  43. There is a convenience method to apply a gate to every qubit in the register.
  44. The following applies a Hadamard gate to the whole register,
  45. .. code:: python
  46. import qcgpu
  47. h = qcgpu.gate.h()
  48. register = qcgpu.State(5)
  49. register.apply_all(h)
  50. User Defined Gates
  51. ------------------
  52. Gates in QCGPU are represented by the ``qcgpu.Gate`` class.
  53. The only gates that can be defined by the user are single qubit gates.
  54. The process of creating a gate is
  55. .. code:: python
  56. import qcgpu
  57. import numpy as np
  58. gate_matrix = np.array([
  59. [1, 0],
  60. [0, np.exp(1j * np.pi / 4)]
  61. ])
  62. gate = qcgpu.Gate(gate_matrix)
  63. The input to the ``Gate`` constructor is checked to be a 2x2 unitary
  64. matrix.
  65. This newly created gate can then be applied the long hand way,
  66. .. code:: python
  67. import qcgpu
  68. register = qcgpu.State(2)
  69. register.apply_gate(gate, 0)