Skip to content

Commit

Permalink
Make notebooks runnable with nb-tester
Browse files Browse the repository at this point in the history
  • Loading branch information
frankharkins committed Dec 11, 2024
1 parent d08bff6 commit 2d2fb9c
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 64 deletions.
4 changes: 3 additions & 1 deletion docs/guides/algorithmiq-tem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@
"service = QiskitRuntimeService()\n",
"backend_name = service.least_busy(operational=True).name\n",
"\n",
"instance = \"<IQP_HUB/IQP_GROUP/IQP_PROJECT>\"\n",
"# Instance is a string of the form \"<IQP_HUB/IQP_GROUP/IQP_PROJECT>\".\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",
Expand Down
72 changes: 40 additions & 32 deletions docs/guides/functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,15 @@
"\n",
" <span id=\"save-account\"></span>**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 = \"<your-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=\"<your-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()`."
]
},
{
Expand All @@ -99,6 +77,8 @@
"outputs": [],
"source": [
"# Load saved credentials\n",
"from qiskit_ibm_catalog import QiskitFunctionsCatalog\n",
"\n",
"catalog = QiskitFunctionsCatalog()"
]
},
Expand Down Expand Up @@ -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": [
Expand All @@ -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"
]
Expand Down
6 changes: 5 additions & 1 deletion docs/guides/ibm-circuit-function.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@
"cell_type": "code",
"execution_count": 8,
"id": "4070e592",
"metadata": {},
"metadata": {
"tags": [
"raises-exception"
]
},
"outputs": [
{
"name": "stdout",
Expand Down
33 changes: 25 additions & 8 deletions docs/guides/q-ctrl-optimization-solver.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \"<YOUR_IQP_HUB>\"\n",
"group = \"<YOUR_IQP_GROUP>\"\n",
"project = \"<YOUR_IQP_PROJECT>\"\n",
"\n",
"# Authentication\n",
"catalog = QiskitFunctionsCatalog()\n",
"\n",
"# Access Function\n",
Expand Down Expand Up @@ -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": [],
Expand All @@ -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",
")"
]
},
Expand Down Expand Up @@ -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",
")"
]
},
Expand Down
38 changes: 23 additions & 15 deletions docs/guides/q-ctrl-performance-management.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \"<YOUR_IQP_HUB>\"\n",
"group = \"<YOUR_IQP_GROUP>\"\n",
"project = \"<YOUR_IQP_PROJECT>\"\n",
"\n",
"# Authentication\n",
"catalog = QiskitFunctionsCatalog()\n",
"\n",
"# Access Function\n",
Expand Down Expand Up @@ -203,22 +198,38 @@
"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,
"id": "26952e1f",
"metadata": {},
"outputs": [],
"source": [
"# Choose a backend or remove this option to default to the least busy device\n",
"backend_name = \"<CHOOSE_A_BACKEND>\"\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",
")"
]
},
Expand Down Expand Up @@ -447,15 +458,12 @@
"metadata": {},
"outputs": [],
"source": [
"# Choose a backend or remove this option to default to the least busy device\n",
"backend_name = \"<CHOOSE_A_BACKEND>\"\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",
")"
]
},
Expand Down
27 changes: 23 additions & 4 deletions docs/guides/qedma-qesem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
")"
]
},
Expand All @@ -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",
")"
]
},
Expand Down
28 changes: 25 additions & 3 deletions docs/guides/qunasys-quri-chemistry.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
")"
]
},
Expand Down Expand Up @@ -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",
")"
]
},
Expand Down Expand Up @@ -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",
")"
]
},
Expand Down

0 comments on commit 2d2fb9c

Please sign in to comment.