{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "Creating custom pipelines in QAT\n", "========================================\n", "\n", "We can bring together all the granular components of a pipeline to create a custom pipeline.\n", "See below." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "# used to disable output from logs; not shown in the docs because of the\n", "# remove-cell tag\n", "import logging\n", "\n", "logging.disable(logging.CRITICAL)" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "Let's recreate the echo pipeline!\n", "* `AutoFrontend`: dispatches the correct frontend, decided by the input type.\n", "* `DefaultMiddleend`: Applies default optimization and lowering of pulse-level IR.\n", "* `WaveformBackend`: Transforms the IR into a binary for \"Waveform\" hardware.\n", "* `SimpleRuntime`: Applies a hybrid of execution on the hardware, followed by a results post-processing pipeline with an EchoEngine in this example.\n", "* `EchoEngine`: Executes the binary against an Echo simulator." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "from qat.backend.waveform import WaveformBackend\n", "from qat.core.pipeline import Pipeline\n", "from qat.engines.waveform import EchoEngine\n", "from qat.frontend import AutoFrontend\n", "from qat.middleend.default import DefaultMiddleend\n", "from qat.model.loaders.lucy import LucyModelLoader\n", "from qat.model.target_data import DefaultTargetData\n", "from qat.runtime import SimpleRuntime\n", "from qat.runtime.results_pipeline import get_results_pipeline\n", "\n", "model = LucyModelLoader(qubit_count=8).load()\n", "target_data = DefaultTargetData()\n", "results_pipeline = get_results_pipeline(model=model, target_data=target_data)\n", "new_echo8 = Pipeline(\n", " name=\"new_echo8\",\n", " frontend=AutoFrontend.default_for_pydantic(model),\n", " middleend=DefaultMiddleend(model, target_data),\n", " backend=WaveformBackend(model, target_data),\n", " runtime=SimpleRuntime(EchoEngine(), results_pipeline=results_pipeline),\n", " model=model,\n", " target_data=target_data,\n", ")" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "If we want to make this an updateable pipeline, we can do so as shown below. The `_build_pipeline` acts a factory method with the recipe to build a pipeline provided the model and target data." ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "from qat.backend.waveform import WaveformBackend\n", "from qat.core.pipeline import Pipeline\n", "from qat.engines.waveform import EchoEngine\n", "from qat.frontend import AutoFrontend\n", "from qat.middleend.default import DefaultMiddleend\n", "from qat.model.loaders.lucy import LucyModelLoader\n", "from qat.model.target_data import DefaultTargetData, TargetData\n", "from qat.pipelines.updateable import Model, PipelineConfig, UpdateablePipeline\n", "from qat.runtime import SimpleRuntime\n", "from qat.runtime.results_pipeline import get_results_pipeline\n", "\n", "\n", "class MyCoolPipeline(UpdateablePipeline):\n", " @staticmethod\n", " def _build_pipeline(\n", " config: PipelineConfig,\n", " model: Model,\n", " target_data: TargetData | None,\n", " *args,\n", " **kwargs,\n", " ) -> Pipeline:\n", " results_pipeline = get_results_pipeline(model=model, target_data=target_data)\n", " return Pipeline(\n", " name=\"new_echo8\",\n", " frontend=AutoFrontend.default_for_pydantic(model),\n", " middleend=DefaultMiddleend(model, target_data),\n", " backend=WaveformBackend(model, target_data),\n", " runtime=SimpleRuntime(EchoEngine(), results_pipeline=results_pipeline),\n", " model=model,\n", " target_data=target_data,\n", " )\n", "\n", "\n", "model = LucyModelLoader(qubit_count=8).load()\n", "target_data = DefaultTargetData()\n", "pipeline_instance = MyCoolPipeline(\n", " config=dict(name=\"my_pipeline\"), model=model, target_data=target_data\n", ")" ] } ], "metadata": { "jupytext": { "main_language": "python", "notebook_metadata_filter": "-kernelspec" }, "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }