Introduction
\[\newcommand{\ket}[1]{\left|{#1}\right\rangle}\]\[\newcommand{\bra}[1]{\left\langle{#1}\right|}\]\[\newcommand{\braket}[2]{\left\langle{#1}\middle|{#2}\right\rangle}\]
In quantum computing a quantum logic gate is a basic quantum circuit operating on a small number of qubits. A qubit is useless unless it is used to carry out a quantum calculation. The quantum calculations are achieved by performing a series of fundamental operations, known as quantum logic gates. They are the building blocks of quantum circuits similar to the classical logic gates in conventional digital circuits.
Just to only understand the theory aspect of it, a set of python code chunks are attached so that some of the coding enthusiasts can try.
DISCLAIMER! This is optional. This document is created using Rmarkdown, so to use python in Rstudios, a library called reticulate is used. Now the path of the pythons virtual environment, in this case ‘r-reticulate’ is specified. ‘repl_python()’ opens the python shell.
## Python 3.9.16 (C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe)
## Reticulate 1.30 REPL -- A Python interpreter in R.
## Enter 'exit' or 'quit' to exit the REPL and return to R.
## exit
Single Qubit Gates
X-gate or Quantum NOT-gate
-In quantum computing the quantum NOT-gate for qubits takes \(\ket{0}\) to \(\ket{1}\) and vice versa. It is analogous to to the classical NOT-gate. - The matrix is representation of X-gate is
\[ X = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \]
The math of the X-gate acting on \(\ket{0}\) is as follows, \[ X\ket{0} = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \ket{1} \]
Do try the same for X-gate acting on \(\ket{1}\). The result expected is \(X\ket{1} = \ket{0}\).
Lets simulate the X-gate action on qubits. Import the libraries, if not install do install them.
from qiskit import *
from qiskit.visualization import plot_bloch_multivector, visualize_transition
from qiskit_textbook.tools import array_to_latex
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DC83F4FC40>
visualize_transition(qcx,trace = True, saveas = "x0gate.gif", fpg = 100, spg = 2)
The expected result is,
To understand the action of X-gate on \(\ket{1}\), inbuilt \(\ket{1}\) is not used, instead X-gate acts on \(\ket{0}\) to give \(\ket{1}\). Use this method to get \(\ket{1}\). This also helps to understand how to build simple quantum logic.
# Allowing to create a 1 qubit. By default |0> is used.
# creating a |0>
qcx = QuantumCircuit(1)
# obtaining |1>
qcx.x(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAAC9E3D0>
# Y|1>
qcx.x(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAD20EC10>
# circuit diagram
qcx.draw('mpl')
Y-gate
- Y-gate is maps \(\ket{0}\) state to \(i\ket{1}\) state and \(\ket{1}\) state to \(-i\ket{0}\).
- The Y-gate operation is as follows, \[ Y\ket{0} = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\begin{bmatrix} 1 \\ 0 \end{bmatrix} = i\begin{bmatrix} 0 \\ 1 \end{bmatrix} = i\ket{0} \]
# creating |0>
qcy = QuantumCircuit(1)
# Action of Y-gate on |0>
qcy.y(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAAD087F0>
# circuit diagram
qcy.draw('mpl')
Do try the same for Y-gate acting on \(\ket{1}\). The result expected is \(Y\ket{1} = -i\ket{0}\).
# creating |0>
qcy = QuantumCircuit(1)
# passing |0> to X-gate to get |1>
qcy.x(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCABEAB2E0>
# Action of Y-gate on |1>
qcy.y(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCABFB0580>
# circuit diagram
qcy.draw('mpl')
Z-gate
-The Z-gate is represented by Pauli Z-matrix. - Z-gate maps input state \(\ket{k}\) to \((-1)^{k}\ket{k}\). - For input \(\ket{0}\) the output remains unchanged. - For input \(\ket{1}\) the output is \(\ket{1}\).
- The Z-gate operation as follows, \[ Z\ket{0} = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} 1 \\ 0 \end{bmatrix}=\begin{bmatrix} 1 \\ 0 \end{bmatrix}= \ket{0} \]
# creating |0>
qcz = QuantumCircuit(1)
# Action of Z-gate on |0>
qcz.z(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAAC78F70>
# circuit diaagram
qcz.draw('mpl')
Do try the same for Z-gate acting on \(\ket{1}\). The result expected is \(Z\ket{1} = -\ket{1}\).
# creating |0>
qcz = QuantumCircuit(1)
# Action of X-gate on |0> to get |1>
qcz.x(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCABE5EC40>
# Action of Z-gate on |1>
qcz.z(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAD25C1F0>
# circuit diagram
qcz.draw('mpl')
Hadamard Gate
- The Hadamard gate is a true quantum gate and is one of the most important in quantum computing. It is as similar to \(\sqrt{NOT}\)-gate.
- It is a self inverse gate. It is used to create the superpositions of \(\ket{0}\) and \(\ket{1}\) states.
- The matrix representation of Hadamard gate is as follows, \[ H = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \]
- The Hadamard gate and the output states for the \(\ket{0}\) and \(\ket{1}\) input states are represented as follows. The Hadamard gate satisfies Unitary condition, \(H^{\dagger}H = HH^{\dagger} = I\).
qch = QuantumCircuit(1)
qch.h(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAD20ED00>
qch.draw('mpl')
Do try the same for H-gate acting on \(\ket{1}\).
The code for this gate as follows,
qch = QuantumCircuit(1)
qch.x(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCABE234C0>
qch.h(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAD0E1F70>
qch.draw('mpl')
Working of H-gate.
- \(H\ket{0} =\frac{1}{\sqrt{2}}\begin{bmatrix}1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix}1 \\0\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix}1 \\1\end{bmatrix}=\frac{\ket{0}+\ket{1}}{\sqrt{2}} = \ket{+}\)|
- \(H\ket{1} = \frac{1}{\sqrt{2}}\begin{bmatrix}1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix}0 \\1\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix}1 \\-1\end{bmatrix} = \frac{\ket{0}-\ket{1}}{\sqrt{2}}=\ket{-}\)|
Just as part of the academic curiosity, let use observe how H-gate acts on \(\ket{+}\) and \(\ket{-}\) respectively.
# creating |0>
qchp = QuantumCircuit(1)
# Action of H-gate to get |+>
qchp.h(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAAAD9850>
# Action of H-gate on |+>
qchp.h(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAAB21FD0>
# circuit diagram
qchp.draw('mpl')
# creating |0>
qchm = QuantumCircuit(1)
# Action of X-gate to get |1>
qchm.x(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCABFB0940>
# Action of H-gate to get |->
qchm.h(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCABFB0C10>
# Action of H-gate on |->
qchm.h(0)
## <qiskit.circuit.instructionset.InstructionSet object at 0x000001DCAC05C0A0>
qchm.draw('mpl')