Skip to content

Commit

Permalink
Migrate tutorials and a how-to guide to v2 primitives (#552)
Browse files Browse the repository at this point in the history
* Use Qiskit Runtime SamplerV2 in local mode instead of `BackendSampler`

* Bump qiskit-ibm-runtime version

There will be no 0.22.1 release, it seems, but 0.23.0 is out now
so we can use it instead.

* Migrate first cutting tutorial to v2 primitives

* Migrate first tutorial to use a fake backend

but now it executes very slowly

* Change to `FakeManilaV2`, a 5-qubit backend

Now the execution time is reasonable.

* Bump qiskit-aer minimum version to 0.14.0

* Bump qiskit-aer version to 0.14.0.1

It appears there is no 0.14.0 release on pypi

* Update remaining tutorials and a how-to

* s/simulator/local simulator/;
  • Loading branch information
garrison authored Apr 19, 2024
1 parent 299c849 commit 6cc05a4
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 174 deletions.
14 changes: 8 additions & 6 deletions docs/circuit_cutting/how-tos/how_to_specify_cut_wires.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"import numpy as np\n",
"from qiskit import QuantumCircuit\n",
"from qiskit.quantum_info import SparsePauliOp\n",
"from qiskit_aer.primitives import Estimator, Sampler\n",
"from qiskit_aer.primitives import EstimatorV2, SamplerV2\n",
"\n",
"from circuit_knitting.cutting import (\n",
" partition_problem,\n",
Expand Down Expand Up @@ -336,9 +336,9 @@
"metadata": {},
"outputs": [],
"source": [
"sampler = Sampler(run_options={\"shots\": 2**12})\n",
"sampler = SamplerV2()\n",
"results = {\n",
" label: sampler.run(subexperiment).result()\n",
" label: sampler.run(subexperiment, shots=2**12).result()\n",
" for label, subexperiment in subexperiments.items()\n",
"}"
]
Expand Down Expand Up @@ -376,8 +376,10 @@
}
],
"source": [
"estimator = Estimator(run_options={\"shots\": None}, approximation=True)\n",
"exact_expval = estimator.run(qc_0, observable).result().values[0]\n",
"estimator = EstimatorV2()\n",
"exact_expval = (\n",
" estimator.run([(qc_0.decompose(\"cut_wire\"), observable)]).result()[0].data.evs\n",
")\n",
"print(f\"Reconstructed expectation value: {np.real(np.round(final_expval, 8))}\")\n",
"print(f\"Exact expectation value: {np.round(exact_expval, 8)}\")\n",
"print(f\"Error in estimation: {np.real(np.round(final_expval-exact_expval, 8))}\")\n",
Expand All @@ -403,7 +405,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.0"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,68 +241,91 @@
},
{
"cell_type": "markdown",
"id": "d8870454-2173-4454-90b4-a034779510e0",
"id": "444b0038-b3d5-469c-85b2-4c1d9d8fce05",
"metadata": {},
"source": [
"### Run the subexperiments using the Qiskit Sampler primitive"
"### Choose a backend\n",
"\n",
"Here we are using a fake backend, which will result in Qiskit Runtime running in local mode (i.e., on a local simulator)."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7430eef1",
"id": "36c89aa0-70aa-4615-8198-77ec85e8aa93",
"metadata": {},
"outputs": [],
"source": [
"from qiskit_aer.primitives import Sampler\n",
"\n",
"# Set up a Qiskit Aer Sampler primitive for each circuit partition\n",
"samplers = {\n",
" label: Sampler(run_options={\"shots\": 2**12}) for label in subexperiments.keys()\n",
"}\n",
"from qiskit_ibm_runtime.fake_provider import FakeManilaV2\n",
"\n",
"# Retrieve results from each partition's subexperiments\n",
"results = {\n",
" label: sampler.run(subexperiments[label]).result()\n",
" for label, sampler in samplers.items()\n",
"}"
"backend = FakeManilaV2()"
]
},
{
"cell_type": "markdown",
"id": "68eb0522-8c01-4f56-aacc-0bfc60159896",
"id": "85a1d76a-5c47-4210-a742-9b22f02f7200",
"metadata": {},
"source": [
"To use the Qiskit Runtime Sampler, replace the code above with this commented block."
"### Prepare the subexperiments for the backend\n",
"\n",
"We must transpile the circuits with our backend as the target before submitting them to Qiskit Runtime."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3c9a7e78-4bf9-4dbd-a8f9-3db185591d9c",
"id": "184e0f36-1279-48a3-aab7-b7603bb71f66",
"metadata": {},
"outputs": [],
"source": [
"# from qiskit_ibm_runtime import Session, Options, Sampler, QiskitRuntimeService\n",
"# from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"#\n",
"# service = QiskitRuntimeService()\n",
"# backend = service.least_busy(operational=True, simulator=False)\n",
"#\n",
"# # Prepare transpiler for target backend\n",
"# pass_manager = generate_preset_pass_manager(optimization_level=1, backend=backend)\n",
"#\n",
"# with Session(backend=backend) as session:\n",
"# # Set up Qiskit Runtime Sampler primitives.\n",
"# samplers = {\n",
"# label: Sampler(Options(execution={\"shots\": 2**12})) for label in subexperiments.keys()\n",
"# }\n",
"#\n",
"# # Retrieve results from each subexperiment\n",
"# results = {\n",
"# label: sampler.run(pass_manager.run(subexperiments[label])).result()\n",
"# for label, sampler in samplers.items()\n",
"# }"
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"\n",
"# Transpile the subexperiments to ISA circuits\n",
"pass_manager = generate_preset_pass_manager(optimization_level=1, backend=backend)\n",
"isa_subexperiments = {\n",
" label: pass_manager.run(partition_subexpts)\n",
" for label, partition_subexpts in subexperiments.items()\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "d8870454-2173-4454-90b4-a034779510e0",
"metadata": {},
"source": [
"### Run the subexperiments using the Qiskit Runtime Sampler primitive"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3bc1c07b-b286-4a5f-8b0a-67efd4a6309e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/garrison/Qiskit/qiskit-ibm-runtime/qiskit_ibm_runtime/session.py:157: UserWarning: Session is not supported in local testing mode or when using a simulator.\n",
" warnings.warn(\n"
]
}
],
"source": [
"from qiskit_ibm_runtime import SamplerV2, Batch\n",
"\n",
"# Set up a Qiskit Runtime Sampler primitive for each circuit partition\n",
"samplers = {label: SamplerV2(backend=backend) for label in subexperiments.keys()}\n",
"\n",
"# Submit each partition's subexperiments as a single batch\n",
"with Batch(backend=backend):\n",
" jobs = {\n",
" label: sampler.run(isa_subexperiments[label], shots=2**12)\n",
" for label, sampler in samplers.items()\n",
" }\n",
"\n",
"# Retrive results\n",
"results = {label: job.result() for label, job in jobs.items()}"
]
},
{
Expand All @@ -317,7 +340,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "7d57339c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -345,26 +368,26 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"id": "e3385ba5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reconstructed expectation value: 0.52991384\n",
"Reconstructed expectation value: 0.6991539\n",
"Exact expectation value: 0.56254612\n",
"Error in estimation: -0.03263228\n",
"Relative error in estimation: -0.05800819\n"
"Error in estimation: 0.13660778\n",
"Relative error in estimation: 0.24283836\n"
]
}
],
"source": [
"from qiskit_aer.primitives import Estimator\n",
"from qiskit_aer.primitives import EstimatorV2\n",
"\n",
"estimator = Estimator(run_options={\"shots\": None}, approximation=True)\n",
"exact_expval = estimator.run(qc, observable).result().values[0]\n",
"estimator = EstimatorV2()\n",
"exact_expval = estimator.run([(qc, observable)]).result()[0].data.evs\n",
"print(f\"Reconstructed expectation value: {np.real(np.round(final_expval, 8))}\")\n",
"print(f\"Exact expectation value: {np.round(exact_expval, 8)}\")\n",
"print(f\"Error in estimation: {np.real(np.round(final_expval-exact_expval, 8))}\")\n",
Expand All @@ -390,7 +413,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.0"
}
},
"nbformat": 4,
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 6cc05a4

Please sign in to comment.