|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "699607db", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "\n", |
| 9 | + "# Linear Algebra Foundations for Quantum Computing — Chapter 1\n", |
| 10 | + "\n", |
| 11 | + "Quantum computing is built on the language of linear algebra. Every quantum state, transformation, \n", |
| 12 | + "and measurement can be described using vectors, matrices, and specific algebraic operations. \n", |
| 13 | + "\n", |
| 14 | + "In this notebook, we review the key concepts of linear algebra and explain how they form the \n", |
| 15 | + "foundation for quantum mechanics and quantum information. For each idea, we will also see how \n", |
| 16 | + "to implement the operations in Python using `numpy`, so that the mathematics can be both \n", |
| 17 | + "visualized and practiced computationally.\n" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "id": "d52bb644", |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "import numpy as np\n", |
| 28 | + "np.set_printoptions(precision=4, suppress=True)" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "id": "7e445cdf", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "\n", |
| 37 | + "## Vectors\n", |
| 38 | + "\n", |
| 39 | + "A **vector** is one of the most fundamental mathematical objects we will encounter. \n", |
| 40 | + "In quantum computing, a vector represents the **state of a quantum system**. \n", |
| 41 | + "\n", |
| 42 | + "- For a qubit, this state is written as a column vector in a two-dimensional complex vector space. \n", |
| 43 | + " The “zero” state of a qubit is represented as the vector {math}`[1, 0]^T`, and the “one” state \n", |
| 44 | + " as {math}`[0, 1]^T`. \n", |
| 45 | + "- More generally, a quantum state can be a linear combination of these basis states, such as \n", |
| 46 | + " {math}`\\alpha|0\\rangle + \\beta|1\\rangle`, which is simply a vector with complex entries. \n", |
| 47 | + "\n", |
| 48 | + "The length, or **norm**, of a vector plays a critical role in quantum mechanics. The squared \n", |
| 49 | + "magnitudes of the vector entries represent **probabilities**, which must always sum to 1. For \n", |
| 50 | + "this reason, all valid quantum states are **normalized vectors** with unit length. Normalization \n", |
| 51 | + "ensures that when a measurement is performed, the total probability of all possible outcomes is \n", |
| 52 | + "exactly one. \n", |
| 53 | + "\n", |
| 54 | + "Vectors also provide a geometric picture of quantum states. In the case of a single qubit, \n", |
| 55 | + "we can think of the vector as pointing in some direction in two-dimensional space (or \n", |
| 56 | + "equivalently on the surface of the Bloch sphere). For higher-dimensional systems, this \n", |
| 57 | + "geometric picture becomes abstract, but the idea that vectors encode the “configuration” \n", |
| 58 | + "of the system remains the same.\n" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "id": "82e29645", |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "\n", |
| 69 | + "x = np.array([3, 4], dtype=float)\n", |
| 70 | + "print(\"x =\", x)\n", |
| 71 | + "print(\"Norm of x =\", np.linalg.norm(x))\n", |
| 72 | + "print(\"Normalized x =\", x/np.linalg.norm(x))\n" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "id": "02c6543d", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "\n", |
| 81 | + "## Complex Numbers and Conjugation\n", |
| 82 | + "\n", |
| 83 | + "Unlike classical probabilities, quantum amplitudes are expressed as **complex numbers**. \n", |
| 84 | + "A complex number has both a real part and an imaginary part, and can be written as \n", |
| 85 | + "{math}`z = a + ib`. \n", |
| 86 | + "\n", |
| 87 | + "- The **magnitude** or modulus {math}`|z|` tells us the length of this number in the complex plane. \n", |
| 88 | + "- The **angle** (or phase) encodes additional information that has no classical counterpart. \n", |
| 89 | + "- The **complex conjugate** flips the sign of the imaginary part, and the conjugate transpose \n", |
| 90 | + " (Hermitian adjoint) is crucial in quantum mechanics because it ensures probabilities are \n", |
| 91 | + " always non-negative. \n", |
| 92 | + "\n", |
| 93 | + "The reason complex numbers are essential is that they allow **interference**. When two amplitudes \n", |
| 94 | + "with phases interact, they can cancel or reinforce each other. This is what makes phenomena like \n", |
| 95 | + "the double-slit experiment possible, and it is what gives quantum computing its unique power.\n" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "id": "e71a28e0", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "z = 3 + 4j\n", |
| 106 | + "print('|z| =', abs(z), 'conjugate =', np.conj(z))" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "markdown", |
| 111 | + "id": "1d1490b0", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "\n", |
| 115 | + "## Inner Product (Dot Product)\n", |
| 116 | + "\n", |
| 117 | + "The dot product of two vectors, also known as the **inner product**, measures the degree of \n", |
| 118 | + "“overlap” between them. In Euclidean geometry, the dot product tells us whether two vectors are \n", |
| 119 | + "perpendicular, and how much one vector points in the direction of another. \n", |
| 120 | + "\n", |
| 121 | + "In quantum mechanics, the inner product of two states {math}`|u\\rangle` and {math}`|v\\rangle` \n", |
| 122 | + "is written {math}`\\langle u, v \\rangle`. \n", |
| 123 | + "\n", |
| 124 | + "- If the result is zero, the states are **orthogonal**, meaning that they represent mutually \n", |
| 125 | + " exclusive outcomes. \n", |
| 126 | + "- More generally, the probability of finding a system prepared in state {math}`|v\\rangle` \n", |
| 127 | + " when measured in the basis that includes {math}`|u\\rangle` is proportional to \n", |
| 128 | + " {math}`|\\langle u, v \\rangle|^2`. \n", |
| 129 | + "\n", |
| 130 | + "This makes the dot product one of the most important tools in quantum mechanics. \n", |
| 131 | + "It is the bridge between abstract vector states and physical probabilities that \n", |
| 132 | + "can be observed in the laboratory.\n" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": null, |
| 138 | + "id": "43ffc98e", |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "\n", |
| 143 | + "u = np.array([1, 1j])\n", |
| 144 | + "v = np.array([1, -1j])\n", |
| 145 | + "print(\"<u,v> =\", np.vdot(u,v))\n" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "markdown", |
| 150 | + "id": "d88095f6", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "\n", |
| 154 | + "## Matrices\n", |
| 155 | + "\n", |
| 156 | + "Matrices represent **linear transformations**, and in quantum computing, they describe how \n", |
| 157 | + "quantum states evolve or how they are manipulated by gates. \n", |
| 158 | + "\n", |
| 159 | + "- **Hermitian matrices** are equal to their own conjugate transpose. In quantum mechanics, \n", |
| 160 | + " Hermitian matrices represent **observables** — physical quantities like energy, spin, \n", |
| 161 | + " or position. Their eigenvalues correspond to the possible results of a measurement. \n", |
| 162 | + "\n", |
| 163 | + "- **Unitary matrices** satisfy {math}`U^\\dagger U = I`. These matrices preserve vector \n", |
| 164 | + " length (and therefore probability) and represent **valid quantum evolutions**. Every \n", |
| 165 | + " quantum gate is described by a unitary matrix. \n", |
| 166 | + "\n", |
| 167 | + "The special properties of these matrices ensure that quantum mechanics is both \n", |
| 168 | + "mathematically consistent and physically meaningful.\n" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": null, |
| 174 | + "id": "845ffaba", |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [], |
| 177 | + "source": [ |
| 178 | + "\n", |
| 179 | + "H = (1/np.sqrt(2))*np.array([[1,1],[1,-1]])\n", |
| 180 | + "print(\"Hadamard gate H:\\n\", H)\n", |
| 181 | + "print(\"Check H^†H = I:\", np.allclose(H.conj().T @ H, np.eye(2)))\n" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "markdown", |
| 186 | + "id": "44f90777", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "\n", |
| 190 | + "## Tensor Product\n", |
| 191 | + "\n", |
| 192 | + "One of the most striking differences between classical and quantum systems is how they combine. \n", |
| 193 | + "Classically, two systems are described by pairing their states. In quantum mechanics, we use the \n", |
| 194 | + "**tensor product**. \n", |
| 195 | + "\n", |
| 196 | + "- If one system is described by a vector of dimension {math}`m` and another by a vector of \n", |
| 197 | + " dimension {math}`n`, the joint system lives in a space of dimension {math}`mn`. \n", |
| 198 | + "- A single qubit is described by a 2-dimensional vector. Two qubits together require a \n", |
| 199 | + " 4-dimensional vector, and three qubits require 8 dimensions. \n", |
| 200 | + "\n", |
| 201 | + "This exponential growth is one reason quantum computing is so powerful: the state space grows \n", |
| 202 | + "extremely quickly with the number of qubits. \n", |
| 203 | + "\n", |
| 204 | + "Tensor products are also the source of **entanglement**, one of the most profound quantum \n", |
| 205 | + "phenomena. An entangled state cannot be written as a simple tensor product of two smaller states. \n", |
| 206 | + "This property has no classical analogue and is the basis for quantum communication and \n", |
| 207 | + "quantum cryptography.\n" |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + "cell_type": "code", |
| 212 | + "execution_count": null, |
| 213 | + "id": "1c54e7d5", |
| 214 | + "metadata": {}, |
| 215 | + "outputs": [], |
| 216 | + "source": [ |
| 217 | + "\n", |
| 218 | + "zero = np.array([1,0])\n", |
| 219 | + "one = np.array([0,1])\n", |
| 220 | + "print(\"|0>⊗|1> =\", np.kron(zero,one))\n" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "markdown", |
| 225 | + "id": "95fb491f", |
| 226 | + "metadata": {}, |
| 227 | + "source": [ |
| 228 | + "\n", |
| 229 | + "## Unitarity\n", |
| 230 | + "\n", |
| 231 | + "A central principle of quantum mechanics is that **probability is conserved**. When a quantum \n", |
| 232 | + "state evolves over time or is manipulated by gates, the total probability of all outcomes \n", |
| 233 | + "must remain equal to one. \n", |
| 234 | + "\n", |
| 235 | + "This requirement is exactly captured by **unitary matrices**. A unitary transformation is \n", |
| 236 | + "essentially a rotation in a high-dimensional complex vector space. It changes the direction \n", |
| 237 | + "of the state vector but never its length. \n", |
| 238 | + "\n", |
| 239 | + "This is why every valid quantum gate, from the Hadamard gate to the more complex controlled-NOT \n", |
| 240 | + "gate, is represented by a unitary matrix. Without unitarity, the mathematical structure of \n", |
| 241 | + "quantum mechanics would not correspond to the physical reality of conserved probability.\n" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "code", |
| 246 | + "execution_count": null, |
| 247 | + "id": "a36dece1", |
| 248 | + "metadata": {}, |
| 249 | + "outputs": [], |
| 250 | + "source": [ |
| 251 | + "\n", |
| 252 | + "X = np.array([[0,1],[1,0]],dtype=complex)\n", |
| 253 | + "print(\"X gate:\\n\", X)\n", |
| 254 | + "print(\"Unitary check:\", np.allclose(X.conj().T @ X, np.eye(2)))\n" |
| 255 | + ] |
| 256 | + }, |
| 257 | + { |
| 258 | + "cell_type": "markdown", |
| 259 | + "id": "00c0e352", |
| 260 | + "metadata": {}, |
| 261 | + "source": [ |
| 262 | + "\n", |
| 263 | + "## Exercises\n", |
| 264 | + "\n", |
| 265 | + "1. Normalize the vector {math}`w = (3, 4i)` and verify its norm is 1. \n", |
| 266 | + "2. Show that {math}`a = (1, i)` and {math}`b = (1, -i)` are orthogonal by computing their inner product. \n", |
| 267 | + "3. Construct the projector onto {math}`u = \\frac{1}{\\sqrt{2}}(1, 1)` and apply it to the vector {math}`x = (2, 1)`. \n", |
| 268 | + "4. Compute the tensor product {math}`|0\\rangle \\otimes |1\\rangle`. Which basis vector of the two-qubit system does this correspond to? \n", |
| 269 | + "5. Verify that the Pauli-X matrix {math}`\\begin{bmatrix}0&1\\\\1&0\\end{bmatrix}` is unitary.\n" |
| 270 | + ] |
| 271 | + } |
| 272 | + ], |
| 273 | + "metadata": {}, |
| 274 | + "nbformat": 4, |
| 275 | + "nbformat_minor": 5 |
| 276 | +} |
0 commit comments