----------------------------------------- Computing Tensor Network State Amplitudes ----------------------------------------- The following code example illustrates how to define a tensor network state and then compute a slice of amplitudes of the tensor network state. The full code can be found in the `NVIDIA/cuQuantum `_ repository (`here `_). -------------------------- Headers and error handling -------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #1 :end-before: Sphinx: 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/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #2 :end-before: Sphinx: Amplitudes #3 ----------------------------------------- Initialize the cuTensorNet library handle ----------------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #3 :end-before: Sphinx: Amplitudes #4 --------------------------- Define quantum gates on GPU --------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #4 :end-before: Sphinx: Amplitudes #5 ------------------------------------------- Allocate the amplitudes slice tensor on GPU ------------------------------------------- Here we allocate GPU memory for the requested amplitudes slice tensor. .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #5 :end-before: Sphinx: Amplitudes #6 ---------------------------------- Allocate the scratch buffer on GPU ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #6 :end-before: Sphinx: Amplitudes #7 ---------------------------------- 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/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #7 :end-before: Sphinx: Amplitudes #8 ------------------- Apply quantum gates ------------------- Let's construct the GHZ quantum circuit by applying the corresponding quantum gates. .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #8 :end-before: Sphinx: Amplitudes #9 ------------------------------------ Create the state amplitudes accessor ------------------------------------ Once the quantum circuit has been constructed, let's create the amplitudes accessor object that will compute the requested slice of state amplitudes. .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #9 :end-before: Sphinx: Amplitudes #10 --------------------------------------- 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/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #10 :end-before: Sphinx: Amplitudes #11 ------------------------------------------------------ Prepare the computation of the amplitudes slice tensor ------------------------------------------------------ Let's create a workspace descriptor and prepare the computation of the amplitudes slice tensor. .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #11 :end-before: Sphinx: Amplitudes #12 -------------------- Set up the workspace -------------------- Now we can set up the required workspace buffer. .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #12 :end-before: Sphinx: Amplitudes #13 ----------------------------------------------- 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/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #13 :end-before: Sphinx: Amplitudes #14 -------------- Free resources -------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/amplitudes_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: Amplitudes #14