-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathqsim_base_custatevec.cu
More file actions
171 lines (138 loc) · 4.4 KB
/
qsim_base_custatevec.cu
File metadata and controls
171 lines (138 loc) · 4.4 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <unistd.h>
#include <algorithm>
#include <complex>
#include <limits>
#include <string>
#include <custatevec.h>
#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_mqubit.h"
#include "../lib/io_file.h"
#include "../lib/operation.h"
#include "../lib/run_qsim.h"
#include "../lib/simulator_custatevec.h"
#include "../lib/util_custatevec.h"
struct Options {
std::string circuit_file;
unsigned maxtime = std::numeric_limits<unsigned>::max();
unsigned seed = 1;
unsigned max_fused_size = 2;
unsigned verbosity = 0;
};
Options GetOptions(int argc, char* argv[]) {
constexpr char usage[] = "usage:\n ./qsim_base -c circuit -d maxtime "
"-s seed -f max_fused_size -v verbosity\n";
Options opt;
int k;
while ((k = getopt(argc, argv, "c:d:s:f:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
break;
case 'd':
opt.maxtime = std::atoi(optarg);
break;
case 's':
opt.seed = std::atoi(optarg);
break;
case 'f':
opt.max_fused_size = std::atoi(optarg);
break;
case 'v':
opt.verbosity = std::atoi(optarg);
break;
default:
qsim::IO::errorf(usage);
exit(1);
}
}
return opt;
}
bool ValidateOptions(const Options& opt) {
if (opt.circuit_file.empty()) {
qsim::IO::errorf("circuit file is not provided.\n");
return false;
}
return true;
}
template <typename StateSpace, typename State>
void PrintAmplitudes(
unsigned num_qubits, const StateSpace& state_space, const State& state) {
static constexpr char const* bits[8] = {
"000", "001", "010", "011", "100", "101", "110", "111",
};
uint64_t size = std::min(uint64_t{8}, uint64_t{1} << num_qubits);
unsigned s = 3 - std::min(unsigned{3}, num_qubits);
for (uint64_t i = 0; i < size; ++i) {
auto a = state_space.GetAmpl(state, i);
qsim::IO::messagef("%s:%16.8g%16.8g%16.8g\n",
bits[i] + s, std::real(a), std::imag(a), std::norm(a));
}
}
int main(int argc, char* argv[]) {
using namespace qsim;
auto opt = GetOptions(argc, argv);
if (!ValidateOptions(opt)) {
return 1;
}
using fp_type = float;
Circuit<Operation<fp_type>> circuit;
if (!CircuitQsimParser<IOFile>::FromFile(opt.maxtime, opt.circuit_file,
circuit)) {
return 1;
}
struct Factory {
using Simulator = qsim::SimulatorCuStateVec<fp_type>;
using StateSpace = Simulator::StateSpace;
Factory() {
ErrorCheck(cublasCreate(&cublas_handle));
ErrorCheck(custatevecCreate(&custatevec_handle));
}
~Factory() {
ErrorCheck(cublasDestroy(cublas_handle));
ErrorCheck(custatevecDestroy(custatevec_handle));
}
StateSpace CreateStateSpace() const {
return StateSpace(cublas_handle, custatevec_handle);
}
Simulator CreateSimulator() const {
return Simulator(cublas_handle, custatevec_handle);
}
cublasHandle_t cublas_handle;
custatevecHandle_t custatevec_handle;
};
using Simulator = Factory::Simulator;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Fuser = MultiQubitGateFuser<IO>;
using Runner = QSimRunner<IO, Fuser, Factory>;
Factory factory;
StateSpace state_space = factory.CreateStateSpace();
State state = state_space.Create(circuit.num_qubits);
if (state_space.IsNull(state)) {
IO::errorf("not enough memory: is the number of qubits too large?\n");
return 1;
}
state_space.SetStateZero(state);
Runner::Parameter param;
param.max_fused_size = opt.max_fused_size;
param.seed = opt.seed;
param.verbosity = opt.verbosity;
if (Runner::Run(param, factory, circuit, state)) {
PrintAmplitudes(circuit.num_qubits, state_space, state);
}
return 0;
}