Program Listing for File pauli_operator.hpp

Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/qulacs-rtd/checkouts/latest/src/cppsim/pauli_operator.hpp)

#pragma once

#pragma once

#include <boost/dynamic_bitset.hpp>
#include <cassert>
#include <iostream>
#include <vector>

#include "exception.hpp"
#include "type.hpp"
#include "utility.hpp"

class QuantumStateBase;

class DllExport SinglePauliOperator {
protected:
    UINT _index;
    UINT _pauli_id;

public:
    SinglePauliOperator(UINT index_, UINT pauli_id_)
        : _index(index_), _pauli_id(pauli_id_) {
        if (pauli_id_ > 3) {
            throw InvalidPauliIdentifierException(
                "Error: SinglePauliOperator(UINT, UINT): index must be "
                "either of 0,1,2,3");
        }
    };

    UINT index() const { return _index; }

    UINT pauli_id() const { return _pauli_id; }

    boost::property_tree::ptree to_ptree() const {
        boost::property_tree::ptree pt;
        pt.put("name", "SinglePauliOperator");
        pt.put("index", _index);
        pt.put("pauli_id", _pauli_id);
        return pt;
    }
};

class DllExport PauliOperator {
private:
    std::vector<SinglePauliOperator> _pauli_list;
    CPPCTYPE _coef;
    boost::dynamic_bitset<> _z;
    boost::dynamic_bitset<> _x;

public:
    std::vector<UINT> get_index_list() const {
        std::vector<UINT> index_list;
        std::transform(_pauli_list.cbegin(), _pauli_list.cend(),
            std::back_inserter(index_list),
            [](const SinglePauliOperator& val) { return val.index(); });
        return index_list;
    }

    UINT get_qubit_count() const {
        std::vector<UINT> index_list = get_index_list();
        if (index_list.size() == 0) return 0;
        return *std::max_element(index_list.begin(), index_list.end()) + 1;
    }

    std::vector<UINT> get_pauli_id_list() const {
        std::vector<UINT> pauli_id_list;
        std::transform(_pauli_list.cbegin(), _pauli_list.cend(),
            std::back_inserter(pauli_id_list),
            [](const SinglePauliOperator& val) { return val.pauli_id(); });
        return pauli_id_list;
    }

    explicit PauliOperator(CPPCTYPE coef = 1.) : _coef(coef){};

    explicit PauliOperator(std::string strings, CPPCTYPE coef = 1.);

    PauliOperator(const std::vector<UINT>& target_qubit_index_list,
        std::string Pauli_operator_type_list, CPPCTYPE coef = 1.);

    explicit PauliOperator(
        const std::vector<UINT>& pauli_list, CPPCTYPE coef = 1.);

    PauliOperator(const std::vector<UINT>& target_qubit_index_list,
        const std::vector<UINT>& target_qubit_pauli_list, CPPCTYPE coef = 1.);

    PauliOperator(const boost::dynamic_bitset<>& x,
        const boost::dynamic_bitset<>& z, CPPCTYPE coef = 1.);

    virtual CPPCTYPE get_coef() const { return _coef; }

    virtual boost::dynamic_bitset<> get_x_bits() const { return _x; }

    virtual boost::dynamic_bitset<> get_z_bits() const { return _z; }

    virtual ~PauliOperator(){};

    virtual void add_single_Pauli(UINT qubit_index, UINT pauli_type);

    virtual CPPCTYPE get_expectation_value(const QuantumStateBase* state) const;

    virtual CPPCTYPE get_expectation_value_single_thread(
        const QuantumStateBase* state) const;

    virtual CPPCTYPE get_transition_amplitude(const QuantumStateBase* state_bra,
        const QuantumStateBase* state_ket) const;

    virtual PauliOperator* copy() const;

    virtual void change_coef(CPPCTYPE new_coef);

    virtual std::string get_pauli_string() const;

    virtual boost::property_tree::ptree to_ptree() const;

    PauliOperator operator*(const PauliOperator& target) const;

    PauliOperator operator*(CPPCTYPE target) const;

    PauliOperator& operator*=(const PauliOperator& target);

    PauliOperator& operator*=(CPPCTYPE target);
};