Computing Matrix Product State marginal distribution tensor#
The following code example illustrates how to define a tensor network state, compute its Matrix Product State (MPS) factorization, and then compute a marginal distribution tensor for the MPS-factorized state. The full code can be found in the NVIDIA/cuQuantum repository (here).
Headers and error handling#
Define the tensor network state and the desired marginal distribution tensor#
Let’s define a tensor network state corresponding to a 16-qubit quantum circuit and request the marginal distribution tensor for qubits 0 and 1.
Initialize the cuTensorNet library handle#
Define quantum gates on GPU#
Allocate MPS tensors#
Here we set the shapes of MPS tensors and allocate GPU memory for their storage.
Allocate the marginal distribution tensor on GPU#
Here we allocate the marginal distribution tensor, that is, the reduced density matrix for qubits 0 and 1, on GPU.
Allocate the scratch buffer on GPU#
Create a pure tensor network state#
Now let’s create a pure tensor network state for a 16-qubit quantum circuit.
Apply quantum gates#
Let’s construct the GHZ quantum circuit by applying the corresponding quantum gates.
Request MPS factorization for the final quantum circuit state#
Here we express our intent to factorize the final quantum circuit state using MPS factorization. The provided shapes of the MPS tensors refer to their maximal size limit during the MPS renormalization procedure. The actually computed shapes of the final MPS tensors may be smaller. No computation is done here yet.
Configure MPS factorization procedure#
After expressing our intent to perform MPS factorization of the final quantum circuit state, we can also configure the MPS factorization procedure by resetting different options, for example, the SVD algorithm.
Prepare the computation of MPS factorization#
Let’s create a workspace descriptor and prepare the computation of MPS factorization.
Compute MPS factorization#
Once the MPS factorization procedure has been configured and prepared, let’s compute the MPS factorization of the final quantum circuit state.
Create the marginal distribution object#
Once the quantum circuit has been constructed, let’s create the marginal distribution object that will compute the marginal distribution tensor (reduced density matrix) for qubits 0 and 1.
Configure the marginal distribution object#
Now we can configure the marginal distribution object by setting the number of hyper-samples to be used by the tensor network contraction path finder.
Prepare the computation of the marginal distribution tensor#
Let’s prepare the computation of the marginal distribution tensor.
Set up the workspace#
Now we can set up the required workspace buffer.
Compute the marginal distribution tensor#
Once everything has been set up, we compute the requested marginal distribution tensor (reduced density matrix) for qubits 0 and 1, copy it back to Host memory, and print it.