Skip to content

Commit

Permalink
update entanglement forging notebook to use larger active space and b…
Browse files Browse the repository at this point in the history
…rickwork ansatz (#72)
  • Loading branch information
kevinsung authored Nov 6, 2023
1 parent 2e6eb74 commit 32c3857
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions docs/tutorials/05-entanglement-forging.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"import pyscf.mcscf\n",
"import ffsim\n",
"import math\n",
"import numpy as np\n",
"\n",
"# Build a water molecule\n",
"radius_1 = 0.958 # position for the first H atom\n",
Expand All @@ -44,7 +43,7 @@
"hartree_fock.kernel()\n",
"\n",
"# Define active space\n",
"active_space = [1, 2, 4, 5, 6]\n",
"active_space = range(1, mol.nao_nr())\n",
"\n",
"# Get molecular data and molecular Hamiltonian (one- and two-body tensors)\n",
"mol_data = ffsim.MolecularData.from_hartree_fock(\n",
Expand All @@ -68,24 +67,31 @@
"source": [
"import numpy as np\n",
"\n",
"n_reps = 1\n",
"\n",
"# Construct ansatz operator\n",
"interaction_pairs = [(0, 1), (3, 4), (1, 4), (0, 2), (3, 4)]\n",
"thetas = np.zeros(n_reps * len(interaction_pairs))\n",
"def brickwork(norb: int, n_layers: int):\n",
" for i in range(n_layers):\n",
" for j in range(i % 2, norb - 1, 2):\n",
" yield (j, j + 1)\n",
"\n",
"\n",
"n_layers = norb\n",
"interaction_pairs = list(brickwork(norb, n_layers))\n",
"rng = np.random.default_rng(1234)\n",
"thetas = rng.uniform(-np.pi, np.pi, size=len(interaction_pairs))\n",
"operator = ffsim.HopGateAnsatzOperator(interaction_pairs, thetas=thetas)\n",
"\n",
"# Construct ansatz state\n",
"reference_occupations_spatial = [(0, 1, 2), (1, 2, 3), (1, 2, 4)]\n",
"reference_occupations_spatial = [(0, 1, 2, 3), (1, 2, 3, 4), (0, 1, 2, 4)]\n",
"reference_occupations = list(\n",
" zip(reference_occupations_spatial, reference_occupations_spatial)\n",
")\n",
"hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb=norb, nelec=nelec)\n",
"ansatz_state = ffsim.multireference_state(\n",
" hamiltonian, operator, reference_occupations, norb=norb, nelec=nelec\n",
" mol_hamiltonian, operator, reference_occupations, norb=norb, nelec=nelec\n",
")\n",
"\n",
"# Compute the energy ⟨ψ|H|ψ⟩ of the ansatz state\n",
"hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb=norb, nelec=nelec)\n",
"energy = np.real(np.vdot(ansatz_state, hamiltonian @ ansatz_state))\n",
"print(f\"Energy at initialialization: {energy}\")"
]
Expand All @@ -111,7 +117,7 @@
"\n",
"\n",
"result = scipy.optimize.minimize(\n",
" fun, x0=operator.thetas, method=\"COBYLA\", options=dict(maxiter=100)\n",
" fun, x0=operator.thetas, method=\"L-BFGS-B\", options=dict(maxfun=100)\n",
")\n",
"\n",
"print(f\"Number of parameters: {len(result.x)}\")\n",
Expand All @@ -135,7 +141,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.12"
},
"orig_nbformat": 4
},
Expand Down

0 comments on commit 32c3857

Please sign in to comment.