|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "2e2023d1", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Quickstart" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "a82fbaaa", |
| 14 | + "metadata": { |
| 15 | + "tags": [ |
| 16 | + "version-info" |
| 17 | + ] |
| 18 | + }, |
| 19 | + "source": [] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "id": "7bbbdb4b", |
| 24 | + "metadata": { |
| 25 | + "jp-MarkdownHeadingCollapsed": true |
| 26 | + }, |
| 27 | + "source": [ |
| 28 | + "Build your first quantum circuit in under two minutes, on your local environment - no sign-in or API key necessary.\n", |
| 29 | + "\n", |
| 30 | + "<Admonition type=\"note\" title=\"New to Python and virtual environments?\">\n", |
| 31 | + "\n", |
| 32 | + "* Download Python and use a virtual environment with Qiskit (recommended).\n", |
| 33 | + "\n", |
| 34 | + "<details>\n", |
| 35 | + "<summary>Click to expand for more information about **Python**.</summary>\n", |
| 36 | + "\n", |
| 37 | + "- To install Python, first check the \"Programming Language\" section on the [Qiskit PyPI project page](https://pypi.org/project/qiskit/) to determine which Python versions are supported by the most recent release. For download instructions, see the [Python Beginners Guide.](https://wiki.python.org/moin/BeginnersGuide/Download)\n", |
| 38 | + "\n", |
| 39 | + "<Admonition type = \"note\">\n", |
| 40 | + "These instructions use the standard Python distribution from [pypi.org](https://pypi.org/). However, you can use other Python distributions, such as [Anaconda](https://docs.anaconda.com/anaconda/) or [miniconda](https://docs.anaconda.com/miniconda/), along with other dependency management workflows like [Poetry](https://python-poetry.org/docs/).\n", |
| 41 | + "</Admonition>\n", |
| 42 | + "</details>\n", |
| 43 | + "\n", |
| 44 | + " <details>\n", |
| 45 | + " <summary>\n", |
| 46 | + " Click to expand for more information on **virtual environments**.\n", |
| 47 | + " </summary>\n", |
| 48 | + " - Use [Python virtual environments](https://docs.python.org/3.10/tutorial/venv.html) to separate Qiskit from other applications.\n", |
| 49 | + " A Python virtual environment is an isolated space to work with Python for a specific purpose — so you can install whatever packages you wish, and set up libraries, dependencies, and so on, without affecting the \"base\" Python environment on your machine.\n", |
| 50 | + "\n", |
| 51 | + " One important advantage of a virtual environment is that if your Python environment becomes corrupted, you can easily delete it and start over!\n", |
| 52 | + "\n", |
| 53 | + " Choose a preferred location in which to store information about your virtual environments. Typically they're stored in a directory named `.venv` within each project directory.\n", |
| 54 | + "\n", |
| 55 | + " To set up a virtual environment, navigate to your project directory and create a minimal environment with only Python installed in it.\n", |
| 56 | + "\n", |
| 57 | + " <OperatingSystemTabs>\n", |
| 58 | + " <TabItem value=\"mac\" label=\"macOS\">\n", |
| 59 | + " ```shell\n", |
| 60 | + " python3 -m venv .venv\n", |
| 61 | + " ```\n", |
| 62 | + " </TabItem>\n", |
| 63 | + "\n", |
| 64 | + " <TabItem value=\"linux\" label=\"Linux\">\n", |
| 65 | + " ```shell\n", |
| 66 | + " python3 -m venv .venv\n", |
| 67 | + " ```\n", |
| 68 | + " </TabItem>\n", |
| 69 | + "\n", |
| 70 | + " <TabItem value=\"win\" label=\"Windows\">\n", |
| 71 | + " ```text\n", |
| 72 | + " python -m venv .venv\n", |
| 73 | + " ```\n", |
| 74 | + " </TabItem>\n", |
| 75 | + " </OperatingSystemTabs>\n", |
| 76 | + "\n", |
| 77 | + " Next, activate your new environment.\n", |
| 78 | + "\n", |
| 79 | + " <OperatingSystemTabs>\n", |
| 80 | + " <TabItem value=\"mac\" label=\"macOS\">\n", |
| 81 | + " ```shell\n", |
| 82 | + " source .venv/bin/activate\n", |
| 83 | + " ```\n", |
| 84 | + " </TabItem>\n", |
| 85 | + "\n", |
| 86 | + " <TabItem value=\"linux\" label=\"Linux\">\n", |
| 87 | + " ```shell\n", |
| 88 | + " source .venv/bin/activate\n", |
| 89 | + " ```\n", |
| 90 | + " </TabItem>\n", |
| 91 | + "\n", |
| 92 | + " <TabItem value=\"win\" label=\"Windows\">\n", |
| 93 | + " If using PowerShell:\n", |
| 94 | + "\n", |
| 95 | + " ```text\n", |
| 96 | + " .venv\\Scripts\\Activate.ps1\n", |
| 97 | + " ```\n", |
| 98 | + " If using Git Bash:\n", |
| 99 | + "\n", |
| 100 | + " ```text\n", |
| 101 | + " source .venv/scripts/activate\n", |
| 102 | + " ```\n", |
| 103 | + " If using command prompt:\n", |
| 104 | + "\n", |
| 105 | + " ```text\n", |
| 106 | + " .venv\\Scripts\\activate\n", |
| 107 | + " ```\n", |
| 108 | + " </TabItem>\n", |
| 109 | + " </OperatingSystemTabs>\n", |
| 110 | + " </details>\n", |
| 111 | + "\n", |
| 112 | + "</Admonition>\n", |
| 113 | + "\n", |
| 114 | + "\n", |
| 115 | + "## 1. Install Qiskit\n", |
| 116 | + "\n", |
| 117 | + "Run the following command in your terminal to install the Qiskit and Matplotlib packages, as well as the Qiskit visualization module.\n", |
| 118 | + "\n", |
| 119 | + "```shell\n", |
| 120 | + "pip install qiskit matplotlib qiskit[visualization]\n", |
| 121 | + "\n", |
| 122 | + "# On a zsh terminal, use this line instead:\n", |
| 123 | + "# pip install qiskit matplotlib 'qiskit[visualization]'\n", |
| 124 | + "```" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "id": "c8da788c", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "## 2. Build your circuit\n", |
| 133 | + "\n", |
| 134 | + "Open a Python environment, then run this code to build a Bell state (two entangled qubits)." |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "id": "86784142", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "name": "stdout", |
| 145 | + "output_type": "stream", |
| 146 | + "text": [ |
| 147 | + "{'00': 505, '11': 519}\n" |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "from qiskit import QuantumCircuit\n", |
| 153 | + "from qiskit.primitives import StatevectorSampler\n", |
| 154 | + "\n", |
| 155 | + "qc = QuantumCircuit(2)\n", |
| 156 | + "qc.h(0)\n", |
| 157 | + "qc.cx(0, 1)\n", |
| 158 | + "qc.measure_all()\n", |
| 159 | + "\n", |
| 160 | + "sampler = StatevectorSampler()\n", |
| 161 | + "result = sampler.run([qc], shots=1024).result()\n", |
| 162 | + "print(result[0].data.meas.get_counts())" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "id": "9a3180c2-56d7-4224-91fa-13d8cb87d93d", |
| 168 | + "metadata": {}, |
| 169 | + "source": [ |
| 170 | + "The expected output is a near-even split between '00' and '11'." |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "id": "a110ac90", |
| 176 | + "metadata": {}, |
| 177 | + "source": [ |
| 178 | + "## 3. Visualize your results\n", |
| 179 | + "\n", |
| 180 | + "To get a histogram of your results, add the following code to your program." |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": null, |
| 186 | + "id": "dc4ff012", |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [ |
| 189 | + { |
| 190 | + "data": { |
| 191 | + "text/plain": [ |
| 192 | + "<Image src=\"/docs/images/guides/quick-start/extracted-outputs/dc4ff012-0.avif\" alt=\"Output of the previous code cell\" />" |
| 193 | + ] |
| 194 | + }, |
| 195 | + "execution_count": 10, |
| 196 | + "metadata": {}, |
| 197 | + "output_type": "execute_result" |
| 198 | + } |
| 199 | + ], |
| 200 | + "source": [ |
| 201 | + "from qiskit.visualization import plot_histogram\n", |
| 202 | + "\n", |
| 203 | + "counts = result[0].data.meas.get_counts()\n", |
| 204 | + "plot_histogram(counts)\n", |
| 205 | + "\n", |
| 206 | + "# Include the next line if you are not using Python in a Jupyter notebook\n", |
| 207 | + "# plt.show()" |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + "cell_type": "markdown", |
| 212 | + "id": "738d4fc4-29c9-46ab-9879-3365e2149d04", |
| 213 | + "metadata": {}, |
| 214 | + "source": [ |
| 215 | + "This result is a signature of quantum entanglement.\n", |
| 216 | + "\n", |
| 217 | + "## 4. See what happens\n", |
| 218 | + "\n", |
| 219 | + "Try changing the code to see how it affects the results. For example:\n", |
| 220 | + "\n", |
| 221 | + "- Add a third qubit by changing to `QuantumCircuit(3)`, and add a second CX gate with `qc.cx(1,2)`. The measurements should then change to 000 and 111, which means all three of these qubits have been entangled.\n", |
| 222 | + "\n", |
| 223 | + "- See your results shift by adding `qc.x(1)` to the end of the circuit." |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "markdown", |
| 228 | + "id": "b6062a16", |
| 229 | + "metadata": {}, |
| 230 | + "source": [ |
| 231 | + "## Next steps\n", |
| 232 | + "\n", |
| 233 | + "<Admonition type=\"tip\" title=\"Recommendations\">\n", |
| 234 | + "- Run a circuit on real quantum hardware in the [Hello world](/docs/tutorials/hello-world) tutorial.\n", |
| 235 | + "- Not ready to run on hardware? Start your quantum journey with the [Basics of quantum information](/learning/courses/basics-of-quantum-information) course.\n", |
| 236 | + "</Admonition>" |
| 237 | + ] |
| 238 | + } |
| 239 | + ], |
| 240 | + "metadata": { |
| 241 | + "description": "Build and visualize a quantum circuit in under two minutes, no sign-in or API key necessary.", |
| 242 | + "kernelspec": { |
| 243 | + "display_name": "Python 3", |
| 244 | + "language": "python", |
| 245 | + "name": "python3" |
| 246 | + }, |
| 247 | + "language_info": { |
| 248 | + "codemirror_mode": { |
| 249 | + "name": "ipython", |
| 250 | + "version": 3 |
| 251 | + }, |
| 252 | + "file_extension": ".py", |
| 253 | + "mimetype": "text/x-python", |
| 254 | + "name": "python", |
| 255 | + "nbconvert_exporter": "python", |
| 256 | + "pygments_lexer": "ipython3", |
| 257 | + "version": "3" |
| 258 | + }, |
| 259 | + "title": "Quickstart" |
| 260 | + }, |
| 261 | + "nbformat": 4, |
| 262 | + "nbformat_minor": 5 |
| 263 | +} |
0 commit comments