
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/QuantumIsing_2d_groundstate_threaded.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_2d_groundstate_threaded.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_QuantumIsing_2d_groundstate_threaded.py:


Calculate the ground states of a 2d quantum Ising model using threading
=======================================================================

Calculate the ground states of a 2d quantum Ising model using threading
to parallelize within one machine.

.. GENERATED FROM PYTHON SOURCE LINES 18-92

.. code-block:: Python


    # pylint: disable=invalid-name

    import numpy as np

    import qtealeaves as qtl
    from qtealeaves.models import get_quantum_ising_2d


    def main(tn_type=5, output_folder=None):
        """
        Main method for the ground state simulation of a
        quantum Ising model in 2D. This setup uses threading
        on the python side.

        **Arguments**

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

        output_folder : str | None, optional
            Output folder. Default to None.
        """
        gfields = np.linspace(0, 2, 21)

        if output_folder is None:
            output_folder = lambda params: "QI2d_g%2.4f" % (params["g"])

        model, my_ops = get_quantum_ising_2d()

        my_conv = qtl.convergence_parameters.TNConvergenceParameters(
            max_iter=5, max_bond_dimension=16
        )
        my_obs = qtl.observables.TNObservables()

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

        params = []
        for gfield in gfields:
            params.append(
                {
                    "L": 8,
                    # model parameters
                    "J": 1.0,
                    "g": gfield,
                }
            )

        simulation.run(params, delete_existing_folder=True, nthreads=8)

        for elem in params:
            results = simulation.get_static_obs(elem)
            print(
                "TTN ground state energy is %4.8f at field g=%2.4f"
                % (results["energy"], elem["g"])
            )

        print(f"\nExample `{__file__}` ran successfully; no asserts implemented.")

        return


    if __name__ == "__main__":
        main()


.. _sphx_glr_download_auto_examples_QuantumIsing_2d_groundstate_threaded.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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