------------------------------------------------ Computing Matrix Product State Expectation Value ------------------------------------------------ The following code example illustrates how to define a tensor network state and then compute its Matrix Product State (MPS) factorization, followed by computing the expectation value of a tensor network operator over 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #1 :end-before: Sphinx: MPS Expectation #2 ------------------------------- Define the tensor network state ------------------------------- Let's define a tensor network state corresponding to a 16-qubit quantum circuit. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #2 :end-before: Sphinx: MPS Expectation #3 ----------------------------------------- Initialize the cuTensorNet library handle ----------------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #3 :end-before: Sphinx: MPS Expectation #4 --------------------------- Define quantum gates on GPU --------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #4 :end-before: Sphinx: MPS Expectation #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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #5 :end-before: Sphinx: MPS Expectation #6 ---------------------------------- Allocate the scratch buffer on GPU ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #6 :end-before: Sphinx: MPS Expectation #7 ---------------------------------- 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #7 :end-before: Sphinx: MPS Expectation #8 ------------------- Apply quantum gates ------------------- Let's construct the GHZ quantum circuit by applying the corresponding quantum gates. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #8 :end-before: Sphinx: MPS Expectation #9 ------------------------------------------------------------- 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #9 :end-before: Sphinx: MPS Expectation #10 ------------------------------------- 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #10 :end-before: Sphinx: MPS Expectation #11 -------------------------------------------- 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #11 :end-before: Sphinx: MPS Expectation #12 ------------------------- 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #12 :end-before: Sphinx: MPS Expectation #13 ----------------------------------- Construct a tensor network operator ----------------------------------- Now let's create an empty tensor network operator for 16-qubit states and then append three components to it, where each component is a direct product of Pauli matrices scaled by some complex coefficient (like in the Jordan-Wigner representation). .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #13 :end-before: Sphinx: MPS Expectation #14 ----------------------------------- Create the expectation value object ----------------------------------- Once the quantum circuit and the tensor network operator have been constructed, and the final quantum circuit state has been factorized using the MPS representation, let's create the expectation value object that will compute the expectation value of the specified tensor network operator over the final MPS-factorized state of the specified quantum circuit. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #14 :end-before: Sphinx: MPS Expectation #15 ------------------------------------------- Configure the expectation value calculation ------------------------------------------- Now we can configure the expectation value 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_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #15 :end-before: Sphinx: MPS Expectation #16 ----------------------------------------- Prepare the expectation value calculation ----------------------------------------- Let's prepare the computation of the desired expectation value. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #16 :end-before: Sphinx: MPS Expectation #17 -------------------- Set up the workspace -------------------- Now we can set up the required workspace buffer. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #17 :end-before: Sphinx: MPS Expectation #18 --------------------------------------- Compute the requested expectation value --------------------------------------- Once everything has been set up, we compute the requested expectation value and print it. Note that the returned expectation value is not normalized. The 2-norm of the tensor network state is returned as a separate argument. .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #18 :end-before: Sphinx: MPS Expectation #19 -------------- Free resources -------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mps_expectation_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Expectation #19