Code example (MPS marginal distribution)#
The following code example illustrates how to define a tensor network state, compute its Matrix Product State (MPS) factorization, and then compute a marginal distribution tensor for the MPS-factorized state. The full code can be found in the NVIDIA/cuQuantum repository (here).
Headers and error handling#
7#include <cstdlib>
8#include <cstdio>
9#include <cassert>
10#include <complex>
11#include <vector>
12#include <bitset>
13#include <iostream>
14
15#include <cuda_runtime.h>
16#include <cutensornet.h>
17
18
19#define HANDLE_CUDA_ERROR(x) \
20{ const auto err = x; \
21 if( err != cudaSuccess ) \
22 { printf("CUDA error %s in line %d\n", cudaGetErrorString(err), __LINE__); fflush(stdout); std::abort(); } \
23};
24
25#define HANDLE_CUTN_ERROR(x) \
26{ const auto err = x; \
27 if( err != CUTENSORNET_STATUS_SUCCESS ) \
28 { printf("cuTensorNet error %s in line %d\n", cutensornetGetErrorString(err), __LINE__); fflush(stdout); std::abort(); } \
29};
30
31
32int main()
33{
34 static_assert(sizeof(size_t) == sizeof(int64_t), "Please build this sample on a 64-bit architecture!");
35
36 constexpr std::size_t fp64size = sizeof(double);
Define the tensor network state and the desired marginal distribution tensor#
Let’s define a tensor network state corresponding to a 16-qubit quantum circuit and request the marginal distribution tensor for qubits 0 and 1.
40 // Quantum state configuration
41 constexpr int32_t numQubits = 16;
42 const std::vector<int64_t> qubitDims(numQubits,2); // qubit dimensions
43 constexpr int32_t numMarginalModes = 2; // rank of the marginal (reduced density matrix)
44 const std::vector<int32_t> marginalModes({0,1}); // open qubits (must be in acsending order)
45 std::cout << "Quantum circuit: " << numQubits << " qubits\n";
Initialize the cuTensorNet library handle#
49 // Initialize the cuTensorNet library
50 HANDLE_CUDA_ERROR(cudaSetDevice(0));
51 cutensornetHandle_t cutnHandle;
52 HANDLE_CUTN_ERROR(cutensornetCreate(&cutnHandle));
53 std::cout << "Initialized cuTensorNet library on GPU 0\n";
Define quantum gates on GPU#
57 // Define necessary quantum gate tensors in Host memory
58 const double invsq2 = 1.0 / std::sqrt(2.0);
59 // Hadamard gate
60 const std::vector<std::complex<double>> h_gateH {{invsq2, 0.0}, {invsq2, 0.0},
61 {invsq2, 0.0}, {-invsq2, 0.0}};
62 // CX gate
63 const std::vector<std::complex<double>> h_gateCX {{1.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0},
64 {0.0, 0.0}, {1.0, 0.0}, {0.0, 0.0}, {0.0, 0.0},
65 {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {1.0, 0.0},
66 {0.0, 0.0}, {0.0, 0.0}, {1.0, 0.0}, {0.0, 0.0}};
67
68 // Copy quantum gates to Device memory
69 void *d_gateH{nullptr}, *d_gateCX{nullptr};
70 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateH, 4 * (2 * fp64size)));
71 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateCX, 16 * (2 * fp64size)));
72 std::cout << "Allocated quantum gate memory on GPU\n";
73 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateH, h_gateH.data(), 4 * (2 * fp64size), cudaMemcpyHostToDevice));
74 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateCX, h_gateCX.data(), 16 * (2 * fp64size), cudaMemcpyHostToDevice));
75 std::cout << "Copied quantum gates to GPU memory\n";
Allocate MPS tensors#
Here we set the shapes of MPS tensors and allocate GPU memory for their storage.
79 // Determine the MPS representation and allocate buffers for the MPS tensors
80 const int64_t maxExtent = 2; // GHZ state can be exactly represented with max bond dimension of 2
81 std::vector<std::vector<int64_t>> extents;
82 std::vector<int64_t*> extentsPtr(numQubits);
83 std::vector<void*> d_mpsTensors(numQubits, nullptr);
84 for (int32_t i = 0; i < numQubits; i++) {
85 if (i == 0) { // left boundary MPS tensor
86 extents.push_back({2, maxExtent});
87 HANDLE_CUDA_ERROR(cudaMalloc(&d_mpsTensors[i], 2 * maxExtent * 2 * fp64size));
88 }
89 else if (i == numQubits-1) { // right boundary MPS tensor
90 extents.push_back({maxExtent, 2});
91 HANDLE_CUDA_ERROR(cudaMalloc(&d_mpsTensors[i], 2 * maxExtent * 2 * fp64size));
92 }
93 else { // middle MPS tensors
94 extents.push_back({maxExtent, 2, maxExtent});
95 HANDLE_CUDA_ERROR(cudaMalloc(&d_mpsTensors[i], 2 * maxExtent * maxExtent * 2 * fp64size));
96 }
97 extentsPtr[i] = extents[i].data();
98 }
Allocate the marginal distribution tensor on GPU#
Here we allocate the marginal distribution tensor, that is, the reduced density matrix for qubits 0 and 1, on GPU.
102 // Allocate the specified quantum circuit reduced density matrix (marginal) in Device memory
103 void *d_rdm{nullptr};
104 std::size_t rdmDim = 1;
105 for(const auto & mode: marginalModes) rdmDim *= qubitDims[mode];
106 const std::size_t rdmSize = rdmDim * rdmDim;
107 HANDLE_CUDA_ERROR(cudaMalloc(&d_rdm, rdmSize * (2 * fp64size)));
Allocate the scratch buffer on GPU#
111 // Query the free memory on Device
112 std::size_t freeSize{0}, totalSize{0};
113 HANDLE_CUDA_ERROR(cudaMemGetInfo(&freeSize, &totalSize));
114 const std::size_t scratchSize = (freeSize - (freeSize % 4096)) / 2; // use half of available memory with alignment
115 void *d_scratch{nullptr};
116 void *h_scratch{nullptr}; // host scratch workspace buffer (attached if the prepare call reports a need)
117 int64_t hostWorksize{0};
118 HANDLE_CUDA_ERROR(cudaMalloc(&d_scratch, scratchSize));
119 std::cout << "Allocated " << scratchSize << " bytes of scratch memory on GPU\n";
Create a pure tensor network state#
Now let’s create a pure tensor network state for a 16-qubit quantum circuit.
123 // Create the initial quantum state
124 cutensornetState_t quantumState;
125 HANDLE_CUTN_ERROR(cutensornetCreateState(cutnHandle, CUTENSORNET_STATE_PURITY_PURE, numQubits, qubitDims.data(),
126 CUDA_C_64F, &quantumState));
127 std::cout << "Created the initial quantum state\n";
Apply quantum gates#
Let’s construct the GHZ quantum circuit by applying the corresponding quantum gates.
131 // Construct the final quantum circuit state (apply quantum gates) for the GHZ circuit
132 int64_t id;
133 HANDLE_CUTN_ERROR(cutensornetStateApplyTensorOperator(cutnHandle, quantumState, 1, std::vector<int32_t>{{0}}.data(),
134 d_gateH, nullptr, 1, 0, 1, &id));
135 for(int32_t i = 1; i < numQubits; ++i) {
136 HANDLE_CUTN_ERROR(cutensornetStateApplyTensorOperator(cutnHandle, quantumState, 2, std::vector<int32_t>{{i-1,i}}.data(),
137 d_gateCX, nullptr, 1, 0, 1, &id));
138 }
139 std::cout << "Applied quantum gates\n";
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.
143 // Specify the final target MPS representation (use default fortran strides)
144 HANDLE_CUTN_ERROR(cutensornetStateFinalizeMPS(cutnHandle, quantumState,
145 CUTENSORNET_BOUNDARY_CONDITION_OPEN, extentsPtr.data(), /*strides=*/nullptr));
146 std::cout << "Requested the final MPS factorization of the quantum circuit state\n";
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.
150 // Optional, set up the SVD method for MPS truncation.
151 cutensornetTensorSVDAlgo_t algo = CUTENSORNET_TENSOR_SVD_ALGO_GESVDJ;
152 HANDLE_CUTN_ERROR(cutensornetStateConfigure(cutnHandle, quantumState,
153 CUTENSORNET_STATE_CONFIG_MPS_SVD_ALGO, &algo, sizeof(algo)));
154 std::cout << "Configured the MPS factorization computation\n";
Prepare the computation of MPS factorization#
Let’s create a workspace descriptor and prepare the computation of MPS factorization.
158 // Prepare the MPS computation and attach workspace
159 cutensornetWorkspaceDescriptor_t workDesc;
160 HANDLE_CUTN_ERROR(cutensornetCreateWorkspaceDescriptor(cutnHandle, &workDesc));
161 std::cout << "Created the workspace descriptor\n";
162 HANDLE_CUTN_ERROR(cutensornetStatePrepare(cutnHandle, quantumState, scratchSize, workDesc, 0x0));
163 std::cout << "Prepared the computation of the quantum circuit state\n";
164 double flops {0.0};
165 HANDLE_CUTN_ERROR(cutensornetStateGetInfo(cutnHandle, quantumState,
166 CUTENSORNET_STATE_INFO_FLOPS, &flops, sizeof(flops)));
167 if(flops > 0.0) {
168 std::cout << "Total flop count = " << (flops/1e9) << " GFlop\n";
169 }else if(flops < 0.0) {
170 std::cout << "ERROR: Negative Flop count!\n";
171 std::abort();
172 }
173
174 int64_t worksize {0};
175 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle,
176 workDesc,
177 CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
178 CUTENSORNET_MEMSPACE_DEVICE,
179 CUTENSORNET_WORKSPACE_SCRATCH,
180 &worksize));
181 std::cout << "Scratch GPU workspace size (bytes) for MPS computation = " << worksize << std::endl;
182 if(worksize <= scratchSize) {
183 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_DEVICE,
184 CUTENSORNET_WORKSPACE_SCRATCH, d_scratch, worksize));
185 }else{
186 std::cout << "ERROR: Insufficient workspace size on Device!\n";
187 std::abort();
188 }
189
190 // Query the host scratch workspace size and attach a host scratch buffer if one is required
191 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle, workDesc, CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
192 CUTENSORNET_MEMSPACE_HOST, CUTENSORNET_WORKSPACE_SCRATCH, &hostWorksize));
193 std::cout << "Scratch Host workspace size (bytes) = " << hostWorksize << std::endl;
194 if(hostWorksize > 0) {
195 h_scratch = std::realloc(h_scratch, hostWorksize);
196 if(h_scratch == nullptr) { std::cout << "ERROR: Host scratch allocation failed!\n"; std::abort(); }
197 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_HOST,
198 CUTENSORNET_WORKSPACE_SCRATCH, h_scratch, hostWorksize));
199 }
200 std::cout << "Set the workspace buffer for the MPS factorization computation\n";
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.
204 // Execute MPS computation
205 HANDLE_CUTN_ERROR(cutensornetStateCompute(cutnHandle, quantumState,
206 workDesc, extentsPtr.data(), /*strides=*/nullptr, d_mpsTensors.data(), 0));
207 std::cout << "Computed the MPS factorization\n";
Create the marginal distribution object#
Once the quantum circuit has been constructed, let’s create the marginal distribution object that will compute the marginal distribution tensor (reduced density matrix) for qubits 0 and 1.
211 // Specify the desired reduced density matrix (marginal)
212 cutensornetStateMarginal_t marginal;
213 HANDLE_CUTN_ERROR(cutensornetCreateMarginal(cutnHandle, quantumState, numMarginalModes, marginalModes.data(),
214 0, nullptr, std::vector<int64_t>{{1,2,4,8}}.data(), &marginal)); // using explicit strides
215 std::cout << "Created the specified quantum circuit reduced densitry matrix (marginal)\n";
Configure the marginal distribution object#
Now we can configure the marginal distribution object by setting the number of hyper-samples to be used by the tensor network contraction path finder.
219 // Configure the computation of the specified quantum circuit reduced density matrix (marginal)
220 const int32_t numHyperSamples = 8; // desired number of hyper samples used in the tensor network contraction path finder
221 HANDLE_CUTN_ERROR(cutensornetMarginalConfigure(cutnHandle, marginal,
222 CUTENSORNET_MARGINAL_CONFIG_NUM_HYPER_SAMPLES, &numHyperSamples, sizeof(numHyperSamples)));
223 std::cout << "Configured the specified quantum circuit reduced density matrix (marginal) computation\n";
Prepare the computation of the marginal distribution tensor#
Let’s prepare the computation of the marginal distribution tensor.
227 // Prepare the specified quantum circuit reduced densitry matrix (marginal)
228 HANDLE_CUTN_ERROR(cutensornetMarginalPrepare(cutnHandle, marginal, scratchSize, workDesc, 0x0));
229 std::cout << "Prepared the specified quantum circuit reduced density matrix (marginal)\n";
230 flops = 0.0;
231 HANDLE_CUTN_ERROR(cutensornetMarginalGetInfo(cutnHandle, marginal,
232 CUTENSORNET_MARGINAL_INFO_FLOPS, &flops, sizeof(flops)));
233 std::cout << "Total flop count = " << (flops/1e9) << " GFlop\n";
234 if(flops <= 0.0) {
235 std::cout << "ERROR: Invalid Flop count!\n";
236 std::abort();
237 }
Set up the workspace#
Now we can set up the required workspace buffer.
241 // Attach the workspace buffer
242 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle,
243 workDesc,
244 CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
245 CUTENSORNET_MEMSPACE_DEVICE,
246 CUTENSORNET_WORKSPACE_SCRATCH,
247 &worksize));
248 std::cout << "Required scratch GPU workspace size (bytes) for marginal computation = " << worksize << std::endl;
249 if(worksize <= scratchSize) {
250 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_DEVICE,
251 CUTENSORNET_WORKSPACE_SCRATCH, d_scratch, worksize));
252 }else{
253 std::cout << "ERROR: Insufficient workspace size on Device!\n";
254 std::abort();
255 }
256
257 // Query the host scratch workspace size and attach a host scratch buffer if one is required
258 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle, workDesc, CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
259 CUTENSORNET_MEMSPACE_HOST, CUTENSORNET_WORKSPACE_SCRATCH, &hostWorksize));
260 std::cout << "Scratch Host workspace size (bytes) = " << hostWorksize << std::endl;
261 if(hostWorksize > 0) {
262 h_scratch = std::realloc(h_scratch, hostWorksize);
263 if(h_scratch == nullptr) { std::cout << "ERROR: Host scratch allocation failed!\n"; std::abort(); }
264 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_HOST,
265 CUTENSORNET_WORKSPACE_SCRATCH, h_scratch, hostWorksize));
266 }
267 std::cout << "Set the workspace buffer\n";
268
Compute the marginal distribution tensor#
Once everything has been set up, we compute the requested marginal distribution tensor (reduced density matrix) for qubits 0 and 1, copy it back to Host memory, and print it.
271 // Compute the specified quantum circuit reduced densitry matrix (marginal)
272 HANDLE_CUTN_ERROR(cutensornetMarginalCompute(cutnHandle, marginal, nullptr, workDesc, d_rdm, 0));
273 std::cout << "Computed the specified quantum circuit reduced density matrix (marginal)\n";
274 std::vector<std::complex<double>> h_rdm(rdmSize);
275 HANDLE_CUDA_ERROR(cudaMemcpy(h_rdm.data(), d_rdm, rdmSize * (2 * fp64size), cudaMemcpyDeviceToHost));
276 std::cout << "Reduced density matrix for " << numMarginalModes << " qubits:\n";
277 for(std::size_t i = 0; i < rdmDim; ++i) {
278 for(std::size_t j = 0; j < rdmDim; ++j) {
279 std::cout << " " << h_rdm[i + j * rdmDim];
280 }
281 std::cout << std::endl;
282 }
Free resources#
286 // Destroy the workspace descriptor
287 HANDLE_CUTN_ERROR(cutensornetDestroyWorkspaceDescriptor(workDesc));
288 std::cout << "Destroyed the workspace descriptor\n";
289
290 // Destroy the quantum circuit reduced density matrix
291 HANDLE_CUTN_ERROR(cutensornetDestroyMarginal(marginal));
292 std::cout << "Destroyed the quantum circuit state reduced density matrix (marginal)\n";
293
294 // Destroy the quantum circuit state
295 HANDLE_CUTN_ERROR(cutensornetDestroyState(quantumState));
296 std::cout << "Destroyed the quantum circuit state\n";
297
298 for (int32_t i = 0; i < numQubits; i++) {
299 HANDLE_CUDA_ERROR(cudaFree(d_mpsTensors[i]));
300 }
301 std::free(h_scratch);
302 HANDLE_CUDA_ERROR(cudaFree(d_scratch));
303 HANDLE_CUDA_ERROR(cudaFree(d_rdm));
304 HANDLE_CUDA_ERROR(cudaFree(d_gateCX));
305 HANDLE_CUDA_ERROR(cudaFree(d_gateH));
306 std::cout << "Freed memory on GPU\n";
307
308 // Finalize the cuTensorNet library
309 HANDLE_CUTN_ERROR(cutensornetDestroy(cutnHandle));
310 std::cout << "Finalized the cuTensorNet library\n";
311
312 return 0;
313}