-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathvectorspace.h
More file actions
185 lines (144 loc) · 4.45 KB
/
vectorspace.h
File metadata and controls
185 lines (144 loc) · 4.45 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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.
#ifndef VECTORSPACE_H_
#define VECTORSPACE_H_
#ifdef _WIN32
#include <malloc.h>
#endif
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <utility>
namespace qsim {
namespace detail {
inline void do_not_free(void*) {}
inline void free(void* ptr) {
#ifdef _WIN32
_aligned_free(ptr);
#else
::free(ptr);
#endif
}
} // namespace detail
// Routines for vector manipulations.
template <typename Impl, typename For, typename FP>
class VectorSpace {
public:
using fp_type = FP;
private:
using Pointer = std::unique_ptr<fp_type, decltype(&detail::free)>;
public:
class Vector {
public:
Vector() = delete;
Vector(Pointer&& ptr, unsigned num_qubits)
: ptr_(std::move(ptr)), num_qubits_(num_qubits) {}
fp_type* get() {
return ptr_.get();
}
const fp_type* get() const {
return ptr_.get();
}
fp_type* release() {
num_qubits_ = 0;
return ptr_.release();
}
unsigned num_qubits() const {
return num_qubits_;
}
static constexpr bool requires_copy_to_host() {
return false;
}
private:
Pointer ptr_;
unsigned num_qubits_;
};
template <typename... ForArgs>
VectorSpace(ForArgs&&... args) : for_(args...) {}
static Vector Create(unsigned num_qubits) {
auto size = sizeof(fp_type) * Impl::MinSize(num_qubits);
#ifdef _WIN32
Pointer ptr{(fp_type*) _aligned_malloc(size, 64), &detail::free};
return Vector{std::move(ptr), ptr.get() != nullptr ? num_qubits : 0};
#else
void* p = nullptr;
if (posix_memalign(&p, 64, size) == 0) {
return Vector{Pointer{(fp_type*) p, &detail::free}, num_qubits};
} else {
return Null();
}
#endif
}
// It is the client's responsibility to make sure that p has at least
// Impl::MinSize(num_qubits) elements.
static Vector Create(fp_type* p, unsigned num_qubits) {
return Vector{Pointer{p, &detail::do_not_free}, num_qubits};
}
static Vector Null() {
return Vector{Pointer{nullptr, &detail::free}, 0};
}
static bool IsNull(const Vector& vec) {
return vec.get() == nullptr;
}
static void Free(fp_type* ptr) {
detail::free(ptr);
}
bool Copy(const Vector& src, Vector& dest) const {
if (src.num_qubits() != dest.num_qubits()) {
return false;
}
auto f = [](unsigned n, unsigned m, uint64_t i,
const fp_type* src, fp_type* dest) {
dest[i] = src[i];
};
for_.Run(Impl::MinSize(src.num_qubits()), f, src.get(), dest.get());
return true;
}
// It is the client's responsibility to make sure that dest has at least
// Impl::MinSize(src.num_qubits()) elements.
bool Copy(const Vector& src, fp_type* dest) const {
auto f = [](unsigned n, unsigned m, uint64_t i,
const fp_type* src, fp_type* dest) {
dest[i] = src[i];
};
for_.Run(Impl::MinSize(src.num_qubits()), f, src.get(), dest);
return true;
}
// It is the client's responsibility to make sure that src has at least
// Impl::MinSize(dest.num_qubits()) elements.
bool Copy(const fp_type* src, Vector& dest) const {
auto f = [](unsigned n, unsigned m, uint64_t i,
const fp_type* src, fp_type* dest) {
dest[i] = src[i];
};
for_.Run(Impl::MinSize(dest.num_qubits()), f, src, dest.get());
return true;
}
// It is the client's responsibility to make sure that src has at least
// min(size, Impl::MinSize(dest.num_qubits())) elements.
bool Copy(const fp_type* src, uint64_t size, Vector& dest) const {
auto f = [](unsigned n, unsigned m, uint64_t i,
const fp_type* src, fp_type* dest) {
dest[i] = src[i];
};
size = std::min(size, Impl::MinSize(dest.num_qubits()));
for_.Run(size, f, src, dest.get());
return true;
}
static void DeviceSync() {}
protected:
For for_;
};
} // namespace qsim
#endif // VECTORSPACE_H_