{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using `QubitPlaceholder`s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In PyQuil, we typically use integers to identify qubits" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H 0\n", "CNOT 0 1\n", "\n" ] } ], "source": [ "from pyquil import Program\n", "from pyquil.gates import CNOT, H\n", "print(Program(H(0), CNOT(0, 1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, when running on real, near-term QPUs we care about what particular physical qubits our program will run on. In fact, we may want to run the same program on an assortment of different qubits. This is where using `QubitPlaceholder`s comes in." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H {q5186245072}\n", "CNOT {q5186245072} {q5186245128}\n", "\n" ] } ], "source": [ "from pyquil.quilatom import QubitPlaceholder\n", "q0 = QubitPlaceholder()\n", "q1 = QubitPlaceholder()\n", "prog = Program(H(q0), CNOT(q0, q1))\n", "print(prog)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you try to use this program directly, it will not work" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Qubit q5186245072 has not been assigned an index\n" ] } ], "source": [ "try:\n", " print(prog.out())\n", "except RuntimeError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead, you must explicitly map the placeholders to physical qubits. By default, the function `address_qubits` will address qubits from 0 to N." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H 0\n", "CNOT 0 1\n", "\n" ] } ], "source": [ "from pyquil.quil import address_qubits\n", "print(address_qubits(prog))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The real power comes into play when you provide an explicit mapping" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H 14\n", "CNOT 14 19\n", "\n" ] } ], "source": [ "print(address_qubits(prog, qubit_mapping={\n", " q0: 14,\n", " q1: 19,\n", "}))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Register\n", "Usually, your algorithm will use an assortment of qubits. You can use the convenience function `QubitPlaceholder.register()` to request a list of qubits to build your program." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H 0\n", "H 2\n", "H 4\n", "H 6\n", "H 8\n", "H 10\n", "H 12\n", "H 14\n", "\n" ] } ], "source": [ "qbyte = QubitPlaceholder.register(8)\n", "prog2 = Program(H(q) for q in qbyte)\n", "print(address_qubits(prog2, {q: i*2 for i, q in enumerate(qbyte)}))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }