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.
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).
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.
Setup gate split options#
Then we setup the SVD truncation parameters and the algorithm cutensornetGateSplitAlgo_t to use for the gate split process.
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.
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.
Free resources#
After the simulation, we free up all the data pointers allocated in the main function.
All cuTensorNet library objects owned by the MPSHelper will be freed once out of scope.