forked from quantumlib/qsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsim_base_cuda.cu
172 lines (140 loc) · 4.47 KB
/
qsim_base_cuda.cu
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
172
// 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 "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_mqubit.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/run_qsim.h"
#include "../lib/simulator_cuda.h"
struct Options {
std::string circuit_file;
unsigned maxtime = std::numeric_limits<unsigned>::max();
unsigned seed = 1;
unsigned max_fused_size = 2;
unsigned num_threads = 256;
unsigned num_dblocks = 16;
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 -t num_threads"
"-n num_dblocks -v verbosity\n";
Options opt;
int k;
while ((k = getopt(argc, argv, "c:d:s:f:t:n: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 't':
opt.num_threads = std::atoi(optarg);
break;
case 'n':
opt.num_dblocks = 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<GateQSim<fp_type>> circuit;
if (!CircuitQsimParser<IOFile>::FromFile(opt.maxtime, opt.circuit_file,
circuit)) {
return 1;
}
struct Factory {
using Simulator = qsim::SimulatorCUDA<fp_type>;
using StateSpace = Simulator::StateSpace;
Factory(const StateSpace::Parameter& param) : param(param) {}
StateSpace CreateStateSpace() const {
return StateSpace(param);
}
Simulator CreateSimulator() const {
return Simulator();
}
const StateSpace::Parameter& param;
};
using Simulator = Factory::Simulator;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Fuser = MultiQubitGateFuser<IO, GateQSim<fp_type>>;
using Runner = QSimRunner<IO, Fuser, Factory>;
StateSpace::Parameter param1;
param1.num_threads = opt.num_threads;
param1.num_dblocks = opt.num_dblocks;
Factory factory(param1);
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 param3;
param3.max_fused_size = opt.max_fused_size;
param3.seed = opt.seed;
param3.verbosity = opt.verbosity;
if (Runner::Run(param3, factory, circuit, state)) {
PrintAmplitudes(circuit.num_qubits, state_space, state);
}
return 0;
}