----------------------------------------------------------- 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 -------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #1 :end-before: Sphinx: MPS Marginal #2 ---------------------------------------------------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #2 :end-before: Sphinx: MPS Marginal #3 ----------------------------------------- Initialize the cuTensorNet library handle ----------------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #3 :end-before: Sphinx: MPS Marginal #4 --------------------------- Define quantum gates on GPU --------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #4 :end-before: Sphinx: MPS Marginal #5 -------------------- Allocate MPS tensors -------------------- Here we set the shapes of MPS tensors and allocate GPU memory for their storage. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #5 :end-before: Sphinx: MPS Marginal #6 ------------------------------------------------ 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #6 :end-before: Sphinx: MPS Marginal #7 ---------------------------------- Allocate the scratch buffer on GPU ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #7 :end-before: Sphinx: MPS Marginal #8 ---------------------------------- Create a pure tensor network state ---------------------------------- Now let's create a pure tensor network state for a 16-qubit quantum circuit. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #8 :end-before: Sphinx: MPS Marginal #9 ------------------- Apply quantum gates ------------------- Let's construct the GHZ quantum circuit by applying the corresponding quantum gates. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #9 :end-before: Sphinx: MPS Marginal #10 ------------------------------------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #10 :end-before: Sphinx: MPS Marginal #11 ------------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #11 :end-before: Sphinx: MPS Marginal #12 -------------------------------------------- Prepare the computation of MPS factorization -------------------------------------------- Let's create a workspace descriptor and prepare the computation of MPS factorization. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #12 :end-before: Sphinx: MPS Marginal #13 ------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #13 :end-before: Sphinx: MPS Marginal #14 --------------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #14 :end-before: Sphinx: MPS Marginal #15 ------------------------------------------ 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #15 :end-before: Sphinx: MPS Marginal #16 ----------------------------------------------------------- Prepare the computation of the marginal distribution tensor ----------------------------------------------------------- Let's prepare the computation of the marginal distribution tensor. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #16 :end-before: Sphinx: MPS Marginal #17 -------------------- Set up the workspace -------------------- Now we can set up the required workspace buffer. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #17 :end-before: Sphinx: MPS Marginal #18 ---------------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #18 :end-before: Sphinx: MPS Marginal #19 -------------- Free resources -------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_marginal_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Marginal #19