359 lines
12 KiB
Plaintext
359 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a47ac9aa-4bb9-4240-806e-9beabfd2dda2",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Introduction to Qiskit Functions"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0be0fe02-bc50-45a6-aaf6-2120dac862c2",
|
|
"metadata": {
|
|
"tags": [
|
|
"version-info"
|
|
]
|
|
},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "098d8fd4-1558-40df-a1b1-ad70fdef8863",
|
|
"metadata": {},
|
|
"source": [
|
|
"<Admonition type=\"note\">\n",
|
|
"Qiskit Functions are an experimental feature available only to IBM Quantum™ Premium Plan users. They are in preview release status and subject to change.\n",
|
|
"</Admonition>\n",
|
|
"\n",
|
|
"Qiskit Functions simplify and accelerate utility-scale algorithm discovery and application development, by abstracting away parts of the quantum software development workflow. In this way, Qiskit Functions free up time normally spent hand-writing code and fine-tuning experiments.\n",
|
|
"\n",
|
|
"![Overview of Qiskit Functions](/images/guides/functions/functions-overview.svg)\n",
|
|
"Functions come in two forms:\n",
|
|
"\n",
|
|
"| Type | What does it do? | Example inputs and outputs | Who is it for? |\n",
|
|
"| - | - | - | - |\n",
|
|
"| Circuit function | Simplified interface for running circuits. Abstracts transpilation, error suppression, error mitigation | **Input**: Abstract `PUB`s <br/> **Output**: Mitigated expectation values | Researchers using Qiskit to discover new algorithms and applications, without needing to focus on optimizing for hardware or handling error. Circuit functions can be used to build custom application functions. |\n",
|
|
"| Application function | Covers higher-level tasks, like exploring algorithms and domain-specific use cases. Abstracts quantum workflow to solve tasks, with classical inputs and outputs | **Input**: Molecules, graphs <br/> **Output**: Energy, cost | Researchers in non-quantum domains, integrating quantum into existing large-scale classical workflows, without needing to map classical data to quantum circuits. |"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "df18227b-787a-4dc6-a8c7-7abd0f67110c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Functions are provided by IBM® and third-party partners. Each is performant for specific workload characteristics and have unique performance-tuning options. Premium Plan users can get started with IBM Qiskit Functions for free, or procure a license from one of the partners who have contributed a function to the catalog.\n",
|
|
"\n",
|
|
"## Get started with Qiskit Functions\n",
|
|
"\n",
|
|
"### Install Qiskit Functions Catalog client\n",
|
|
"\n",
|
|
"To start using Qiskit Functions, install the IBM Qiskit Functions Catalog client:\n",
|
|
"\n",
|
|
"`pip install qiskit-ibm-catalog`\n",
|
|
"\n",
|
|
"Once installed, authenticate using your [IBM Quantum Platform API token](http://quantum.ibm.com/) as follows:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "be135691-adee-4a1c-98bf-e76deb12319f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit_ibm_catalog import QiskitFunctionsCatalog\n",
|
|
"\n",
|
|
"catalog = QiskitFunctionsCatalog(token=\"<YOUR_IQP_API_TOKEN>\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "42843fcd-1779-4a74-8d6a-7d00c22e374e",
|
|
"metadata": {},
|
|
"source": [
|
|
"Optionally, you can use the `save_account()` method to save your credentials for easy access later on, before initializing the service. This saves your credentials in the same place as `QiskitRuntimeService.save_account()`, so you can skip this step if you had previously used `QiskitRuntimeService` to save your account. *Note that account credentials are saved in plain text, so only do so if you are using a trusted device.*"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "54fc48c8-3a65-478c-9bc3-ec6eb4db425a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit_ibm_catalog import QiskitFunctionsCatalog\n",
|
|
"\n",
|
|
"# This can be skipped if you previously did QiskitRuntimeService.save_account()\n",
|
|
"QiskitFunctionsCatalog.save_account(token=\"<YOUR_IQP_API_TOKEN>\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5154cbea-ccfa-42ea-a9ef-c31699c3b2c0",
|
|
"metadata": {},
|
|
"source": [
|
|
"Once you have authenticated, you can list the functions from the Qiskit Functions Catalog that you have access to:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "55b73ad8-6e3b-4b76-8ba5-ae7ea339ffcc",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[QiskitFunction(ibm/circuit-function)]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"catalog.list()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a1c73799-30ec-44dc-b4b9-06e59d521d8c",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Run enabled functions\n",
|
|
"\n",
|
|
"Once a catalog object has been instantiated, you can select a function using `catalog.load(provider/function-name)`:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "4ac22236-2a34-46fb-890f-30d6ba70081e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"ibm_cf = catalog.load(\"ibm/circuit-function\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "deb844d3-051a-4db1-b0a1-4ba9801ef95e",
|
|
"metadata": {},
|
|
"source": [
|
|
"Each Qiskit Function has custom inputs, options, and outputs. Check the specific documentation pages for the function you want to run for more information. By default, all users can only run one function job at a time:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "9b9a7a3c-cc98-4c19-93cd-f59793515c70",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0ede4d0f-06d6-4360-86a1-5028ca511b3f\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"job = ibm_cf.run(instance=..., pubs=[], backend=\"backend_name\")\n",
|
|
"\n",
|
|
"job.job_id"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b1518e8e-04ea-4e4e-9f30-10f87dd4ee82",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Check job status\n",
|
|
"\n",
|
|
"<Admonition type=\"tip\">\n",
|
|
"Currently, the IBM Quantum workloads table only reflects Qiskit Runtime workloads. Use `job.status()` to see your Qiskit Function workload's current status.\n",
|
|
"</Admonition>\n",
|
|
"\n",
|
|
"With your Qiskit Function `job_id`, you can check the status of running jobs. This includes the following statuses:\n",
|
|
"\n",
|
|
"- **`QUEUED`**: The remote program is in the Qiskit Function queue. The queue priority is based on how much you've used Qiskit Functions.\n",
|
|
"- **`INITIALIZING`**: The remote program is starting; this includes setting up the remote environment and installing dependencies.\n",
|
|
"- **`RUNNING`**: The program is running.\n",
|
|
"- **`DONE`**: The program is complete, and you can retrieve result data with `job.results()`.\n",
|
|
"- **`ERROR`**: The program stopped running because of a problem. Use `job.result()` to get the error message.\n",
|
|
"- **`CANCELED`**: The program was canceled; either by a user, the service, or the server."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "d811be20-cf0d-4070-a126-b40af6a33132",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"'DONE'\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"job.status()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8e6528b5-c36f-48e1-bb2f-2416ccb12902",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Retrieve results\n",
|
|
"\n",
|
|
"Once a program is `DONE`, you can use `job.results()` to fetch the result. This output format varies with each function, so be sure to follow the specific documentation:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "04f6a446-fbc6-438a-bc07-00fc9d551d83",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"PubResult(data=DataBin(evs=np.ndarray(<shape=(20,), dtype=float64>), stds=np.ndarray(<shape=(20,), dtype=float64>), evs_noise_factors=np.ndarray(<shape=(20, 3), dtype=float64>), stds_noise_factors=np.ndarray(<shape=(20, 3), dtype=float64>), ensemble_stds_noise_factors=np.ndarray(<shape=(20, 3), dtype=float64>), evs_extrapolated=np.ndarray(<shape=(20, 2, 4), dtype=float64>), stds_extrapolated=np.ndarray(<shape=(20, 2, 4), dtype=float64>), shape=(20,)), metadata={'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {'zne': {'extrapolator': array(['exponential', 'exponential', 'exponential', 'exponential',\n",
|
|
" 'exponential', 'exponential', 'exponential', 'exponential',\n",
|
|
" 'exponential', 'exponential', 'exponential', 'exponential',\n",
|
|
" 'exponential', 'exponential', 'exponential', 'exponential',\n",
|
|
" 'exponential', 'exponential', 'exponential', 'exponential'],\n",
|
|
" dtype='<U11')}, 'layer_noise': {'noise_overhead': 1, 'total_mitigated_layers': 0, 'unique_mitigated_layers': 0, 'unique_mitigated_layers_noise_overhead': []}}, 'num_randomizations': 32})\n",
|
|
" \n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"result = job.result()\n",
|
|
"print(result)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "76c32136-f449-454c-896b-ff9d0edf9abc",
|
|
"metadata": {},
|
|
"source": [
|
|
"At any time, you can also cancel a job:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "7ddc2394-64a9-4fdd-bb3d-7729433fcd9c",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"'Job has been stopped.'\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"job.stop()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5b5d7c8d-7a96-41e2-93aa-528513acdd21",
|
|
"metadata": {},
|
|
"source": [
|
|
"### List previously run jobs run with Qiskit Functions\n",
|
|
"\n",
|
|
"You can use `jobs()` to list all jobs submitted to Qiskit Functions:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "c1a2832b-1ca3-4ef1-9a8a-3f3804b3aedf",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[<Job | 0ede4d0f-06d6-4360-86a1-5028ca511b3f>,\n",
|
|
" <Job | abf78e9a-b554-4e38-966a-f99cff877b8c>,\n",
|
|
" <Job | 90e1109e-809f-4768-a2dc-f45bf71a97b4>,\n",
|
|
" <Job | 313050f2-aa78-4d7d-99f4-44bdfe98e4d7>]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"old_jobs = catalog.jobs()\n",
|
|
"old_jobs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9137071a-e39c-4b66-afbd-697db38cc670",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Fetch error messages\n",
|
|
"\n",
|
|
"If a program status is `ERROR`, use `job.result()` to fetch the error message to help debug as follows:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "abfdf746-bc46-4df7-85b3-4a7a61ab7ca7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(job.result())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7c8c3e5d-1de2-4fed-be03-d134dc3fd907",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Next steps\n",
|
|
"\n",
|
|
"<Admonition type=\"info\" title=\"Recommendations\">\n",
|
|
"\n",
|
|
"- [Explore circuit functions](./ibm-circuit-function) to build new algorithms and applications, without needing to manage transpilation or error handling.\n",
|
|
"- [Explore application functions](./q-ctrl-optimization-solver) to solve domain-specific tasks, with classical inputs and outputs.\n",
|
|
"\n",
|
|
"</Admonition>"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"description": "Explore IBM Qiskit Functions Catalog, abstracted circuit function and application function services designed to accelerate research and application prototyping.",
|
|
"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"
|
|
},
|
|
"title": "Introduction to Qiskit Functions"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|