Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 93 additions & 61 deletions docs/guides/function-template-hamiltonian-simulation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,6 @@
"Throughout, the code is saved to `./source_files/template_hamiltonian_simulation.py`. This file is the function template you can upload to and run remotely with Qiskit Serverless."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "35f5800c-163d-47a6-9fcf-13377be57282",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"# This cell is hidden from users, it just creates a new folder\n",
"from pathlib import Path\n",
"\n",
"Path(\"./source_files\").mkdir(exist_ok=True)"
]
},
{
"cell_type": "markdown",
"id": "115c14aa-5028-46f9-ab19-b49d47519636",
Expand All @@ -102,8 +85,14 @@
}
],
"source": [
"%%writefile ./source_files/template_hamiltonian_simulation.py\n",
"# This cell is hidden from users, it just creates a new folder\n",
"from pathlib import Path\n",
"\n",
"Path(\"./source_files\").mkdir(exist_ok=True)\n",
"\n",
"PROGRAM_FILE = \"./source_files/template_hamiltonian_simulation.py\"\n",
"\n",
"source = \"\"\"\n",
"from qiskit import QuantumCircuit\n",
"from qiskit_serverless import get_arguments, save_result\n",
"\n",
Expand Down Expand Up @@ -131,7 +120,11 @@
"\n",
"hamiltonian = arguments[\"hamiltonian\"]\n",
"observable = arguments[\"observable\"]\n",
"initial_state = arguments.get(\"initial_state\", QuantumCircuit(hamiltonian.num_qubits))"
"initial_state = arguments.get(\"initial_state\", QuantumCircuit(hamiltonian.num_qubits))\n",
"\"\"\"\n",
"\n",
"with open(PROGRAM_FILE, \"w\") as file:\n",
" file.write(source)"
]
},
{
Expand All @@ -149,8 +142,7 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"\n",
"source = \"\"\"\n",
"import numpy as np\n",
"import json\n",
"from mergedeep import merge\n",
Expand Down Expand Up @@ -185,7 +177,12 @@
"# Merge with user-provided options\n",
"estimator_options = merge(\n",
" arguments.get(\"estimator_options\", {}), estimator_default_options\n",
")"
")\n",
"\"\"\"\n",
"\n",
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand All @@ -211,9 +208,13 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"source = \"\"\"\n",
"print(\"estimator_options =\", json.dumps(estimator_options, indent=4))\n",
"\"\"\"\n",
"\n",
"print(\"estimator_options =\", json.dumps(estimator_options, indent=4))"
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -241,14 +242,18 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"\n",
"source = \"\"\"\n",
"# Perform parameter validation\n",
"\n",
"if not 0.0 < aqc_stopping_fidelity <= 1.0:\n",
" raise ValueError(\n",
" f\"Invalid stopping fidelity: {aqc_stopping_fidelity}. It must be a positive float no greater than 1.\"\n",
" )"
" )\n",
"\"\"\"\n",
"\n",
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -276,9 +281,13 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"source = \"\"\"\n",
"output = {}\n",
"\"\"\"\n",
"\n",
"output = {}"
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -308,8 +317,7 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"\n",
"source = \"\"\"\n",
"import os\n",
"os.environ[\"NUMBA_CACHE_DIR\"] = \"/data\"\n",
"\n",
Expand Down Expand Up @@ -420,7 +428,12 @@
" synthesis=SuzukiTrotter(reps=remainder_num_trotter_steps),\n",
" time=remainder_evolution_time,\n",
" )\n",
" final_circuit.compose(remainder_circuit, inplace=True)"
" final_circuit.compose(remainder_circuit, inplace=True)\n",
"\"\"\"\n",
"\n",
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -448,8 +461,7 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"\n",
"source = \"\"\"\n",
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
"from qiskit.transpiler import generate_preset_pass_manager\n",
"\n",
Expand All @@ -463,7 +475,12 @@
"\n",
"isa_2qubit_depth = isa_circuit.depth(lambda x: x.operation.num_qubits == 2)\n",
"print(\"ISA circuit two-qubit depth:\", isa_2qubit_depth)\n",
"output[\"twoqubit_depth\"] = isa_2qubit_depth"
"output[\"twoqubit_depth\"] = isa_2qubit_depth\n",
"\"\"\"\n",
"\n",
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -491,15 +508,19 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"\n",
"source = \"\"\"\n",
"# Exit now if dry run; don't execute on hardware\n",
"if dry_run:\n",
" import sys\n",
"\n",
" print(\"Exiting before hardware execution since `dry_run` is True.\")\n",
" save_result(output)\n",
" sys.exit(0)"
" sys.exit(0)\n",
"\"\"\"\n",
"\n",
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand All @@ -525,8 +546,7 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"\n",
"source = \"\"\"\n",
"# ## Step 3: Execute quantum experiments on backend\n",
"from qiskit_ibm_runtime import EstimatorV2 as Estimator\n",
"\n",
Expand All @@ -551,7 +571,12 @@
"\n",
"# Save expectation values to Qiskit Serverless\n",
"print(\"Hardware expectation values\", hw_expvals)\n",
"output[\"hw_expvals\"] = hw_expvals[0]"
"output[\"hw_expvals\"] = hw_expvals[0]\n",
"\"\"\"\n",
"\n",
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -579,9 +604,13 @@
}
],
"source": [
"%%writefile --append ./source_files/template_hamiltonian_simulation.py\n",
"source = \"\"\"\n",
"save_result(output)\n",
"\"\"\"\n",
"\n",
"save_result(output)"
"# \"a\" appends to the file rather than overwriting it\n",
"with open(PROGRAM_FILE, \"a\") as file:\n",
" file.write(source)"
]
},
{
Expand Down Expand Up @@ -1119,6 +1148,22 @@
"</Admonition>"
]
},
{
"cell_type": "markdown",
"id": "dfcb6598-d78c-4204-b0cc-3ef74e70d73b",
"metadata": {},
"source": [
"<Accordion>\n",
"<AccordionItem title=\"**Full program source code**\">\n",
"\n",
"Here is the entire source of `./source_files/template_hamiltonian_simulation.py` as one code block.\n",
"\n",
"<CodeCellPlaceholder tag=\"id-full-source\" />\n",
"\n",
" </AccordionItem>\n",
"</Accordion>"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -1138,8 +1183,7 @@
}
],
"source": [
"%%writefile ./source_files/template_hamiltonian_simulation_full.py\n",
"\n",
"source = \"\"\"\n",
"from qiskit import QuantumCircuit\n",
"from qiskit_serverless import get_arguments, save_result\n",
"\n",
Expand Down Expand Up @@ -1376,23 +1420,11 @@
"# Save expectation values to Qiskit Serverless\n",
"output[\"hw_expvals\"] = hw_expvals[0]\n",
"\n",
"save_result(output)"
]
},
{
"cell_type": "markdown",
"id": "d7f1776a-a8f6-43a3-85b7-33975c4eeec0",
"metadata": {},
"source": [
"<Accordion>\n",
"<AccordionItem title=\"**Full program source code**\">\n",
"\n",
"Here is the entire source of `./source_files/template_hamiltonian_simulation.py` as one code block.\n",
"save_result(output)\n",
"\"\"\"\n",
"\n",
"<CodeCellPlaceholder tag=\"id-full-source\" />\n",
"\n",
" </AccordionItem>\n",
"</Accordion>"
"with open(PROGRAM_FILE, \"w\") as file:\n",
" file.write(source)"
]
},
{
Expand Down
Loading
Loading