Skip to content

Commit 6858cd0

Browse files
reserved space of vectors where possible
1 parent 71d8da0 commit 6858cd0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/algorithms/StatePreparation.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ template <typename T>
8383
const std::vector<double>& vector)
8484
-> std::vector<double> {
8585
std::vector<double> result;
86+
result.reserve(matrix.size());
8687
for (const auto& matrixVec : matrix) {
8788
double sum{0};
8889
for (size_t i = 0; i < matrixVec.size(); ++i) {
@@ -175,13 +176,20 @@ template <typename T>
175176
// works out Ry and Rz rotation angles used to disentangle LSB qubit
176177
// rotations make up block diagonal matrix U
177178
[[nodiscard]] auto
178-
rotationsToDisentangle(const std::vector<std::complex<double>>& amplitudes, double EPS)
179+
rotationsToDisentangle(const std::vector<std::complex<double>>& amplitudes,
180+
double EPS)
179181
-> std::tuple<std::vector<std::complex<double>>, std::vector<double>,
180182
std::vector<double>> {
183+
size_t amplitudesHalf = amplitudes.size() / 2;
181184
std::vector<std::complex<double>> remainingVector;
182185
std::vector<double> thetas;
183186
std::vector<double> phis;
184-
for (size_t i = 0; i < (amplitudes.size() / 2); ++i) {
187+
188+
remainingVector.reserve(amplitudesHalf);
189+
thetas.reserve(amplitudesHalf);
190+
phis.reserve(amplitudesHalf);
191+
192+
for (size_t i = 0; i < amplitudesHalf; ++i) {
185193
auto [remains, theta, phi] =
186194
blochAngles(amplitudes[2 * i], amplitudes[2 * i + 1], EPS);
187195
remainingVector.emplace_back(remains);

0 commit comments

Comments
 (0)