qiskit-documentation/docs/guides/local-testing-mode.ipynb

259 lines
9.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "8eb549ef-ba9c-486e-8b62-e90af6830102",
"metadata": {},
"source": [
"<span id=\"test-locally\"></span>\n",
"# Qiskit Runtime local testing mode"
]
},
{
"cell_type": "markdown",
"id": "9a2510a7-1f0a-40d4-914b-68bffff7a27a",
"metadata": {
"tags": [
"version-info"
]
},
"source": [
"<details>\n",
"<summary><b>Package versions</b></summary>\n",
"\n",
"The code on this page was developed using the following requirements.\n",
"We recommend using these versions or newer.\n",
"\n",
"```\n",
"qiskit[all]~=1.2.4\n",
"qiskit-aer~=0.15.1\n",
"qiskit-ibm-runtime~=0.31.0\n",
"qiskit-serverless~=0.17.1\n",
"qiskit-ibm-catalog~=0.1\n",
"```\n",
"</details>"
]
},
{
"cell_type": "markdown",
"id": "c0d59170-4f83-4396-9f35-ad78300d6b89",
"metadata": {},
"source": [
"Local testing mode (available with `qiskit-ibm-runtime` 0.22.0 or later) can be used to help develop and test programs before fine-tuning them and sending them to real quantum hardware. After using local testing mode to verify your program, all you need to change is the backend name to run it on a QPU.\n",
"\n",
"To use local testing mode, specify one of the fake backends from ``qiskit_ibm_runtime.fake_provider`` or specify a Qiskit Aer backend when instantiating a Qiskit Runtime primitive or a session.\n",
"\n",
"- **Fake backends**: The [fake backends](/api/qiskit-ibm-runtime/fake_provider) in ``qiskit_ibm_runtime.fake_provider`` mimic the behaviors of IBM&reg; QPUs by using QPU snapshots. The QPU snapshots contain important information about the QPU, such as the coupling map, basis gates, and qubit properties, which are useful for testing the transpiler and performing noisy simulations of the QPU. The noise model from the snapshot is automatically applied during simulation.\n",
"- **Aer simulator**: Simulators from [Qiskit Aer](simulate-with-qiskit-aer) provide higher-performance simulation that can handle larger circuits and [custom noise models](./build-noise-models). It also supports Clifford simulation mode, which can efficiently simulate Clifford circuits with a large number of qubits.\n",
"\n",
"\n",
"<Admonition type=\"note\" title=\"Notes\">\n",
"You can specify all Qiskit Runtime options in local testing mode. However, all options except shots are ignored when run on a local simulator.\n",
"</Admonition>\n",
"\n",
"\n",
"{/* If you currently use the Qiskit SDK, you can use the existing Qiskit Aer primitives for local testing. See [exact and noisy simulation with Qiskit Aer primitives](./simulate-with-qiskit-aer) for details. */}"
]
},
{
"cell_type": "markdown",
"id": "f03f205c-996f-461c-ac3f-9629689d3cc0",
"metadata": {},
"source": [
"## Fake backends example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1f3a8ee3-d4a5-4237-a56b-6b50ea061739",
"metadata": {},
"outputs": [],
"source": [
"from qiskit.circuit import QuantumCircuit\n",
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
"from qiskit_ibm_runtime.fake_provider import FakeManilaV2\n",
"\n",
"# Bell Circuit\n",
"qc = QuantumCircuit(2)\n",
"qc.h(0)\n",
"qc.cx(0, 1)\n",
"qc.measure_all()\n",
"\n",
"# Run the sampler job locally using FakeManilaV2\n",
"fake_manila = FakeManilaV2()\n",
"pm = generate_preset_pass_manager(backend=fake_manila, optimization_level=1)\n",
"isa_qc = pm.run(qc)\n",
"\n",
"# You can use a fixed seed to get fixed results.\n",
"options = {\"simulator\": {\"seed_simulator\": 42}}\n",
"sampler = Sampler(mode=fake_manila, options=options)\n",
"\n",
"result = sampler.run([isa_qc]).result()"
]
},
{
"cell_type": "markdown",
"id": "6483a298-211a-4a31-bea2-21670ff408db",
"metadata": {},
"source": [
"## AerSimulator examples\n",
"\n",
"Example with sessions, without noise:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c5edb89a-65e6-40fa-a541-bbdcf6cc7043",
"metadata": {},
"outputs": [],
"source": [
"from qiskit_aer import AerSimulator\n",
"from qiskit.circuit import QuantumCircuit\n",
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"from qiskit_ibm_runtime import Session, SamplerV2 as Sampler\n",
"\n",
"# Bell Circuit\n",
"qc = QuantumCircuit(2)\n",
"qc.h(0)\n",
"qc.cx(0, 1)\n",
"qc.measure_all()\n",
"\n",
"# Run the sampler job locally using AerSimulator.\n",
"# Session syntax is supported but ignored because local mode doesn't support sessions.\n",
"aer_sim = AerSimulator()\n",
"pm = generate_preset_pass_manager(backend=aer_sim, optimization_level=1)\n",
"isa_qc = pm.run(qc)\n",
"with Session(backend=aer_sim) as session:\n",
" sampler = Sampler(mode=session)\n",
" result = sampler.run([isa_qc]).result()"
]
},
{
"cell_type": "markdown",
"id": "e371b2b0-0f9f-4357-bdbd-7de4a5d77304",
"metadata": {},
"source": [
"To simulate with noise, specify a QPU (quantum hardware) and submit it to Aer. Aer builds a noise model based on the calibration data from that QPU, and instantiates an Aer backend with that model. If you prefer, you can [build a noise model](./build-noise-models).\n",
"\n",
"Example with noise:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5cba81b8-058e-4626-a72b-fad91a00410c",
"metadata": {},
"outputs": [],
"source": [
"from qiskit_aer import AerSimulator\n",
"from qiskit.circuit import QuantumCircuit\n",
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"from qiskit_ibm_runtime import SamplerV2 as Sampler, QiskitRuntimeService\n",
"\n",
"# Bell Circuit\n",
"qc = QuantumCircuit(2)\n",
"qc.h(0)\n",
"qc.cx(0, 1)\n",
"qc.measure_all()\n",
"\n",
"service = QiskitRuntimeService()\n",
"\n",
"# Specify a QPU to use for the noise model\n",
"real_backend = service.backend(\"ibm_brisbane\")\n",
"aer = AerSimulator.from_backend(real_backend)\n",
"\n",
"# Run the sampler job locally using AerSimulator.\n",
"pm = generate_preset_pass_manager(backend=aer, optimization_level=1)\n",
"isa_qc = pm.run(qc)\n",
"sampler = Sampler(mode=aer)\n",
"result = sampler.run([isa_qc]).result()"
]
},
{
"cell_type": "markdown",
"id": "3647532a-9a58-4931-965d-13794416f9a8",
"metadata": {},
"source": [
"<span id=\"clifford-sim\"></span>\n",
"### Clifford simulation\n",
"\n",
"Because Clifford circuits can be simulated efficiently with verifiable results, Clifford simulation is a very useful tool. For an in-depth example, see [Efficient simulation of stabilizer circuits with Qiskit Aer primitives](./simulate-stabilizer-circuits).\n",
"\n",
"Example:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "061dfd9b-766d-4aaf-a3a3-ce6b1c1e7eca",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from qiskit.circuit.library import EfficientSU2\n",
"from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
"\n",
"n_qubits = 500 # <---- note this uses 500 qubits!\n",
"circuit = EfficientSU2(n_qubits)\n",
"circuit.measure_all()\n",
"\n",
"rng = np.random.default_rng(1234)\n",
"params = rng.choice(\n",
" [0, np.pi / 2, np.pi, 3 * np.pi / 2],\n",
" size=circuit.num_parameters,\n",
")\n",
"\n",
"# Tell Aer to use the stabilizer (clifford) simulation method\n",
"aer_sim = AerSimulator(method=\"stabilizer\")\n",
"\n",
"pm = generate_preset_pass_manager(backend=aer_sim, optimization_level=1)\n",
"isa_qc = pm.run(qc)\n",
"sampler = Sampler(mode=aer_sim)\n",
"result = sampler.run([isa_qc]).result()"
]
},
{
"cell_type": "markdown",
"id": "40fe0cb0-74bf-4bf4-afec-a66c7b166b0c",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
" - Review detailed [primitives examples.](./primitives-examples)\n",
" - Read [Migrate to V2 primitives](/migration-guides/v2-primitives).\n",
" - Practice with primitives by working through the [Cost function lesson](https://learning.quantum.ibm.com/course/variational-algorithm-design/cost-functions#primitives) in IBM Quantum Learning.\n",
" - Learn how to transpile locally in the [Transpile](./transpile) section.\n",
" - Try the [Submit pre-transpiled circuits](https://learning.quantum.ibm.com/tutorial/submitting-user-transpiled-circuits-using-primitives) tutorial.\n",
"</Admonition>"
]
}
],
"metadata": {
"description": "Use local testing mode to simulate quantum hardware in Qiskit Runtime",
"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": "Qiskit Runtime local testing mode"
},
"nbformat": 4,
"nbformat_minor": 2
}