Sampling the Matrix Product State (circuit with Matrix Product Operators)#
The following code example illustrates how to define a tensor network state by a circuit with Matrix Product Operators (MPO), then compute its Matrix Product State (MPS) factorization, and, finally, sample 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 number of output samples to generate#
Let’s define all parameters of the tensor network state for a quantum circuit with the given number of qubits and request to produce a given number of output samples for the full qubit register. We also configure the maximal bond dimension for the MPS and MPO tensor network representations used in this example. For the MPO, we also define the number of sites (qubits) it acts on as well as the number of layers of the MPO.
Initialize the cuTensorNet library handle#
Define and allocate MPO tensors#
Here we define the shapes of the MPO tensors, fill them in with random data on Host, and, finally, transfer them into Device memory.
Define and allocate MPS tensors#
Here we define the shapes of the MPS tensors and allocate Device memory for their storage.
Allocate the scratch buffer on GPU#
Create a pure tensor network state#
Now let’s create a pure tensor network state for a quantum circuit with the given number of qubits (vacuum state).
Construct necessary MPO tensor network operators#
Let’s construct two MPO tensor network operators acting on different subsets of qubits.
Apply MPO tensor network operators to the quantum circuit state#
Let’s construct the target quantum circuit by applying MPO tensor network operators in an alternating fashion.
Request MPS factorization for the final quantum circuit state#
Here we express our intent to factorize the final state of the quantum circuit using MPS factorization. The provided shapes (mode extents) of the MPS tensors represent maximum size limits during renormalization. Computed mode extents for the final MPS tensors are less than or equal to these limits. Note: that 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 setting different options, for example, the SVD algorithm.
Prepare the computation of MPS factorization#
Let’s create a workspace descriptor and prepare the computation of the MPS factorization of the final quantum circuit state.
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 tensor network state sampler#
Once the quantum circuit state has been constructed and factorized using the MPS representation, let’s create the tensor network state sampler for all qubits.
Configure the tensor network state sampler#
Optionally, we can configure the tensor network state sampler by setting the number of hyper-samples to be used by the tensor network contraction path finder.
Prepare the tensor network state sampler#
Now let’s prepare the tensor network state sampler.
Set up the workspace#
Now we can set up the required workspace buffer.
Perform sampling of the final quantum circuit state#
Once everything had been set up, we perform sampling of the final MPS-factorized quantum circuit state and print the output samples.