------------------------------------------------------------------------- Sampling the Matrix Product State (circuit with Matrix Product Operators) ------------------------------------------------------------------------- The following code example illustrates how to define a tensor network state by a circuit with Matrix Product Operators (MPO), 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/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #1 :end-before: Sphinx: MPO-MPS Sampler #2 ------------------------------------------------------------------------------------ Define the tensor network state and the desired number of output samples to generate ------------------------------------------------------------------------------------ Let's define all parameters of the 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. We also configure the maximal bond dimension for the MPS and MPO tensor network representations used in this example. For the MPO, we also define the number of sites (qubits) it acts on as well as the number of layers of the MPO. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #2 :end-before: Sphinx: MPO-MPS Sampler #3 ----------------------------------------- Initialize the cuTensorNet library handle ----------------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #3 :end-before: Sphinx: MPO-MPS Sampler #4 ------------------------------- Define and allocate MPO tensors ------------------------------- Here we define the shapes of the MPO tensors, fill them in with random data on Host, and, finally, transfer them into Device memory. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #4 :end-before: Sphinx: MPO-MPS Sampler #5 ------------------------------- Define and allocate MPS tensors ------------------------------- Here we define the shapes of the MPS tensors and allocate Device memory for their storage. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #5 :end-before: Sphinx: MPO-MPS Sampler #6 ---------------------------------- Allocate the scratch buffer on GPU ---------------------------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #6 :end-before: Sphinx: MPO-MPS Sampler #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 (vacuum state). .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #7 :end-before: Sphinx: MPO-MPS Sampler #8 ------------------------------------------------ Construct necessary MPO tensor network operators ------------------------------------------------ Let's construct two MPO tensor network operators acting on different subsets of qubits. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #8 :end-before: Sphinx: MPO-MPS Sampler #9 --------------------------------------------------------------- Apply MPO tensor network operators to the quantum circuit state --------------------------------------------------------------- Let's construct the target quantum circuit by applying MPO tensor network operators in an alternating fashion. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #9 :end-before: Sphinx: MPO-MPS Sampler #10 ------------------------------------------------------------- Request MPS factorization for the final quantum circuit state ------------------------------------------------------------- Here we express our intent to factorize the final state of the quantum circuit using MPS factorization. The provided shapes (mode extents) of the MPS tensors represent maximum size limits during renormalization. Computed mode extents for the final MPS tensors are less than or equal to these limits. Note: that no computation is done here yet. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #10 :end-before: Sphinx: MPO-MPS Sampler #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 setting different options, for example, the SVD algorithm. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #11 :end-before: Sphinx: MPO-MPS Sampler #12 -------------------------------------------- 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/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #12 :end-before: Sphinx: MPO-MPS Sampler #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/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #13 :end-before: Sphinx: MPO-MPS Sampler #14 --------------------------------------- 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 all qubits. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #14 :end-before: Sphinx: MPO-MPS Sampler #15 ------------------------------------------ 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/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #15 :end-before: Sphinx: MPO-MPS Sampler #16 ---------------------------------------- Prepare the tensor network state sampler ---------------------------------------- Now let's prepare the tensor network state sampler. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #16 :end-before: Sphinx: MPO-MPS Sampler #17 -------------------- Set up the workspace -------------------- Now we can set up the required workspace buffer. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #17 :end-before: Sphinx: MPO-MPS Sampler #18 --------------------------------------------------- Perform sampling of the final quantum circuit state --------------------------------------------------- Once everything had been set up, we perform sampling of the final MPS-factorized quantum circuit state and print the output samples. .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #18 :end-before: Sphinx: MPO-MPS Sampler #19 -------------- Free resources -------------- .. literalinclude:: ../../../../tensor_network/samples/high_level/mpo_mps_sampling_example.cu :language: c++ :linenos: :lineno-match: :start-after: Sphinx: MPO-MPS Sampler #19