Skip to content

Commit

Permalink
Merge pull request #839 from mhucka/mhucka-fix-type-mismatch-warnings
Browse files Browse the repository at this point in the history
Fix type mismatch warnings
  • Loading branch information
mhucka authored Jan 10, 2025
2 parents 9101a91 + 67b41af commit e28d106
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 89 deletions.
12 changes: 6 additions & 6 deletions tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class TfqInnerProductOp : 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 necessary.
for (int i = 0; i < fused_circuits.size(); i++) {
for (size_t i = 0; i < fused_circuits.size(); i++) {
int nq = num_qubits[i];
if (nq > largest_nq) {
// need to switch to larger statespace.
Expand All @@ -191,18 +191,18 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
// the state if there is a possibility that circuit[i] and
// circuit[i + 1] produce the same state.
ss.SetStateZero(sv);
for (int j = 0; j < fused_circuits[i].size(); j++) {
for (size_t j = 0; j < fused_circuits[i].size(); j++) {
qsim::ApplyFusedGate(sim, fused_circuits[i][j], sv);
}
for (int j = 0; j < other_fused_circuits[i].size(); j++) {
for (size_t j = 0; j < other_fused_circuits[i].size(); j++) {
// (#679) Just ignore empty program
if (fused_circuits[i].size() == 0) {
(*output_tensor)(i, j) = std::complex<float>(1, 0);
continue;
}

ss.SetStateZero(scratch);
for (int k = 0; k < other_fused_circuits[i][j].size(); k++) {
for (size_t k = 0; k < other_fused_circuits[i][j].size(); k++) {
qsim::ApplyFusedGate(sim, other_fused_circuits[i][j][k], scratch);
}

Expand Down Expand Up @@ -260,13 +260,13 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
// no need to update scratch_state since ComputeExpectation
// will take care of things for us.
ss.SetStateZero(sv);
for (int j = 0; j < fused_circuits[cur_batch_index].size(); j++) {
for (size_t j = 0; j < fused_circuits[cur_batch_index].size(); j++) {
qsim::ApplyFusedGate(sim, fused_circuits[cur_batch_index][j], sv);
}
}

ss.SetStateZero(scratch);
for (int k = 0;
for (size_t k = 0;
k <
other_fused_circuits[cur_batch_index][cur_internal_index].size();
k++) {
Expand Down
10 changes: 5 additions & 5 deletions tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel {
"other_programs must be rank 2. Got ", context->input(3).dims())));

// Create the output Tensor.
const int output_dim_batch_size = context->input(0).dim_size(0);
const int output_dim_internal_size = context->input(3).dim_size(1);
const int output_dim_symbol_size = context->input(1).dim_size(0);
const size_t output_dim_batch_size = context->input(0).dim_size(0);
const size_t output_dim_internal_size = context->input(3).dim_size(1);
const size_t output_dim_symbol_size = context->input(1).dim_size(0);
OP_REQUIRES(context, output_dim_symbol_size > 0,
tensorflow::errors::InvalidArgument(absl::StrCat(
"The number of symbols must be a positive integer, got ",
Expand Down Expand Up @@ -403,13 +403,13 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel {
// if applicable compute control qubit mask and control value bits.
uint64_t mask = 0;
uint64_t cbits = 0;
for (int k = 0; k < cur_gate.controlled_by.size(); k++) {
for (size_t k = 0; k < cur_gate.controlled_by.size(); k++) {
uint64_t control_loc = cur_gate.controlled_by[k];
mask |= uint64_t{1} << control_loc;
cbits |= ((cur_gate.cmask >> k) & 1) << control_loc;
}

for (int k = 0;
for (size_t k = 0;
k < gradient_gates[cur_batch_index][l - 1].grad_gates.size();
k++) {
// Copy sv_adj onto scratch2 in anticipation of non-unitary
Expand Down
28 changes: 14 additions & 14 deletions tensorflow_quantum/core/ops/noise/tfq_noisy_expectation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {

tensorflow::GuardedPhiloxRandom random_gen;
int max_n_shots = 1;
for (int i = 0; i < num_samples.size(); i++) {
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t i = 0; i < num_samples.size(); i++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
max_n_shots = std::max(max_n_shots, num_samples[i][j]);
}
}
Expand All @@ -194,12 +194,12 @@ class TfqNoisyExpectationOp : 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 necessary.
for (int i = 0; i < ncircuits.size(); i++) {
for (size_t i = 0; i < ncircuits.size(); i++) {
int nq = num_qubits[i];

// (#679) Just ignore empty program
if (ncircuits[i].channels.size() == 0) {
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -226,7 +226,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
sv, unused_stats);

// Use this trajectory as a source for all expectation calculations.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
if (run_samples[j] >= num_samples[i][j]) {
continue;
}
Expand All @@ -238,14 +238,14 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
run_samples[j]++;
}
bool break_loop = true;
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
if (run_samples[j] < num_samples[i][j]) {
break_loop = false;
break;
}
}
if (break_loop) {
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
rolling_sums[j] /= num_samples[i][j];
(*output_tensor)(i, j) = static_cast<float>(rolling_sums[j]);
}
Expand Down Expand Up @@ -286,8 +286,8 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {

tensorflow::GuardedPhiloxRandom random_gen;
int max_n_shots = 1;
for (int i = 0; i < num_samples.size(); i++) {
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t i = 0; i < num_samples.size(); i++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
max_n_shots = std::max(max_n_shots, num_samples[i][j]);
}
}
Expand All @@ -310,13 +310,13 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
random_gen.ReserveSamples128(ncircuits.size() * max_n_shots + 1);
tensorflow::random::SimplePhilox rand_source(&local_gen);

for (int i = 0; i < ncircuits.size(); i++) {
for (size_t i = 0; i < ncircuits.size(); i++) {
int nq = num_qubits[i];
int rep_offset = rep_offsets[start][i];

// (#679) Just ignore empty program
if (ncircuits[i].channels.size() == 0) {
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -343,7 +343,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
sim, sv, unused_stats);

// Compute expectations across all ops using this trajectory.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
int p_reps = (num_samples[i][j] + num_threads - 1) / num_threads;
if (run_samples[j] >= p_reps + rep_offset) {
continue;
Expand All @@ -360,7 +360,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {

// Check if we have run enough trajectories for all ops.
bool break_loop = true;
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
int p_reps = (num_samples[i][j] + num_threads - 1) / num_threads;
if (run_samples[j] < p_reps + rep_offset) {
break_loop = false;
Expand All @@ -370,7 +370,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
if (break_loop) {
// Lock writing to this batch index in output_tensor.
batch_locks[i].lock();
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
rolling_sums[j] /= num_samples[i][j];
(*output_tensor)(i, j) += static_cast<float>(rolling_sums[j]);
}
Expand Down
28 changes: 14 additions & 14 deletions tensorflow_quantum/core/ops/noise/tfq_noisy_sampled_expectation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
tensorflow::GuardedPhiloxRandom random_gen;
int max_psum_length = 1;
int max_n_shots = 1;
for (int i = 0; i < pauli_sums.size(); i++) {
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t i = 0; i < pauli_sums.size(); i++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
max_psum_length =
std::max(max_psum_length, pauli_sums[i][j].terms().size());
max_n_shots = std::max(max_n_shots, num_samples[i][j]);
Expand All @@ -198,12 +198,12 @@ class TfqNoisySampledExpectationOp : 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 necessary.
for (int i = 0; i < ncircuits.size(); i++) {
for (size_t i = 0; i < ncircuits.size(); i++) {
int nq = num_qubits[i];

// (#679) Just ignore empty program
if (ncircuits[i].channels.empty()) {
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -230,7 +230,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
sv, unused_stats);

// Use this trajectory as a source for all expectation calculations.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
if (run_samples[j] >= num_samples[i][j]) {
continue;
}
Expand All @@ -242,14 +242,14 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
run_samples[j]++;
}
bool break_loop = true;
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
if (run_samples[j] < num_samples[i][j]) {
break_loop = false;
break;
}
}
if (break_loop) {
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
rolling_sums[j] /= num_samples[i][j];
(*output_tensor)(i, j) = static_cast<float>(rolling_sums[j]);
}
Expand Down Expand Up @@ -291,8 +291,8 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
tensorflow::GuardedPhiloxRandom random_gen;
int max_psum_length = 1;
int max_n_shots = 1;
for (int i = 0; i < pauli_sums.size(); i++) {
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t i = 0; i < pauli_sums.size(); i++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
max_psum_length =
std::max(max_psum_length, pauli_sums[i][j].terms().size());
max_n_shots = std::max(max_n_shots, num_samples[i][j]);
Expand All @@ -316,13 +316,13 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
auto local_gen = random_gen.ReserveSamples128(num_rand);
tensorflow::random::SimplePhilox rand_source(&local_gen);

for (int i = 0; i < ncircuits.size(); i++) {
for (size_t i = 0; i < ncircuits.size(); i++) {
int nq = num_qubits[i];
int rep_offset = rep_offsets[start][i];

// (#679) Just ignore empty program
if (ncircuits[i].channels.empty()) {
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -349,7 +349,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
sim, sv, unused_stats);

// Compute expectations across all ops using this trajectory.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (size_t j = 0; j < pauli_sums[i].size(); j++) {
int p_reps = (num_samples[i][j] + num_threads - 1) / num_threads;
if (run_samples[j] >= p_reps + rep_offset) {
continue;
Expand All @@ -366,7 +366,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {

// Check if we have run enough trajectories for all ops.
bool break_loop = true;
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
int p_reps = (num_samples[i][j] + num_threads - 1) / num_threads;
if (run_samples[j] < p_reps + rep_offset) {
break_loop = false;
Expand All @@ -376,7 +376,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
if (break_loop) {
// Lock writing to this batch index in output_tensor.
batch_locks[i].lock();
for (int j = 0; j < num_samples[i].size(); j++) {
for (size_t j = 0; j < num_samples[i].size(); j++) {
rolling_sums[j] /= num_samples[i][j];
(*output_tensor)(i, j) += static_cast<float>(rolling_sums[j]);
}
Expand Down
8 changes: 4 additions & 4 deletions tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ 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.
for (int i = 0; i < ncircuits.size(); i++) {
for (size_t i = 0; i < ncircuits.size(); i++) {
int nq = num_qubits[i];

if (nq > largest_nq) {
Expand All @@ -182,7 +182,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel {

QTSimulator::RunOnce(param, ncircuits[i], rand_source.Rand64(), ss, sim,
sv, gathered_samples);
uint64_t q_ind = 0;
int q_ind = 0;
uint64_t mask = 1;
bool val = 0;
while (q_ind < nq) {
Expand Down Expand Up @@ -253,7 +253,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel {
auto local_gen = random_gen.ReserveSamples32(needed_random);
tensorflow::random::SimplePhilox rand_source(&local_gen);

for (int i = 0; i < ncircuits.size(); i++) {
for (size_t i = 0; i < ncircuits.size(); i++) {
int nq = num_qubits[i];
int j = start > 0 ? offset_prefix_sum[start - 1][i] : 0;
int needed_samples = offset_prefix_sum[start][i] - j;
Expand All @@ -279,7 +279,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel {
QTSimulator::RunOnce(param, ncircuits[i], rand_source.Rand64(), ss,
sim, sv, gathered_samples);

uint64_t q_ind = 0;
int q_ind = 0;
uint64_t mask = 1;
bool val = 0;
while (q_ind < nq) {
Expand Down
16 changes: 9 additions & 7 deletions tensorflow_quantum/core/ops/tfq_adj_grad_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
}

ss.SetStateZero(sv);
for (int j = 0; j < full_fuse[i].size(); j++) {
for (size_t j = 0; j < full_fuse[i].size(); j++) {
qsim::ApplyFusedGate(sim, full_fuse[i][j], sv);
}

Expand Down Expand Up @@ -241,13 +241,14 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
// if applicable compute control qubit mask and control value bits.
uint64_t mask = 0;
uint64_t cbits = 0;
for (int k = 0; k < cur_gate.controlled_by.size(); k++) {
for (size_t k = 0; k < cur_gate.controlled_by.size(); k++) {
uint64_t control_loc = cur_gate.controlled_by[k];
mask |= uint64_t{1} << control_loc;
cbits |= ((cur_gate.cmask >> k) & 1) << control_loc;
}

for (int k = 0; k < gradient_gates[i][j - 1].grad_gates.size(); k++) {
for (size_t k = 0; k < gradient_gates[i][j - 1].grad_gates.size();
k++) {
// Copy sv onto scratch2 in anticipation of non-unitary "gradient
// gate".
ss.Copy(sv, scratch2);
Expand Down Expand Up @@ -307,7 +308,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
auto scratch = ss.Create(largest_nq);
auto scratch2 = ss.Create(largest_nq);

for (int i = 0; i < partial_fused_circuits.size(); i++) {
for (size_t i = 0; i < partial_fused_circuits.size(); i++) {
int nq = num_qubits[i];

if (nq > largest_nq) {
Expand All @@ -324,7 +325,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
}

ss.SetStateZero(sv);
for (int j = 0; j < full_fuse[i].size(); j++) {
for (size_t j = 0; j < full_fuse[i].size(); j++) {
qsim::ApplyFusedGate(sim, full_fuse[i][j], sv);
}

Expand Down Expand Up @@ -352,13 +353,14 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
// if applicable compute control qubit mask and control value bits.
uint64_t mask = 0;
uint64_t cbits = 0;
for (int k = 0; k < cur_gate.controlled_by.size(); k++) {
for (size_t k = 0; k < cur_gate.controlled_by.size(); k++) {
uint64_t control_loc = cur_gate.controlled_by[k];
mask |= uint64_t{1} << control_loc;
cbits |= ((cur_gate.cmask >> k) & 1) << control_loc;
}

for (int k = 0; k < gradient_gates[i][j - 1].grad_gates.size(); k++) {
for (size_t k = 0; k < gradient_gates[i][j - 1].grad_gates.size();
k++) {
// Copy sv onto scratch2 in anticipation of non-unitary "gradient
// gate".
ss.Copy(sv, scratch2);
Expand Down
Loading

0 comments on commit e28d106

Please sign in to comment.