|
| 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 | +namespace tfq { |
| 19 | + |
| 20 | +// Balance the number of trajectory computations done between |
| 21 | +// threads. num_samples is a 2d vector containing the number of reps |
| 22 | +// requested for each pauli_sum[i,j]. After running thread_offsets |
| 23 | +// contains 0/-1 values that will offset the work for each thread. |
| 24 | +// to make it as close to uniform as possible. **Assumes circuits |
| 25 | +// have roughly equal simulation cost** |
| 26 | +void BalanceTrajectory(const std::vector<std::vector<int>>& num_samples, |
| 27 | + const int& num_threads, |
| 28 | + std::vector<std::vector<int>>* thread_offsets) { |
| 29 | + std::vector<int> rep_limits(num_samples.size(), -1); |
| 30 | + std::vector<int> height(num_threads, 0); |
| 31 | + |
| 32 | + for (int i = 0; i < num_samples.size(); i++) { |
| 33 | + for (int j = 0; j < num_samples[i].size(); j++) { |
| 34 | + rep_limits[i] = std::max(rep_limits[i], num_samples[i][j]); |
| 35 | + } |
| 36 | + } |
| 37 | + int prev_max_height = -1; |
| 38 | + for (int j = 0; j < num_samples.size(); j++) { |
| 39 | + int run_ceiling = ((rep_limits[j] + num_threads - 1) / num_threads); |
| 40 | + int num_lo = num_threads * run_ceiling - rep_limits[j]; |
| 41 | + int num_hi = num_threads - num_lo; |
| 42 | + int cur_max = prev_max_height; |
| 43 | + for (int i = 0; i < num_threads; i++) { |
| 44 | + if (height[i] == cur_max && num_lo) { |
| 45 | + // previously had extra work on this thread and |
| 46 | + // have remaining low budget to give. |
| 47 | + height[i]++; |
| 48 | + (*thread_offsets)[i][j] = -1; |
| 49 | + num_lo--; |
| 50 | + } else if (height[i] == cur_max - 1 && num_hi) { |
| 51 | + // previously had less work on this thread and |
| 52 | + // remaining high budget to give. |
| 53 | + height[i] += 2; |
| 54 | + (*thread_offsets)[i][j] = 0; |
| 55 | + num_hi--; |
| 56 | + } else if (num_hi) { |
| 57 | + height[i] += 2; |
| 58 | + (*thread_offsets)[i][j] = 0; |
| 59 | + num_hi--; |
| 60 | + } else { |
| 61 | + height[i]++; |
| 62 | + (*thread_offsets)[i][j] = -1; |
| 63 | + num_lo--; |
| 64 | + } |
| 65 | + prev_max_height = std::max(height[i], prev_max_height); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Simpler case of TrajectoryBalance where num_samples is fixed |
| 71 | +// across all circuits. |
| 72 | +void BalanceTrajectory(const int& num_samples, const int& num_threads, |
| 73 | + std::vector<std::vector<int>>* thread_offsets) { |
| 74 | + std::vector<int> height(num_threads, 0); |
| 75 | + |
| 76 | + int prev_max_height = -1; |
| 77 | + for (int j = 0; j < (*thread_offsets)[0].size(); j++) { |
| 78 | + int run_ceiling = ((num_samples + num_threads - 1) / num_threads); |
| 79 | + int num_lo = num_threads * run_ceiling - num_samples; |
| 80 | + int num_hi = num_threads - num_lo; |
| 81 | + int cur_max = prev_max_height; |
| 82 | + for (int i = 0; i < num_threads; i++) { |
| 83 | + if (height[i] == cur_max && num_lo) { |
| 84 | + // previously had extra work on this thread and |
| 85 | + // have remaining low budget to give. |
| 86 | + height[i]++; |
| 87 | + (*thread_offsets)[i][j] = -1; |
| 88 | + num_lo--; |
| 89 | + } else if (height[i] == cur_max - 1 && num_hi) { |
| 90 | + // previously had less work on this thread and |
| 91 | + // remaining high budget to give. |
| 92 | + height[i] += 2; |
| 93 | + (*thread_offsets)[i][j] = 0; |
| 94 | + num_hi--; |
| 95 | + } else if (num_hi) { |
| 96 | + height[i] += 2; |
| 97 | + (*thread_offsets)[i][j] = 0; |
| 98 | + num_hi--; |
| 99 | + } else { |
| 100 | + height[i]++; |
| 101 | + (*thread_offsets)[i][j] = -1; |
| 102 | + num_lo--; |
| 103 | + } |
| 104 | + prev_max_height = std::max(height[i], prev_max_height); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace tfq |
0 commit comments