Pauli gates provide the ability to flip bits and rotate phases within a quantum system. However, these operations typically map standard basis states directly to other standard basis states. For example, starting with $|0\rangle$ and applying an $X$ gate results in $|1\rangle$. This behavior mimics classical logic operations. To access the computational capabilities of a quantum system, it is necessary to move away from these definite states and enter a superposition. The primary tool for this is the Hadamard gate, denoted as $H$.The Hadamard gate is arguably the most significant single-qubit gate. It transforms a definite state into a superposition where the probabilities of measuring $0$ or $1$ are equal. If you think of a classical coin, it is always heads or tails. The Hadamard gate acts like a coin toss that lands on its edge, perfectly balanced between the two outcomes until a measurement forces it to fall.Mathematical DefinitionWhen the Hadamard gate acts on the standard basis state $|0\rangle$, it produces a state with equal probability amplitudes for $|0\rangle$ and $|1\rangle$. We define this operation using the following equation:$$ H|0\rangle = \frac{1}{\sqrt{2}}|0\rangle + \frac{1}{\sqrt{2}}|1\rangle $$You might wonder why we use the coefficient $\frac{1}{\sqrt{2}}$. Recall from the previous chapter that the probabilities are calculated by squaring the magnitude of the amplitude. To ensure the total probability sums to 1 (100%), we must satisfy:$$ \left|\frac{1}{\sqrt{2}}\right|^2 + \left|\frac{1}{\sqrt{2}}\right|^2 = \frac{1}{2} + \frac{1}{2} = 1 $$We call this resulting state the "plus state," denoted as $|+\rangle$.$$ |+\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}} $$When the Hadamard gate acts on the state $|1\rangle$, it produces a similar superposition, but with an important difference in the phase. This is known as the "minus state," or $|-\rangle$.$$ H|1\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}} = |-\rangle $$Notice the negative sign. If you were to measure the probability of the minus state, you would still calculate $|\frac{-1}{\sqrt{2}}|^2 = \frac{1}{2}$. Both $|+\rangle$ and $|-\rangle$ have a 50% chance of being measured as 0 and a 50% chance of being measured as 1. The difference lies in the relative phase between the vectors, which becomes important when these qubits interact with other gates later in a circuit.Matrix RepresentationIn linear algebra terms, the Hadamard gate is represented by a specific unitary matrix. We can write $H$ as:$$ H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \ 1 & -1 \end{bmatrix} $$This matrix allows us to calculate the output for any input state vector. If we apply this matrix to the vector representation of $|0\rangle$ (which is $\begin{bmatrix} 1 \ 0 \end{bmatrix}$), we can trace the linear algebra:$$ H|0\rangle = \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} $$The result is a vector where the top component corresponds to $|0\rangle$ and the bottom component corresponds to $|1\rangle$, both with the scaling factor $\frac{1}{\sqrt{2}}$.Visualizing the TransformationIt is helpful to visualize this transformation geometrically. On the Bloch sphere, the basis states $|0\rangle$ and $|1\rangle$ sit at the North and South poles effectively on the Z-axis. The Hadamard gate performs a rotation that moves these states to the equator (the X-axis).Specifically, the Hadamard gate performs a rotation of 90 degrees ($\pi/2$ radians) around the Y-axis, followed by a rotation of 180 degrees ($\pi$ radians) around the X-axis. A simpler way to visualize it is a single 180-degree rotation around a diagonal axis located specifically between the X and Z axes.The following diagram illustrates the transition flow from basis states to superposition states.digraph G { rankdir=LR; node [fontname="Sans-Serif", shape=box, style=filled, color="#dee2e6", fillcolor="#f8f9fa"]; edge [fontname="Sans-Serif", color="#adb5bd"]; subgraph cluster_0 { label = "Basis States"; color = "#e9ecef"; style = filled; node [fillcolor="#ffffff", color="#adb5bd"]; zero [label="|0⟩\n[1, 0]"]; one [label="|1⟩\n[0, 1]"]; } subgraph cluster_1 { label = "Operation"; color = "#e9ecef"; style = filled; node [shape=circle, fixedsize=true, width=0.8, fillcolor="#4dabf7", fontcolor="white", color="#339af0"]; H1 [label="H"]; H2 [label="H"]; } subgraph cluster_2 { label = "Superposition States"; color = "#e9ecef"; style = filled; node [fillcolor="#ffffff", color="#adb5bd"]; plus [label="|+⟩\n(|0⟩ + |1⟩)/√2"]; minus [label="|-⟩\n(|0⟩ - |1⟩)/√2"]; } zero -> H1 [color="#339af0", penwidth=2]; one -> H2 [color="#fa5252", penwidth=2]; H1 -> plus [color="#339af0", penwidth=2]; H2 -> minus [color="#fa5252", penwidth=2]; }The Hadamard gate maps the Zero state to the Plus state and the One state to the Minus state.ReversibilityAn important property of the Hadamard gate is that it is its own inverse. In quantum mechanics, operations must be reversible (unitary). If you apply the Hadamard gate twice in succession, you return to your original state.$$ H(H|0\rangle) = H(|+\rangle) = |0\rangle $$$$ H(H|1\rangle) = H(|-\rangle) = |1\rangle $$Mathematically, this means that $H^2 = I$, where $I$ is the Identity matrix. This feature allows us to uncompute superposition. In many quantum algorithms, we start by applying Hadamard gates to initialize a superposition, perform a computation, and then apply Hadamard gates again to interfere the states back into a measurable basis state.Implementing H in PythonTo ground this theory in practice, we can simulate the matrix mechanics of the Hadamard gate using NumPy. This helps reinforce the linear algebra occurring behind the scenes of a quantum circuit simulator.We define the state vector for $|0\rangle$, create the Hadamard matrix, and compute the dot product.import numpy as np # Define the standard basis state |0> ket_0 = np.array([[1], [0]]) # Define the Hadamard Matrix # 1/sqrt(2) * [[1, 1], [1, -1]] H_gate = (1 / np.sqrt(2)) * np.array([[1, 1], [1, -1]]) # Apply the gate using matrix multiplication (dot product) superposition_state = np.dot(H_gate, ket_0) print("Input State:\n", ket_0) print("\nHadamard Matrix:\n", H_gate) print("\nResulting State (|+>):\n", superposition_state)Running this code reveals the numerical structure of the $|+\rangle$ state:$$ \begin{bmatrix} 0.70710678 \ 0.70710678 \end{bmatrix} $$Here, $0.707$ is the numerical approximation of $\frac{1}{\sqrt{2}}$. If you were to calculate the probabilities by squaring these numbers ($0.707^2$), you would get approximately $0.5$, confirming the 50/50 probability distribution.In the next section, we will look at phase gates, which allow us to modify the complex angle of these states without changing their probability magnitudes.