Skip to content

Fix C++ int type mismatch warnings in core/ops files#1070

Draft
mhucka wants to merge 6 commits into
tensorflow:masterfrom
mhucka:fix-misc-warnings
Draft

Fix C++ int type mismatch warnings in core/ops files#1070
mhucka wants to merge 6 commits into
tensorflow:masterfrom
mhucka:fix-misc-warnings

Conversation

@mhucka
Copy link
Copy Markdown
Member

@mhucka mhucka commented May 13, 2026

PR #476 (dating from 2021) aimed to resolve numerous warnings emitted by the C++ compiler. Most have already been resolved since the time the PR was written, but some still remained. This PR addresses most (possibly all) the rest that can be addressed. (Some cannot be addressed directly because they come from other libraries.)

All of these concern the core ops (tensorflow_quantum/core/ops/) and all the warnings are essentially in the same category of thing, so while this PR touches many files, it seems appropriate to handle them together:

  • Changed the data type of variables max_num_qubits and min_num_qubits from int to uint64_t to match the underlying data structures and vector iterations where they were being used.

  • Changed the data types of some iterator and limit variables (int q_ind, int64_t q_ind, int nq, and int largest_nq) to uint64_t to resolve compiler warnings when evaluated against the updated max_num_qubits mentioned above.

  • Changed the data type of output_dim_size from int to size_t where this variable was directly assigned the result of maps.size() and similar standard library size methods.

  • Retained the data types of variables like max_psum_length have been left as int because they interface with Protobuf sizes, which are signed ints.

  • In configure.sh, removed -Wno-unused-but-set-variable from the flags used during builds of tensorflow_quantum/core/ops/noise/tfq_.*, since the underlying reason for the warnings has been addressed by this PR.

PR tensorflow#476 (dating from 2021)
aimed to resolve numerous warnings emitted by the C++ compiler. Most
have already been resolved since the time the PR was written, but some
still remained. This PR addresses most (possibly all) the rest that can
be addressed. (Some cannot be addressed directly because they come from
other libraries.)

All of these concern the core ops (`tensorflow_quantum/core/ops/`) and
all the warnings are essentially in the same category of thing, so while
this PR touches many files, it seems appropriate to handle them together:

* Changed the data type of variables `max_num_qubits` and
  `min_num_qubits` from `int` to `uint64_t` to match the underlying data
  structures and vector iterations where they were being used.

* Changed the data types of some iterator and limit variables (`int
  q_ind`, `int64_t q_ind`, `int nq`, and `int largest_nq`) to `uint64_t`
  to resolve compiler warnings when evaluated against the newly updated
  `max_num_qubits` mentioned above.

* Changed the data type of `output_dim_size` from `int` to `size_t`
  where this variable was directly assigned the result of `maps.size()`
  and similar standard library size methods.

* Retained the data types of variables like `max_psum_length` have been
  left as `int` because they interface with Protobuf sizes, which are
  signed ints.

* In `configure.sh`, removed `-Wno-unused-but-set-variable` from the
  flags used during builds of
  `tensorflow_quantum/core/ops/noise/tfq_.*`, since the underlying
  reason for the warnings has been addressed.
@mhucka
Copy link
Copy Markdown
Member Author

mhucka commented May 13, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors various quantum simulation kernels to use 64-bit integers for qubit counts and dimensions, and removes compiler warning suppressions in the configuration script. Feedback highlights several remaining overflow risks, specifically recommending the use of 64-bit literals for bit-shifts and int64_t for loop indices and lambda parameters in parallelized workloads to handle large batch sizes safely.

Comment thread tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc
Comment thread tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc
Comment thread tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_expectation.cc
Comment thread tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_samples.cc
Comment thread tensorflow_quantum/core/ops/tfq_simulate_state_op.cc
Comment thread tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc
Comment thread tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc
Comment thread tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc
Comment thread tensorflow_quantum/core/ops/tfq_simulate_state_op.cc
@mhucka
Copy link
Copy Markdown
Member Author

mhucka commented May 18, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates multiple quantum operations to use 64-bit integers for qubit counts, loop indices, and parallel work parameters to prevent overflow and support larger simulations. Feedback focuses on ensuring consistency by updating batch index variables to int64_t where they are derived from 64-bit loop indices. The reviewer also noted that tfq_adj_grad_op.cc was missed in the type update and suggested a performance optimization in tfq_calculate_unitary_op.cc to hoist a repeated bit-shift calculation out of a hot loop.

Comment thread tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc
Comment thread tensorflow_quantum/core/ops/math_ops/tfq_inner_product_grad.cc
Comment thread tensorflow_quantum/core/ops/math_ops/tfq_simulate_1d_expectation.cc
Comment thread tensorflow_quantum/core/ops/tfq_adj_grad_op.cc Outdated
Comment thread tensorflow_quantum/core/ops/tfq_calculate_unitary_op.cc Outdated
Comment thread tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc
Comment thread tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc
@mhucka
Copy link
Copy Markdown
Member Author

mhucka commented May 18, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request performs a comprehensive refactoring of integer types across the codebase, replacing 32-bit integers with 64-bit equivalents (int64_t, uint64_t, and size_t) for qubit counts, loop indices, and tensor dimensions. These changes help prevent potential overflows and improve type safety in simulation kernels. Additionally, the PR removes obsolete compiler warning suppressions in configure.sh and corrects minor typos in source comments. There are no review comments to address, and I have no further feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant