{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Passing input arguments to your QiskitPattern\n", "\n", "In this document, we will learn how to pass arguments to our pattern.\n", "\n", "Let's create another file with our pattern [./source_files/pattern_with_arguments.py](./source_files/pattern_with_arguments.py). \n", "\n", "Instead of having the circuit defined inside the pattern (like we did in first example), we will pass it as an argument. We will also save the results, so we can access them later by calling [save_result](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.core.save_result.html#qiskit_serverless.core.save_result).\n", "\n", "Here is the pattern:\n", "\n", "```python\n", "# source_files/pattern_with_arguments.py\n", "\n", "from qiskit_serverless import get_arguments, save_result\n", "\n", "from qiskit.primitives import Sampler\n", "\n", "# get all arguments passed to this pattern\n", "arguments = get_arguments()\n", "\n", "# get specific argument that we are interested in\n", "circuit = arguments.get(\"circuit\")\n", "\n", "sampler = Sampler()\n", "\n", "quasi_dists = sampler.run(circuit).result().quasi_dists\n", "\n", "print(f\"Quasi distribution: {quasi_dists[0]}\")\n", "\n", "# saving results of a pattern\n", "save_result({\n", " \"quasi_dists\": quasi_dists[0]\n", "})\n", "```\n", "\n", "As you can see, the circuit construction is not inside the pattern anymore. Instead, we parse the arguments by calling the [get_arguments](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.serializers.get_arguments.html#qiskit_serverless.serializers.get_arguments) function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we will create circuit that we want to pass as an argument to the pattern." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
        ┌───┐      ░ ┌─┐   \n",
       "   q_0: ┤ H ├──■───░─┤M├───\n",
       "        └───┘┌─┴─┐ ░ └╥┘┌─┐\n",
       "   q_1: ─────┤ X ├─░──╫─┤M├\n",
       "             └───┘ ░  ║ └╥┘\n",
       "meas: 2/══════════════╩══╩═\n",
       "                      0  1 
" ], "text/plain": [ " ┌───┐ ░ ┌─┐ \n", " q_0: ┤ H ├──■───░─┤M├───\n", " └───┘┌─┴─┐ ░ └╥┘┌─┐\n", " q_1: ─────┤ X ├─░──╫─┤M├\n", " └───┘ ░ ║ └╥┘\n", "meas: 2/══════════════╩══╩═\n", " 0 1 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit import QuantumCircuit\n", "\n", "circuit = QuantumCircuit(2)\n", "circuit.h(0)\n", "circuit.cx(0, 1)\n", "circuit.measure_all()\n", "circuit.draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's create and configure our client" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> ⚠ This provider is set up with default credentials to a test cluster intended to run on your machine. For information on setting up infrastructure on your local machine, check out the guide on [local infrastructure setup](https://qiskit.github.io/qiskit-serverless/deployment/local.html)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit_serverless import ServerlessClient\n", "import os\n", "\n", "client = ServerlessClient(\n", " token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n", " host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n", ")\n", "client" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'pattern-with-arguments'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit_serverless import QiskitFunction\n", "\n", "function = QiskitFunction(\n", " title=\"pattern-with-arguments\",\n", " entrypoint=\"pattern_with_arguments.py\",\n", " working_dir=\"./source_files/\",\n", ")\n", "\n", "client.upload(function)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the function using the `run` method and by passing arguments." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "QiskitFunction(pattern-with-arguments)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_function = client.get(\"pattern-with-arguments\")\n", "my_function" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job = my_function.run(circuit=circuit)\n", "job" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retrieve the results from the client" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'quasi_dists': {'0': 0.4999999999999999, '3': 0.4999999999999999}}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "job.result()" ] } ], "metadata": { "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.19" } }, "nbformat": 4, "nbformat_minor": 4 }