|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Executing Quantum Photonic Circuits \n", |
| 9 | + "\n", |
| 10 | + "In CUDA-Q, there are 2 ways in which one can execute quantum photonic kernels: \n", |
| 11 | + "\n", |
| 12 | + "1. `sample`: yields measurement counts \n", |
| 13 | + "3. `get_state`: yields the quantum statevector of the computation \n", |
| 14 | + "\n", |
| 15 | + "## Sample\n", |
| 16 | + "\n", |
| 17 | + "Quantum states collapse upon measurement and hence need to be sampled many times to gather statistics. The CUDA-Q `sample` call enables this: \n", |
| 18 | + "\n" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "import cudaq\n", |
| 28 | + "import numpy as np\n", |
| 29 | + "\n", |
| 30 | + "qumode_count = 2\n", |
| 31 | + "\n", |
| 32 | + "# Define the simulation target.\n", |
| 33 | + "cudaq.set_target(\"orca-photonics\")\n", |
| 34 | + "\n", |
| 35 | + "# Define a quantum kernel function.\n", |
| 36 | + "\n", |
| 37 | + "\n", |
| 38 | + "@cudaq.kernel\n", |
| 39 | + "def kernel(qumode_count: int):\n", |
| 40 | + " level = qumode_count + 1\n", |
| 41 | + " qumodes = [qudit(level) for _ in range(qumode_count)]\n", |
| 42 | + "\n", |
| 43 | + " # Apply the create gate to the qumodes.\n", |
| 44 | + " for i in range(qumode_count):\n", |
| 45 | + " create(qumodes[i]) # |00⟩ -> |11⟩\n", |
| 46 | + "\n", |
| 47 | + " # Apply the beam_splitter gate to the qumodes.\n", |
| 48 | + " beam_splitter(qumodes[0], qumodes[1], np.pi / 6)\n", |
| 49 | + "\n", |
| 50 | + " # measure all qumodes\n", |
| 51 | + " mz(qumodes)\n", |
| 52 | + "\n", |
| 53 | + "\n", |
| 54 | + "result = cudaq.sample(kernel, qumode_count, shots_count=1000)\n", |
| 55 | + "\n", |
| 56 | + "print(result)" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "\n", |
| 64 | + "## Get state\n", |
| 65 | + "\n", |
| 66 | + "The `get_state` function gives us access to the quantum statevector of the computation." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "import cudaq\n", |
| 76 | + "import numpy as np\n", |
| 77 | + "\n", |
| 78 | + "qumode_count = 2\n", |
| 79 | + "\n", |
| 80 | + "# Define the simulation target.\n", |
| 81 | + "cudaq.set_target(\"orca-photonics\")\n", |
| 82 | + "\n", |
| 83 | + "# Define a quantum kernel function.\n", |
| 84 | + "\n", |
| 85 | + "\n", |
| 86 | + "@cudaq.kernel\n", |
| 87 | + "def kernel(qumode_count: int):\n", |
| 88 | + " level = qumode_count + 1\n", |
| 89 | + " qumodes = [qudit(level) for _ in range(qumode_count)]\n", |
| 90 | + "\n", |
| 91 | + " # Apply the create gate to the qumodes.\n", |
| 92 | + " for i in range(qumode_count):\n", |
| 93 | + " create(qumodes[i]) # |00⟩ -> |11⟩\n", |
| 94 | + "\n", |
| 95 | + " # Apply the beam_splitter gate to the qumodes.\n", |
| 96 | + " beam_splitter(qumodes[0], qumodes[1], np.pi / 6)\n", |
| 97 | + "\n", |
| 98 | + " # measure some of all qumodes if need to be measured\n", |
| 99 | + " # mz(qumodes)\n", |
| 100 | + "\n", |
| 101 | + "\n", |
| 102 | + "# Compute the statevector of the kernel\n", |
| 103 | + "result = cudaq.get_state(kernel, qumode_count)\n", |
| 104 | + "\n", |
| 105 | + "print(np.array(result))" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "The statevector generated by the `get_state` command follows little-endian convention for associating numbers with their digit string representations, which places the least significant digit on the right. That is, for the example of a 2-qumode system of level 3 (in which possible states are 0, 1, and 2), we have the following translation between integers and digit string:\n", |
| 113 | + "$$\\begin{matrix} \n", |
| 114 | + "\\text{Integer} & \\text{digit string representation}\\\\\n", |
| 115 | + "& \\text{least significant bit on right}\\\\\n", |
| 116 | + "0 = \\textcolor{blue}{0}*3^1 + \\textcolor{red}{0}*3^0 & \\textcolor{blue}{0}\\textcolor{red}{0} \\\\\n", |
| 117 | + "1 = \\textcolor{blue}{0}*3^1 + \\textcolor{red}{1}*3^0 & \\textcolor{blue}{0}\\textcolor{red}{1}\\\\\n", |
| 118 | + "2 = \\textcolor{blue}{0}*3^1 + \\textcolor{red}{2}*3^0 & \\textcolor{blue}{0}\\textcolor{red}{2}\\\\\n", |
| 119 | + "3 = \\textcolor{blue}{1}*3^1 + \\textcolor{red}{0}*3^0 & \\textcolor{blue}{1}\\textcolor{red}{0} \\\\\n", |
| 120 | + "4 = \\textcolor{blue}{1}*3^1 + \\textcolor{red}{1}*3^0 & \\textcolor{blue}{1}\\textcolor{red}{1} \\\\\n", |
| 121 | + "5 = \\textcolor{blue}{1}*3^1 + \\textcolor{red}{2}*3^0 & \\textcolor{blue}{1}\\textcolor{red}{2} \\\\\n", |
| 122 | + "6 = \\textcolor{blue}{2}*3^1 + \\textcolor{red}{0}*3^0 & \\textcolor{blue}{2}\\textcolor{red}{0} \\\\\n", |
| 123 | + "7 = \\textcolor{blue}{2}*3^1 + \\textcolor{red}{1}*3^0 & \\textcolor{blue}{2}\\textcolor{red}{1} \\\\\n", |
| 124 | + "8 = \\textcolor{blue}{2}*3^1 + \\textcolor{red}{2}*3^0 & \\textcolor{blue}{2}\\textcolor{red}{2} \n", |
| 125 | + "\\end{matrix}\n", |
| 126 | + "$$\n" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "attachments": {}, |
| 131 | + "cell_type": "markdown", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "\n", |
| 135 | + "## Parallelization Techniques\n", |
| 136 | + "\n", |
| 137 | + "The most intensive task in the computation is the execution of the quantum photonic kernel hence each execution function: `sample`, and `get_state` can be parallelized given access to multiple quantum processing units (multi-QPU). We emulate each QPU with a CPU." |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": null, |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [], |
| 145 | + "source": [ |
| 146 | + "print(cudaq.__version__)" |
| 147 | + ] |
| 148 | + } |
| 149 | + ], |
| 150 | + "metadata": { |
| 151 | + "kernelspec": { |
| 152 | + "display_name": "Python 3", |
| 153 | + "language": "python", |
| 154 | + "name": "python3" |
| 155 | + }, |
| 156 | + "language_info": { |
| 157 | + "codemirror_mode": { |
| 158 | + "name": "ipython", |
| 159 | + "version": 3 |
| 160 | + }, |
| 161 | + "file_extension": ".py", |
| 162 | + "mimetype": "text/x-python", |
| 163 | + "name": "python", |
| 164 | + "nbconvert_exporter": "python", |
| 165 | + "pygments_lexer": "ipython3", |
| 166 | + "version": "3.10.12" |
| 167 | + } |
| 168 | + }, |
| 169 | + "nbformat": 4, |
| 170 | + "nbformat_minor": 4 |
| 171 | +} |
0 commit comments