Code example (MPS expectation value)#
The following code example illustrates how to define a tensor network state and then compute its Matrix Product State (MPS) factorization, followed by computing the expectation value of a tensor network operator over 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#
Let’s define a tensor network state corresponding to a 16-qubit quantum circuit.
40 // Quantum state configuration
41 constexpr int32_t numQubits = 16; // number of qubits
42 const std::vector<int64_t> qubitDims(numQubits,2); // qubit dimensions
43 std::cout << "Quantum circuit: " << numQubits << " qubits\n";
Initialize the cuTensorNet library handle#
47 // Initialize the cuTensorNet library
48 HANDLE_CUDA_ERROR(cudaSetDevice(0));
49 cutensornetHandle_t cutnHandle;
50 HANDLE_CUTN_ERROR(cutensornetCreate(&cutnHandle));
51 std::cout << "Initialized cuTensorNet library on GPU 0\n";
Define quantum gates on GPU#
55 // Define necessary quantum gate tensors in Host memory
56 const double invsq2 = 1.0 / std::sqrt(2.0);
57 // Hadamard gate
58 const std::vector<std::complex<double>> h_gateH {{invsq2, 0.0}, {invsq2, 0.0},
59 {invsq2, 0.0}, {-invsq2, 0.0}};
60 // Pauli X gate
61 const std::vector<std::complex<double>> h_gateX {{0.0, 0.0}, {1.0, 0.0},
62 {1.0, 0.0}, {0.0, 0.0}};
63 // Pauli Y gate
64 const std::vector<std::complex<double>> h_gateY {{0.0, 0.0}, {0.0, -1.0},
65 {0.0, 1.0}, {0.0, 0.0}};
66 // Pauli Z gate
67 const std::vector<std::complex<double>> h_gateZ {{1.0, 0.0}, {0.0, 0.0},
68 {0.0, 0.0}, {-1.0, 0.0}};
69 // CX gate
70 const std::vector<std::complex<double>> h_gateCX {{1.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0},
71 {0.0, 0.0}, {1.0, 0.0}, {0.0, 0.0}, {0.0, 0.0},
72 {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {1.0, 0.0},
73 {0.0, 0.0}, {0.0, 0.0}, {1.0, 0.0}, {0.0, 0.0}};
74
75 // Copy quantum gates to Device memory
76 void *d_gateH{nullptr}, *d_gateX{nullptr}, *d_gateY{nullptr}, *d_gateZ{nullptr}, *d_gateCX{nullptr};
77 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateH, 4 * (2 * fp64size)));
78 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateX, 4 * (2 * fp64size)));
79 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateY, 4 * (2 * fp64size)));
80 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateZ, 4 * (2 * fp64size)));
81 HANDLE_CUDA_ERROR(cudaMalloc(&d_gateCX, 16 * (2 * fp64size)));
82 std::cout << "Allocated quantum gate memory on GPU\n";
83 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateH, h_gateH.data(), 4 * (2 * fp64size), cudaMemcpyHostToDevice));
84 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateX, h_gateX.data(), 4 * (2 * fp64size), cudaMemcpyHostToDevice));
85 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateY, h_gateY.data(), 4 * (2 * fp64size), cudaMemcpyHostToDevice));
86 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateZ, h_gateZ.data(), 4 * (2 * fp64size), cudaMemcpyHostToDevice));
87 HANDLE_CUDA_ERROR(cudaMemcpy(d_gateCX, h_gateCX.data(), 16 * (2 * fp64size), cudaMemcpyHostToDevice));
88 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.
92 // Determine the MPS representation and allocate buffers for the MPS tensors
93 const int64_t maxExtent = 2; // GHZ state can be exactly represented with max bond dimension of 2
94 std::vector<std::vector<int64_t>> extents;
95 std::vector<int64_t*> extentsPtr(numQubits);
96 std::vector<void*> d_mpsTensors(numQubits, nullptr);
97 for (int32_t i = 0; i < numQubits; i++) {
98 if (i == 0) { // left boundary MPS tensor
99 extents.push_back({2, maxExtent});
100 HANDLE_CUDA_ERROR(cudaMalloc(&d_mpsTensors[i], 2 * maxExtent * 2 * fp64size));
101 }
102 else if (i == numQubits-1) { // right boundary MPS tensor
103 extents.push_back({maxExtent, 2});
104 HANDLE_CUDA_ERROR(cudaMalloc(&d_mpsTensors[i], 2 * maxExtent * 2 * fp64size));
105 }
106 else { // middle MPS tensors
107 extents.push_back({maxExtent, 2, maxExtent});
108 HANDLE_CUDA_ERROR(cudaMalloc(&d_mpsTensors[i], 2 * maxExtent * maxExtent * 2 * fp64size));
109 }
110 extentsPtr[i] = extents[i].data();
111 }
Allocate the scratch buffer on GPU#
115 // Query the free memory on Device
116 std::size_t freeSize{0}, totalSize{0};
117 HANDLE_CUDA_ERROR(cudaMemGetInfo(&freeSize, &totalSize));
118 const std::size_t scratchSize = (freeSize - (freeSize % 4096)) / 2; // use half of available memory with alignment
119 void *d_scratch{nullptr};
120 void *h_scratch{nullptr}; // host scratch workspace buffer (attached if the prepare call reports a need)
121 int64_t hostWorksize{0};
122 HANDLE_CUDA_ERROR(cudaMalloc(&d_scratch, scratchSize));
123 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.
127 // Create the initial quantum state
128 cutensornetState_t quantumState;
129 HANDLE_CUTN_ERROR(cutensornetCreateState(cutnHandle, CUTENSORNET_STATE_PURITY_PURE, numQubits, qubitDims.data(),
130 CUDA_C_64F, &quantumState));
131 std::cout << "Created the initial quantum state\n";
Apply quantum gates#
Let’s construct the GHZ quantum circuit by applying the corresponding quantum gates.
135 // Construct the final quantum circuit state (apply quantum gates) for the GHZ circuit
136 int64_t id;
137 HANDLE_CUTN_ERROR(cutensornetStateApplyTensorOperator(cutnHandle, quantumState, 1, std::vector<int32_t>{{0}}.data(),
138 d_gateH, nullptr, 1, 0, 1, &id));
139 for(int32_t i = 1; i < numQubits; ++i) {
140 HANDLE_CUTN_ERROR(cutensornetStateApplyTensorOperator(cutnHandle, quantumState, 2, std::vector<int32_t>{{i-1,i}}.data(),
141 d_gateCX, nullptr, 1, 0, 1, &id));
142 }
143 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.
147 // Specify the final target MPS representation (use default fortran strides)
148 HANDLE_CUTN_ERROR(cutensornetStateFinalizeMPS(cutnHandle, quantumState,
149 CUTENSORNET_BOUNDARY_CONDITION_OPEN, extentsPtr.data(), /*strides=*/nullptr ));
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.
153 // Optional, set up the SVD method for truncation.
154 cutensornetTensorSVDAlgo_t algo = CUTENSORNET_TENSOR_SVD_ALGO_GESVDJ;
155 HANDLE_CUTN_ERROR(cutensornetStateConfigure(cutnHandle, quantumState,
156 CUTENSORNET_STATE_CONFIG_MPS_SVD_ALGO, &algo, sizeof(algo)));
157 std::cout << "Configured the MPS computation\n";
Prepare the computation of MPS factorization#
Let’s create a workspace descriptor and prepare the computation of MPS factorization.
161 // Prepare the MPS computation and attach workspace
162 cutensornetWorkspaceDescriptor_t workDesc;
163 HANDLE_CUTN_ERROR(cutensornetCreateWorkspaceDescriptor(cutnHandle, &workDesc));
164 std::cout << "Created the workspace descriptor\n";
165 HANDLE_CUTN_ERROR(cutensornetStatePrepare(cutnHandle, quantumState, scratchSize, workDesc, 0x0));
166 std::cout << "Prepared the computation of the quantum circuit state\n";
167 double flops {0.0};
168 HANDLE_CUTN_ERROR(cutensornetStateGetInfo(cutnHandle, quantumState,
169 CUTENSORNET_STATE_INFO_FLOPS, &flops, sizeof(flops)));
170 if(flops > 0.0) {
171 std::cout << "Total flop count = " << (flops/1e9) << " GFlop\n";
172 }else if(flops < 0.0) {
173 std::cout << "ERROR: Negative Flop count!\n";
174 std::abort();
175 }
176
177 int64_t worksize {0};
178 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle,
179 workDesc,
180 CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
181 CUTENSORNET_MEMSPACE_DEVICE,
182 CUTENSORNET_WORKSPACE_SCRATCH,
183 &worksize));
184 std::cout << "Scratch GPU workspace size (bytes) for MPS computation = " << worksize << std::endl;
185 if(worksize <= scratchSize) {
186 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_DEVICE,
187 CUTENSORNET_WORKSPACE_SCRATCH, d_scratch, worksize));
188 }else{
189 std::cout << "ERROR: Insufficient workspace size on Device!\n";
190 std::abort();
191 }
192
193 // Query the host scratch workspace size and attach a host scratch buffer if one is required
194 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle, workDesc, CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
195 CUTENSORNET_MEMSPACE_HOST, CUTENSORNET_WORKSPACE_SCRATCH, &hostWorksize));
196 std::cout << "Scratch Host workspace size (bytes) = " << hostWorksize << std::endl;
197 if(hostWorksize > 0) {
198 h_scratch = std::realloc(h_scratch, hostWorksize);
199 if(h_scratch == nullptr) { std::cout << "ERROR: Host scratch allocation failed!\n"; std::abort(); }
200 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_HOST,
201 CUTENSORNET_WORKSPACE_SCRATCH, h_scratch, hostWorksize));
202 }
203 std::cout << "Set the workspace buffer for MPS 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.
207 // Execute MPS computation
208 HANDLE_CUTN_ERROR(cutensornetStateCompute(cutnHandle, quantumState,
209 workDesc, extentsPtr.data(), /*strides=*/nullptr, d_mpsTensors.data(), 0));
Construct a tensor network operator#
Now let’s create an empty tensor network operator for 16-qubit states and then append three components to it, where each component is a direct product of Pauli matrices scaled by some complex coefficient (like in the Jordan-Wigner representation).
213 // Create an empty tensor network operator
214 cutensornetNetworkOperator_t hamiltonian;
215 HANDLE_CUTN_ERROR(cutensornetCreateNetworkOperator(cutnHandle, numQubits, qubitDims.data(), CUDA_C_64F, &hamiltonian));
216 // Append component (0.5 * Z1 * Z2) to the tensor network operator
217 {
218 const int32_t numModes[] = {1, 1}; // Z1 acts on 1 mode, Z2 acts on 1 mode
219 const int32_t modesZ1[] = {1}; // state modes Z1 acts on
220 const int32_t modesZ2[] = {2}; // state modes Z2 acts on
221 const int32_t * stateModes[] = {modesZ1, modesZ2}; // state modes (Z1 * Z2) acts on
222 const void * gateData[] = {d_gateZ, d_gateZ}; // GPU pointers to gate data
223 HANDLE_CUTN_ERROR(cutensornetNetworkOperatorAppendProduct(cutnHandle, hamiltonian, cuDoubleComplex{0.5,0.0},
224 2, numModes, stateModes, NULL, gateData, &id));
225 }
226 // Append component (0.25 * Y3) to the tensor network operator
227 {
228 const int32_t numModes[] = {1}; // Y3 acts on 1 mode
229 const int32_t modesY3[] = {3}; // state modes Y3 acts on
230 const int32_t * stateModes[] = {modesY3}; // state modes (Y3) acts on
231 const void * gateData[] = {d_gateY}; // GPU pointers to gate data
232 HANDLE_CUTN_ERROR(cutensornetNetworkOperatorAppendProduct(cutnHandle, hamiltonian, cuDoubleComplex{0.25,0.0},
233 1, numModes, stateModes, NULL, gateData, &id));
234 }
235 // Append component (0.13 * Y0 X2 Z3) to the tensor network operator
236 {
237 const int32_t numModes[] = {1, 1, 1}; // Y0 acts on 1 mode, X2 acts on 1 mode, Z3 acts on 1 mode
238 const int32_t modesY0[] = {0}; // state modes Y0 acts on
239 const int32_t modesX2[] = {2}; // state modes X2 acts on
240 const int32_t modesZ3[] = {3}; // state modes Z3 acts on
241 const int32_t * stateModes[] = {modesY0, modesX2, modesZ3}; // state modes (Y0 * X2 * Z3) acts on
242 const void * gateData[] = {d_gateY, d_gateX, d_gateZ}; // GPU pointers to gate data
243 HANDLE_CUTN_ERROR(cutensornetNetworkOperatorAppendProduct(cutnHandle, hamiltonian, cuDoubleComplex{0.13,0.0},
244 3, numModes, stateModes, NULL, gateData, &id));
245 }
246 std::cout << "Constructed a tensor network operator: (0.5 * Z1 * Z2) + (0.25 * Y3) + (0.13 * Y0 * X2 * Z3)" << std::endl;
Create the expectation value object#
Once the quantum circuit and the tensor network operator have been constructed, and the final quantum circuit state has been factorized using the MPS representation, let’s create the expectation value object that will compute the expectation value of the specified tensor network operator over the final MPS-factorized state of the specified quantum circuit.
250 // Specify the quantum circuit expectation value
251 cutensornetStateExpectation_t expectation;
252 HANDLE_CUTN_ERROR(cutensornetCreateExpectation(cutnHandle, quantumState, hamiltonian, &expectation));
253 std::cout << "Created the specified quantum circuit expectation value\n";
Configure the expectation value calculation#
Now we can configure the expectation value object by setting the number of hyper-samples to be used by the tensor network contraction path finder.
257 // Configure the computation of the specified quantum circuit expectation value
258 const int32_t numHyperSamples = 8; // desired number of hyper samples used in the tensor network contraction path finder
259 HANDLE_CUTN_ERROR(cutensornetExpectationConfigure(cutnHandle, expectation,
260 CUTENSORNET_EXPECTATION_CONFIG_NUM_HYPER_SAMPLES, &numHyperSamples, sizeof(numHyperSamples)));
Prepare the expectation value calculation#
Let’s prepare the computation of the desired expectation value.
264 // Prepare the specified quantum circuit expectation value for computation
265 HANDLE_CUTN_ERROR(cutensornetExpectationPrepare(cutnHandle, expectation, scratchSize, workDesc, 0x0));
266 std::cout << "Prepared the specified quantum circuit expectation value\n";
267 flops = 0.0;
268 HANDLE_CUTN_ERROR(cutensornetExpectationGetInfo(cutnHandle, expectation,
269 CUTENSORNET_EXPECTATION_INFO_FLOPS, &flops, sizeof(flops)));
270 std::cout << "Total flop count = " << (flops/1e9) << " GFlop\n";
271 if(flops <= 0.0) {
272 std::cout << "ERROR: Invalid Flop count!\n";
273 std::abort();
274 }
Set up the workspace#
Now we can set up the required workspace buffer.
278 // Attach the workspace buffer
279 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle,
280 workDesc,
281 CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
282 CUTENSORNET_MEMSPACE_DEVICE,
283 CUTENSORNET_WORKSPACE_SCRATCH,
284 &worksize));
285 std::cout << "Required scratch GPU workspace size (bytes) = " << worksize << std::endl;
286 if(worksize <= scratchSize) {
287 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_DEVICE,
288 CUTENSORNET_WORKSPACE_SCRATCH, d_scratch, worksize));
289 }else{
290 std::cout << "ERROR: Insufficient workspace size on Device!\n";
291 std::abort();
292 }
293
294 // Query the host scratch workspace size and attach a host scratch buffer if one is required
295 HANDLE_CUTN_ERROR(cutensornetWorkspaceGetMemorySize(cutnHandle, workDesc, CUTENSORNET_WORKSIZE_PREF_RECOMMENDED,
296 CUTENSORNET_MEMSPACE_HOST, CUTENSORNET_WORKSPACE_SCRATCH, &hostWorksize));
297 std::cout << "Scratch Host workspace size (bytes) = " << hostWorksize << std::endl;
298 if(hostWorksize > 0) {
299 h_scratch = std::realloc(h_scratch, hostWorksize);
300 if(h_scratch == nullptr) { std::cout << "ERROR: Host scratch allocation failed!\n"; std::abort(); }
301 HANDLE_CUTN_ERROR(cutensornetWorkspaceSetMemory(cutnHandle, workDesc, CUTENSORNET_MEMSPACE_HOST,
302 CUTENSORNET_WORKSPACE_SCRATCH, h_scratch, hostWorksize));
303 }
304 std::cout << "Set the workspace buffer\n";
Compute the requested expectation value#
Once everything has been set up, we compute the requested expectation value and print it. Note that the returned expectation value is not normalized. The 2-norm of the tensor network state is returned as a separate argument.
308 // Compute the specified quantum circuit expectation value
309 std::complex<double> expectVal{0.0,0.0}, stateNorm2{0.0,0.0};
310 HANDLE_CUTN_ERROR(cutensornetExpectationCompute(cutnHandle, expectation, workDesc,
311 static_cast<void*>(&expectVal), static_cast<void*>(&stateNorm2), 0x0));
312 std::cout << "Computed the specified quantum circuit expectation value\n";
313 expectVal /= stateNorm2;
314 std::cout << "Expectation value = (" << expectVal.real() << ", " << expectVal.imag() << ")\n";
315 std::cout << "Squared 2-norm of the state = (" << stateNorm2.real() << ", " << stateNorm2.imag() << ")\n";
Free resources#
319 // Destroy the workspace descriptor
320 HANDLE_CUTN_ERROR(cutensornetDestroyWorkspaceDescriptor(workDesc));
321 std::cout << "Destroyed the workspace descriptor\n";
322
323 // Destroy the quantum circuit expectation value
324 HANDLE_CUTN_ERROR(cutensornetDestroyExpectation(expectation));
325 std::cout << "Destroyed the quantum circuit state expectation value\n";
326
327 // Destroy the tensor network operator
328 HANDLE_CUTN_ERROR(cutensornetDestroyNetworkOperator(hamiltonian));
329 std::cout << "Destroyed the tensor network operator\n";
330
331 // Destroy the quantum circuit state
332 HANDLE_CUTN_ERROR(cutensornetDestroyState(quantumState));
333 std::cout << "Destroyed the quantum circuit state\n";
334
335 for (int32_t i = 0; i < numQubits; i++) {
336 HANDLE_CUDA_ERROR(cudaFree(d_mpsTensors[i]));
337 }
338 std::free(h_scratch);
339 HANDLE_CUDA_ERROR(cudaFree(d_scratch));
340 HANDLE_CUDA_ERROR(cudaFree(d_gateCX));
341 HANDLE_CUDA_ERROR(cudaFree(d_gateZ));
342 HANDLE_CUDA_ERROR(cudaFree(d_gateY));
343 HANDLE_CUDA_ERROR(cudaFree(d_gateX));
344 HANDLE_CUDA_ERROR(cudaFree(d_gateH));
345 std::cout << "Freed memory on GPU\n";
346
347 // Finalize the cuTensorNet library
348 HANDLE_CUTN_ERROR(cutensornetDestroy(cutnHandle));
349 std::cout << "Finalized the cuTensorNet library\n";
350
351 return 0;
352}