{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "c8d1e5f7",
      "metadata": {},
      "source": [
        "---\n",
        "title: Get started with Rimay Quantum Feature Extraction by Kipu Quantum\n",
        "description: Load the Kipu Quantum Rimay function, choose a function size, upload data, map it to quantum-transformed features, and fetch error messages.\n",
        "---\n",
        "\n",
        "{/* cspell:ignore Rimay, Simen */}\n",
        "\n",
        "# Get started with Rimay Quantum Feature Extraction by Kipu Quantum\n",
        "\n",
        "*See the [overview](/docs/functions/kipu-rimay) and the [API reference](/docs/functions/kipu-rimay/api-reference).*\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  IBM Quantum® Functions are a beta feature available only to IBM Quantum Premium Plan, Flex Plan, and On-Prem (through the IBM Quantum Platform API) Plan users. Beta features are subject to change.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "171acef7",
      "metadata": {
        "tags": [
          "version-info"
        ]
      },
      "source": [
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f959fdf8",
      "metadata": {},
      "source": [
        "## Set up and load the function\n",
        "\n",
        "Authenticate using your [IBM Quantum Platform API key](/), and select the quantum function as follows:\n",
        "\n",
        "<Admonition type=\"note\">\n",
        "  The following code assumes that you have saved your credentials. If you have not, follow the instructions in [Install the Quantum Functions Catalog client](/docs/ibm-quantum-platform/functions-get-started#install-qiskit-functions-catalog-client) to save your account with your API key.\n",
        "</Admonition>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "57eab620",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[QiskitFunction(kipu-quantum/rimay)]"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from qiskit_ibm_catalog import FunctionsCatalog\n",
        "\n",
        "catalog = FunctionsCatalog(channel=\"ibm_quantum_platform\")\n",
        "\n",
        "# Verify that you have access to the function\n",
        "catalog.list()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "4a0d9f46",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Access function\n",
        "rimay = catalog.load(\"kipu-quantum/rimay\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d1eca393",
      "metadata": {},
      "source": [
        "## Function sizes\n",
        "\n",
        "Every job runs at one size — `S`, `M`, or `L`. If the specified size is too small, the job is rejected. If the size is larger than needed, it runs on a bigger compute profile than the work requires.\n",
        "\n",
        "The size of a Rimay job depends on the number of samples it maps: the rows of `x_train` and `x_test` for `fit-transform`, and the rows of `x_inference` for `transform`. The labels in `y_train` do not count, and a `delete` run maps no samples.\n",
        "\n",
        "| Size | Samples per job  | Compute profile | vCPU | RAM    |\n",
        "| :--- | :--------------- | :-------------- | :--- | :----- |\n",
        "| `S`  | Up to 2,999      | `8x32`          | 8    | 32 GB  |\n",
        "| `M`  | 3,000 to 9,999   | `8x32`          | 8    | 32 GB  |\n",
        "| `L`  | 10,000 to 15,000 | `16x128`        | 16   | 128 GB |\n",
        "\n",
        "The number of samples is the only criterion. A size changes nothing but the compute profile: the [limits](#limits) on features, samples, and shots are the same for every size. `S` and `M` run on the same compute profile and differ in the number of samples each covers.\n",
        "\n",
        "The default is `M`, used when a job does not request a size. A job of more than 9,999 samples must therefore request `L`.\n",
        "\n",
        "You can also read the mapping off the function itself with `rimay.sizes_map`, and the size that a job runs at when it does not request one with `rimay.default_size`.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7b146f24",
      "metadata": {},
      "source": [
        "### Calculate the size before submitting\n",
        "\n",
        "Run this on your own inputs. It reads them and does nothing else: no job is submitted and nothing is reserved, so you can call it as often as you like.\n",
        "\n",
        "Pass the data sets themselves, not their file names, in the same order as `input_file`, together with the `mode` of the run. [Step 2 of the example](#2-run-the-function) shows the call.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "a4c5f41b",
      "metadata": {},
      "outputs": [],
      "source": [
        "from os import PathLike\n",
        "from typing import Any, Literal, Sequence, Sized\n",
        "\n",
        "Size = Literal[\"S\", \"M\", \"L\"]\n",
        "\n",
        "# Upper bounds (inclusive) in samples.\n",
        "_SMALL = 2_999\n",
        "_MEDIUM = 9_999\n",
        "\n",
        "FIT_TRANSFORM, TRANSFORM, DELETE = \"fit-transform\", \"transform\", \"delete\"\n",
        "\n",
        "# Which positions of input_file hold samples, per mode. The labels in\n",
        "# y_train submit no circuits of their own, so they are not counted.\n",
        "SAMPLE_POSITIONS = {FIT_TRANSFORM: (0, 2), TRANSFORM: (0,)}\n",
        "\n",
        "\n",
        "def calculate_function_size(problem: Sequence[Sized], **kwargs: Any) -> Size:\n",
        "    \"\"\"Return the size (\"S\", \"M\" or \"L\") required to run the function.\"\"\"\n",
        "    metrics = _measure(problem, **kwargs)\n",
        "    if _is_small(metrics):\n",
        "        return \"S\"\n",
        "    if _is_medium(metrics):\n",
        "        return \"M\"\n",
        "    return \"L\"\n",
        "\n",
        "\n",
        "def _measure(problem: Sequence[Sized], **kwargs: Any) -> dict:\n",
        "    \"\"\"The samples the job submits.\"\"\"\n",
        "    return {\"samples\": count_samples(problem, kwargs.get(\"mode\"))}\n",
        "\n",
        "\n",
        "def count_samples(problem: Sequence[Sized], mode: Any) -> int:\n",
        "    \"\"\"Rows across the data sets of ``problem`` that carry samples.\"\"\"\n",
        "    if mode == DELETE:\n",
        "        return 0\n",
        "    if mode not in SAMPLE_POSITIONS:\n",
        "        raise ValueError(f\"cannot size mode {mode!r}\")\n",
        "    if _is_name(problem) or (\n",
        "        isinstance(problem, (list, tuple))\n",
        "        and any(_is_name(data) for data in problem)\n",
        "    ):\n",
        "        raise TypeError(\n",
        "            \"pass the data, not the file name: read each input_file, e.g. with \"\n",
        "            \"pandas.read_csv, and pass the data sets in input_file order\"\n",
        "        )\n",
        "    if not isinstance(problem, (list, tuple)):\n",
        "        raise TypeError(\n",
        "            \"pass a list of data sets in input_file order, e.g. [x_train, y_train], \"\n",
        "            f\"not a single {type(problem).__name__}\"\n",
        "        )\n",
        "    return sum(\n",
        "        len(problem[i]) for i in SAMPLE_POSITIONS[mode] if i < len(problem)\n",
        "    )\n",
        "\n",
        "\n",
        "def _is_name(value: Any) -> bool:\n",
        "    \"\"\"A file name or path rather than data.\"\"\"\n",
        "    return isinstance(value, (str, bytes, PathLike))\n",
        "\n",
        "\n",
        "def _is_small(metrics: dict) -> bool:\n",
        "    \"\"\"Determine if the metrics fit T-size 'S'.\"\"\"\n",
        "    return metrics[\"samples\"] <= _SMALL\n",
        "\n",
        "\n",
        "def _is_medium(metrics: dict) -> bool:\n",
        "    \"\"\"Determine if the metrics fit T-size 'M'.\"\"\"\n",
        "    return metrics[\"samples\"] <= _MEDIUM"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "98ec81b7",
      "metadata": {},
      "source": [
        "If the size is too small, the job fails with error `4600` and the message names the size to use instead. If it is larger than needed, the job runs normally and records a `4601` advisory event.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "81671e71",
      "metadata": {},
      "source": [
        "## Inputs\n",
        "\n",
        "The `mode` determines which parameters a run needs. For a description of every input parameter, see the [API reference](/docs/functions/kipu-rimay/api-reference#inputs).\n",
        "\n",
        "### Input files\n",
        "\n",
        "* Upload every file with `catalog.file_upload()` before you run the function.\n",
        "* An upload replaces any earlier file of the same name, even for a job that you submitted but that has not started yet. To run several jobs at the same time, give the files of each job their own names, for example `run1_x_train.csv` and `run2_x_train.csv`.\n",
        "* Every file is a CSV file with a header row. Every column of a feature file is one feature, and every value must be numeric.\n",
        "* `y_train` holds exactly one numeric column, with one label per row of `x_train`. Map categorical labels to numbers, for example good and bad to 1 and 0.\n",
        "* `x_test` and `x_inference` must have the same columns as `x_train`, in the same order.\n",
        "\n",
        "<Admonition type=\"caution\" title=\"Keep the mapping_id and backend_name\">\n",
        "  The `mapping_id` is the only way to reach the mapping that your fit produced, and it cannot be looked up or recovered. It authorizes the use of that mapping, so treat it like a credential and do not share it. If you lose it, you must fit a new mapping, which costs another run on quantum hardware. Also record the `backend_name` of the fit, because the mapping runs only on that processor.\n",
        "</Admonition>\n",
        "\n",
        "### Limits\n",
        "\n",
        "* Every feature is mapped to one qubit, so the processor must have at least as many qubits as your data has features. If you have more features than that, reduce them with a feature selection technique first.\n",
        "* Every sample becomes one circuit, and a run counts training and test samples together. A run can hold at most 15,000 samples.\n",
        "* A single job can request at most 10 million executions, so the number of samples times `shots` must not exceed 10 million.\n",
        "* A job must finish within 24 hours, including the time its circuits wait in the processor's queue. A job that runs longer is stopped without a result. On a busy processor, keep large datasets well below these limits.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c0e6f3c9",
      "metadata": {},
      "source": [
        "## Outputs\n",
        "\n",
        "`job.result()` returns a dictionary. The quantum-transformed features themselves are written to CSV files that you download with `catalog.file_download()`. For a description of every key of the dictionary, see the [API reference](/docs/functions/kipu-rimay/api-reference#output). For the time that each execution phase took, see [Run metadata](#run-metadata).\n",
        "\n",
        "Every output file holds one row per input row, in the order of the input file, and one column per quantum-transformed feature. The columns whose names start with `corr_` are the two-body expectation values. The files hold no label column. The labels of your training data stay in your `y_train` file.\n",
        "\n",
        "### Run metadata\n",
        "\n",
        "`resource_usage` holds one entry for each execution phase that the run went through: `\"RUNNING: MAPPING\"`, `\"RUNNING: POST_PROCESSING\"`, and, on a QPU, usually `\"RUNNING: WAITING_FOR_QPU\"`. Each entry holds `CPU_TIME`, the wall-clock time of the phase in seconds, and `QPU_TIME`, the QPU time in seconds. On a QPU, the QPU time that IBM billed is recorded under `\"RUNNING: MAPPING\"`, the phase in which the circuits ran.\n",
        "\n",
        "`\"RUNNING: WAITING_FOR_QPU\"` is the time your jobs waited in the IBM queue. The run takes this time from the IBM job timestamps, and does not count it in `\"RUNNING: MAPPING\"`. If IBM does not report those timestamps for every job, the entry is absent, and the wait is part of `\"RUNNING: MAPPING\"`.\n",
        "\n",
        "IBM® finishes its usage calculation some time after a job finishes. The run waits up to about a minute for it. Usage that is still being calculated after that time is not included in `QPU_TIME`, and the final usage is shown on IBM Quantum Platform. If the run cannot read the usage from IBM, `QPU_TIME` is `None`.\n",
        "\n",
        "For example:\n",
        "\n",
        "```python\n",
        "\"metadata\": {\n",
        "    \"version\": \"0.3.0\",\n",
        "    \"resource_usage\": {\n",
        "        \"RUNNING: MAPPING\": {\"CPU_TIME\": 12.4, \"QPU_TIME\": 6.0},\n",
        "        \"RUNNING: WAITING_FOR_QPU\": {\"CPU_TIME\": 28.8, \"QPU_TIME\": 0.0},\n",
        "        \"RUNNING: POST_PROCESSING\": {\"CPU_TIME\": 0.3, \"QPU_TIME\": 0.0},\n",
        "    },\n",
        "}\n",
        "```\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "620cfb4e",
      "metadata": {},
      "source": [
        "## Example\n",
        "\n",
        "This example shows how to upload data, run the function, and retrieve the quantum-transformed features. For an end-to-end example that includes the preprocessing and the training of the classical model, see the [tutorial](/docs/tutorials/classify-credit-risk-with-rimay-quantum-feature-extraction).\n",
        "\n",
        "### 1. Upload the data\n",
        "\n",
        "This example uses a small synthetic dataset with four numeric features and a binary label, so that a run needs only four qubits and finishes quickly. The next cell writes it to four CSV files: the training features, the training labels, the test features, and the features for step 4 of this example. To use your own data, write your preprocessed training and test sets to files with the same layout instead.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "956f535c",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "95"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from pathlib import Path\n",
        "\n",
        "Path(\"x_train.csv\").write_text(\"\"\"f0,f1,f2,f3\n",
        "0.7227,1.3355,1.9595,2.5562\n",
        "0.2731,0.5779,0.8983,1.1624\n",
        "0.1139,0.1817,0.2163,0.2426\n",
        "0.0341,0.1311,0.0854,0.09\n",
        "0.8996,1.6951,2.497,3.3407\n",
        "0.9669,1.8906,2.7705,3.6569\n",
        "0.6366,1.2821,1.8793,2.4602\n",
        "0.7718,1.4979,2.2223,2.933\n",
        "0.5465,1.1008,1.67,2.2195\n",
        "0.9475,1.9423,2.8942,3.8199\n",
        "0.8829,1.6842,2.4703,3.2865\n",
        "0.0675,0.0365,0.0705,0.0162\n",
        "\"\"\")\n",
        "Path(\"y_train.csv\").write_text(\"label\\n1\\n0\\n0\\n0\\n1\\n1\\n0\\n1\\n0\\n1\\n1\\n0\\n\")\n",
        "Path(\"x_test.csv\").write_text(\"\"\"f0,f1,f2,f3\n",
        "0.543,1.0786,1.5684,2.0607\n",
        "0.9928,1.9037,2.9302,3.8422\n",
        "0.2269,0.3637,0.4628,0.597\n",
        "0.9896,1.9511,2.8913,3.8208\n",
        "\"\"\")\n",
        "Path(\"x_inference.csv\").write_text(\"\"\"f0,f1,f2,f3\n",
        "0.2708,0.542,0.8506,1.0897\n",
        "0.3585,0.6025,0.9517,1.2609\n",
        "0.8871,1.6559,2.4577,3.2992\n",
        "\"\"\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "486683a8",
      "metadata": {},
      "source": [
        "Upload the training and test files. You upload `x_inference.csv` in step 4.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "03ce912e",
      "metadata": {},
      "outputs": [],
      "source": [
        "for file_name in (\"x_train.csv\", \"y_train.csv\", \"x_test.csv\"):\n",
        "    catalog.file_upload(file_name, rimay)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cf0da934",
      "metadata": {},
      "source": [
        "### 2. Run the function\n",
        "\n",
        "Calculate the size of the run from the data that you uploaded, with the calculator from [Function sizes](#calculate-the-size-before-submitting).\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "9890e712",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "S\n"
          ]
        }
      ],
      "source": [
        "import pandas as pd\n",
        "\n",
        "x_train = pd.read_csv(\"x_train.csv\")\n",
        "y_train = pd.read_csv(\"y_train.csv\")\n",
        "x_test = pd.read_csv(\"x_test.csv\")\n",
        "\n",
        "size = calculate_function_size(\n",
        "    [x_train, y_train, x_test], mode=\"fit-transform\"\n",
        ")\n",
        "print(size)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "39dc6c05",
      "metadata": {},
      "source": [
        "Select a processor with at least as many qubits as your data has features, and run the function in `fit-transform` mode with that size. The sample data has four features, so any IBM Quantum processor can run it.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "f0780a06",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "85dabc9f-a769-4f4a-8e92-f3be73f85ca2\n"
          ]
        }
      ],
      "source": [
        "backend = catalog.least_busy(operational=True, simulator=False)\n",
        "\n",
        "job = rimay.run(\n",
        "    mode=\"fit-transform\",\n",
        "    input_file=[\"x_train.csv\", \"y_train.csv\", \"x_test.csv\"],\n",
        "    backend_name=backend.name,\n",
        "    shots=5000,\n",
        "    function_size=size,\n",
        ")\n",
        "print(job.job_id)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "36753b18",
      "metadata": {},
      "source": [
        "Check the workload's [status](/docs/ibm-quantum-platform/functions-get-started#check-job-status) or return [results](/docs/ibm-quantum-platform/functions-get-started#retrieve-results) as follows:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "f4bf7fb9",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "'DONE'"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Monitor the job status\n",
        "job.status()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "181e547d",
      "metadata": {},
      "outputs": [],
      "source": [
        "result = job.result()\n",
        "\n",
        "# Keep both: the mapping_id is the only handle on the mapping this run\n",
        "# fitted, and the mapping runs only on the processor it was fitted on.\n",
        "mapping_id = result[\"mapping_id\"]\n",
        "fit_backend_name = result[\"backend_name\"]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c3b52288",
      "metadata": {},
      "source": [
        "### 3. Retrieve the results\n",
        "\n",
        "Download the quantum-transformed features. Pass `target_name` so that each file is saved under its own name.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "ef852641",
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "693iB [00:00, 171kiB/s]\n",
            "254iB [00:00, 111kiB/s]\n"
          ]
        }
      ],
      "source": [
        "for file_name in result[\"output_files\"]:\n",
        "    catalog.file_download(file_name, rimay, target_name=file_name)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5c62addd",
      "metadata": {},
      "source": [
        "### 4. Map new data\n",
        "\n",
        "After you train your model, map new data with the fitted mapping, for example to run inference. This example maps `x_inference.csv`, which the first cell of this example wrote. Pass the `mapping_id` that the fit returned, so that the new rows are mapped with the same mapping instead of a new one.\n",
        "\n",
        "A mapping is built for the qubit layout of one processor, so pass the same `backend_name` as the fit, which the fit returned in `result[\"backend_name\"]`. Do not use `least_busy()` to choose the processor, because it might return a different one. Rimay rejects any other processor before it submits anything. The shot count also comes from the fit.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "25bc4d9e",
      "metadata": {},
      "outputs": [],
      "source": [
        "catalog.file_upload(\"x_inference.csv\", rimay)\n",
        "\n",
        "x_inference = pd.read_csv(\"x_inference.csv\")\n",
        "size = calculate_function_size([x_inference], mode=\"transform\")\n",
        "\n",
        "job = rimay.run(\n",
        "    mode=\"transform\",\n",
        "    input_file=[\"x_inference.csv\"],\n",
        "    # The processor the mapping was fitted on\n",
        "    backend_name=fit_backend_name,\n",
        "    shots=5000,\n",
        "    mapping_id=mapping_id,\n",
        "    function_size=size,\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "63f47947",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "'DONE'"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Monitor the job status\n",
        "job.status()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "6a485425",
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "202iB [00:00, 73.5kiB/s]\n"
          ]
        }
      ],
      "source": [
        "result = job.result()\n",
        "\n",
        "for file_name in result[\"output_files\"]:\n",
        "    catalog.file_download(file_name, rimay, target_name=file_name)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "772b3628",
      "metadata": {},
      "source": [
        "### 5. Delete a mapping\n",
        "\n",
        "When you no longer need a fitted mapping, delete it. A `delete` run does no quantum work, so it needs no processor, no shots, and no function size. You cannot undo a deletion. After deletion, the `mapping_id` no longer refers to anything, and a `transform` run with it fails.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "d2a8355e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'output_files': [],\n",
              " 'mode': 'delete',\n",
              " 'deleted': True,\n",
              " 'mapping_id': '30L40joyUPYfnvgwo2d7MXXSzqvHkggR-Kzn81wfIYw',\n",
              " 'metadata': {'version': '0.6.0', 'resource_usage': {}}}"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "job = rimay.run(mode=\"delete\", mapping_id=mapping_id)\n",
        "job.result()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e974136f",
      "metadata": {},
      "source": [
        "## Fetch error messages\n",
        "\n",
        "If your workload status is `ERROR`, use `job.error_message()` to fetch the error message, as follows:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "4232f3ef",
      "metadata": {},
      "outputs": [
        {
          "ename": "QiskitServerlessException",
          "evalue": "\n| Message: Http bad request.\n| Code: 400\n| Details: 'minimum' validation failed, schema requires 1: rejected value 0['shots']",
          "output_type": "error",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mQiskitServerlessException\u001b[0m                 Traceback (most recent call last)",
            "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m job \u001b[38;5;241m=\u001b[39m \u001b[43mrimay\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m      2\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfit-transform\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m      3\u001b[0m \u001b[43m    \u001b[49m\u001b[43minput_file\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mx_train.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43my_train.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m      4\u001b[0m \u001b[43m    \u001b[49m\u001b[43mbackend_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbackend\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m      5\u001b[0m \u001b[43m    \u001b[49m\u001b[43mshots\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m      6\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m      7\u001b[0m job\u001b[38;5;241m.\u001b[39merror_message()\n",
            "File \u001b[0;32m~/GitHub/HJY/kipu-rimay/.venv/lib/python3.13/site-packages/qiskit_serverless/core/function.py:273\u001b[0m, in \u001b[0;36mRunnableQiskitFunction.run\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m    271\u001b[0m compute_profile \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcompute_profile\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m    272\u001b[0m function_size \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfunction_size\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 273\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_run_service\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    274\u001b[0m \u001b[43m    \u001b[49m\u001b[43mprogram\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m    275\u001b[0m \u001b[43m    \u001b[49m\u001b[43marguments\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    276\u001b[0m \u001b[43m    \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    277\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcompute_profile\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcompute_profile\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    278\u001b[0m \u001b[43m    \u001b[49m\u001b[43mfunction_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunction_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    279\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
            "File \u001b[0;32m~/GitHub/HJY/kipu-rimay/.venv/lib/python3.13/site-packages/qiskit_serverless/core/clients/serverless_client.py:1105\u001b[0m, in \u001b[0;36mIBMServerlessClient.run\u001b[0;34m(self, program, arguments, config, provider, compute_profile, function_size, suppress_low_usage_warning)\u001b[0m\n\u001b[1;32m   1098\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbackends():\n\u001b[1;32m   1099\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m QiskitServerlessException(\n\u001b[1;32m   1100\u001b[0m             \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInstance \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minstance\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m has no backends available. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1101\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot run a Qiskit Function without an accessible backend. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1102\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCheck your instance configuration at https://quantum.cloud.ibm.com/instances.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1103\u001b[0m         )\n\u001b[0;32m-> 1105\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1106\u001b[0m \u001b[43m    \u001b[49m\u001b[43mprogram\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprogram\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1107\u001b[0m \u001b[43m    \u001b[49m\u001b[43marguments\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43marguments\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1108\u001b[0m \u001b[43m    \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1109\u001b[0m \u001b[43m    \u001b[49m\u001b[43mprovider\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprovider\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1110\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcompute_profile\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcompute_profile\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1111\u001b[0m \u001b[43m    \u001b[49m\u001b[43mfunction_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunction_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1112\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
            "File \u001b[0;32m~/GitHub/HJY/kipu-rimay/.venv/lib/python3.13/site-packages/qiskit_serverless/core/clients/serverless_client.py:365\u001b[0m, in \u001b[0;36mServerlessClient.run\u001b[0;34m(self, program, arguments, config, provider, compute_profile, function_size)\u001b[0m\n\u001b[1;32m    362\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    363\u001b[0m     data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mconfig\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m asdict(Configuration())\n\u001b[0;32m--> 365\u001b[0m response_data \u001b[38;5;241m=\u001b[39m \u001b[43msafe_json_request_as_dict\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    366\u001b[0m \u001b[43m    \u001b[49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mrequests\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpost\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    367\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    368\u001b[0m \u001b[43m        \u001b[49m\u001b[43mjson\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    369\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mget_headers\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minstance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minstance\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mchannel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchannel\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    370\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mREQUESTS_TIMEOUT\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    371\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    372\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    373\u001b[0m job_id \u001b[38;5;241m=\u001b[39m response_data\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    374\u001b[0m span\u001b[38;5;241m.\u001b[39mset_attribute(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mjob.id\u001b[39m\u001b[38;5;124m\"\u001b[39m, job_id)\n",
            "File \u001b[0;32m~/GitHub/HJY/kipu-rimay/.venv/lib/python3.13/site-packages/qiskit_serverless/utils/json.py:164\u001b[0m, in \u001b[0;36msafe_json_request_as_dict\u001b[0;34m(request)\u001b[0m\n\u001b[1;32m    150\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21msafe_json_request_as_dict\u001b[39m(\n\u001b[1;32m    151\u001b[0m     request: Callable[\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m, requests\u001b[38;5;241m.\u001b[39mResponse],\n\u001b[1;32m    152\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Dict[\u001b[38;5;28mstr\u001b[39m, Any]:\n\u001b[1;32m    153\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Returns parsed json data from request.\u001b[39;00m\n\u001b[1;32m    154\u001b[0m \n\u001b[1;32m    155\u001b[0m \u001b[38;5;124;03m    Args:\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    162\u001b[0m \u001b[38;5;124;03m        parsed json response as dict structure\u001b[39;00m\n\u001b[1;32m    163\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m--> 164\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[43msafe_json_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    165\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(response, Dict):\n\u001b[1;32m    166\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
            "File \u001b[0;32m~/GitHub/HJY/kipu-rimay/.venv/lib/python3.13/site-packages/qiskit_serverless/utils/json.py:231\u001b[0m, in \u001b[0;36msafe_json_request\u001b[0;34m(request)\u001b[0m\n\u001b[1;32m    228\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m error_string \u001b[38;5;129;01min\u001b[39;00m json_data\u001b[38;5;241m.\u001b[39mvalues():\n\u001b[1;32m    229\u001b[0m         error_msg \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(error_string)\n\u001b[0;32m--> 231\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m QiskitServerlessException(\n\u001b[1;32m    232\u001b[0m         format_err_msg(\n\u001b[1;32m    233\u001b[0m             response\u001b[38;5;241m.\u001b[39mstatus_code,\n\u001b[1;32m    234\u001b[0m             \u001b[38;5;28mstr\u001b[39m(error_msg),\n\u001b[1;32m    235\u001b[0m         )\n\u001b[1;32m    236\u001b[0m     )\n\u001b[1;32m    238\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m json_data\n",
            "\u001b[0;31mQiskitServerlessException\u001b[0m: \n| Message: Http bad request.\n| Code: 400\n| Details: 'minimum' validation failed, schema requires 1: rejected value 0['shots']"
          ]
        }
      ],
      "source": [
        "job = rimay.run(\n",
        "    mode=\"fit-transform\",\n",
        "    input_file=[\"x_train.csv\", \"y_train.csv\"],\n",
        "    backend_name=backend.name,\n",
        "    shots=0,\n",
        ")\n",
        "job.error_message()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "15041ac6",
      "metadata": {},
      "source": [
        "Every error that Rimay reports carries a code from the [IBM Quantum error codes](/docs/errors), a message, and a solution that names the value that you passed and the value that is allowed. The following table lists the most common errors:\n",
        "\n",
        "| Code   | Cause                                                                                                                                                                                                                               |\n",
        "| :----- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n",
        "| `1221` | A parameter is missing or has the wrong type, `mode` is not valid, a file named in `input_file` was not uploaded, the files do not match each other, a feature is not numeric, or the `mapping_id` does not match a stored mapping. |\n",
        "| `1221` | A `\"transform\"` run names a different `backend_name` than the fit that produced its mapping.                                                                                                                                        |\n",
        "| `1216` | Your instance has no processor with the `backend_name` that you passed. The solution lists the processors it does have.                                                                                                             |\n",
        "| `1200` | Your API key does not have access to the instance that the job runs under.                                                                                                                                                          |\n",
        "| `3200` | `backend_name` does not start with `ibm_`.                                                                                                                                                                                          |\n",
        "| `4500` | The data has more features than the processor has qubits.                                                                                                                                                                           |\n",
        "| `6000` | The number of samples times `shots` exceeds 10 million executions.                                                                                                                                                                  |\n",
        "| `6001` | `shots` is smaller than 1.                                                                                                                                                                                                          |\n",
        "| `6002` | The data has more samples than a run allows.                                                                                                                                                                                        |\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2e6a9b40",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "* Visit the [API reference](/docs/functions/kipu-rimay/api-reference) for this quantum function.\n",
        "* Try the [Classify credit risk with Kipu Quantum's Rimay quantum feature extraction](/docs/tutorials/classify-credit-risk-with-rimay-quantum-feature-extraction) tutorial.\n",
        "* Learn how to choose a [problem size](/docs/managed-workflows/managed-workflow-compute-profiles#match-sizes).\n",
        "* Review [Simen, A., et al. (2025). Digitized Counterdiabatic Quantum Feature Extraction. arXiv preprint arXiv:2510.13807](https://arxiv.org/abs/2510.13807).\n",
        "* Review [Zhang, Q., et al. (2026). Quantum-enhanced satellite image classification. arXiv preprint arXiv:2602.18350](https://arxiv.org/abs/2602.18350).\n",
        "* Review the [functions overview](/docs/guides/functions), includes available functions and cost per execution, when applicable.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "id": "a1b8767d",
      "source": "© IBM Corp., 2017-2026"
    }
  ],
  "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"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}