Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tensorflow_quantum/core/ops/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,19 @@ cc_library(
],
)

cc_test(
name = "tfq_simulate_utils_test",
size = "small",
srcs = ["tfq_simulate_utils_test.cc"],
linkstatic = 0,
deps = [
":tfq_simulate_utils",
"@com_google_googletest//:gtest_main",
"@local_config_tf//:libtensorflow_framework",
"@local_config_tf//:tf_header_lib",
],
)

py_library(
name = "tfq_adj_grad_op_py",
srcs = ["tfq_adj_grad_op.py"],
Expand Down
16 changes: 12 additions & 4 deletions tensorflow_quantum/core/ops/tfq_simulate_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ limitations under the License.

#include "tensorflow_quantum/core/ops/tfq_simulate_utils.h"

#include <algorithm>

#include "tensorflow/core/framework/device_base.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/lib/core/threadpool.h"

namespace tfq {

int GetBlockSize(tensorflow::OpKernelContext* context, const int output_size) {
int size = output_size /
context->device()->tensorflow_cpu_worker_threads()->num_threads;
return std::max(size, 1);
if (output_size <= 0 || context == nullptr || context->device() == nullptr) {
return 1;
}
const tensorflow::DeviceBase::CpuWorkerThreads* worker_threads =
context->device()->tensorflow_cpu_worker_threads();
if (worker_threads == nullptr || worker_threads->num_threads <= 0) {
return 1;
}
return std::max(1, output_size / worker_threads->num_threads);
}

} // namespace tfq
5 changes: 3 additions & 2 deletions tensorflow_quantum/core/ops/tfq_simulate_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ limitations under the License.

namespace tfq {

// Returns a block size that distributes the work evenly across the different
// circuits.
// Returns a positive block size (>= 1) that distributes `output_size` work
// items across CPU worker threads, or 1 if `output_size <= 0` or `context` /
// CPU worker threads are unavailable or non-positive.
int GetBlockSize(tensorflow::OpKernelContext* context, const int output_size);

} // namespace tfq
Expand Down
142 changes: 142 additions & 0 deletions tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* Copyright 2026 The TensorFlow Quantum Authors. 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

http://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.
==============================================================================*/

#include "tensorflow_quantum/core/ops/tfq_simulate_utils.h"

#include <limits>

#include "gtest/gtest.h"
#include "tensorflow/core/framework/device_base.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/platform/env.h"

namespace tfq {
namespace {

class FakeCpuDevice : public tensorflow::DeviceBase {
public:
FakeCpuDevice() : tensorflow::DeviceBase(tensorflow::Env::Default()) {}
};

class NullWorkerThreadsDevice : public tensorflow::DeviceBase {
public:
NullWorkerThreadsDevice()
: tensorflow::DeviceBase(tensorflow::Env::Default()) {}

const CpuWorkerThreads* tensorflow_cpu_worker_threads() const override {
return nullptr;
}
};

TEST(TfqSimulateUtilsTest, NullContextReturnsOne) {
EXPECT_EQ(GetBlockSize(nullptr, 100), 1);
EXPECT_EQ(GetBlockSize(nullptr, 0), 1);
EXPECT_EQ(GetBlockSize(nullptr, -5), 1);
EXPECT_EQ(GetBlockSize(nullptr, std::numeric_limits<int>::min()), 1);
}

TEST(TfqSimulateUtilsTest, NullDeviceReturnsOne) {
// OpKernelContext requires a non-null device during construction, and
// context.device() reads params.device dynamically afterward.
FakeCpuDevice device;
tensorflow::OpKernelContext::Params params;
params.device = &device;
tensorflow::OpKernelContext context(&params, 0);
params.device = nullptr;

EXPECT_EQ(GetBlockSize(&context, 100), 1);
EXPECT_EQ(GetBlockSize(&context, 0), 1);
EXPECT_EQ(GetBlockSize(&context, -5), 1);

// Restore a valid device before OpKernelContext destruction.
params.device = &device;
}

TEST(TfqSimulateUtilsTest, NonPositiveOutputSizeShortCircuitsBeforeThreads) {
// DeviceBase::tensorflow_cpu_worker_threads() CHECK-aborts if unconfigured,
// proving non-positive output_size returns before querying worker threads.
FakeCpuDevice unconfigured_device;
tensorflow::OpKernelContext::Params params;
params.device = &unconfigured_device;
tensorflow::OpKernelContext context(&params, 0);

EXPECT_EQ(GetBlockSize(&context, 0), 1);
EXPECT_EQ(GetBlockSize(&context, -5), 1);
EXPECT_EQ(GetBlockSize(&context, std::numeric_limits<int>::min()), 1);
}

TEST(TfqSimulateUtilsTest, NullWorkerThreadsReturnsOne) {
NullWorkerThreadsDevice device;
tensorflow::OpKernelContext::Params params;
params.device = &device;
tensorflow::OpKernelContext context(&params, 0);

EXPECT_EQ(GetBlockSize(&context, 100), 1);
}

TEST(TfqSimulateUtilsTest, ZeroOrNegativeThreadsReturnsOne) {
tensorflow::DeviceBase::CpuWorkerThreads worker_threads;
FakeCpuDevice device;
device.set_tensorflow_cpu_worker_threads(&worker_threads);

tensorflow::OpKernelContext::Params params;
params.device = &device;
tensorflow::OpKernelContext context(&params, 0);

worker_threads.num_threads = 0;
EXPECT_EQ(GetBlockSize(&context, 100), 1);

worker_threads.num_threads = -2;
EXPECT_EQ(GetBlockSize(&context, 100), 1);

worker_threads.num_threads = std::numeric_limits<int>::min();
EXPECT_EQ(GetBlockSize(&context, 100), 1);
}

TEST(TfqSimulateUtilsTest, ValidInputsPreserveFloorDivisionClampedToOne) {
tensorflow::DeviceBase::CpuWorkerThreads worker_threads;
FakeCpuDevice device;
worker_threads.num_threads = 4;
device.set_tensorflow_cpu_worker_threads(&worker_threads);

tensorflow::OpKernelContext::Params params;
params.device = &device;
tensorflow::OpKernelContext context(&params, 0);

EXPECT_EQ(GetBlockSize(&context, 100), 25);
EXPECT_EQ(GetBlockSize(&context, 8), 2);
EXPECT_EQ(GetBlockSize(&context, 7), 1);
EXPECT_EQ(GetBlockSize(&context, 4), 1);
EXPECT_EQ(GetBlockSize(&context, 3), 1);
EXPECT_EQ(GetBlockSize(&context, 0), 1);
EXPECT_EQ(GetBlockSize(&context, -5), 1);
EXPECT_EQ(GetBlockSize(&context, 2147483647), 536870911);
}

TEST(TfqSimulateUtilsTest, SingleWorkerThreadReturnsFullOutputSize) {
tensorflow::DeviceBase::CpuWorkerThreads worker_threads;
FakeCpuDevice device;
worker_threads.num_threads = 1;
device.set_tensorflow_cpu_worker_threads(&worker_threads);

tensorflow::OpKernelContext::Params params;
params.device = &device;
tensorflow::OpKernelContext context(&params, 0);

EXPECT_EQ(GetBlockSize(&context, 100), 100);
}

} // namespace
} // namespace tfq
Loading