----------------------------------------------- Computing Matrix Product State (MPS) Amplitudes ----------------------------------------------- The following code example illustrates how to define a tensor network state, factorize it as a Matrix Product State (MPS), and then compute a slice of amplitudes of the factorized MPS state. The full code can be found in the `NVIDIA/cuQuantum `_ repository (`here `_). -------------------------- Headers and error handling -------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #1 :end-before: Sphinx: MPS Amplitudes #2 ------------------------------------------------------------------------- Define the tensor network state and the desired slice of state amplitudes ------------------------------------------------------------------------- Let's define a tensor network state corresponding to a 6-qubit quantum circuit and request a slice of state amplitudes where qubits 0 and 1 are fixed at value 1. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #2 :end-before: Sphinx: MPS Amplitudes #3 ----------------------------------------- Initialize the cuTensorNet library handle ----------------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #3 :end-before: Sphinx: MPS Amplitudes #4 --------------------------- Define quantum gates on GPU --------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #4 :end-before: Sphinx: MPS Amplitudes #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_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #5 :end-before: Sphinx: MPS Amplitudes #6 ------------------------------------------- Allocate the amplitudes slice tensor on GPU ------------------------------------------- Here we allocate GPU memory for the requested amplitudes slice tensor. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #6 :end-before: Sphinx: MPS Amplitudes #7 ---------------------------------- Allocate the scratch buffer on GPU ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #7 :end-before: Sphinx: MPS Amplitudes #8 ---------------------------------- Create a pure tensor network state ---------------------------------- Now let's create a pure tensor network state for a 6-qubit quantum circuit. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #8 :end-before: Sphinx: MPS Amplitudes #9 ------------------- Apply quantum gates ------------------- Let's construct the GHZ quantum circuit by applying the corresponding quantum gates. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #9 :end-before: Sphinx: MPS Amplitudes #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_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #10 :end-before: Sphinx: MPS Amplitudes #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. Starting with cuTensorNet v2.7.0, the MPS gauge option can now be enabled. By setting it to `CUTENSORNET_STATE_MPS_GAUGE_SIMPLE`, the simple update algorithm is utilized to enhance the accuracy of the MPS factorization. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #11 :end-before: Sphinx: MPS Amplitudes #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_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #12 :end-before: Sphinx: MPS Amplitudes #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_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #13 :end-before: Sphinx: MPS Amplitudes #14 ------------------------------------ Create the state amplitudes accessor ------------------------------------ Once the factorized MPS representation of the final quantum circuit state has been computed, let's create the amplitudes accessor object that will compute the requested slice of state amplitudes. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #14 :end-before: Sphinx: MPS Amplitudes #15 --------------------------------------- Configure the state amplitudes accessor --------------------------------------- Now we can configure the state amplitudes accessor 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_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #15 :end-before: Sphinx: MPS Amplitudes #16 ------------------------------------------------------ Prepare the computation of the amplitudes slice tensor ------------------------------------------------------ Let's prepare the computation of the amplitudes slice tensor. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #16 :end-before: Sphinx: MPS Amplitudes #17 -------------------- Set up the workspace -------------------- Now we can set up the required workspace buffer. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #17 :end-before: Sphinx: MPS Amplitudes #18 ----------------------------------------------- Compute the specified slice of state amplitudes ----------------------------------------------- Once everything has been set up, we compute the requested slice of state amplitudes, copy it back to Host memory, and print it. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #18 :end-before: Sphinx: MPS Amplitudes #19 -------------- Free resources -------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Amplitudes #19