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#

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.

Initialize the cuTensorNet library handle#

Define quantum gates on GPU#

Allocate the amplitudes slice tensor on GPU#

Here we allocate GPU memory for the requested amplitudes slice tensor.

Allocate the scratch buffer on GPU#

Create a pure tensor network state#

Now let’s create a pure tensor network state for a 6-qubit quantum circuit.

Apply quantum gates#

Let’s construct the GHZ quantum circuit by applying the corresponding quantum gates.

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.

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.

Prepare the computation of the amplitudes slice tensor#

Let’s create a workspace descriptor and prepare the computation of the amplitudes slice tensor.

Set up the workspace#

Now we can set up the required workspace buffer.

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.

Free resources#