-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqft.cpp
More file actions
83 lines (62 loc) · 1.7 KB
/
Copy pathqft.cpp
File metadata and controls
83 lines (62 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "qft.h"
#include <stdio.h>
#include <cuda_runtime.h>
#include <cufft.h>
#include <device_launch_parameters.h>
#include <ostream>
#include <iostream>
#include <cuComplex.h>
#include <complex>
#include <chrono>
#include "types.h"
#include "quantumRegister.h"
#define RANK 2
void CuFFT_QFT_Calc(amp *i, amp *o)
{
}
state_map CalculateQFT(std::vector<string> statenames, std::vector<amp> statevals)
{
cufftHandle plan;
cufftComplex* inputData;
cufftComplex* outputData;
std::vector<std::complex<double>> fcompl;
for (auto v : statevals)
{
fcompl.push_back(v);
}
if (cufftPlan1d(&plan, statevals.size(), CUFFT_C2C, 1) != CUFFT_SUCCESS) {
fprintf(stderr, "CUFFT error: Plan creation failed");
}
cudaMallocManaged((void**)&inputData, sizeof(cufftComplex) * statevals.size() * 1);
if (cudaGetLastError() != cudaSuccess) {
fprintf(stderr, "Cuda error: Failed to allocate memory for input data\n");
}
int i = 0;
for (auto v : fcompl)
{
inputData[i].x = v.real();
inputData[i].y = v.imag();
i++;
}
cudaMallocManaged((void**)&outputData, sizeof(cufftComplex) * statevals.size() * 1);
if (cudaGetLastError() != cudaSuccess) {
fprintf(stderr, "Cuda error: Failed to allocate memory for output data\n");
}
if (cufftExecC2C(plan, inputData, outputData, CUFFT_FORWARD) != CUFFT_SUCCESS)
{
fprintf(stderr, "CUFFT error: ExecC2C failed");
}
if (cudaDeviceSynchronize() != cudaSuccess) {
fprintf(stderr, "Cuda error: Failed to synchronize\n");
}
cufftDestroy(plan);
cudaFree(inputData);
std::map<std::string, amp> a;
for (int i = 0; i < statevals.size(); i++)
{
amp temp(outputData[i].x, outputData[i].y);
a.insert({ statenames[i], temp });
}
cudaFree(outputData);
return a;
}