Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings of c++ compilation #476

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 11 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 @@ -174,7 +174,8 @@ 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 (std::vector<QsimFusedCircuit>::size_type i = 0;
i < fused_circuits.size(); i++) {
int nq = num_qubits[i];
if (nq > largest_nq) {
// need to switch to larger statespace.
Expand All @@ -186,18 +187,21 @@ 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 (QsimFusedCircuit::size_type 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 (std::vector<QsimFusedCircuit>::size_type 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 (QsimFusedCircuit::size_type k = 0;
k < other_fused_circuits[i][j].size(); k++) {
qsim::ApplyFusedGate(sim, other_fused_circuits[i][j][k], scratch);
}

Expand Down Expand Up @@ -255,13 +259,14 @@ 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 (QsimFusedCircuit::size_type 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 (QsimFusedCircuit::size_type 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 @@ -56,9 +56,9 @@ class TfqInnerProductGradOp : public tensorflow::OpKernel {
"Expected 5 inputs, got ", num_inputs, " inputs.")));

// 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 unsigned int output_dim_batch_size = context->input(0).dim_size(0);
const unsigned int output_dim_internal_size = context->input(3).dim_size(1);
const unsigned int 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 @@ -398,13 +398,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 (unsigned int 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 (unsigned int 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 @@ -175,8 +175,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 (unsigned int i = 0; i < num_samples.size(); i++) {
for (unsigned int j = 0; j < num_samples[i].size(); j++) {
max_n_shots = std::max(max_n_shots, num_samples[i][j]);
}
}
Expand All @@ -188,12 +188,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 (unsigned int 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 (unsigned int j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -220,7 +220,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
scratch, sv, unused_stats);

// Use this trajectory as a source for all expectation calculations.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (unsigned int j = 0; j < pauli_sums[i].size(); j++) {
if (run_samples[j] >= num_samples[i][j]) {
continue;
}
Expand All @@ -232,14 +232,14 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
run_samples[j]++;
}
bool break_loop = true;
for (int j = 0; j < num_samples[i].size(); j++) {
for (unsigned int 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 (unsigned int 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 @@ -280,8 +280,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 (unsigned int i = 0; i < num_samples.size(); i++) {
for (unsigned int j = 0; j < num_samples[i].size(); j++) {
max_n_shots = std::max(max_n_shots, num_samples[i][j]);
}
}
Expand All @@ -304,13 +304,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 (unsigned int 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 (unsigned int j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -337,7 +337,7 @@ class TfqNoisyExpectationOp : public tensorflow::OpKernel {
sim, scratch, sv, unused_stats);

// Compute expectations across all ops using this trajectory.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (unsigned int 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 @@ -354,7 +354,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 (unsigned int 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 @@ -364,7 +364,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 (unsigned int 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 @@ -177,8 +177,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 (unsigned int i = 0; i < pauli_sums.size(); i++) {
for (unsigned int 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 @@ -192,12 +192,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 (unsigned int 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 (unsigned int j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -224,7 +224,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
scratch, sv, unused_stats);

// Use this trajectory as a source for all expectation calculations.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (unsigned int j = 0; j < pauli_sums[i].size(); j++) {
if (run_samples[j] >= num_samples[i][j]) {
continue;
}
Expand All @@ -236,14 +236,14 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
run_samples[j]++;
}
bool break_loop = true;
for (int j = 0; j < num_samples[i].size(); j++) {
for (unsigned int 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 (unsigned int 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 @@ -285,8 +285,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 (unsigned int i = 0; i < pauli_sums.size(); i++) {
for (unsigned int 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 @@ -310,13 +310,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 (unsigned int 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 (unsigned int j = 0; j < pauli_sums[i].size(); j++) {
(*output_tensor)(i, j) = -2.0;
}
continue;
Expand All @@ -343,7 +343,7 @@ class TfqNoisySampledExpectationOp : public tensorflow::OpKernel {
sim, scratch, sv, unused_stats);

// Compute expectations across all ops using this trajectory.
for (int j = 0; j < pauli_sums[i].size(); j++) {
for (unsigned int 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 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 (unsigned int 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 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 (unsigned int 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
22 changes: 11 additions & 11 deletions tensorflow_quantum/core/ops/noise/tfq_noisy_samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,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 unsigned int output_dim_size = maps.size();
tensorflow::TensorShape output_shape;
output_shape.AddDim(output_dim_size);
output_shape.AddDim(num_samples);
Expand Down Expand Up @@ -132,7 +132,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel {

private:
void ComputeLarge(const std::vector<int>& num_qubits,
const int max_num_qubits, const int num_samples,
const uint64_t max_num_qubits, const int num_samples,
const std::vector<NoisyQsimCircuit>& ncircuits,
tensorflow::OpKernelContext* context,
tensorflow::TTypes<int8_t, 3>::Tensor* output_tensor) {
Expand All @@ -145,7 +145,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);
Expand All @@ -160,8 +160,8 @@ 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++) {
int nq = num_qubits[i];
for (unsigned int i = 0; i < ncircuits.size(); i++) {
uint64_t nq = num_qubits[i];

if (nq > largest_nq) {
// need to switch to larger statespace.
Expand Down Expand Up @@ -203,7 +203,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel {
}

void ComputeSmall(const std::vector<int>& num_qubits,
const int max_num_qubits, const int num_samples,
const uint64_t max_num_qubits, const int num_samples,
const std::vector<NoisyQsimCircuit>& ncircuits,
tensorflow::OpKernelContext* context,
tensorflow::TTypes<int8_t, 3>::Tensor* output_tensor) {
Expand Down Expand Up @@ -243,7 +243,7 @@ class TfqNoisySamplesOp : public tensorflow::OpKernel {
auto DoWork = [&](int start, int 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);
Expand All @@ -255,8 +255,8 @@ 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++) {
int nq = num_qubits[i];
for (unsigned int i = 0; i < ncircuits.size(); 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) {
Expand Down
Loading