{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Passing input arguments to your QiskitFunction\n", "\n", "In this document, we will learn how to pass arguments to our function.\n", "\n", "Let's create another file with our function [./source_files/function_with_arguments.py](./source_files/function_with_arguments.py). \n", "\n", "Instead of having the circuit defined inside the function (like we did in the 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 function:\n", "\n", "```python\n", "from qiskit_serverless import get_arguments, save_result\n", "from qiskit.primitives import Sampler\n", "\n", "# get all arguments passed to this function\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 the execution\n", "save_result({\n", " \"quasi_dists\": quasi_dists[0]\n", "})\n", "```\n", "\n", "As you can see, the circuit construction is not inside the function 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 function." ] }, { "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": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "