Program Listing for File SparseLinAlg.hpp

Return to documentation for file (pennylane_lightning/core/src/simulators/lightning_qubit/utils/SparseLinAlg.hpp)

// Copyright 2018-2023 Xanadu Quantum Technologies Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <complex>
#include <vector>

namespace Pennylane::LightningQubit::Util {
template <class fp_precision, class index_type>
std::vector<std::complex<fp_precision>>
apply_Sparse_Matrix(const std::complex<fp_precision> *vector_ptr,
                    const index_type vector_size, const index_type *row_map_ptr,
                    [[maybe_unused]] const index_type row_map_size,
                    const index_type *entries_ptr,
                    const std::complex<fp_precision> *values_ptr,
                    [[maybe_unused]] const index_type numNNZ) {
    std::vector<std::complex<fp_precision>> result;
    result.resize(vector_size);
    std::size_t count = 0;
    for (index_type i = 0; i < vector_size; i++) {
        result[i] = 0.0;
        for (index_type j = 0; j < row_map_ptr[i + 1] - row_map_ptr[i]; j++) {
            result[i] += values_ptr[count] * vector_ptr[entries_ptr[count]];
            count++;
        }
    }
    return result;
};
} // namespace Pennylane::LightningQubit::Util