{ "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": [ "