Skip to content

Commit 99bc56e

Browse files
authored
Merge branch 'master' into mhucka-update-nbformat
2 parents 1ce1910 + 5fecece commit 99bc56e

File tree

13 files changed

+339
-215
lines changed

13 files changed

+339
-215
lines changed

WORKSPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ EIGEN_COMMIT = "aa6964bf3a34fd607837dd8123bc42465185c4f8"
1212

1313
http_archive(
1414
name = "eigen",
15+
sha256 = "35ba771e30c735a4215ed784d7e032086cf89fe6622dce4d793c45dd74373362",
1516
build_file_content = """
1617
cc_library(
1718
name = "eigen3",
@@ -41,7 +42,7 @@ http_archive(
4142
patches = [
4243
"//third_party/tf:tf.patch",
4344
],
44-
# sha256 = "e52cda3bae45f0ae0fccd4055e9fa29892b414f70e2df94df9a3a10319c75fff",
45+
sha256 = "f771db8d96ca13c72f73c85c9cfb6f5358e2de3dd62a97a9ae4b672fe4c6d094",
4546
strip_prefix = "tensorflow-2.15.0",
4647
urls = [
4748
"https://github.com/tensorflow/tensorflow/archive/refs/tags/v2.15.0.zip",

configure.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ write_to_bazelrc "build -c opt"
102102
write_to_bazelrc "build --cxxopt=\"-D_GLIBCXX_USE_CXX11_ABI=1\""
103103
write_to_bazelrc "build --cxxopt=\"-std=c++17\""
104104

105+
# The transitive inclusion of build rules from TensorFlow ends up including
106+
# and building two copies of zlib (one from bazel_rules, one from the TF code
107+
# baase itself). The version of zlib you get (at least in TF 2.15.0) ends up
108+
# producing many compiler warnings that "a function declaration without a
109+
# prototype is deprecated". It's difficult to patch the particular build rules
110+
# involved, so the approach taken here is to silence those warnings for stuff
111+
# in external/. TODO: figure out how to patch the BUILD files and put it there.
112+
write_to_bazelrc "build --per_file_copt=external/.*@-Wno-deprecated-non-prototype"
113+
write_to_bazelrc "build --host_per_file_copt=external/.*@-Wno-deprecated-non-prototype"
114+
115+
# Similarly, these are other harmless warnings about unused functions coming
116+
# from things pulled in by the TF bazel config rules.
117+
write_to_bazelrc "build --per_file_copt=external/com_google_protobuf/.*@-Wno-unused-function"
118+
write_to_bazelrc "build --host_per_file_copt=external/com_google_protobuf/.*@-Wno-unused-function"
119+
120+
# The following supress warnings coming from qsim.
121+
# TODO: fix the code in qsim & update TFQ to use the updated version.
122+
write_to_bazelrc "build --per_file_copt=tensorflow_quantum/core/ops/noise/tfq_.*@-Wno-unused-but-set-variable"
123+
write_to_bazelrc "build --host_per_file_copt=tensorflow_quantum/core/ops/noise/tfq_.*@-Wno-unused-but-set-variable"
124+
write_to_bazelrc "build --per_file_copt=tensorflow_quantum/core/ops/math_ops/tfq_.*@-Wno-deprecated-declarations"
125+
write_to_bazelrc "build --host_per_file_copt=tensorflow_quantum/core/ops/math_ops/tfq_.*@-Wno-deprecated-declarations"
126+
105127

106128
if is_windows; then
107129
# Use pywrap_tensorflow instead of tensorflow_framework on Windows

tensorflow_quantum/core/ops/noise/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ cc_binary(
6060
"//tensorflow_quantum/core/ops:parse_context",
6161
"//tensorflow_quantum/core/ops:tfq_simulate_utils",
6262
"//tensorflow_quantum/core/src:circuit_parser_qsim",
63+
"//tensorflow_quantum/core/src:util_balance_trajectory",
6364
"//tensorflow_quantum/core/src:util_qsim",
6465
"@qsim//lib:qsim_lib",
6566
# tensorflow core framework

tensorflow_quantum/core/ops/noise/tfq_noisy_expectation.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ limitations under the License.
4141
#include "tensorflow_quantum/core/ops/parse_context.h"
4242
#include "tensorflow_quantum/core/proto/pauli_sum.pb.h"
4343
#include "tensorflow_quantum/core/proto/program.pb.h"
44+
#include "tensorflow_quantum/core/src/util_balance_trajectory.h"
4445
#include "tensorflow_quantum/core/src/util_qsim.h"
4546

4647
namespace tfq {

tensorflow_quantum/core/ops/noise/tfq_noisy_sampled_expectation.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ limitations under the License.
4141
#include "tensorflow_quantum/core/ops/parse_context.h"
4242
#include "tensorflow_quantum/core/proto/pauli_sum.pb.h"
4343
#include "tensorflow_quantum/core/proto/program.pb.h"
44+
#include "tensorflow_quantum/core/src/util_balance_trajectory.h"
4445
#include "tensorflow_quantum/core/src/util_qsim.h"
4546

4647
namespace tfq {

tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ limitations under the License.
4040
#include "tensorflow_quantum/core/ops/parse_context.h"
4141
#include "tensorflow_quantum/core/proto/program.pb.h"
4242
#include "tensorflow_quantum/core/src/circuit_parser_qsim.h"
43+
#include "tensorflow_quantum/core/src/util_balance_trajectory.h"
4344
#include "tensorflow_quantum/core/src/util_qsim.h"
4445

4546
namespace tfq {

tensorflow_quantum/core/src/BUILD

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ cc_test(
9393

9494
cc_library(
9595
name = "util_qsim",
96-
srcs = [],
9796
hdrs = ["util_qsim.h"],
9897
deps = [
9998
":circuit_parser_qsim",
10099
"//tensorflow_quantum/core/proto:pauli_sum_cc_proto",
101100
"//tensorflow_quantum/core/proto:projector_sum_cc_proto",
101+
"@com_google_absl//absl/functional:any_invocable",
102102
"@com_google_absl//absl/container:inlined_vector", # unclear why needed.
103103
"@com_google_absl//absl/status",
104104
"@com_google_absl//absl/status:statusor",
@@ -126,6 +126,25 @@ cc_test(
126126
],
127127
)
128128

129+
cc_library(
130+
name = "util_balance_trajectory",
131+
srcs = ["util_balance_trajectory.cc"],
132+
hdrs = ["util_balance_trajectory.h"],
133+
deps = [
134+
],
135+
)
136+
137+
cc_test(
138+
name = "util_balance_trajectory_test",
139+
size = "small",
140+
srcs = ["util_balance_trajectory_test.cc"],
141+
linkstatic = 0,
142+
deps = [
143+
":util_balance_trajectory",
144+
"@com_google_googletest//:gtest_main",
145+
],
146+
)
147+
129148
cc_library(
130149
name = "program_resolution",
131150
srcs = ["program_resolution.cc"],
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#ifndef UTIL_BALANCE_TRAJECTORY_H_
17+
#define UTIL_BALANCE_TRAJECTORY_H_
18+
19+
#include <algorithm>
20+
#include <cstdint>
21+
#include <vector>
22+
23+
namespace tfq {
24+
25+
void BalanceTrajectory(const std::vector<std::vector<int>>& num_samples,
26+
const int& num_threads,
27+
std::vector<std::vector<int>>* thread_offsets);
28+
29+
void BalanceTrajectory(const int& num_samples, const int& num_threads,
30+
std::vector<std::vector<int>>* thread_offsets);
31+
32+
} // namespace tfq
33+
34+
#endif // UTIL_BALANCE_TRAJECTORY_H_

0 commit comments

Comments
 (0)