diff --git a/configure.sh b/configure.sh index 2ed76a617..67e0d9c17 100755 --- a/configure.sh +++ b/configure.sh @@ -217,8 +217,6 @@ write_bazelrc "build --host_per_file_copt=external/eigen/.*@-Wno-maybe-uninitial # The following warnings come from qsim. # TODO: fix the code in qsim & update TFQ to use the updated version. -write_bazelrc "build --per_file_copt=tensorflow_quantum/core/ops/noise/tfq_.*@-Wno-unused-but-set-variable" -write_bazelrc "build --host_per_file_copt=tensorflow_quantum/core/ops/noise/tfq_.*@-Wno-unused-but-set-variable" write_bazelrc "build --per_file_copt=tensorflow_quantum/core/ops/math_ops/tfq_.*@-Wno-deprecated-declarations" write_bazelrc "build --host_per_file_copt=tensorflow_quantum/core/ops/math_ops/tfq_.*@-Wno-deprecated-declarations" diff --git a/tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc b/tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc index df83b6222..9006138b5 100644 --- a/tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc +++ b/tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc @@ -94,8 +94,8 @@ class TfqInnerProductOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -117,10 +117,10 @@ class TfqInnerProductOp : public tensorflow::OpKernel { std::vector(output_dim_internal_size, QsimFusedCircuit({}))); - auto construct_f2 = [&](int start, int end) { - for (int i = start; i < end; i++) { - int ii = i / output_dim_internal_size; - int jj = i % output_dim_internal_size; + auto construct_f2 = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { + int64_t ii = i / output_dim_internal_size; + int64_t jj = i % output_dim_internal_size; Status status = QsimCircuitFromProgram( other_programs[ii][jj], {}, num_qubits[ii], &other_qsim_circuits[ii][jj], &other_fused_circuits[ii][jj]); @@ -138,8 +138,8 @@ class TfqInnerProductOp : public tensorflow::OpKernel { "No symbols are allowed in these circuits."))); } - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -170,7 +170,7 @@ class TfqInnerProductOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -180,7 +180,7 @@ class TfqInnerProductOp : public tensorflow::OpKernel { // we no longer parallelize over circuits. Each time we encounter a // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. largest_nq = nq; @@ -215,7 +215,7 @@ class TfqInnerProductOp : public tensorflow::OpKernel { } void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector& fused_circuits, const std::vector>& other_fused_circuits, tensorflow::OpKernelContext* context, @@ -226,21 +226,21 @@ class TfqInnerProductOp : public tensorflow::OpKernel { const int output_dim_internal_size = output_tensor->dimension(1); - auto DoWork = [&](int start, int end) { - int old_batch_index = -2; - int cur_batch_index = -1; - int largest_nq = 1; - int cur_internal_index; + auto DoWork = [&](int64_t start, int64_t end) { + int64_t old_batch_index = -2; + int64_t cur_batch_index = -1; + uint64_t largest_nq = 1; + int64_t cur_internal_index; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); auto scratch = ss.Create(largest_nq); - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { cur_batch_index = i / output_dim_internal_size; cur_internal_index = i % output_dim_internal_size; - const int nq = num_qubits[cur_batch_index]; + const uint64_t nq = num_qubits[cur_batch_index]; // (#679) Just ignore empty program if (fused_circuits[cur_batch_index].size() == 0) { diff --git a/tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc b/tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc index 1563ef37a..bfe93b123 100644 --- a/tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc +++ b/tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc @@ -119,8 +119,8 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram( programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i], &gate_meta[i]); @@ -145,10 +145,10 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { std::vector(output_dim_internal_size, QsimFusedCircuit({}))); - auto construct_f2 = [&](int start, int end) { - for (int i = start; i < end; i++) { - int ii = i / output_dim_internal_size; - int jj = i % output_dim_internal_size; + auto construct_f2 = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { + int64_t ii = i / output_dim_internal_size; + int64_t jj = i % output_dim_internal_size; Status status = QsimCircuitFromProgram( other_programs[ii][jj], {}, num_qubits[ii], &other_qsim_circuits[ii][jj], &other_fused_circuits[ii][jj]); @@ -166,8 +166,8 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { "No symbols are allowed in these circuits."))); } - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -224,7 +224,7 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -236,7 +236,7 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { // a larger circuit we will grow the Statevector as necessary. for (std::vector>>::size_type i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. largest_nq = nq; @@ -315,7 +315,7 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { } void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector& maps, const std::vector& qsim_circuits, const std::vector& fused_circuits, @@ -332,11 +332,11 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { const int output_dim_internal_size = other_fused_circuits[0].size(); - auto DoWork = [&](int start, int end) { - int old_batch_index = -2; - int cur_batch_index = -1; - int largest_nq = 1; - int cur_internal_index; + auto DoWork = [&](int64_t start, int64_t end) { + int64_t old_batch_index = -2; + int64_t cur_batch_index = -1; + uint64_t largest_nq = 1; + int64_t cur_internal_index; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); @@ -344,11 +344,11 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel { auto sv_adj = ss.Create(largest_nq); auto scratch = ss.Create(largest_nq); auto scratch2 = ss.Create(largest_nq); - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { cur_batch_index = i / output_dim_internal_size; cur_internal_index = i % output_dim_internal_size; - const int nq = num_qubits[cur_batch_index]; + const uint64_t nq = num_qubits[cur_batch_index]; if (cur_batch_index != old_batch_index) { // We've run into a new state vector we must compute. diff --git a/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_expectation.cc b/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_expectation.cc index aacf82cb6..c9fb796cf 100644 --- a/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_expectation.cc +++ b/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_expectation.cc @@ -107,8 +107,8 @@ class TfqSimulateMPS1DExpectationOp : public tensorflow::OpKernel { QsimFusedCircuit({})); Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -127,9 +127,9 @@ class TfqSimulateMPS1DExpectationOp : public tensorflow::OpKernel { // Find largest circuit for tensor size padding and allocate // the output tensor. - int max_num_qubits = 0; - int min_num_qubits = 1 << 30; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + uint64_t min_num_qubits = 1 << 30; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); min_num_qubits = std::min(min_num_qubits, num); } @@ -148,7 +148,7 @@ class TfqSimulateMPS1DExpectationOp : public tensorflow::OpKernel { int bond_dim_; void ComputeSmall(const std::vector& num_qubits, - const int max_num_qubits, + const uint64_t max_num_qubits, const std::vector& unfused_circuits, const std::vector>& pauli_sums, tensorflow::OpKernelContext* context, @@ -160,11 +160,11 @@ class TfqSimulateMPS1DExpectationOp : public tensorflow::OpKernel { Status compute_status = ::tensorflow::Status(); auto c_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { - int old_batch_index = -2; - int cur_batch_index = -1; - int largest_nq = 1; - int cur_op_index; + auto DoWork = [&](int64_t start, int64_t end) { + int64_t old_batch_index = -2; + int64_t cur_batch_index = -1; + uint64_t largest_nq = 1; + int64_t cur_op_index; // Note: ForArgs in MPSSimulator and MPSStateState are currently unused. // So, this 1 is a dummy for qsim::For. @@ -172,11 +172,11 @@ class TfqSimulateMPS1DExpectationOp : public tensorflow::OpKernel { StateSpace ss = StateSpace(1); auto sv = ss.Create(largest_nq, bond_dim_); auto scratch = ss.Create(largest_nq, bond_dim_); - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { cur_batch_index = i / output_dim_op_size; cur_op_index = i % output_dim_op_size; - const int nq = num_qubits[cur_batch_index]; + const uint64_t nq = num_qubits[cur_batch_index]; // (#679) Just ignore empty program auto unfused_gates = unfused_circuits[cur_batch_index].gates; diff --git a/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_sampled_expectation.cc b/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_sampled_expectation.cc index e7014eb46..08dc9dc59 100644 --- a/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_sampled_expectation.cc +++ b/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_sampled_expectation.cc @@ -120,8 +120,8 @@ class TfqSimulateMPS1DSampledExpectationOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -140,9 +140,9 @@ class TfqSimulateMPS1DSampledExpectationOp : public tensorflow::OpKernel { // Find largest circuit for tensor size padding and allocate // the output tensor. - int max_num_qubits = 0; - int min_num_qubits = 1 << 30; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + uint64_t min_num_qubits = 1 << 30; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); min_num_qubits = std::min(min_num_qubits, num); } @@ -160,7 +160,7 @@ class TfqSimulateMPS1DSampledExpectationOp : public tensorflow::OpKernel { private: int bond_dim_; void ComputeSmall(const std::vector& num_qubits, - const int max_num_qubits, + const uint64_t max_num_qubits, const std::vector& unfused_circuits, const std::vector>& pauli_sums, const std::vector>& num_samples, @@ -186,11 +186,11 @@ class TfqSimulateMPS1DSampledExpectationOp : public tensorflow::OpKernel { Status compute_status = ::tensorflow::Status(); auto c_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { - int old_batch_index = -2; - int cur_batch_index = -1; - int largest_nq = 1; - int cur_op_index; + auto DoWork = [&](int64_t start, int64_t end) { + int64_t old_batch_index = -2; + int64_t cur_batch_index = -1; + uint64_t largest_nq = 1; + int64_t cur_op_index; // Note: ForArgs in MPSSimulator and MPSStateState are currently unused. // So, this 1 is a dummy for qsim::For. @@ -207,11 +207,11 @@ class TfqSimulateMPS1DSampledExpectationOp : public tensorflow::OpKernel { auto local_gen = random_gen.ReserveSamples32(n_random); tensorflow::random::SimplePhilox rand_source(&local_gen); - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { cur_batch_index = i / output_dim_op_size; cur_op_index = i % output_dim_op_size; - const int nq = num_qubits[cur_batch_index]; + const uint64_t nq = num_qubits[cur_batch_index]; // (#679) Just ignore empty program auto unfused_gates = unfused_circuits[cur_batch_index].gates; diff --git a/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_samples.cc b/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_samples.cc index 608f94689..85db6278d 100644 --- a/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_samples.cc +++ b/tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_samples.cc @@ -87,8 +87,8 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -107,9 +107,9 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { // Find largest circuit for tensor size padding and allocate // the output tensor. - int max_num_qubits = 0; - int min_num_qubits = 1 << 30; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + uint64_t min_num_qubits = 1 << 30; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); min_num_qubits = std::min(min_num_qubits, num); } @@ -118,7 +118,7 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { tensorflow::errors::InvalidArgument( "All input circuits require minimum 3 qubits.")); - const int output_dim_size = maps.size(); + const size_t output_dim_size = maps.size(); tensorflow::TensorShape output_shape; output_shape.AddDim(output_dim_size); output_shape.AddDim(num_samples); @@ -142,7 +142,7 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { int bond_dim_; void ComputeSmall(const std::vector& num_qubits, - const int max_num_qubits, const int num_samples, + const uint64_t max_num_qubits, const int num_samples, const std::vector& unfused_circuits, tensorflow::OpKernelContext* context, tensorflow::TTypes::Tensor* output_tensor) { @@ -153,8 +153,8 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { tensorflow::GuardedPhiloxRandom random_gen; random_gen.Init(tensorflow::random::New64(), tensorflow::random::New64()); - auto DoWork = [&](int start, int end) { - int largest_nq = 1; + auto DoWork = [&](int64_t start, int64_t end) { + uint64_t largest_nq = 1; // Note: ForArgs in MPSSimulator and MPSStateState are currently unused. // So, this 1 is a dummy for qsim::For. Simulator sim = Simulator(1); @@ -166,8 +166,8 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { auto local_gen = random_gen.ReserveSamples32(unfused_circuits.size() + 1); tensorflow::random::SimplePhilox rand_source(&local_gen); - for (int i = start; i < end; i++) { - int nq = num_qubits[i]; + for (int64_t i = start; i < end; i++) { + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -190,7 +190,7 @@ class TfqSimulateMPS1DSamplesOp : public tensorflow::OpKernel { &results); for (int j = 0; j < num_samples; j++) { - int64_t q_ind = 0; + uint64_t q_ind = 0; while (q_ind < max_num_qubits - nq) { (*output_tensor)(i, j, static_cast(q_ind)) = -2; q_ind++; diff --git a/tensorflow_quantum/core/ops/noise/tfq_noisy_expectation.cc b/tensorflow_quantum/core/ops/noise/tfq_noisy_expectation.cc index 5a13fa1af..824035453 100644 --- a/tensorflow_quantum/core/ops/noise/tfq_noisy_expectation.cc +++ b/tensorflow_quantum/core/ops/noise/tfq_noisy_expectation.cc @@ -119,8 +119,8 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = NoisyQsimCircuitFromProgram( programs[i], maps[i], num_qubits[i], false, &qsim_circuits[i]); NESTED_FN_STATUS_SYNC(parse_status, local, p_lock); @@ -132,8 +132,8 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { programs.size(), num_cycles, construct_f); OP_REQUIRES_OK(context, parse_status); - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -173,7 +173,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { qsim::MultiQubitGateFuser, Simulator>; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -195,7 +195,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { // we no longer parallelize over circuits. Each time we encounter a // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < ncircuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; // (#679) Just ignore empty program if (ncircuits[i].channels.size() == 0) { @@ -256,7 +256,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { } void ComputeSmall(const std::vector& num_qubits, - const int max_num_qubits, + const uint64_t max_num_qubits, const std::vector& ncircuits, const std::vector>& pauli_sums, const std::vector>& num_samples, @@ -295,10 +295,10 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { Status compute_status = ::tensorflow::Status(); auto c_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { // Begin simulation. const auto tfq_for = qsim::SequentialFor(1); - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -311,7 +311,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel { tensorflow::random::SimplePhilox rand_source(&local_gen); for (size_t i = 0; i < ncircuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; int rep_offset = rep_offsets[start][i]; // (#679) Just ignore empty program diff --git a/tensorflow_quantum/core/ops/noise/tfq_noisy_sampled_expectation.cc b/tensorflow_quantum/core/ops/noise/tfq_noisy_sampled_expectation.cc index ed59331b1..df02a8d88 100644 --- a/tensorflow_quantum/core/ops/noise/tfq_noisy_sampled_expectation.cc +++ b/tensorflow_quantum/core/ops/noise/tfq_noisy_sampled_expectation.cc @@ -120,8 +120,8 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = NoisyQsimCircuitFromProgram( programs[i], maps[i], num_qubits[i], false, &qsim_circuits[i]); NESTED_FN_STATUS_SYNC(parse_status, local, p_lock); @@ -133,8 +133,8 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { programs.size(), num_cycles, construct_f); OP_REQUIRES_OK(context, parse_status); - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -174,7 +174,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { qsim::MultiQubitGateFuser, Simulator>; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -199,7 +199,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { // we no longer parallelize over circuits. Each time we encounter a // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < ncircuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; // (#679) Just ignore empty program if (ncircuits[i].channels.empty()) { @@ -260,7 +260,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { } void ComputeSmall(const std::vector& num_qubits, - const int max_num_qubits, + const uint64_t max_num_qubits, const std::vector& ncircuits, const std::vector>& pauli_sums, const std::vector>& num_samples, @@ -302,10 +302,10 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { Status compute_status = ::tensorflow::Status(); auto c_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { // Begin simulation. const auto tfq_for = qsim::SequentialFor(1); - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -317,7 +317,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel { tensorflow::random::SimplePhilox rand_source(&local_gen); for (size_t i = 0; i < ncircuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; int rep_offset = rep_offsets[start][i]; // (#679) Just ignore empty program diff --git a/tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc b/tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc index a09f826b9..1cb2bcc53 100644 --- a/tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc +++ b/tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc @@ -85,8 +85,8 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { auto r = NoisyQsimCircuitFromProgram( programs[i], maps[i], num_qubits[i], true, &qsim_circuits[i]); NESTED_FN_STATUS_SYNC(parse_status, r, p_lock); @@ -98,12 +98,12 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { programs.size(), num_cycles, construct_f); OP_REQUIRES_OK(context, parse_status); - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } - const int output_dim_size = maps.size(); + const size_t output_dim_size = maps.size(); tensorflow::TensorShape output_shape; output_shape.AddDim(output_dim_size); output_shape.AddDim(num_samples); @@ -133,7 +133,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { private: void ComputeLarge(const std::vector& num_qubits, - const int max_num_qubits, const int num_samples, + const uint64_t max_num_qubits, const int num_samples, const std::vector& ncircuits, tensorflow::OpKernelContext* context, tensorflow::TTypes::Tensor* output_tensor) { @@ -146,7 +146,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { qsim::MultiQubitGateFuser, Simulator>; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -159,9 +159,9 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { // Simulate programs one by one. Parallelizing over state vectors // we no longer parallelize over circuits. Each time we encounter a - // a larger circuit we will grow the Statevector as nescessary. + // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < ncircuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -182,7 +182,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { QTSimulator::RunOnce(param, ncircuits[i], rand_source.Rand64(), ss, sim, sv, gathered_samples); - int q_ind = 0; + uint64_t q_ind = 0; uint64_t mask = 1; bool val = 0; while (q_ind < nq) { @@ -202,7 +202,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { } void ComputeSmall(const std::vector& num_qubits, - const int max_num_qubits, const int num_samples, + const uint64_t max_num_qubits, const int num_samples, const std::vector& ncircuits, tensorflow::OpKernelContext* context, tensorflow::TTypes::Tensor* output_tensor) { @@ -239,10 +239,10 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { tensorflow::GuardedPhiloxRandom random_gen; random_gen.Init(tensorflow::random::New64(), tensorflow::random::New64()); - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { // Begin simulation. const auto tfq_for = qsim::SequentialFor(1); - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -254,7 +254,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { tensorflow::random::SimplePhilox rand_source(&local_gen); for (size_t i = 0; i < ncircuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; int j = start > 0 ? offset_prefix_sum[start - 1][i] : 0; int needed_samples = offset_prefix_sum[start][i] - j; if (needed_samples <= 0) { @@ -279,7 +279,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel { QTSimulator::RunOnce(param, ncircuits[i], rand_source.Rand64(), ss, sim, sv, gathered_samples); - int q_ind = 0; + uint64_t q_ind = 0; uint64_t mask = 1; bool val = 0; while (q_ind < nq) { diff --git a/tensorflow_quantum/core/ops/parse_context.cc b/tensorflow_quantum/core/ops/parse_context.cc index f926d15bb..305fc2a25 100644 --- a/tensorflow_quantum/core/ops/parse_context.cc +++ b/tensorflow_quantum/core/ops/parse_context.cc @@ -77,8 +77,8 @@ Status ParsePrograms(OpKernelContext* context, const std::string& input_name, const int num_programs = program_strings.dimension(0); programs->assign(num_programs, Program()); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { OP_REQUIRES_OK(context, ParseProto(program_strings(i), &programs->at(i))); } }; @@ -112,8 +112,8 @@ Status ParsePrograms2D(OpKernelContext* context, const std::string& input_name, const int num_entries = program_strings.dimension(1); programs->assign(num_programs, std::vector(num_entries, Program())); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { OP_REQUIRES_OK( context, ParseProto(program_strings(i / num_entries, i % num_entries), @@ -181,8 +181,8 @@ Status GetProgramsAndNumQubits( // Resolve qubit ID's in parallel. num_qubits->assign(programs->size(), -1); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Program& program = (*programs)[i]; unsigned int this_num_qubits; if (p_sums) { @@ -232,8 +232,8 @@ tensorflow::Status GetProgramsAndNumQubits( // Resolve qubit ID's in parallel. num_qubits->assign(programs->size(), -1); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Program& program = (*programs)[i]; unsigned int this_num_qubits; OP_REQUIRES_OK(context, ResolveQubitIds(&program, &this_num_qubits, @@ -270,7 +270,7 @@ Status GetPauliSums(OpKernelContext* context, p_sums->assign(sum_specs.dimension(0), std::vector(sum_specs.dimension(1), PauliSum())); const int op_dim = sum_specs.dimension(1); - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { for (int ii = start; ii < end; ii++) { const int i = ii / op_dim; const int j = ii % op_dim; @@ -328,8 +328,8 @@ Status GetSymbolMaps(OpKernelContext* context, std::vector* maps) { maps->assign(symbol_values.dimension(0), SymbolMap()); const int symbol_dim = symbol_values.dimension(1); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { for (int j = 0; j < symbol_dim; j++) { const std::string& name = symbol_names(j); const float value = symbol_values(i, j); diff --git a/tensorflow_quantum/core/ops/tfq_adj_grad_op.cc b/tensorflow_quantum/core/ops/tfq_adj_grad_op.cc index 088c6dcde..2fa3e3767 100644 --- a/tensorflow_quantum/core/ops/tfq_adj_grad_op.cc +++ b/tensorflow_quantum/core/ops/tfq_adj_grad_op.cc @@ -111,8 +111,8 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &full_fuse[i], &gate_meta[i]); @@ -144,8 +144,8 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel { context->input(4).dim_size(1), " gradient entries and ", context->input(3).dim_size(1), " paulis per circuit."))); - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -171,7 +171,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel { private: void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector& qsim_circuits, const std::vector& maps, const std::vector>>& full_fuse, @@ -187,17 +187,17 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel { using Simulator = qsim::Simulator; using StateSpace = Simulator::StateSpace; - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); auto scratch = ss.Create(largest_nq); auto scratch2 = ss.Create(largest_nq); - for (int i = start; i < end; i++) { - int nq = num_qubits[i]; + for (int64_t i = start; i < end; i++) { + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. largest_nq = nq; @@ -301,7 +301,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -309,7 +309,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel { auto scratch2 = ss.Create(largest_nq); for (size_t i = 0; i < partial_fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. diff --git a/tensorflow_quantum/core/ops/tfq_calculate_unitary_op.cc b/tensorflow_quantum/core/ops/tfq_calculate_unitary_op.cc index 4f1f662ca..a3242f848 100644 --- a/tensorflow_quantum/core/ops/tfq_calculate_unitary_op.cc +++ b/tensorflow_quantum/core/ops/tfq_calculate_unitary_op.cc @@ -70,8 +70,8 @@ class TfqCalculateUnitaryOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -86,18 +86,18 @@ class TfqCalculateUnitaryOp : public tensorflow::OpKernel { // Find largest circuit for tensor size padding and allocate // the output tensor. - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } // TODO(pmassey): Investigate creating a matrix that isn't just the maximum // required size. - const int output_dim_size = maps.size(); + const size_t output_dim_size = maps.size(); tensorflow::TensorShape output_shape; output_shape.AddDim(output_dim_size); - output_shape.AddDim(1 << max_num_qubits); - output_shape.AddDim(1 << max_num_qubits); + output_shape.AddDim(uint64_t(1) << max_num_qubits); + output_shape.AddDim(uint64_t(1) << max_num_qubits); tensorflow::Tensor *output = nullptr; OP_REQUIRES_OK(context, context->allocate_output(0, output_shape, &output)); @@ -110,14 +110,14 @@ class TfqCalculateUnitaryOp : public tensorflow::OpKernel { using Unitary = UnitarySpace::Unitary; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Unitary u = UnitarySpace(tfq_for).CreateUnitary(largest_nq); // Simulate programs one by one. Parallelizing over state vectors // we no longer parallelize over circuits. Each time we encounter a - // a larger circuit we will grow the unitary as nescessary. + // a larger circuit we will grow the unitary as necessary. for (size_t i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; UCalculator sim = UCalculator(tfq_for); UnitarySpace us = UnitarySpace(tfq_for); if (nq > largest_nq) { @@ -135,10 +135,11 @@ class TfqCalculateUnitaryOp : public tensorflow::OpKernel { auto copy_f = [i, nq, max_num_qubits, &output_tensor, &us, &u]( uint64_t start, uint64_t end) { uint64_t crossover = uint64_t(1) << nq; + uint64_t divisor = uint64_t(1) << max_num_qubits; for (uint64_t l = start; l < end; l++) { - uint64_t j = l / (1 << max_num_qubits); - uint64_t k = l % (1 << max_num_qubits); + uint64_t j = l / divisor; + uint64_t k = l % divisor; if (k < crossover && j < crossover) { output_tensor(static_cast(i), static_cast(j), static_cast(k)) = us.GetEntry(u, k, j); @@ -149,7 +150,7 @@ class TfqCalculateUnitaryOp : public tensorflow::OpKernel { } } }; - const uint64_t num_cycles_copy = 10 * (1 << max_num_qubits); + const uint64_t num_cycles_copy = 10 * (uint64_t(1) << max_num_qubits); context->device()->tensorflow_cpu_worker_threads()->workers->ParallelFor( (uint64_t(1) << max_num_qubits) * (uint64_t(1) << max_num_qubits), num_cycles_copy, copy_f); diff --git a/tensorflow_quantum/core/ops/tfq_circuit_append_op.cc b/tensorflow_quantum/core/ops/tfq_circuit_append_op.cc index 582bd1681..9dac4e44f 100644 --- a/tensorflow_quantum/core/ops/tfq_circuit_append_op.cc +++ b/tensorflow_quantum/core/ops/tfq_circuit_append_op.cc @@ -51,9 +51,9 @@ class TfqCircuitAppendOp : public tensorflow::OpKernel { 0, context->input(0).shape(), &output)); auto output_tensor = output->flat(); - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { std::string temp; - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { for (int j = 0; j < programs_to_append.at(i).circuit().moments().size(); j++) { Moment *new_moment = programs.at(i).mutable_circuit()->add_moments(); @@ -64,7 +64,7 @@ class TfqCircuitAppendOp : public tensorflow::OpKernel { } }; - const int output_dim_size = programs.size(); + const size_t output_dim_size = programs.size(); const int block_size = GetBlockSize(context, output_dim_size); context->device() ->tensorflow_cpu_worker_threads() diff --git a/tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc b/tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc index 669ea6368..5f15a5d56 100644 --- a/tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc +++ b/tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc @@ -57,8 +57,8 @@ class TfqPsDecomposeOp : public tensorflow::OpKernel { const int max_buffer_moments = 5; - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Program cur_program = programs.at(i); Program new_program; std::string temp; diff --git a/tensorflow_quantum/core/ops/tfq_ps_symbol_replace_op.cc b/tensorflow_quantum/core/ops/tfq_ps_symbol_replace_op.cc index d5b4ef9a7..7003e2b8e 100644 --- a/tensorflow_quantum/core/ops/tfq_ps_symbol_replace_op.cc +++ b/tensorflow_quantum/core/ops/tfq_ps_symbol_replace_op.cc @@ -82,10 +82,10 @@ class TfqPsSymbolReplaceOp : public tensorflow::OpKernel { programs.size(), std::vector>( n_symbols, std::vector())); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { - int sidx = i % n_symbols; - int pidx = i / n_symbols; + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { + int64_t sidx = i % n_symbols; + int64_t pidx = i / n_symbols; std::string symbol_to_replace = symbols(sidx); std::string temp_symbol_holder; Program cur_program = programs.at(pidx); @@ -159,10 +159,10 @@ class TfqPsSymbolReplaceOp : public tensorflow::OpKernel { // end. // spinning up and down parallelization for string copying might not be // worth it. - auto DoWork2 = [&](int start, int end) { - for (int i = start; i < end; i++) { - int sidx = i % n_symbols; - int pidx = i / n_symbols; + auto DoWork2 = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { + int64_t sidx = i % n_symbols; + int64_t pidx = i / n_symbols; for (size_t j = 0; j < output_programs.at(pidx).at(sidx).size(); j++) { output_tensor(pidx, sidx, j) = diff --git a/tensorflow_quantum/core/ops/tfq_ps_weights_from_symbols_op.cc b/tensorflow_quantum/core/ops/tfq_ps_weights_from_symbols_op.cc index 7ffebfd22..87003fea5 100644 --- a/tensorflow_quantum/core/ops/tfq_ps_weights_from_symbols_op.cc +++ b/tensorflow_quantum/core/ops/tfq_ps_weights_from_symbols_op.cc @@ -79,8 +79,8 @@ class TfqPsWeightsFromSymbolOp : public tensorflow::OpKernel { std::vector n_single_symbol(programs.size(), 0); - auto DoWork = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Program cur_program = programs.at(i); for (int j = 0; j < cur_program.circuit().moments().size(); j++) { Moment cur_moment = cur_program.circuit().moments().at(j); @@ -143,8 +143,8 @@ class TfqPsWeightsFromSymbolOp : public tensorflow::OpKernel { auto output_tensor = output->tensor(); - auto DoWork2 = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto DoWork2 = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { for (int j = 0; j < n_symbols; j++) { for (size_t k = 0; k < output_results.at(i).at(j).size(); k++) { output_tensor(i, j, k) = output_results.at(i).at(j).at(k); diff --git a/tensorflow_quantum/core/ops/tfq_resolve_parameters_op.cc b/tensorflow_quantum/core/ops/tfq_resolve_parameters_op.cc index 8d7ce06cf..cc28a1938 100644 --- a/tensorflow_quantum/core/ops/tfq_resolve_parameters_op.cc +++ b/tensorflow_quantum/core/ops/tfq_resolve_parameters_op.cc @@ -61,9 +61,9 @@ class TfqResolveParametersOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { + auto DoWork = [&](int64_t start, int64_t end) { std::string temp; - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { Program program = programs[i]; Status local = ResolveSymbols(maps[i], &program, false); NESTED_FN_STATUS_SYNC(parse_status, local, p_lock); diff --git a/tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc b/tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc index 6f0561217..bcb7c106e 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc @@ -93,8 +93,8 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -107,8 +107,8 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel { programs.size(), num_cycles, construct_f); OP_REQUIRES_OK(context, parse_status); - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -139,7 +139,7 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -149,7 +149,7 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel { // we no longer parallelize over circuits. Each time we encounter a // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -180,7 +180,7 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel { } void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector>>& fused_circuits, const std::vector>& pauli_sums, tensorflow::OpKernelContext* context, @@ -193,21 +193,21 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel { Status compute_status = ::tensorflow::Status(); auto c_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { - int old_batch_index = -2; - int cur_batch_index = -1; - int largest_nq = 1; - int cur_op_index; + auto DoWork = [&](int64_t start, int64_t end) { + int64_t old_batch_index = -2; + int64_t cur_batch_index = -1; + uint64_t largest_nq = 1; + int64_t cur_op_index; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); auto scratch = ss.Create(largest_nq); - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { cur_batch_index = i / output_dim_op_size; cur_op_index = i % output_dim_op_size; - const int nq = num_qubits[cur_batch_index]; + const uint64_t nq = num_qubits[cur_batch_index]; // (#679) Just ignore empty program if (fused_circuits[cur_batch_index].size() == 0) { diff --git a/tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc b/tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc index 552644a43..ae2e2114d 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc @@ -112,8 +112,8 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -126,8 +126,8 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { programs.size(), num_cycles, construct_f); OP_REQUIRES_OK(context, parse_status); - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } @@ -159,7 +159,7 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -181,7 +181,7 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { // we no longer parallelize over circuits. Each time we encounter a // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -212,7 +212,7 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { } void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector>>& fused_circuits, const std::vector>& pauli_sums, const std::vector>& num_samples, @@ -238,11 +238,11 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { Status compute_status = ::tensorflow::Status(); auto c_lock = tensorflow::mutex(); - auto DoWork = [&](int start, int end) { - int old_batch_index = -2; - int cur_batch_index = -1; - int largest_nq = 1; - int cur_op_index; + auto DoWork = [&](int64_t start, int64_t end) { + int64_t old_batch_index = -2; + int64_t cur_batch_index = -1; + uint64_t largest_nq = 1; + int64_t cur_op_index; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); @@ -255,11 +255,11 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel { auto local_gen = random_gen.ReserveSamples32(n_random); tensorflow::random::SimplePhilox rand_source(&local_gen); - for (int i = start; i < end; i++) { + for (int64_t i = start; i < end; i++) { cur_batch_index = i / output_dim_op_size; cur_op_index = i % output_dim_op_size; - const int nq = num_qubits[cur_batch_index]; + const uint64_t nq = num_qubits[cur_batch_index]; // (#679) Just ignore empty program if (fused_circuits[cur_batch_index].size() == 0) { diff --git a/tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc b/tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc index 447f66c70..d1d99fd8f 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc @@ -79,8 +79,8 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -95,12 +95,12 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { // Find largest circuit for tensor size padding and allocate // the output tensor. - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } - const int output_dim_size = maps.size(); + const size_t output_dim_size = maps.size(); tensorflow::TensorShape output_shape; output_shape.AddDim(output_dim_size); output_shape.AddDim(num_samples); @@ -130,7 +130,7 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { private: void ComputeLarge( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const int num_samples, const std::vector>>& fused_circuits, tensorflow::OpKernelContext* context, @@ -141,7 +141,7 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -153,9 +153,9 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { // Simulate programs one by one. Parallelizing over state vectors // we no longer parallelize over circuits. Each time we encounter a - // a larger circuit we will grow the Statevector as nescessary. + // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -169,7 +169,7 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { auto samples = ss.Sample(sv, num_samples, rand_source.Rand32()); for (int j = 0; j < num_samples; j++) { - int q_ind = 0; + uint64_t q_ind = 0; uint64_t mask = 1; bool val = 0; while (q_ind < nq) { @@ -189,7 +189,7 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { } void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const int num_samples, const std::vector>>& fused_circuits, tensorflow::OpKernelContext* context, @@ -201,8 +201,8 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { tensorflow::GuardedPhiloxRandom random_gen; random_gen.Init(tensorflow::random::New64(), tensorflow::random::New64()); - auto DoWork = [&](int start, int end) { - int largest_nq = 1; + auto DoWork = [&](int64_t start, int64_t end) { + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); @@ -210,8 +210,8 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { auto local_gen = random_gen.ReserveSamples32(fused_circuits.size() + 1); tensorflow::random::SimplePhilox rand_source(&local_gen); - for (int i = start; i < end; i++) { - int nq = num_qubits[i]; + for (int64_t i = start; i < end; i++) { + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -225,7 +225,7 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel { auto samples = ss.Sample(sv, num_samples, rand_source.Rand32()); for (int j = 0; j < num_samples; j++) { - int q_ind = 0; + uint64_t q_ind = 0; uint64_t mask = 1; bool val = 0; while (q_ind < nq) { diff --git a/tensorflow_quantum/core/ops/tfq_simulate_state_op.cc b/tensorflow_quantum/core/ops/tfq_simulate_state_op.cc index cbc3756cf..b95b9634b 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_state_op.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_state_op.cc @@ -71,8 +71,8 @@ class TfqSimulateStateOp : public tensorflow::OpKernel { Status parse_status = ::tensorflow::Status(); auto p_lock = tensorflow::mutex(); - auto construct_f = [&](int start, int end) { - for (int i = start; i < end; i++) { + auto construct_f = [&](int64_t start, int64_t end) { + for (int64_t i = start; i < end; i++) { Status local = QsimCircuitFromProgram(programs[i], maps[i], num_qubits[i], &qsim_circuits[i], &fused_circuits[i]); @@ -87,15 +87,15 @@ class TfqSimulateStateOp : public tensorflow::OpKernel { // Find largest circuit for tensor size padding and allocate // the output tensor. - int max_num_qubits = 0; - for (const int num : num_qubits) { + uint64_t max_num_qubits = 0; + for (const uint64_t num : num_qubits) { max_num_qubits = std::max(max_num_qubits, num); } - const int output_dim_size = maps.size(); + const size_t output_dim_size = maps.size(); tensorflow::TensorShape output_shape; output_shape.AddDim(output_dim_size); - output_shape.AddDim(1 << max_num_qubits); + output_shape.AddDim(uint64_t(1) << max_num_qubits); tensorflow::Tensor* output = nullptr; OP_REQUIRES_OK(context, context->allocate_output(0, output_shape, &output)); @@ -118,7 +118,7 @@ class TfqSimulateStateOp : public tensorflow::OpKernel { private: void ComputeLarge( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector>>& fused_circuits, tensorflow::OpKernelContext* context, tensorflow::TTypes, 1>::Matrix* output_tensor) { @@ -128,16 +128,16 @@ class TfqSimulateStateOp : public tensorflow::OpKernel { using StateSpace = Simulator::StateSpace; // Begin simulation. - int largest_nq = 1; + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); // Simulate programs one by one. Parallelizing over state vectors // we no longer parallelize over circuits. Each time we encounter a - // a larger circuit we will grow the Statevector as nescessary. + // a larger circuit we will grow the Statevector as necessary. for (size_t i = 0; i < fused_circuits.size(); i++) { - int nq = num_qubits[i]; + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace. @@ -172,7 +172,7 @@ class TfqSimulateStateOp : public tensorflow::OpKernel { } void ComputeSmall( - const std::vector& num_qubits, const int max_num_qubits, + const std::vector& num_qubits, const uint64_t max_num_qubits, const std::vector>>& fused_circuits, tensorflow::OpKernelContext* context, tensorflow::TTypes, 1>::Matrix* output_tensor) { @@ -180,13 +180,13 @@ class TfqSimulateStateOp : public tensorflow::OpKernel { using Simulator = qsim::Simulator; using StateSpace = Simulator::StateSpace; - auto DoWork = [&](int start, int end) { - int largest_nq = 1; + auto DoWork = [&](int64_t start, int64_t end) { + uint64_t largest_nq = 1; Simulator sim = Simulator(tfq_for); StateSpace ss = StateSpace(tfq_for); auto sv = ss.Create(largest_nq); - for (int i = start; i < end; i++) { - int nq = num_qubits[i]; + for (int64_t i = start; i < end; i++) { + uint64_t nq = num_qubits[i]; if (nq > largest_nq) { // need to switch to larger statespace.