----------------------------------------------- Sampling the Matrix Product State (QFT Circuit) ----------------------------------------------- The following code example illustrates how to define a tensor network state for a given quantum circuit (QFT), 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 -------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #1 :end-before: Sphinx: MPS Sampler QFT #2 ------------------------------------------------------------------------------------ Define the tensor network state and the desired number of output samples to generate ------------------------------------------------------------------------------------ Let's define a 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #2 :end-before: Sphinx: MPS Sampler QFT #3 ----------------------------------------- Initialize the cuTensorNet library handle ----------------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #3 :end-before: Sphinx: MPS Sampler QFT #4 ---------------------------------- Define quantum gates in GPU memory ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #4 :end-before: Sphinx: MPS Sampler QFT #5 ---------------------------------- Allocate MPS tensors in GPU memory ---------------------------------- Here we set the shapes of MPS tensors and allocate GPU memory for their storage. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #5 :end-before: Sphinx: MPS Sampler QFT #6 ---------------------------------- Allocate the scratch buffer on GPU ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #6 :end-before: Sphinx: MPS Sampler QFT #7 ---------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #7 :end-before: Sphinx: MPS Sampler QFT #8 ------------------- Apply quantum gates ------------------- Let's construct the QFT quantum circuit with no bit reversal by applying the corresponding quantum gates. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #8 :end-before: Sphinx: MPS Sampler QFT #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 (mode extents) of the MPS tensors refer to their maximal size limit during the MPS renormalization procedure. The actually computed shapes (mode extents) of the final MPS tensors may be smaller than their limits. Note that no computation is done here yet. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #9 :end-before: Sphinx: MPS Sampler QFT #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/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #10 :end-before: Sphinx: MPS Sampler QFT #11 -------------------------------------------- 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #11 :end-before: Sphinx: MPS Sampler QFT #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/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #12 :end-before: Sphinx: MPS Sampler QFT #13 --------------------------------------- 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 the full qubit register (all qubits). .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #13 :end-before: Sphinx: MPS Sampler QFT #14 ------------------------------------------ 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. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #14 :end-before: Sphinx: MPS Sampler QFT #15 ---------------------------------------- Prepare the tensor network state sampler ---------------------------------------- Now let's prepare the tensor network state sampler. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #15 :end-before: Sphinx: MPS Sampler QFT #16 -------------------- Set up the workspace -------------------- Now we can set up the required workspace buffer. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #16 :end-before: Sphinx: MPS Sampler QFT #17 --------------------------------------------------- Perform sampling of the final quantum circuit state --------------------------------------------------- Once everything had been set up, we perform sampling of the quantum circuit state and print the output samples. .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #17 :end-before: Sphinx: MPS Sampler QFT #18 -------------- Free resources -------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/qft_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPS Sampler QFT #18