qiskit-documentation/docs/guides/algorithmiq-tem.ipynb

429 lines
20 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "f7d9993f",
"metadata": {},
"source": [
"{/* cspell:ignore POVM, mathbf, Filippov, Lindblad, Leahy, Rossi, García, Pérez, Minev, Kandala, Temme, informationally */}"
]
},
{
"cell_type": "markdown",
"id": "dde95705",
"metadata": {},
"source": [
"# Tensor-network error mitigation (TEM): A Qiskit Function by Algorithmiq\n",
"\n",
"<LegacyContent>\n",
"<Admonition type=\"note\" title=\"Notes\">\n",
"* This documentation is relevant to IBM Quantum&reg; Platform Classic. If you need the newer version, go to the new [IBM Quantum Platform documentation.](https://quantum.cloud.ibm.com/docs/guides/algorithmiq-tem)\n",
"* Qiskit Functions are an experimental feature available only to IBM Quantum&reg; Premium Plan users. They are in preview release status and subject to change.\n",
"</Admonition>\n",
"</LegacyContent>\n",
"<CloudContent>\n",
"<Admonition type=\"note\" title=\"Notes\">\n",
"* This documentation is relevant to the new IBM Quantum&reg; Platform. If you need the previous version, return to the [IBM Quantum Platform Classic documentation.](https://docs.quantum.ibm.com/guides/algorithmiq-tem)\n",
"* Qiskit Functions are an experimental feature available only to IBM Quantum&reg; Premium Plan users. They are in preview release status and subject to change.\n",
"</Admonition>\n",
"</CloudContent>"
]
},
{
"cell_type": "markdown",
"id": "a5773f7a",
"metadata": {
"tags": [
"version-info"
]
},
"source": []
},
{
"cell_type": "markdown",
"id": "47066e9a",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"Algorithmiqs Tensor-network Error Mitigation (TEM) method is a hybrid\n",
"quantum-classical algorithm designed for performing noise mitigation entirely at\n",
"the classical post-processing stage. With TEM, the user can compute the\n",
"expectation values of observables mitigating the inevitable noise-induced errors\n",
"that occur on quantum hardware with increased accuracy and cost efficiency,\n",
"making it a highly attractive option for quantum researchers and industry\n",
"practitioners alike.\n",
"\n",
"The method consists of constructing a tensor network representing the inverse of\n",
"the global noise channel affecting the state of the quantum processor and then\n",
"applying the map to informationally complete measurement outcomes acquired from\n",
"the noisy state to obtain unbiased estimators for the observables.\n",
"\n",
"As an advantage, TEM leverages informationally complete measurements to give\n",
"access to a vast set of mitigated expectation values of observables and has\n",
"optimal sampling overhead on the quantum hardware, as described in Filippov at\n",
"al. (2023), [arXiv:2307.11740](https://arxiv.org/abs/2307.11740), and Filippov\n",
"at al. (2024), [arXiv:2403.13542](https://arxiv.org/abs/2403.13542). The\n",
"measurement overhead refers to the number of additional measurements required to\n",
"perform efficient error mitigation, a critical factor in the feasibility of\n",
"quantum computations. Therefore, TEM has the potential to enable quantum\n",
"advantage in complex scenarios, such as applications in the fields of quantum\n",
"chaos, many-body physics, Hubbard dynamics, and small molecule chemistry\n",
"simulations.\n",
"\n",
"The main features and benefits of TEM can be summarized as:\n",
"\n",
"1. **Optimal measurement overhead**: TEM is optimal with respect to\n",
"[theoretical bounds](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.131.210601),\n",
"meaning that no method can achieve a smaller measurement overhead. In other\n",
"words, TEM requires the minimum number of additional measurements to perform\n",
"error mitigation. This in turns means that TEM uses minimal quantum runtime.\n",
"2. **Cost-effectiveness**: Since TEM handles noise mitigation entirely in the\n",
"post-processing stage, there is no need to add extra circuits to the quantum\n",
"computer, which not only makes the computation cheaper but also diminishes the\n",
"risk of introducing additional errors due to the imperfections of quantum\n",
"devices.\n",
"3. **Estimation of multiple observables**: Thanks to informationally-complete\n",
"measurements, TEM efficiently estimates multiple observables with the same\n",
"measurement data from the quantum computer.\n",
"4. **Measurement error mitigation**: The TEM Qiskit Function also includes a\n",
"[proprietary measurement error mitigation method](https://journals.aps.org/prresearch/abstract/10.1103/PhysRevResearch.5.033154)\n",
"able to significantly reduce readout errors after a short calibration run.\n",
"5. **Accuracy**: TEM significantly improves the accuracy and reliability of\n",
"digital quantum simulations, making quantum algorithms more precise and\n",
"dependable."
]
},
{
"cell_type": "markdown",
"id": "5f761442",
"metadata": {},
"source": [
"## Description\n",
"\n",
"The TEM function allows you to obtain error-mitigated expectation values for\n",
"multiple observables on a quantum circuit with minimal sampling overhead. The\n",
"circuit is measured with an informationally complete positive operator-valued\n",
"measure (IC-POVM), and the collected measurement outcomes are processed on a\n",
"classical computer. This measurement is used to perform the tensor network\n",
"methods and build a noise-inversion map. The function applies a map that fully\n",
"inverts the whole noisy circuit using tensor networks to represent the noisy\n",
"layers.\n",
"\n",
"![TEM schematics](/docs/images/guides/algorithmiq-tem/tem_scheme.svg \"Error-mitigated estimation of an observable O via post-processing measurement outcomes of the noisy quantum processor. U and N denote an ideal quantum operation and the associated noise map, which can be generally non-local (and extended to grey boxes). D stands for a tensor of operators that are dual to the effects in the IC measurement. The noise mitigation module M is a tensor network that is efficiently contracted from the middle out. The first iteration of the contraction is represented by the dotted purple line, the second one by the dashed line, and the third one by the solid line.\")\n",
"\n",
"Once the circuits are submitted to the function, they are transpiled and\n",
"optimized to minimize the number of layers with two-qubit gates (the noisier\n",
"gates on quantum devices). The noise affecting the layers is learned through\n",
"[Qiskit Runtime](/docs/api/qiskit-ibm-runtime/noise-learner-noise-learner)\n",
"using a sparse Pauli-Lindblad noise model as described in E. van den Berg, Z.\n",
"Minev, A. Kandala, K. Temme, Nat. Phys. (2023).\n",
"[arXiv:2201.09866](https://arxiv.org/abs/2201.09866).\n",
"\n",
"The noise model is an accurate description of the noise on the device able to\n",
"capture subtle features, including qubit cross-talk. However, noise on the\n",
"devices can fluctuate and drift and the learned noise might not be accurate at\n",
"the point at which the estimation is done. This might result in inaccurate\n",
"results."
]
},
{
"cell_type": "markdown",
"id": "2995da29",
"metadata": {},
"source": [
"## Get started\n",
"\n",
"<LegacyContent>\n",
"Authenticate using your [IBM Quantum Platform API key](http://quantum.ibm.com/), and select the TEM function as follows:\n",
"</LegacyContent>\n",
"<CloudContent>\n",
"Authenticate using your [IBM Quantum Platform API key](http://quantum.cloud.ibm.com/), and select the TEM function as follows:\n",
"</CloudContent>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc0a8093",
"metadata": {},
"outputs": [],
"source": [
"from qiskit_ibm_catalog import QiskitFunctionsCatalog\n",
"\n",
"tem_function_name = \"algorithmiq/tem\"\n",
"\n",
"catalog = QiskitFunctionsCatalog()\n",
"\n",
"# Load your function\n",
"tem = catalog.load(tem_function_name)"
]
},
{
"cell_type": "markdown",
"id": "ae7adcc7",
"metadata": {},
"source": [
"## Example\n",
"\n",
"\n",
"The following snippet shows an example where TEM is used to compute the expectation values of an observable given a simple quantum circuit."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab05b87f",
"metadata": {},
"outputs": [],
"source": [
"from qiskit import QuantumCircuit\n",
"from qiskit.quantum_info import SparsePauliOp\n",
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
"\n",
"# Create a quantum circuit\n",
"qc = QuantumCircuit(3)\n",
"qc.u(0.4, 0.9, -0.3, 0)\n",
"qc.u(-0.4, 0.2, 1.3, 1)\n",
"qc.u(-1.2, -1.2, 0.3, 2)\n",
"for _ in range(2):\n",
" qc.barrier()\n",
" qc.cx(0, 1)\n",
" qc.cx(2, 1)\n",
" qc.barrier()\n",
" qc.u(0.4, 0.9, -0.3, 0)\n",
" qc.u(-0.4, 0.2, 1.3, 1)\n",
" qc.u(-1.2, -1.2, 0.3, 2)\n",
"\n",
"# Define the observables\n",
"observable = SparsePauliOp(\"IYX\", 1.0)\n",
"\n",
"# Define the execution options\n",
"service = QiskitRuntimeService()\n",
"backend_name = service.least_busy(operational=True).name\n",
"\n",
"# The following line gets the instance of the active QiskitRuntimeService account.\n",
"instance = service.active_account()[\"instance\"]\n",
"\n",
"pub = (qc, [observable])\n",
"options = {\"default_precision\": 0.02}\n",
"\n",
"job = tem.run(\n",
" pubs=[pub], instance=instance, backend_name=backend_name, options=options\n",
")"
]
},
{
"cell_type": "markdown",
"id": "6e2a97e2",
"metadata": {},
"source": [
"Use the Qiskit Serverless APIs to check your Qiskit Function workload's status:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "47b0db71",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"QUEUED\n"
]
}
],
"source": [
"print(job.status())"
]
},
{
"cell_type": "markdown",
"id": "fbaeace8",
"metadata": {},
"source": [
"You can return the results as:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ad5b9a53",
"metadata": {},
"outputs": [],
"source": [
"result = job.result()\n",
"evs = result[0].data.evs"
]
},
{
"cell_type": "markdown",
"id": "e5e138d3",
"metadata": {},
"source": [
"<Admonition type=\"info\">\n",
" The expected value for the noiseless circuit for the given operator should be around `0.18409094298943401`.\n",
"</Admonition>"
]
},
{
"cell_type": "markdown",
"id": "c6ffb685",
"metadata": {},
"source": [
"## Inputs\n",
"\n",
"**Parameters**\n",
"<LegacyContent>\n",
"Name | Type | Description | Required | Default | Example\n",
"-- | -- | -- | -- | -- | --\n",
"`pubs` | Iterable[EstimatorPubLike] | An iterable of PUB-like (primitive unified bloc) objects, such as tuples `(circuit, observables)` or `(circuit, observables, parameters, precision)`. See [Overview of PUBs](/docs/guides/primitive-input-output#overview-of-pubs) for more information. The circuits don't need to be ISA circuits. | Yes | N/A | (circuit, observables)\n",
"`backend_name` | str | Name of the backend to make the query.| No | If not provided, the least busy backend will be used. | \"ibm_fez\"\n",
"`instance` | str | The hub/group/project to use in that format. | Yes | N/A |\"hub1/group1/project1\"\n",
"`options` | dict | Input options. See `Options` section for more details. | No | See `Options` section for more details.| \\{\"max_bond_dimension\": 100\\}\n",
"\n",
"</LegacyContent>\n",
"<CloudContent>\n",
"Name | Type | Description | Required | Default | Example\n",
"-- | -- | -- | -- | -- | --\n",
"`pubs` | Iterable[EstimatorPubLike] | An iterable of PUB-like (primitive unified bloc) objects, such as tuples `(circuit, observables)` or `(circuit, observables, parameters, precision)`. See [Overview of PUBs](/docs/guides/primitive-input-output#overview-of-pubs) for more information. The circuits don't need to be ISA circuits. | Yes | N/A | (circuit, observables)\n",
"`backend_name` | str | Name of the backend to make the query.| No | If not provided, the least busy backend will be used. | \"ibm_fez\"\n",
"`instance` | str | The cloud resource name ov the instance to use in that format. | Yes | N/A |\"CRN\"\n",
"`options` | dict | Input options. See `Options` section for more details. | No | See `Options` section for more details.| \\{\"max_bond_dimension\": 100\\}\n",
"</CloudContent>\n",
"\n",
"<Admonition type=\"caution\">\n",
" TEM currently has the following limitations:\n",
"\n",
" - Parametrized circuits are not supported. The parameters argument should be set to `None` if precision is specified.\n",
" - Only circuits with linear connectivity are supported. This restriction will be removed in future versions.\n",
" - Non-unitary gates, such as reset, measure, and all forms of control flow are not supported. Support for reset will be added in upcoming releases.\n",
"</Admonition>"
]
},
{
"cell_type": "markdown",
"id": "1ebd5f9b",
"metadata": {},
"source": [
"### Options\n",
"\n",
"A dictionary containing the advanced options for the TEM. The dictionary may contain the keys in the following table. If any of the options are not provided, the default value listed in the table will be used. The default values are good for typical use of TEM.\n",
"\n",
"Name | Choices | Description | Default\n",
"-- | -- | -- | --\n",
"`tem_max_bond_dimension` | int | The maximum bond dimension to be used for the tensor networks. | 500 |\n",
"`tem_compression_cutoff` | float | The cutoff value to be used for the tensor networks. | 1e-16\n",
"`compute_shadows_bias_from_observable` | bool | A boolean flag indicating whether the bias for the classical shadows measurement protocol should be tailored to the PUB observable or not. If False, the classical shadows protocol (equal probability of measuring Z, X, Y) will be used.| False\n",
"`shadows_bias` | np.ndarray | The bias to be used for the randomized classical shadows measurement protocol, a 1d or 2d array of size 3 or shape (num_qubits, 3) respectively. order is ZXY | np.array([1 / 3, 1 / 3, 1 / 3])\n",
"`max_execution_time` | int or `None` | The maximum execution time on the QPU in seconds. If the runtime exceeds this value, the job will be canceled. If `None`, a default limit set by Qiskit Runtime will apply. | `None`\n",
"`num_randomizations` | int | The number of randomizations to be used for noise learning and gate twirling. | 32\n",
"`max_layers_to_learn` | int | The maximum number of unique layers to learn. | 4\n",
"`mitigate_readout_error` | bool | A Boolean flag indicating whether to perform readout error mitigation or not. | True\n",
"`num_readout_calibration_shots` | int | The number of shots to be used for readout error mitigation. | 10000\n",
"`default_precision` | float | The default precision to be used for the PUBs for which the precision is not specified. |0.02\n",
"`seed` | int or `None` | Set the seed of the random number generator for reproducibility. If `None`, don't set the seed. | None"
]
},
{
"cell_type": "markdown",
"id": "7a04364c",
"metadata": {},
"source": [
"## Outputs\n",
"\n",
"A Qiskit [PrimitiveResults](/docs/api/qiskit/qiskit.primitives.PrimitiveResult) containing the TEM-mitigated result. The result for each PUB is returned as a [PubResult](/docs/api/qiskit/qiskit.primitives.PubResult) containing the following fields:\n",
"\n",
"\n",
"Name |Type | Description\n",
"-- | -- | --\n",
"data | DataBin | A Qiskit [DataBin](/docs/api/qiskit/qiskit.primitives.DataBin) containing the TEM mitigated observable and its standard error. The DataBin has the following fields: <ul><li>`evs`: The TEM-mitigated observable value.</li><li>`stds`: The standard error of the TEM-mitigated observable.</li></ul>\n",
"metadata | dict | A dictionary containing additional results. The dictionary contains the following keys: <ul><li>`\"evs_non_mitigated\"`: The observable value without error mitigation.</li><li>`\"stds_non_mitigated\"`: The standard error of the result without error mitigation.</li><li>`\"evs_mitigated_no_readout_mitigation\"`: The observable value with error mitigation but without readout error mitigation.</li><li>`\"stds_mitigated_no_readout_mitigation\"`: The standard error of the result with error mitigation but without readout error mitigation.</li><li>`\"evs_non_mitigated_with_readout_mitigation\"`: The observable value without error mitigation but with readout error mitigation.</li><li>`\"stds_non_mitigated_with_readout_mitigation\"`: The standard error of the result without error mitigation but with readout error mitigation.</li></ul>"
]
},
{
"cell_type": "markdown",
"id": "fd045ee6",
"metadata": {},
"source": [
"## Fetching error messages\n",
"\n",
"If your workload status is ERROR, use job.result() to fetch the error message as follows:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0433171c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PrimitiveResult([PubResult(data=DataBin(evs=np.ndarray(<shape=(1,), dtype=float64>), stds=np.ndarray(<shape=(1,), dtype=float64>)), metadata={'evs_non_mitigated': array([0.07311669]), 'stds_non_mitigated': array([0.05869849]), 'evs_mitigated_no_readout_mitigation': array([0.07794912]), 'stds_mitigated_no_readout_mitigation': array([0.06326868]), 'evs_non_mitigated_with_readout_mitigation': array([0.07334523]), 'stds_non_mitigated_with_readout_mitigation': array([0.06148914])})], metadata={})\n"
]
}
],
"source": [
"print(job.result())"
]
},
{
"cell_type": "markdown",
"id": "e9ec2e67",
"metadata": {},
"source": [
"## Get support\n",
"\n",
"Reach out to [qiskit_ibm@algorithmiq.fi](mailto:qiskit_ibm@algorithmiq.fi)\n",
"\n",
"Be sure to include the following information:\n",
"- Qiskit Function Job ID (`qiskit-ibm-catalog`), `job.job_id`\n",
"- A detailed description of the issue\n",
"- Any relevant error messages or codes\n",
"- Steps to reproduce the issue"
]
},
{
"cell_type": "markdown",
"id": "5a6a25c8",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
"\n",
"- [Request access to Algorithmiq Tensor-network error mitigation](https://quantum.ibm.com/functions?id=4b1b9d76-c18b-4788-b70b-15125111fbe6)\n",
"\n",
"</Admonition>"
]
}
],
"metadata": {
"description": "Introduction to TEM, a Qiskit Function by Algorithmiq, to compute estimations with software post-processing error mitigation using tensor networks.",
"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": "Tensor-network error mitigation (TEM) - A Qiskit Function by Algorithmiq"
},
"nbformat": 4,
"nbformat_minor": 5
}