From 2d2fb9c2436f266e34b3550121058a2cb4c8fe6b Mon Sep 17 00:00:00 2001 From: Frank Harkins Date: Tue, 10 Dec 2024 10:56:36 +0000 Subject: [PATCH] Make notebooks runnable with nb-tester --- docs/guides/algorithmiq-tem.ipynb | 4 +- docs/guides/functions.ipynb | 72 ++++++++++--------- docs/guides/ibm-circuit-function.ipynb | 6 +- docs/guides/q-ctrl-optimization-solver.ipynb | 33 ++++++--- .../q-ctrl-performance-management.ipynb | 38 ++++++---- docs/guides/qedma-qesem.ipynb | 27 +++++-- docs/guides/qunasys-quri-chemistry.ipynb | 28 +++++++- 7 files changed, 144 insertions(+), 64 deletions(-) diff --git a/docs/guides/algorithmiq-tem.ipynb b/docs/guides/algorithmiq-tem.ipynb index 26da891722d..7adba4b0c87 100644 --- a/docs/guides/algorithmiq-tem.ipynb +++ b/docs/guides/algorithmiq-tem.ipynb @@ -175,7 +175,9 @@ "service = QiskitRuntimeService()\n", "backend_name = service.least_busy(operational=True).name\n", "\n", - "instance = \"\"\n", + "# Instance is a string of the form \"\".\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", diff --git a/docs/guides/functions.ipynb b/docs/guides/functions.ipynb index 0aefa7c5f51..ca997d307e7 100644 --- a/docs/guides/functions.ipynb +++ b/docs/guides/functions.ipynb @@ -58,37 +58,15 @@ "\n", " **If you are working in a trusted Python environment (such as on a personal laptop or workstation),** use the `save_account()` method to save your credentials locally. ([Skip to the next step](#functions-untrusted) if you are not using a trusted environment, such as a shared or public computer, to authenticate to IBM Quantum Platform.)\n", "\n", - " To use `save_account()`, run `python` in your shell to open a REPL (read-eval-print loop), then enter the following:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "24dc565f-bb5d-4164-ba44-fc04c2fcaafd", - "metadata": {}, - "outputs": [], - "source": [ - "token = \"\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "32919d16-4b5e-439a-8cda-33687a3d96b4", - "metadata": {}, - "outputs": [], - "source": [ - "from qiskit_ibm_catalog import QiskitFunctionsCatalog\n", + " To use `save_account()`, run `python` in your shell to open a REPL (read-eval-print loop), then enter the following:\n", "\n", - "QiskitFunctionsCatalog.save_account(token=token)" - ] - }, - { - "cell_type": "markdown", - "id": "e458aad5-fa7c-4ba1-8e9e-be770d89a04a", - "metadata": {}, - "source": [ - "Close out of the REPL with `exit()`. From now on, whenever you need to authenticate to the service, you can load your credentials with `QiskitFunctionsCatalog()`." + " ```python\n", + " from qiskit_ibm_catalog import QiskitFunctionsCatalog\n", + "\n", + " QiskitFunctionsCatalog.save_account(token=\"\")\n", + " ```\n", + "\n", + " Close out of the REPL with `exit()`. From now on, whenever you need to authenticate to the service, you can load your credentials with `QiskitFunctionsCatalog()`." ] }, { @@ -99,6 +77,8 @@ "outputs": [], "source": [ "# Load saved credentials\n", + "from qiskit_ibm_catalog import QiskitFunctionsCatalog\n", + "\n", "catalog = QiskitFunctionsCatalog()" ] }, @@ -190,7 +170,31 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, + "id": "139ac1d5-3727-4f44-a26c-0d8a1d8a58f2", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This cell is hidden from users\n", + "# It gets these details programatically so we can test this notebook\n", + "from qiskit_ibm_runtime import QiskitRuntimeService\n", + "from qiskit.circuit.random import random_circuit\n", + "\n", + "service = QiskitRuntimeService()\n", + "instance = service.active_account()[\"instance\"]\n", + "backend_name = service.least_busy().name\n", + "\n", + "circuit = random_circuit(num_qubits=2, depth=2, seed=42)\n", + "observable = \"Z\" * circuit.num_qubits" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "9b9a7a3c-cc98-4c19-93cd-f59793515c70", "metadata": {}, "outputs": [ @@ -203,7 +207,11 @@ } ], "source": [ - "job = ibm_cf.run(instance=..., pubs=[], backend=\"backend_name\")\n", + "job = ibm_cf.run(\n", + " pubs=[(circuit, observable)],\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_kyiv\"\n", + ")\n", "\n", "job.job_id" ] diff --git a/docs/guides/ibm-circuit-function.ipynb b/docs/guides/ibm-circuit-function.ipynb index 8fdfb156ca8..949f09932b2 100644 --- a/docs/guides/ibm-circuit-function.ipynb +++ b/docs/guides/ibm-circuit-function.ipynb @@ -342,7 +342,11 @@ "cell_type": "code", "execution_count": 8, "id": "4070e592", - "metadata": {}, + "metadata": { + "tags": [ + "raises-exception" + ] + }, "outputs": [ { "name": "stdout", diff --git a/docs/guides/q-ctrl-optimization-solver.ipynb b/docs/guides/q-ctrl-optimization-solver.ipynb index 5cdaafb6950..ed0ade71c55 100644 --- a/docs/guides/q-ctrl-optimization-solver.ipynb +++ b/docs/guides/q-ctrl-optimization-solver.ipynb @@ -139,11 +139,6 @@ "# If you have not previously saved your credentials, follow instructions at\n", "# https://docs.quantum.ibm.com/guides/setup-channel#iqp\n", "# to authenticate with your API token.\n", - "hub = \"\"\n", - "group = \"\"\n", - "project = \"\"\n", - "\n", - "# Authentication\n", "catalog = QiskitFunctionsCatalog()\n", "\n", "# Access Function\n", @@ -252,7 +247,26 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, + "id": "cf53550c", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This cell is hidden from users\n", + "from qiskit_ibm_runtime import QiskitRuntimeService\n", + "\n", + "service = QiskitRuntimeService()\n", + "instance = service.active_account()[\"instance\"]\n", + "backend_name = service.least_busy().name" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "16c66d64", "metadata": {}, "outputs": [], @@ -261,7 +275,8 @@ "maxcut_job = solver.run(\n", " problem=problem_as_str,\n", " problem_type=\"maxcut\",\n", - " instance=hub + \"/\" + group + \"/\" + project,\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_kyiv\"\n", ")" ] }, @@ -476,7 +491,9 @@ "source": [ "# Solve the problem\n", "mvc_job = solver.run(\n", - " problem=srepr(cost_function), instance=hub + \"/\" + group + \"/\" + project\n", + " problem=srepr(cost_function),\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_kyiv\"\n", ")" ] }, diff --git a/docs/guides/q-ctrl-performance-management.ipynb b/docs/guides/q-ctrl-performance-management.ipynb index 04b71d81705..1c9ad5fca0f 100644 --- a/docs/guides/q-ctrl-performance-management.ipynb +++ b/docs/guides/q-ctrl-performance-management.ipynb @@ -103,11 +103,6 @@ "# If you have not previously saved your credentials, follow instructions at\n", "# https://docs.quantum.ibm.com/guides/functions\n", "# to authenticate with your API token.\n", - "hub = \"\"\n", - "group = \"\"\n", - "project = \"\"\n", - "\n", - "# Authentication\n", "catalog = QiskitFunctionsCatalog()\n", "\n", "# Access Function\n", @@ -203,6 +198,25 @@ "Run the circuit and optionally define the backend and number of shots." ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b892956", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This cell is hidden from users\n", + "from qiskit_ibm_runtime import QiskitRuntimeService\n", + "\n", + "service = QiskitRuntimeService()\n", + "instance = service.active_account()[\"instance\"]\n", + "backend_name = service.least_busy().name" + ] + }, { "cell_type": "code", "execution_count": null, @@ -210,15 +224,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Choose a backend or remove this option to default to the least busy device\n", - "backend_name = \"\"\n", - "\n", "# Run the circuit using the estimator\n", "qctrl_estimator_job = perf_mgmt.run(\n", " primitive=\"estimator\",\n", " pubs=estimator_pubs,\n", - " instance=hub + \"/\" + group + \"/\" + project,\n", - " backend_name=backend_name,\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_kyiv\", or omit to default to the least busy device\n", ")" ] }, @@ -447,15 +458,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Choose a backend or remove this option to default to the least busy device\n", - "backend_name = \"\"\n", - "\n", "# Run the circuit using the sampler\n", "qctrl_sampler_job = perf_mgmt.run(\n", " primitive=\"sampler\",\n", " pubs=sampler_pubs,\n", - " instance=hub + \"/\" + group + \"/\" + project,\n", - " backend_name=backend_name,\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_kyiv\", or omit to default to the least busy device\n", ")" ] }, diff --git a/docs/guides/qedma-qesem.ipynb b/docs/guides/qedma-qesem.ipynb index 4f19a6984ac..78593883b33 100644 --- a/docs/guides/qedma-qesem.ipynb +++ b/docs/guides/qedma-qesem.ipynb @@ -117,6 +117,25 @@ "To get started, try this basic example of estimating the required QPU time to run the QESEM for a given `pub`:" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "2fb4efc4", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This cell is hidden from users\n", + "from qiskit_ibm_runtime import QiskitRuntimeService\n", + "\n", + "service = QiskitRuntimeService()\n", + "instance = service.active_account()[\"instance\"]\n", + "backend_name = service.least_busy().name" + ] + }, { "cell_type": "code", "execution_count": null, @@ -138,12 +157,12 @@ ")\n", "\n", "job = qesem_function.run(\n", - " instance=\"hub1/group1/project1\",\n", " pubs=[(circ, [avg_magnetization, other_observable])],\n", " options={\n", " \"estimate_time_only\": True,\n", " },\n", - " backend_name=\"ibm_brisbane\",\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_brisbane\"\n", ")" ] }, @@ -163,9 +182,9 @@ "outputs": [], "source": [ "job = qesem_function.run(\n", - " instance=\"hub1/group1/project1\",\n", " pubs=[(circ, [avg_magnetization, other_observable])],\n", - " backend_name=\"ibm_brisbane\",\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_brisbane\"\n", ")" ] }, diff --git a/docs/guides/qunasys-quri-chemistry.ipynb b/docs/guides/qunasys-quri-chemistry.ipynb index 66b03c16b5b..6dd6a52f09f 100644 --- a/docs/guides/qunasys-quri-chemistry.ipynb +++ b/docs/guides/qunasys-quri-chemistry.ipynb @@ -456,6 +456,25 @@ "Finally, execute the function." ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "03c5213a-0a98-4f83-9144-11473016ebda", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This cell is hidden from users\n", + "from qiskit_ibm_runtime import QiskitRuntimeService\n", + "\n", + "service = QiskitRuntimeService()\n", + "instance = service.active_account()[\"instance\"]\n", + "backend_name = service.least_busy().name" + ] + }, { "cell_type": "code", "execution_count": null, @@ -469,7 +488,8 @@ " circuit_options=qsci_double_exc_json,\n", " qsci_setting=qsci_setting,\n", " mitigation_setting=mitigation_setting,\n", - " backend_name=\"ibm_torino\",\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_torino\"\n", ")" ] }, @@ -646,7 +666,8 @@ " \"configuration_recovery\": {\"number_of_states_pick_out\": 10000}\n", " },\n", " max_iter=5,\n", - " backend_name=\"ibm_strasbourg\",\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_strasbourg\"\n", ")" ] }, @@ -784,7 +805,8 @@ " parameters={\"ansatz\": \"DoubleExcitation\", \"state_prep_method\": \"CCSD\"},\n", " qsci_setting={\"n_shots\": 100000, \"number_of_states_pick_out\": 50000},\n", " mitigation_setting={\"configuration_recovery\": True},\n", - " backend=\"ibm_strasbourg\",\n", + " instance=instance, # E.g. \"ibm-q/open/main\"\n", + " backend_name=backend_name, # E.g. \"ibm_strasbourg\"\n", ")" ] },