|
| 1 | +.. _examples: |
| 2 | + |
| 3 | + |
| 4 | +Examples |
| 5 | +======== |
| 6 | + |
| 7 | +We provide some examples how to use the code to calculate a variational |
| 8 | +optimization for typical 2d many-body problems in the `examples/ folder of the |
| 9 | +variPEPS Git repository |
| 10 | +<https://github.com/variPEPS/variPEPS_Python/tree/main/examples>`_. |
| 11 | + |
| 12 | +In this section we want to elaborately walk through the example for the |
| 13 | +Heisenberg AFM on the 2d square lattice to explain a typical usage of the |
| 14 | +library. |
| 15 | + |
| 16 | +Heisenberg antiferromagnet on the square lattice |
| 17 | +------------------------------------------------ |
| 18 | + |
| 19 | +.. figure:: /images/square_lattice.* |
| 20 | + :align: center |
| 21 | + :width: 60% |
| 22 | + :alt: Two dimensional square lattice with red links indicating horizontal and |
| 23 | + blue links indicating vertical interactions. |
| 24 | + |
| 25 | + Two dimensional square lattice |
| 26 | + |
| 27 | +The Hamiltonian for the Heisenberg antiferromagnet with constant exchange |
| 28 | +interaction strength :math:`J>0` is defined as: |
| 29 | + |
| 30 | +.. math:: |
| 31 | +
|
| 32 | + H = J \sum_{\langle i j \rangle} \vec{S}_i \vec{S}_j , |
| 33 | +
|
| 34 | +where :math:`\langle i j \rangle` denotes the sum over all nearest neighbors in |
| 35 | +the lattice. |
| 36 | + |
| 37 | +Our aim is now to find the ground state of the model using the variational iPEPS |
| 38 | +code of the variPEPS library. |
| 39 | + |
| 40 | +Loading of relevant Python modules |
| 41 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + import varipeps |
| 46 | + import jax |
| 47 | + import jax.numpy as jnp |
| 48 | +
|
| 49 | +First of all we have to load the relevant Python modules for our simulation. The |
| 50 | +:obj:`varipeps` module includes the full library to perform the variational |
| 51 | +optimization. Internally it is based on the :obj:`jax` framework and its |
| 52 | +:obj:`numpy`-like interface to execute the calculations. Since we need to define |
| 53 | +arrays later to define for example the Hamiltonian, we need to load this numpy |
| 54 | +interface as well. |
| 55 | + |
| 56 | +variPEPS config settings |
| 57 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + # Config Setting |
| 62 | + ## Set maximal steps for the CTMRG routine |
| 63 | + varipeps.config.ad_custom_max_steps = 100 |
| 64 | + ## Set maximal steps for the fix point routine in the gradient calculation |
| 65 | + varipeps.config.ctmrg_max_steps = 100 |
| 66 | + ## Set convergence threshold for the CTMRG routine |
| 67 | + varipeps.config.ctmrg_convergence_eps = 1e-7 |
| 68 | + ## Set convergence threshold for the fix point routine in the gradient calculation |
| 69 | + varipeps.config.ad_custom_convergence_eps = 5e-8 |
| 70 | + ## Enable/Disable printing of the convergence of the single CTMRG/gradient fix point steps. |
| 71 | + ## Useful to enable this during debugging, should be disabled for batch runs |
| 72 | + varipeps.config.ctmrg_print_steps = True |
| 73 | + varipeps.config.ad_custom_print_steps = False |
| 74 | + ## Select the method used to calculate the descent direction during optimization |
| 75 | + varipeps.config.optimizer_method = varipeps.config.Optimizing_Methods.CG |
| 76 | + ## Select the method used to calculate the (full) projectors in the CTMRG routine |
| 77 | + varipeps.config.ctmrg_full_projector_method = ( |
| 78 | + varipeps.config.Projector_Method.FISHMAN |
| 79 | + ) |
| 80 | + ## Set maximal steps for the optimization routine |
| 81 | + varipeps.config.optimizer_max_steps = 2000 |
| 82 | + ## Increase enviroment bond dimension if truncation error is below this value |
| 83 | + varipeps.config.ctmrg_heuristic_increase_chi_threshold = 1e-4 |
| 84 | +
|
| 85 | +The :obj:`varipeps` library allows to configure a large amount of numerical |
| 86 | +parameters to fine-tune the simulation. In this example we include some common |
| 87 | +options someone can set for a optimization run. For a long and detailed |
| 88 | +description of the config option we want to point to the API description of the |
| 89 | +config class: :obj:`varipeps.config.VariPEPS_Config`. |
| 90 | + |
| 91 | +Model parameters |
| 92 | +^^^^^^^^^^^^^^^^ |
| 93 | + |
| 94 | +.. code-block:: python |
| 95 | +
|
| 96 | + # Set constants for the simulation |
| 97 | + modelName = "HeisenbergModel" |
| 98 | + # Interaction strength |
| 99 | + J = 1 |
| 100 | + # iPEPS bond dimension |
| 101 | + chiB = 2 |
| 102 | + # Physical dimension |
| 103 | + p = 2 |
| 104 | + # Maximal enviroment bond dimension |
| 105 | + maxChi = 36 |
| 106 | + # Start value for enviroment bond dimension |
| 107 | + startChi = chiB**2 if chiB**2 < maxChi else maxChi |
| 108 | +
|
| 109 | +In this block we define some parameters for the model we want to simulate as the |
| 110 | +interaction strength, the physical dimension of our tensor network and the iPEPS |
| 111 | +bond dimension. In the last two lines the start and the maximal enviroment bond |
| 112 | +dimension is defined. A feature of the variPEPS library is that it not only |
| 113 | +supports simulation at a fixed enviroment bond dimension but also a heurisitic |
| 114 | +increase/decrease of the dimension up to a maximal value if the maximal |
| 115 | +truncation error in the CTMRG project calculation is above/below some |
| 116 | +threshold. See in the config block above the parameter |
| 117 | +``ctmrg_heuristic_increase_chi_threshold`` which sets for example the threshold |
| 118 | +when to increase the refinement parameter. The maximal bond dimension ensures |
| 119 | +that the parameter does no increase to too large values where the memory and |
| 120 | +computational resources are exhausted. |
| 121 | + |
| 122 | +Constructing the Hamiltonian |
| 123 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 124 | + |
| 125 | +.. code-block:: python |
| 126 | +
|
| 127 | + # define spin-1/2 matrices |
| 128 | + Id = jnp.eye(2) |
| 129 | + Sx = jnp.array([[0, 1], [1, 0]]) / 2 |
| 130 | + Sy = jnp.array([[0, -1j], [1j, 0]]) / 2 |
| 131 | + Sz = jnp.array([[1, 0], [0, -1]]) / 2 |
| 132 | +
|
| 133 | + # construct Hamiltonian terms |
| 134 | + hamiltonianGates = J * (jnp.kron(Sx, Sx) + jnp.kron(Sy, Sy) + jnp.kron(Sz, Sz)) |
| 135 | +
|
| 136 | + # create function to compute expectation values for the square Heisenberg AFM |
| 137 | + exp_func = varipeps.expectation.Two_Sites_Expectation_Value( |
| 138 | + horizontal_gates=(hamiltonianGates,), |
| 139 | + vertical_gates=(hamiltonianGates,), |
| 140 | + ) |
| 141 | +
|
| 142 | +Here the Hamiltonian is constructed for our model. The Heisenberg AFM on the |
| 143 | +square lattice can be described by the sum of the spin-spin interactions over |
| 144 | +the horizontal and vertical bonds. Since we assume in our example a constant |
| 145 | +interaction strength for all bonds, the expectation value can be calculated by |
| 146 | +the same two site interaction tensor applied in all nearest neighbor |
| 147 | +directions. The expectation function ``exp_func`` is later used in the |
| 148 | +optimization to calculate the energy which is used as cost function to obtain |
| 149 | +the ground state. |
| 150 | + |
| 151 | +Initial unit cell construction |
| 152 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 153 | + |
| 154 | +.. code-block:: python |
| 155 | +
|
| 156 | + # Unit cell structure |
| 157 | + structure = [[0, 1], [1, 0]] |
| 158 | +
|
| 159 | +Here we define the unit cell structure which is used to simulate our model. In |
| 160 | +this example we assume a |
| 161 | +:math:`\scriptsize{\begin{matrix}A&B\\B&A\end{matrix}}`-structure. |
| 162 | + |
| 163 | +.. code-block:: python |
| 164 | +
|
| 165 | + # Create random initialization for the iPEPS unit cell |
| 166 | + unitcell = varipeps.peps.PEPS_Unit_Cell.random( |
| 167 | + structure, # Unit cell structure |
| 168 | + p, # Physical dimension |
| 169 | + chiB, # iPEPS bond dimension |
| 170 | + startChi, # Start value for enviroment bond dimension |
| 171 | + float, # Data type for the tensors: `float` (real) or `complex` tensors |
| 172 | + max_chi=maxChi, # Maximal enviroment bond dimension |
| 173 | + ) |
| 174 | +
|
| 175 | +Using the unit cell structure and the model parameter defined above, we can |
| 176 | +generate an initial unit cell. Here we initialize the iPEPS tensors with random |
| 177 | +numbers. Of course one could use other ways to initialize the tensors, for |
| 178 | +example results from a simple update calculation. |
| 179 | + |
| 180 | +Run the optimization |
| 181 | +^^^^^^^^^^^^^^^^^^^^ |
| 182 | + |
| 183 | +.. code-block:: python |
| 184 | +
|
| 185 | + # Run optimization |
| 186 | + result = varipeps.optimization.optimize_peps_network( |
| 187 | + unitcell, |
| 188 | + exp_func, |
| 189 | + autosave_filename=f"data/autosave_square_chiB_{chiB:d}.hdf5", |
| 190 | + ) |
| 191 | +
|
| 192 | +This function call executes the main function of the library, the variational |
| 193 | +optimization run to obtain a good ground state candidate. The function has |
| 194 | +several option to adapt the optimization for different ansätze (for example the |
| 195 | +spiral iPEPS approach). In our example we just need to supply the initial unit |
| 196 | +cell, the function to calculate the energy expectation value and to allow to |
| 197 | +restore an aborted simulation a file name for autosaves of the optimization. |
| 198 | + |
| 199 | +Evaluate the results |
| 200 | +^^^^^^^^^^^^^^^^^^^^ |
| 201 | + |
| 202 | +In this section we show some exemplary evaluation of the result of the optimization. |
| 203 | + |
| 204 | +.. code-block:: python |
| 205 | +
|
| 206 | + # Calculate magnetic expectation values |
| 207 | + Mag_Gates = [Sx, Sy, Sz] |
| 208 | +
|
| 209 | +
|
| 210 | + def calc_magnetic(unitcell): |
| 211 | + mag_result = [] |
| 212 | + for ti, t in enumerate(unitcell.get_unique_tensors()): |
| 213 | + r = varipeps.expectation.one_site.calc_one_site_multi_gates( |
| 214 | + t.tensor, t, Mag_Gates |
| 215 | + ) |
| 216 | + mag_result += r |
| 217 | + return mag_result |
| 218 | +
|
| 219 | +
|
| 220 | + magnetic_exp_values = calc_magnetic(result.unitcell) |
| 221 | +
|
| 222 | +We assume for our example that we are interested in the single-site magnetic |
| 223 | +expectation values. These could be used to analyse the :math:`z`-magnetization |
| 224 | +or the staggered magnetization of our model at/near the ground state. |
| 225 | + |
| 226 | +.. code-block:: python |
| 227 | +
|
| 228 | + # Define some auxiliary data which should be stored along the final iPEPS unit cell |
| 229 | + auxiliary_data = { |
| 230 | + "best_energy": result.fun, |
| 231 | + "best_run": result.best_run, |
| 232 | + "magnetic_exp_values": magnetic_exp_values, |
| 233 | + } |
| 234 | + for k in sorted(result.max_trunc_error_list.keys()): |
| 235 | + auxiliary_data[f"max_trunc_error_list_{k:d}"] = result.max_trunc_error_list[k] |
| 236 | + auxiliary_data[f"step_energies_{k:d}"] = result.step_energies[k] |
| 237 | + auxiliary_data[f"step_chi_{k:d}"] = result.step_chi[k] |
| 238 | + auxiliary_data[f"step_conv_{k:d}"] = result.step_conv[k] |
| 239 | + auxiliary_data[f"step_runtime_{k:d}"] = result.step_runtime[k] |
| 240 | +
|
| 241 | + # save full iPEPS state |
| 242 | + result.unitcell.save_to_file( |
| 243 | + f"data/heisenberg_square_J_{J:d}_chiB_{chiB:d}_chiMax_{chiM:d}.hdf5", |
| 244 | + auxiliary_data=auxiliary_data, |
| 245 | + ) |
| 246 | +
|
| 247 | +Finally, we want to save the unit cell with the optimized tensors to a file for |
| 248 | +later further evaluation. The library allows to store the data directly into a |
| 249 | +HDF5 file along with user-supplied auxiliary data. Here for example not only |
| 250 | +want to store the plain tensors but also the calculated energy, meta information |
| 251 | +from the optimization run (e.g. energy per step or the runtime per step) and the |
| 252 | +calculated magnetic expectation values. At a later examination of the results, |
| 253 | +these data can be easily loaded along with the tensors of the tensor network. |
0 commit comments