The following code example illustrates how to integrate *cuTensorNet* functionalities to perform basic MPS simulation. The workflow is encapsulated in an ``MPSHelper`` class. The full code can be found in the `NVIDIA/cuQuantum `_ repository (`here `_). ---------------------- Define MPSHelper class ---------------------- We first define an ``MPSHelper`` class to keep track of the modes and extents of all physical and virtual bonds. The simulation settings are also stored in this class. Once out of scope, all resource owned by this class will be freed. .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #2 :end-before: Sphinx: #3 .. note:: For full definition of all the methods, please refer to the sample `here `_. ---------------------------- Setup MPS simulation setting ---------------------------- Next, in the main function, we need to choose the simulation setting for the MPS simulation (i.e., the number of sites, the initial extents, and the data type). .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #11 :end-before: Sphinx: #12 The MPS metadata and all *cuTensorNet* library objects will be managed by the ``MPSHelper`` while the data pointers are explicitly managed in the main function. ----------------------------------- Allocate memory and initialize data ----------------------------------- Next, we allocate memory for the MPS operands and four 2-qubit gate tensors. The largest tensor size for each MPS tensor can be queried through the ``MPSHelper`` class. The MPS tensors are initialized to a state corresponding to ``|00..000>`` and the gate tensors are filled with random values. .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #12 :end-before: Sphinx: #13 ------------------------ Setup gate split options ------------------------ Then we setup the SVD truncation parameters and the algorithm `cutensornetGateSplitAlgo_t` to use for the gate split process. .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #13 :end-before: Sphinx: #14 ------------------------------------- Query and allocate required workspace ------------------------------------- Once all simulation settings are set, we can query the required workspace size. Inside the MPSHelper, the required workspace size is estimated on the largest tensor sizes involved in the simulation. .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #14 :end-before: Sphinx: #15 --------- Execution --------- At this stage, we can execute the simulation by iterating over all the gate tensors. All the metadata of the MPS will be managed and updated inside the ``MPSHelper``. .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #15 :end-before: Sphinx: #16 -------------- Free resources -------------- After the simulation, we free up all the data pointers allocated in the main function. .. literalinclude:: ../../../../tensor_network/samples/approxTN/mps_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: #16 All *cuTensorNet* library objects owned by the MPSHelper will be freed once out of scope.