Program Listing for File StateVectorLQubitRaw.hpp

Return to documentation for file (pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitRaw.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 <stdexcept>
#include <utility>
#include <vector>

#include "BitUtil.hpp"        // log2PerfectPower, isPerfectPowerOf2
#include "CPUMemoryModel.hpp" // getMemoryModel
#include "Error.hpp"
#include "StateVectorLQubit.hpp"

namespace {
using Pennylane::Util::getMemoryModel;
using Pennylane::Util::isPerfectPowerOf2;
using Pennylane::Util::log2PerfectPower;
} // namespace

namespace Pennylane::LightningQubit {
template <class fp_t = double>
class StateVectorLQubitRaw final
    : public StateVectorLQubit<fp_t, StateVectorLQubitRaw<fp_t>> {
  public:
    using PrecisionT = fp_t;
    using ComplexT = std::complex<PrecisionT>;
    using CFP_t = ComplexT;
    using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::External;

  private:
    using BaseType =
        StateVectorLQubit<PrecisionT, StateVectorLQubitRaw<PrecisionT>>;

    ComplexT *data_;
    std::size_t length_;

  public:
    StateVectorLQubitRaw(ComplexT *data, std::size_t length,
                         Threading threading = Threading::SingleThread)
        : BaseType{log2PerfectPower(length), threading,
                   getMemoryModel(static_cast<void *>(data))},
          data_{data}, length_(length) {
        // check length is perfect power of 2
        PL_ABORT_IF_NOT(isPerfectPowerOf2(length),
                        "The size of provided data must be a power of 2.");
    }

    [[nodiscard]] auto getData() const -> ComplexT * { return data_; }

    auto getData() -> ComplexT * { return data_; }

    auto getDataVector() -> std::vector<ComplexT> {
        return std::vector<ComplexT>{data_, data_ + length_};
    }

    [[nodiscard]] auto getLength() const -> std::size_t { return length_; }

    void updateData(const ComplexT *new_data, std::size_t new_size) {
        PL_ASSERT(length_ == new_size);
        std::copy(new_data, new_data + new_size, data_);
    }

    template <class Alloc>
    void updateData(const std::vector<ComplexT, Alloc> &new_data) {
        updateData(new_data.data(), new_data.size());
    }
};
} // namespace Pennylane::LightningQubit