Skip to content

Commit

Permalink
Add std:: to some usual suspects (#865)
Browse files Browse the repository at this point in the history
**Context:** We have several occurrences of missing std namespace.

**Description of the Change:** Add std namespace to usual suspects.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-71935]

---------

Co-authored-by: ringo-but-quantum <[email protected]>
Co-authored-by: Vincent Michaud-Rioux <[email protected]>
Co-authored-by: Luis Alfredo Nuñez Meneses <[email protected]>
  • Loading branch information
4 people authored Aug 26, 2024
1 parent 07108df commit 4c6e4bb
Show file tree
Hide file tree
Showing 117 changed files with 899 additions and 818 deletions.
7 changes: 5 additions & 2 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

### Improvements

* Updated calls of ``size_t`` to ``std::size_t`` everywhere.
[(#816)](https://github.com/PennyLaneAI/pennylane-lightning/pull/816/)

* Update `ctrl_decomp_zyz` tests with `len(control_wires) > 1`.
[(#821)](https://github.com/PennyLaneAI/pennylane-lightning/pull/821)

Expand All @@ -44,14 +47,14 @@

* Multiple calls to the `append_mps_final_state()` API is allowed in `lightning.tensor`.
[(#830)](https://github.com/PennyLaneAI/pennylane-lightning/pull/830)

* Update `generate_samples` in `LightningKokkos` and `LightningGPU` to support `qml.measurements.Shots` type instances.
[(#839)](https://github.com/PennyLaneAI/pennylane-lightning/pull/839)
[(#864)](https://github.com/PennyLaneAI/pennylane-lightning/pull/864)

* LightningQubit gains native support for the `PauliRot` gate.
[(#834)](https://github.com/PennyLaneAI/pennylane-lightning/pull/834)

* The `setBasisState` and `setStateVector` methods of `StateVectorLQubit` and `StateVectorKokkos` are overloaded to support PennyLane-like parameters.
[(#843)](https://github.com/PennyLaneAI/pennylane-lightning/pull/843)

Expand Down
6 changes: 3 additions & 3 deletions doc/lightning_qubit/development/add_gate_kernel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ In this case, you may first create a file and add a class:
/* This defines the required alignment for this kernel. If there is no special requirement,
using std::alignment_of_v is sufficient. */
template <typename PrecisionT>
constexpr static size_t required_alignment = std::alignment_of_v<PrecisionT>;
constexpr static std::size_t required_alignment = std::alignment_of_v<PrecisionT>;
template <class PrecisionT>
static void applyPauliX(std::complex<PrecisionT>* data,
size_t num_qubits,
const std::vector<size_t>& wires,
std::size_t num_qubits,
const std::vector<std::size_t>& wires,
[[maybe_unused]] bool inverse) {
/* Write your implementation */
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ The following simple (C++ style) pseudocode shows how the algorithm is implement

.. code-block::
template<typename PrecisionT, size_t packed_size>
template<typename PrecisionT, std::size_t packed_size>
class ApplyPauliX {
template<size_t wire>
template<std::size_t wire>
void applyInternal(...) {
// Within a row
permutation = compute a permutation within a row for a given wire
Expand All @@ -54,7 +54,7 @@ The following simple (C++ style) pseudocode shows how the algorithm is implement
save row to the memory
}
}
void applyExternal(size_t wire, ...) {
void applyExternal(std::size_t wire, ...) {
// Between rows
for proper index k {
row1 = load k-th row
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.38.0-dev49"
__version__ = "0.38.0-dev50"
5 changes: 3 additions & 2 deletions pennylane_lightning/core/src/algorithms/JacobianData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ template <class StateVectorT> class OpsData {
[[nodiscard]] auto getTotalNumParams() const -> std::size_t {
return std::accumulate(
ops_params_.begin(), ops_params_.end(), std::size_t{0U},
[](size_t acc, auto &params) { return acc + params.size(); });
[](std::size_t acc, auto &params) { return acc + params.size(); });
}
};

Expand Down Expand Up @@ -306,7 +306,8 @@ template <class StateVectorT> class JacobianData {
* (e.g. StatePrep) or Hamiltonian coefficients.
* @endrst
*/
JacobianData(size_t num_params, std::size_t num_elem, const CFP_t *sv_ptr,
JacobianData(std::size_t num_params, std::size_t num_elem,
const CFP_t *sv_ptr,
std::vector<std::shared_ptr<Observable<StateVectorT>>> obs,
OpsData<StateVectorT> ops, std::vector<std::size_t> trainP)
: num_parameters(num_params), num_elements(num_elem), psi(sv_ptr),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ template <typename TypeList> void testAdjointJacobian() {
const auto obs = std::make_shared<NamedObs<StateVectorT>>(
"PauliZ", std::vector<std::size_t>{0});

for (size_t i = 0; i < thetas.size(); i++) {
for (std::size_t i = 0; i < thetas.size(); i++) {
const PrecisionT theta = thetas[i];
std::vector<PrecisionT> local_params{
theta, std::pow(theta, (PrecisionT)3),
Expand Down
8 changes: 4 additions & 4 deletions pennylane_lightning/core/src/bindings/Bindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ auto alignedNumpyArray(CPUMemoryModel memory_model, std::size_t size,
* @param size Size of the array to create
* @param dt Pybind11's datatype object
*/
auto allocateAlignedArray(size_t size, const py::dtype &dt,
auto allocateAlignedArray(std::size_t size, const py::dtype &dt,
bool zeroInit = false) -> py::array {
// TODO: Move memset operations to here to reduce zeroInit pass-throughs.
auto memory_model = bestCPUMemoryModel();
Expand Down Expand Up @@ -480,7 +480,7 @@ void registerBackendAgnosticMeasurements(PyClass &pyclass) {
auto &&result = M.generate_samples(num_shots);
const std::size_t ndim = 2;
const std::vector<std::size_t> shape{num_shots, num_wires};
constexpr auto sz = sizeof(size_t);
constexpr auto sz = sizeof(std::size_t);
const std::vector<std::size_t> strides{sz * num_wires, sz};
// return 2-D NumPy array
return py::array(py::buffer_info(
Expand Down Expand Up @@ -559,7 +559,7 @@ void registerBackendAgnosticAlgorithms(py::module_ &m) {
.def("__repr__", [](const OpsData<StateVectorT> &ops) {
using namespace Pennylane::Util;
std::ostringstream ops_stream;
for (size_t op = 0; op < ops.getSize(); op++) {
for (std::size_t op = 0; op < ops.getSize(); op++) {
ops_stream << "{'name': " << ops.getOpsName()[op];
ops_stream << ", 'params': " << ops.getOpsParams()[op];
ops_stream << ", 'inv': " << ops.getOpsInverses()[op];
Expand Down Expand Up @@ -591,7 +591,7 @@ void registerBackendAgnosticAlgorithms(py::module_ &m) {
const std::vector<std::vector<bool>> &ops_controlled_values) {
std::vector<std::vector<ComplexT>> conv_matrices(
ops_matrices.size());
for (size_t op = 0; op < ops_name.size(); op++) {
for (std::size_t op = 0; op < ops_name.size(); op++) {
const auto m_buffer = ops_matrices[op].request();
if (m_buffer.size) {
const auto m_ptr =
Expand Down
6 changes: 3 additions & 3 deletions pennylane_lightning/core/src/bindings/BindingsMPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void registerBackendAgnosticMeasurementsMPI(PyClass &pyclass) {
auto &&result = M.generate_samples(num_shots);
const std::size_t ndim = 2;
const std::vector<std::size_t> shape{num_shots, num_wires};
constexpr auto sz = sizeof(size_t);
constexpr auto sz = sizeof(std::size_t);
const std::vector<std::size_t> strides{sz * num_wires, sz};
// return 2-D NumPy array
return py::array(py::buffer_info(
Expand Down Expand Up @@ -351,7 +351,7 @@ void registerBackendAgnosticAlgorithmsMPI(py::module_ &m) {
.def("__repr__", [](const OpsData<StateVectorT> &ops) {
using namespace Pennylane::Util;
std::ostringstream ops_stream;
for (size_t op = 0; op < ops.getSize(); op++) {
for (std::size_t op = 0; op < ops.getSize(); op++) {
ops_stream << "{'name': " << ops.getOpsName()[op];
ops_stream << ", 'params': " << ops.getOpsParams()[op];
ops_stream << ", 'inv': " << ops.getOpsInverses()[op];
Expand All @@ -378,7 +378,7 @@ void registerBackendAgnosticAlgorithmsMPI(py::module_ &m) {
const std::vector<std::vector<bool>> &ops_controlled_values) {
std::vector<std::vector<ComplexT>> conv_matrices(
ops_matrices.size());
for (size_t op = 0; op < ops_name.size(); op++) {
for (std::size_t op = 0; op < ops_name.size(); op++) {
const auto m_buffer = ops_matrices[op].request();
if (m_buffer.size) {
const auto m_ptr =
Expand Down
33 changes: 17 additions & 16 deletions pennylane_lightning/core/src/measurements/MeasurementsBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ template <class StateVectorT, class Derived> class MeasurementsBase {
* @return 1-D vector of samples in binary with each sample
* separated by a stride equal to the number of qubits.
*/
auto generate_samples(size_t num_samples) -> std::vector<std::size_t> {
auto generate_samples(std::size_t num_samples) -> std::vector<std::size_t> {
return static_cast<Derived *>(this)->generate_samples(num_samples);
};

Expand All @@ -160,7 +160,7 @@ template <class StateVectorT, class Derived> class MeasurementsBase {
} else if (obs.getObsName().find("Hamiltonian") != std::string::npos) {
auto coeffs = obs.getCoeffs();
auto obsTerms = obs.getObs();
for (size_t obs_term_idx = 0; obs_term_idx < coeffs.size();
for (std::size_t obs_term_idx = 0; obs_term_idx < coeffs.size();
obs_term_idx++) {
result += coeffs[obs_term_idx] * expval(*obsTerms[obs_term_idx],
num_shots, shot_range);
Expand Down Expand Up @@ -202,11 +202,11 @@ template <class StateVectorT, class Derived> class MeasurementsBase {

std::vector<PrecisionT> eigenVals = eigenValues[0];

for (size_t i = 1; i < eigenValues.size(); i++) {
for (std::size_t i = 1; i < eigenValues.size(); i++) {
eigenVals = kronProd(eigenVals, eigenValues[i]);
}

for (size_t i = 0; i < num_samples; i++) {
for (std::size_t i = 0; i < num_samples; i++) {
std::size_t idx = 0;
std::size_t wire_idx = 0;
for (auto &obs_wire : obs_wires) {
Expand Down Expand Up @@ -319,11 +319,11 @@ template <class StateVectorT, class Derived> class MeasurementsBase {

std::size_t num_wires = _statevector.getTotalNumQubits();

std::vector<PrecisionT> prob_shots(size_t{1} << wires.size(), 0.0);
std::vector<PrecisionT> prob_shots(std::size_t{1} << wires.size(), 0.0);

for (auto &it : counts_map) {
std::size_t bitVal = 0;
for (size_t bit = 0; bit < wires.size(); bit++) {
for (std::size_t bit = 0; bit < wires.size(); bit++) {
// Mapping the value of wires[bit]th bit to local [bit]th bit of
// the output
bitVal +=
Expand All @@ -346,12 +346,12 @@ template <class StateVectorT, class Derived> class MeasurementsBase {
*
* @return Floating point std::vector with probabilities.
*/
auto probs(size_t num_shots) -> std::vector<PrecisionT> {
auto probs(std::size_t num_shots) -> std::vector<PrecisionT> {
auto counts_map = counts(num_shots);

std::size_t num_wires = _statevector.getTotalNumQubits();

std::vector<PrecisionT> prob_shots(size_t{1} << num_wires, 0.0);
std::vector<PrecisionT> prob_shots(std::size_t{1} << num_wires, 0.0);

for (auto &it : counts_map) {
prob_shots[it.first] =
Expand Down Expand Up @@ -407,7 +407,7 @@ template <class StateVectorT, class Derived> class MeasurementsBase {
-> std::unordered_map<PrecisionT, std::size_t> {
std::unordered_map<PrecisionT, std::size_t> outcome_map;
auto sample_data = sample(obs, num_shots);
for (size_t i = 0; i < num_shots; i++) {
for (std::size_t i = 0; i < num_shots; i++) {
auto key = sample_data[i];
auto it = outcome_map.find(key);
if (it != outcome_map.end()) {
Expand All @@ -425,18 +425,18 @@ template <class StateVectorT, class Derived> class MeasurementsBase {
*
* @param num_shots Number of wires the sampled observable was performed on
*
* @return std::unordered_map<size_t, std::size_t> with format ``{'outcome':
* num_occurences}``
* @return std::unordered_map<std::size_t, std::size_t> with format
* ``{'outcome': num_occurences}``
*/
auto counts(const std::size_t &num_shots)
-> std::unordered_map<size_t, std::size_t> {
std::unordered_map<size_t, std::size_t> outcome_map;
-> std::unordered_map<std::size_t, std::size_t> {
std::unordered_map<std::size_t, std::size_t> outcome_map;
auto sample_data = sample(num_shots);

std::size_t num_wires = _statevector.getTotalNumQubits();
for (size_t i = 0; i < num_shots; i++) {
for (std::size_t i = 0; i < num_shots; i++) {
std::size_t key = 0;
for (size_t j = 0; j < num_wires; j++) {
for (std::size_t j = 0; j < num_wires; j++) {
key += sample_data[i * num_wires + j] << (num_wires - 1 - j);
}

Expand Down Expand Up @@ -494,7 +494,8 @@ template <class StateVectorT, class Derived> class MeasurementsBase {
// Get a slice of samples based on the shot_range vector
std::size_t shot_idx = 0;
for (const auto &i : shot_range) {
for (size_t j = i * num_qubits; j < (i + 1) * num_qubits; j++) {
for (std::size_t j = i * num_qubits; j < (i + 1) * num_qubits;
j++) {
// TODO some extra work to make it cache-friendly
sub_samples[shot_idx * num_qubits + j - i * num_qubits] =
samples[j];
Expand Down
Loading

0 comments on commit 4c6e4bb

Please sign in to comment.