{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started\n", "\n", "This notebook will guide you through the basic steps necessary to generate and simulate a simple quantum circuit.\n", "\n", "\n", "## Writing a quantum circuit\n", "\n", "Let us write a circuit preparing a Bell state $(|00\\rangle + |11\\rangle)/\\sqrt{2}$:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJkAAABMCAMAAABJceUUAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAGxQTFRF////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////fMh50wAAACJ0Uk5TAEBEUNqI37sid2Yz3cxVqhHume/I5s3x7Y/WtlvYXOLprzri4xUAAAABYktHRACIBR1IAAAACXBIWXMAAABIAAAASABGyWs+AAACiElEQVRo3u2Y23LbIBCG6SZEQoA4JGmbHtKW93/IcpBkYiMjVDPydPgvrJEXlg9YQCxCTU1NTU13qk9wTQ8Hkj3ia3o6kKz7B2sja2T3SdZjTBAuJfOLcXCVraAKGWUUUb7RaVRsNCIgmqHOmEnlHCs3ZkBLyAYz+qcwKesNpJX96Q0gKmDUcjuZNqGw6lLW8/7jrsM552dS2vffurXBMurtZIz7BzU4ZT0D48aKF6FRHyW6Q9L4t81k/UREDOTJtPHauMxmMueYYQQeyvQnE0mcjI+LdTDC/8Ojzqyeqs+B7GnNnhxMBa4VmMkggk58TbxEwxD+MdFAvax9hLwGss9r9uSgjQKIW14TWWZ5njASYXbb2bTRCS7MbNygGW8LWSrMrqwAVb4CQv9dK2Vrc97NtElZL9H0F166a6Cwm9l56SiIPlN0aXvezViXsl6rWSCsu3DwrURi0v8UZv2H2Lk1WYmC/97uFcr2BjAzDPdn1iPJ9lgbWSP7H8j234Rrk+3PHhx5r7quRtbIGtl9qpE1skZ2jwL89W2UR1NcalQcfyNCld44S7KheyQFp342JWb5e2OkomzoHnX+4u8bAJa7asc9OmVDUT4bukMk3JlD10nBCCzZ0J6IGgMnGY3IUDdsrrlkQ3uJa5ANUxJn8j1ubmPJhlpVIdPDBzKUy4NFZFM29JKM4Ky+54s8v033quldFWdDL8koZPUjX+Tne3jOmdBfxdnQWrNp98p4NqXaXnXKhtYim0N+egyiqHYIszpkiI0RmSw7BUI2FDBXuKjeNoGSJzJcdAou2dBKwu7YDGSYyYoNlYswIh0ZcHFfYDZchOK//yg+Hg2SEryDPJqhqampqcnpL9eQIojcrGWGAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE5LTEwLTI5VDExOjQzOjIwKzAxOjAwWdwoOgAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxOS0xMC0yOVQxMTo0MzoyMCswMTowMCiBkIYAAAAUdEVYdHBkZjpWZXJzaW9uAFBERi0xLjUgBVwLOQAAAEp0RVh0c2lnbmF0dXJlADhkNjZiNjA0ZWVjM2Q3MGEzZDY4NTQ1MTYwNGQxNTcxYTVhYzdjZjgzN2YyZTVkY2Y5OGQ4ZmQ1MmQyZTZhODBbOk3HAAAAAElFTkSuQmCC", "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qat.lang.AQASM import Program, H, CNOT\n", "\n", "# Create a Program\n", "qprog = Program()\n", "# Number of qbits\n", "nbqbits = 2\n", "# Allocate some qbits\n", "qbits = qprog.qalloc(nbqbits)\n", "\n", "# Apply some quantum Gates\n", "qprog.apply(H, qbits[0]) \n", "qprog.apply(CNOT, qbits[0], qbits[1])\n", "\n", "# Export this program into a quantum circuit\n", "circuit = qprog.to_circ()\n", "# And display it!\n", "circuit.display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulating the circuit\n", "\n", "To simulate our circuit, we use our linear-algebra-based simulator, linalg. We print out the states of the computational basis with a nonzero amplitude (here, we expect to see the state $|00\\rangle$ (= index 0) and the state $|11\\rangle$ (= index 3), both with amplitude $|1/\\sqrt{2}$)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "State |00> amplitude (0.7071067811865475+0j)\n", "State |11> amplitude (0.7071067811865475+0j)\n" ] } ], "source": [ "# import one Quantum Processor Unit Factory\n", "from qat.qpus import PyLinalg\n", "\n", "# Create a Quantum Processor Unit\n", "pylinalgqpu = PyLinalg()\n", "\n", "# Create a job\n", "job = circuit.to_job()\n", "\n", "# Submit the job to the QPU\n", "result = pylinalgqpu.submit(job)\n", "\n", "# Iterate over the final state vector to get all final components\n", "for sample in result:\n", " print(\"State %s amplitude %s\" % (sample.state, sample.amplitude))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also sample the final state of the system to mimic the measure of the final state of a quantum processor. Here, we expect to see state 0 half of the time, and state 3 half of the time (on average)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "We measured the state |00> (its probability is 0.4 and its amplitude None)\n", "We measured the state |11> (its probability is 0.6 and its amplitude None)\n" ] } ], "source": [ "# Create a job where we specify the number of shots\n", "job = circuit.to_job(nbshots=10)\n", "\n", "# Submit to the QPU\n", "result = pylinalgqpu.submit(job)\n", "\n", "# And use it to draw 10 samples\n", "for sample in result:\n", " print(\"We measured the state {} (its probability is {} and its amplitude {})\".format(sample.state, sample.probability, sample.amplitude))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more information about simulation options, in order to know what you can ask from a simulator, checkout [this tutorial](tutorials/simulation/simulation_overview.ipynb).\n", "\n", "In particular, you will learn how to **directly compute the average of an observable** or **return the state vector as a numpy array**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As for quantum programming, you can learn more about [here](tutorials/lang/writing_quantum_program.ipynb).\n" ] } ], "metadata": { "authors": [ "Simon Martiel", "Jerome Pioux" ], "documentation-tags": { "icon": ":material-outlined:`not_started;3em`" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 2 }