
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/QuantumIsing_1d_groundstate.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_QuantumIsing_1d_groundstate.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_QuantumIsing_1d_groundstate.py:


Ground state simulation of the quantum Ising model.
===================================================

.. GENERATED FROM PYTHON SOURCE LINES 16-98

.. code-block:: Python


    # pylint: disable=invalid-name

    import numpy as np
    import numpy.linalg as nla

    import qtealeaves as qtl
    from qtealeaves.models import get_quantum_ising_1d


    def main(tn_type=5, statics_method=2, output_folder=None):
        """
        Main method for the ground state simulation of 1d quantum
        Ising model.

        **Arguments**

        tn_type : int, optional
            Choose 5 for python-TTN, 6 for python-MPS.
            Default to 5.

        statics_method : integer, optional
            Method to run ground state search for this/all iteration.
            0 : default (2)
            1 : sweep
            2 : sweep with space expansion (can still be reduced to sweep
                during the simulation based on a energy condition)

        output_folder : str | None, optional
            Output folder. Default to None.
        """
        if output_folder is None:
            output_folder = lambda params: "QI1d_L%d" % (params["L"])

        model, my_ops = get_quantum_ising_1d()

        my_conv = qtl.convergence_parameters.TNConvergenceParameters(
            max_iter=7, max_bond_dimension=20, statics_method=statics_method
        )

        my_obs = qtl.observables.TNObservables()
        my_obs += qtl.observables.Local("<sz>", "sz")
        my_obs += qtl.observables.Local("<sx>", "sx")

        simulation = qtl.QuantumGreenTeaSimulation(
            model,
            my_ops,
            my_conv,
            my_obs,
            tn_type=tn_type,
            folder_name_output=output_folder,
            has_log_file=False,
            store_checkpoints=False,
        )

        params = []

        params.append({"L": 8, "J": 1.0, "g": 0.5})

        simulation.run(params, delete_existing_folder=True)

        for elem in params:
            # Without symmetries, we find the global sector
            energy_0 = nla.eigh(model.build_ham(my_ops, elem))[0][0]

            results = simulation.get_static_obs(elem)
            print("TN energies E0", results["energy"])
            print("ED energies E0", energy_0)

            # Run one assert that we are converged sufficiently
            assert np.abs(energy_0 - results["energy"]) < 1e-4

        print(
            f"\nExample `{__file__}` ran successfully; "
            + "ground state energy at least correct up to 1e-4."
        )

        return


    if __name__ == "__main__":
        main()


.. _sphx_glr_download_auto_examples_QuantumIsing_1d_groundstate.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: QuantumIsing_1d_groundstate.ipynb <QuantumIsing_1d_groundstate.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: QuantumIsing_1d_groundstate.py <QuantumIsing_1d_groundstate.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: QuantumIsing_1d_groundstate.zip <QuantumIsing_1d_groundstate.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
