|
| 1 | +/* Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "tensorflow_quantum/core/src/util_balance_trajectory.h" |
| 17 | + |
| 18 | +#include "gtest/gtest.h" |
| 19 | + |
| 20 | +namespace tfq { |
| 21 | +namespace { |
| 22 | + |
| 23 | +static void AssertWellBalanced(const std::vector<std::vector<int>>& n_reps, |
| 24 | + const int& num_threads, |
| 25 | + const std::vector<std::vector<int>>& offsets) { |
| 26 | + auto max_work = std::vector<int>(n_reps.size(), -1); |
| 27 | + for (int i = 0; i < n_reps.size(); i++) { |
| 28 | + for (int j = 0; j < n_reps[0].size(); j++) { |
| 29 | + max_work[i] = std::max(max_work[i], n_reps[i][j]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + for (int i = 0; i < n_reps.size(); i++) { |
| 34 | + int sum = 0; |
| 35 | + int prev_local_work = 0; |
| 36 | + for (int k = 0; k < num_threads; k++) { |
| 37 | + int local_work = (max_work[i] + num_threads - 1) / num_threads; |
| 38 | + local_work += offsets[k][i]; |
| 39 | + sum += local_work; |
| 40 | + if (k > 0) { |
| 41 | + EXPECT_LT(abs(local_work - prev_local_work), 2); |
| 42 | + } |
| 43 | + prev_local_work = local_work; |
| 44 | + } |
| 45 | + EXPECT_EQ(sum, max_work[i]); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +TEST(UtilQsimTest, BalanceTrajectorySimple) { |
| 50 | + std::vector<std::vector<int>> n_reps = {{1, 3, 5, 10, 15}, |
| 51 | + {1, 10, 20, 30, 40}, |
| 52 | + {50, 70, 100, 100, 100}, |
| 53 | + {100, 200, 200, 200, 200}}; |
| 54 | + const int num_threads = 3; |
| 55 | + // [num_threads, n_reps.size()] |
| 56 | + std::vector<std::vector<int>> offsets = { |
| 57 | + {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; |
| 58 | + |
| 59 | + BalanceTrajectory(n_reps, num_threads, &offsets); |
| 60 | + AssertWellBalanced(n_reps, num_threads, offsets); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(UtilQsimTest, BalanceTrajectoryPreventIdle) { |
| 64 | + std::vector<std::vector<int>> n_reps = {{1, 1, 1, 1, 11}, |
| 65 | + {1, 1, 1, 11, 1}, |
| 66 | + {1, 1, 11, 1, 1}, |
| 67 | + {1, 11, 1, 1, 1}, |
| 68 | + {11, 1, 1, 1, 1}}; |
| 69 | + const int num_threads = 10; |
| 70 | + // [num_threads, n_reps.size()] |
| 71 | + std::vector<std::vector<int>> offsets = { |
| 72 | + {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, |
| 73 | + {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, |
| 74 | + {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}; |
| 75 | + |
| 76 | + BalanceTrajectory(n_reps, num_threads, &offsets); |
| 77 | + AssertWellBalanced(n_reps, num_threads, offsets); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(UtilQsimTest, BalanceTrajectoryLowRep) { |
| 81 | + std::vector<std::vector<int>> n_reps = { |
| 82 | + {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, |
| 83 | + {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}}; |
| 84 | + const int num_threads = 5; |
| 85 | + // [num_threads, n_reps.size()] |
| 86 | + std::vector<std::vector<int>> offsets = {{0, 0, 0, 0, 0, 0, 0}, |
| 87 | + {0, 0, 0, 0, 0, 0, 0}, |
| 88 | + {0, 0, 0, 0, 0, 0, 0}, |
| 89 | + {0, 0, 0, 0, 0, 0, 0}, |
| 90 | + {0, 0, 0, 0, 0, 0, 0}}; |
| 91 | + |
| 92 | + BalanceTrajectory(n_reps, num_threads, &offsets); |
| 93 | + AssertWellBalanced(n_reps, num_threads, offsets); |
| 94 | +} |
| 95 | + |
| 96 | +TEST(UtilQsimTest, BalanceTrajectoryFewHigh) { |
| 97 | + std::vector<std::vector<int>> n_reps = { |
| 98 | + {1, 100, 1, 1, 1}, {1, 1, 1, 1, 1000}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, |
| 99 | + {1, 1, 1, 1, 1}, {1, 10, 1, 1, 1}, {1, 1, 1, 1, 1000}}; |
| 100 | + const int num_threads = 5; |
| 101 | + // [num_threads, n_reps.size()] |
| 102 | + std::vector<std::vector<int>> offsets = {{0, 0, 0, 0, 0, 0, 0}, |
| 103 | + {0, 0, 0, 0, 0, 0, 0}, |
| 104 | + {0, 0, 0, 0, 0, 0, 0}, |
| 105 | + {0, 0, 0, 0, 0, 0, 0}, |
| 106 | + {0, 0, 0, 0, 0, 0, 0}}; |
| 107 | + |
| 108 | + BalanceTrajectory(n_reps, num_threads, &offsets); |
| 109 | + AssertWellBalanced(n_reps, num_threads, offsets); |
| 110 | +} |
| 111 | + |
| 112 | +TEST(UtilQsimTest, BalanceTrajectory1D) { |
| 113 | + const int n_reps = 100; |
| 114 | + const int num_threads = 5; |
| 115 | + // [num_threads, batch_size] |
| 116 | + std::vector<std::vector<int>> offsets = {{0, 0, 0, 0, 0, 0, 0}, |
| 117 | + {0, 0, 0, 0, 0, 0, 0}, |
| 118 | + {0, 0, 0, 0, 0, 0, 0}, |
| 119 | + {0, 0, 0, 0, 0, 0, 0}, |
| 120 | + {0, 0, 0, 0, 0, 0, 0}}; |
| 121 | + |
| 122 | + std::vector<std::vector<int>> tmp(offsets[0].size(), |
| 123 | + std::vector<int>(2, n_reps)); |
| 124 | + BalanceTrajectory(n_reps, num_threads, &offsets); |
| 125 | + AssertWellBalanced(tmp, num_threads, offsets); |
| 126 | +} |
| 127 | + |
| 128 | +TEST(UtilQsimTest, BalanceTrajectory1D_2) { |
| 129 | + const int n_reps = 11; |
| 130 | + const int num_threads = 10; |
| 131 | + // [num_threads, batch_size] |
| 132 | + std::vector<std::vector<int>> offsets = { |
| 133 | + {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, |
| 134 | + {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, |
| 135 | + {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}; |
| 136 | + |
| 137 | + std::vector<std::vector<int>> tmp(offsets[0].size(), |
| 138 | + std::vector<int>(2, n_reps)); |
| 139 | + BalanceTrajectory(n_reps, num_threads, &offsets); |
| 140 | + AssertWellBalanced(tmp, num_threads, offsets); |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace |
| 144 | +} // namespace tfq |
0 commit comments