Skip to content

Commit

Permalink
Clean up headers in notebook guides
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-johnson committed Feb 6, 2025
1 parent b46e955 commit 2a1390f
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 101 deletions.
2 changes: 1 addition & 1 deletion docs/how_tos/benchmark_pauli_projection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
"id": "184bf287",
"metadata": {},
"source": [
"### Let's time the projection time for a Pauli String\n",
"### Benchmark SQD Pauli projection functions\n",
"\n",
"The Pauli string under consideration is $\\sigma_z \\otimes ... \\otimes \\sigma_z$.\n",
"\n",
Expand Down
4 changes: 1 addition & 3 deletions docs/how_tos/choose_subspace_dimension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "9e40af77-7f0f-4dd6-ab0a-420cf396050e",
"metadata": {},
"source": [
"# Bound the subspace dimension\n",
"# Bounding the subspace dimension\n",
"\n",
"In this tutorial, we will show the effect of the subspace dimension in the [self-consistent configuration recovery technique](https://arxiv.org/abs/2405.05068).\n",
"\n",
Expand Down Expand Up @@ -311,8 +311,6 @@
"id": "9d78906b-4759-4506-9c69-85d4e67766b3",
"metadata": {},
"source": [
"### Visualize the results\n",
"\n",
"This plot shows that increasing the subspace dimension leads to more accurate results."
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
"id": "1fce97a2",
"metadata": {},
"source": [
"### Check that both implementations yield the same coo_matrix"
"Check that both implementations yield the same coo_matrix"
]
},
{
Expand Down
24 changes: 12 additions & 12 deletions docs/how_tos/select_open_closed_shell.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"id": "a6755afb-ca1e-4473-974b-ba89acc8abce",
"metadata": {},
"source": [
"## Closed-Shell\n",
"### Closed-Shell\n",
"\n",
"This example shows how the bitstrings are manipulated in a (2-electron, 4-orbital) system."
]
Expand All @@ -61,7 +61,7 @@
"id": "c58e988c-a109-44cd-a975-9df43250c318",
"metadata": {},
"source": [
"### Specify by hand a dictionary of measurement outcomes"
"Specify by hand a dictionary of measurement outcomes"
]
},
{
Expand All @@ -79,7 +79,7 @@
"id": "851bc98e-9c08-4e78-9472-36301abc11d8",
"metadata": {},
"source": [
"### Transform the counts dict into a bitstring matrix and probability array for post-processing"
"Transform the counts dict into a bitstring matrix and probability array for post-processing"
]
},
{
Expand Down Expand Up @@ -113,7 +113,7 @@
"id": "eb704101-0fe8-4d12-b572-b1d844e35a90",
"metadata": {},
"source": [
"### Subsample a single batch of size two:\n",
"Subsample a single batch of size two:\n",
"\n",
"- ``n_batches = 1``: Number of batches of configurations used by the different calls to the eigenstate solver\n",
"- ``samples_per_batch = 2``: Number of unique configurations to include in each batch"
Expand Down Expand Up @@ -162,7 +162,7 @@
"id": "93a6d05a",
"metadata": {},
"source": [
"### Obtain decimal representation of the spin-up and spin-down bitstrings used by the eigenstate solver\n",
"Obtain decimal representation of the spin-up and spin-down bitstrings used by the eigenstate solver\n",
"\n",
"The fist element in the tuple corresponds to the decimal representation of the spin-up configurations, while the second element in the tuple corresponds to the decimal representation of the spin-down configurations"
]
Expand Down Expand Up @@ -208,7 +208,7 @@
"id": "03b32ed5",
"metadata": {},
"source": [
"### Basis of the subspace:\n",
"Basis of the subspace:\n",
"\n",
"The eigenstate solver takes all possible pairs of spin-up and spin-down bitstrings to construnct the basis $\\mathcal{B}$ of the subspace:\n",
" \n",
Expand Down Expand Up @@ -300,7 +300,7 @@
"id": "9412e52b",
"metadata": {},
"source": [
"## Open-Shell\n",
"### Open-Shell\n",
"\n",
"This example shows how the bitstrings are manipulated in a (2-electron, 4-orbital) system."
]
Expand All @@ -323,7 +323,7 @@
"id": "9ef78560",
"metadata": {},
"source": [
"### Specify by hand a dictionary of measurement outcomes"
"Specify by hand a dictionary of measurement outcomes"
]
},
{
Expand All @@ -341,7 +341,7 @@
"id": "08b32957",
"metadata": {},
"source": [
"### Transform the counts dict into a bitstring matrix and probability array for post-processing"
"Transform the counts dict into a bitstring matrix and probability array for post-processing"
]
},
{
Expand Down Expand Up @@ -373,7 +373,7 @@
"id": "416bfb6c",
"metadata": {},
"source": [
"### Subsample a single batch of size two:\n",
"Subsample a single batch of size two:\n",
"\n",
"- ``n_batches = 1``: Number of batches of configurations used by the different calls to the eigenstate solver\n",
"- ``samples_per_batch = 2``: Number of unique configurations to include in each batch"
Expand Down Expand Up @@ -420,7 +420,7 @@
"id": "54d699ca",
"metadata": {},
"source": [
"### Obtain decimal representation of the spin-up and spin-down bitstrings used by the eigenstate solver\n",
"Obtain decimal representation of the spin-up and spin-down bitstrings used by the eigenstate solver\n",
"\n",
"The fist element in the tuple corresponds to the decimal representation of the spin-up configurations, while the second element in the tuple corresponds to the decimal representation of the spin-down configurations"
]
Expand Down Expand Up @@ -457,7 +457,7 @@
"id": "e1959b72",
"metadata": {},
"source": [
"### Basis of the subspace:\n",
"Basis of the subspace:\n",
"\n",
"The eigenstate solver takes all possible pairs of spin-up and spin-down bitstrings to construnct the basis $\\mathcal{B}$ of the subspace:\n",
" \n",
Expand Down
137 changes: 75 additions & 62 deletions docs/how_tos/use_oo_to_optimize_hamiltonian_basis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"id": "a6755afb-ca1e-4473-974b-ba89acc8abce",
"metadata": {},
"source": [
"### First we will specify the molecule and its properties\n",
"### Specify the molecule and generate samples\n",
"\n",
"In this example, we will approximate the ground state energy of an $N_2$ molecule and then improve the answer using orbital optimization. This guide studies $N_2$ at equilibrium, which is mean-field dominated. This means the MO basis is already a good choice for our integrals; therefore, we will rotate our integrals **out** of the MO basis in order to illustrate the effects of orbital optimization."
]
Expand Down Expand Up @@ -63,9 +63,29 @@
"hcore, nuclear_repulsion_energy = cas.get_h1cas(mo)\n",
"eri = pyscf.ao2mo.restore(1, cas.get_h2cas(mo), num_orbitals)\n",
"\n",
"# Compute exact energy\n",
"exact_energy = cas.run().e_tot"
]
},
{
"cell_type": "markdown",
"id": "b23a74fc-708c-4bd0-af4e-c9cd189b6346",
"metadata": {},
"source": [
"The MO basis is already a good basis for this problem, so we will rotate out of that basis in this guide in order to highlight the effect of orbital optimization."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9f46d1c6-3b49-45ad-b9ed-972bd58f7e3c",
"metadata": {},
"outputs": [],
"source": [
"# Rotate our integrals out of MO basis\n",
"rng = np.random.default_rng(24)\n",
"num_params = (num_orbitals**2 - num_orbitals) // 2 # antisymmetric, specified by upper triangle\n",
"k_rot = (np.random.rand(num_params) - 0.5) * 0.3\n",
"k_rot = (rng.random(num_params) - 0.5) * 0.5\n",
"hcore_rot, eri_rot = rotate_integrals(hcore, eri, k_rot)"
]
},
Expand All @@ -74,21 +94,18 @@
"id": "c58e988c-a109-44cd-a975-9df43250c318",
"metadata": {},
"source": [
"### Generate a dummy counts dictionary and create the bitstring matrix and probability array"
"Generate samples"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "e9506e0b-ed64-48bb-a97a-ef851b604af1",
"metadata": {},
"outputs": [],
"source": [
"from qiskit_addon_sqd.counts import counts_to_arrays, generate_counts_uniform\n",
"\n",
"# Create a seed to control randomness throughout this workflow\n",
"rng = np.random.default_rng(24)\n",
"\n",
"# Generate random samples\n",
"counts_dict = generate_counts_uniform(10_000, num_orbitals * 2, rand_seed=rng)\n",
"\n",
Expand All @@ -106,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "b72c048e-fe8e-4fc2-b28b-03138249074e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -205,18 +222,56 @@
},
{
"cell_type": "markdown",
"id": "9d78906b-4759-4506-9c69-85d4e67766b3",
"id": "917cf2d0",
"metadata": {},
"source": [
"### Visualize the results with no orbital optimization"
"### Refine the subspace\n",
"\n",
"To refine the subspace, we will take the CI strings of the batch with the lowest energy\n",
"from the last configuration recovery step. Other strategies may be used, like taking the union \n",
"of the CI strings of the batches in the last configuration recovery iteration."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2a587030",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Subspace dimension: 32761\n",
"Energy of that batch from SQD: -108.7531706601421\n"
]
}
],
"source": [
"from qiskit_addon_sqd.fermion import bitstring_matrix_to_ci_strs\n",
"\n",
"best_batch = batches[np.argmin(e_hist[-1])]\n",
"ci_strs_up, ci_strs_dn = bitstring_matrix_to_ci_strs(best_batch, open_shell=open_shell)\n",
"print(f\"Subspace dimension: {len(ci_strs_up) * len(ci_strs_dn)}\")\n",
"print(f\"Energy of that batch from SQD: {e_hist[-1, np.argmin(e_hist[-1])]}\")\n",
"\n",
"# Union strategy\n",
"\n",
"# batches_union = np.concatenate((batches[0], batches[1]), axis = 0)\n",
"# for i in range(n_batches-2):\n",
"# batches_union = np.concatenate((batches_union, batches[ i+ 2]))\n",
"# ci_strs_up, ci_strs_dn = bitstring_matrix_to_ci_strs(\n",
"# batches_union, open_shell=open_shell\n",
"# )\n",
"# print (f\"Subspace dimension: {len(ci_strs_up) * len(ci_strs_dn)}\")"
]
},
{
"cell_type": "markdown",
"id": "e8c6d5e4",
"metadata": {},
"source": [
"### Orbital optimization\n",
"### Perform orbital optimization to improve the energy approximation\n",
"\n",
"We now describe how to optimize the orbitals to further improve the quality of the sqd calculation.\n",
"\n",
Expand Down Expand Up @@ -249,62 +304,16 @@
"- ``learning_rate``: step-size in the gradient descent optimization of $\\kappa$."
]
},
{
"cell_type": "markdown",
"id": "917cf2d0",
"metadata": {},
"source": [
"#### Setup of the subspace\n",
"\n",
"To define the subspace, we will take the CI strings of the batch with the lowest energy\n",
"from the last configuration recovery step. Other strategies may be used, like taking the union \n",
"of the CI strings of the batches in the last configuration recovery iteration."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2a587030",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Subspace dimension: 32041\n",
"Energy of that batch from SQD: -109.13624164091385\n"
]
}
],
"source": [
"from qiskit_addon_sqd.fermion import bitstring_matrix_to_ci_strs\n",
"\n",
"best_batch = batches[np.argmin(e_hist[-1])]\n",
"ci_strs_up, ci_strs_dn = bitstring_matrix_to_ci_strs(best_batch, open_shell=open_shell)\n",
"print(f\"Subspace dimension: {len(ci_strs_up) * len(ci_strs_dn)}\")\n",
"print(f\"Energy of that batch from SQD: {e_hist[-1, np.argmin(e_hist[-1])]}\")\n",
"\n",
"# Union strategy\n",
"\n",
"# batches_union = np.concatenate((batches[0], batches[1]), axis = 0)\n",
"# for i in range(n_batches-2):\n",
"# batches_union = np.concatenate((batches_union, batches[ i+ 2]))\n",
"# ci_strs_up, ci_strs_dn = bitstring_matrix_to_ci_strs(\n",
"# batches_union, open_shell=open_shell\n",
"# )\n",
"# print (f\"Subspace dimension: {len(ci_strs_up) * len(ci_strs_dn)}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "b5e56baf",
"metadata": {},
"outputs": [],
"source": [
"from qiskit_addon_sqd.fermion import optimize_orbitals\n",
"\n",
"k_flat = (np.random.rand(num_params) - 0.5) * 0.01\n",
"k_flat = (rng.random(num_params) - 0.5) * 0.1\n",
"num_iters = 20\n",
"num_steps_grad = 10_000 # relatively cheap to execute\n",
"learning_rate = 0.05\n",
Expand Down Expand Up @@ -333,20 +342,24 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "78a80e64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"improved_energy in the new basis: -109.1429260747482\n"
"Exact energy: -109.04667177808028\n",
"SQD energy: -108.7531706601421\n",
"Energy after OO: -108.80400806164369\n"
]
}
],
"source": [
"print(f\"improved_energy in the new basis: {e_improved + nuclear_repulsion_energy}\")"
"print(f\"Exact energy: {exact_energy}\")\n",
"print(f\"SQD energy: {np.min(e_hist[-1])}\")\n",
"print(f\"Energy after OO: {e_improved + nuclear_repulsion_energy}\")"
]
}
],
Expand Down
Loading

0 comments on commit 2a1390f

Please sign in to comment.