Skip to content

Commit

Permalink
fix to compile gate
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint committed Jan 24, 2025
1 parent 3326bce commit 651958a
Show file tree
Hide file tree
Showing 11 changed files with 1,122 additions and 1,104 deletions.
348 changes: 176 additions & 172 deletions include/scaluq/gate/gate.hpp

Large diffs are not rendered by default.

383 changes: 199 additions & 184 deletions include/scaluq/gate/gate_factory.hpp

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions include/scaluq/gate/gate_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

namespace scaluq {
namespace internal {
template <FloatingPoint Fp>
class DenseMatrixGateImpl : public GateBase<Fp> {
Matrix<Fp> _matrix;
template <Precision Prec>
class DenseMatrixGateImpl : public GateBase<Prec> {
Matrix<Prec> _matrix;
bool _is_unitary;

public:
DenseMatrixGateImpl(std::uint64_t target_mask,
std::uint64_t control_mask,
const ComplexMatrix<Fp>& mat,
const ComplexMatrix& mat,
bool is_unitary = false);

std::shared_ptr<const GateBase<Fp>> get_inverse() const override;
std::shared_ptr<const GateBase<Prec>> get_inverse() const override;

Matrix<Fp> get_matrix_internal() const;
Matrix<Prec> get_matrix_internal() const;

ComplexMatrix<Fp> get_matrix() const override;
ComplexMatrix get_matrix() const override;

void update_quantum_state(StateVector<Fp>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Fp>& states) const override;
void update_quantum_state(StateVector<Prec>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Prec>& states) const override;

std::string to_string(const std::string& indent) const override;

Expand All @@ -40,26 +40,26 @@ class DenseMatrixGateImpl : public GateBase<Fp> {
}
};

template <FloatingPoint Fp>
class SparseMatrixGateImpl : public GateBase<Fp> {
SparseMatrix<Fp> _matrix;
template <Precision Prec>
class SparseMatrixGateImpl : public GateBase<Prec> {
SparseMatrix<Prec> _matrix;
std::uint64_t num_nnz;

public:
SparseMatrixGateImpl(std::uint64_t target_mask,
std::uint64_t control_mask,
const SparseComplexMatrix<Fp>& mat);
const SparseComplexMatrix& mat);

std::shared_ptr<const GateBase<Fp>> get_inverse() const override;
std::shared_ptr<const GateBase<Prec>> get_inverse() const override;

Matrix<Fp> get_matrix_internal() const;
Matrix<Prec> get_matrix_internal() const;

ComplexMatrix<Fp> get_matrix() const override;
ComplexMatrix get_matrix() const override;

SparseComplexMatrix<Fp> get_sparse_matrix() const { return get_matrix().sparseView(); }
SparseComplexMatrix get_sparse_matrix() const { return get_matrix().sparseView(); }

void update_quantum_state(StateVector<Fp>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Fp>& states) const override;
void update_quantum_state(StateVector<Prec>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Prec>& states) const override;

std::string to_string(const std::string& indent) const override;

Expand All @@ -73,21 +73,21 @@ class SparseMatrixGateImpl : public GateBase<Fp> {

} // namespace internal

template <FloatingPoint Fp>
using SparseMatrixGate = internal::GatePtr<internal::SparseMatrixGateImpl<Fp>>;
template <FloatingPoint Fp>
using DenseMatrixGate = internal::GatePtr<internal::DenseMatrixGateImpl<Fp>>;
template <Precision Prec>
using SparseMatrixGate = internal::GatePtr<internal::SparseMatrixGateImpl<Prec>>;
template <Precision Prec>
using DenseMatrixGate = internal::GatePtr<internal::DenseMatrixGateImpl<Prec>>;

#ifdef SCALUQ_USE_NANOBIND
namespace internal {
template <FloatingPoint Fp>
template <Precision Prec>
void bind_gate_gate_matrix_hpp(nb::module_& m) {
DEF_GATE(SparseMatrixGate, Fp, "Specific class of sparse matrix gate.")
.def("matrix", [](const SparseMatrixGate<Fp>& gate) { return gate->get_matrix(); })
DEF_GATE(SparseMatrixGate, Prec, "Specific class of sparse matrix gate.")
.def("matrix", [](const SparseMatrixGate<Prec>& gate) { return gate->get_matrix(); })
.def("sparse_matrix",
[](const SparseMatrixGate<Fp>& gate) { return gate->get_sparse_matrix(); });
DEF_GATE(DenseMatrixGate, Fp, "Specific class of dense matrix gate.")
.def("matrix", [](const DenseMatrixGate<Fp>& gate) { return gate->get_matrix(); });
[](const SparseMatrixGate<Prec>& gate) { return gate->get_sparse_matrix(); });
DEF_GATE(DenseMatrixGate, Prec, "Specific class of dense matrix gate.")
.def("matrix", [](const DenseMatrixGate<Prec>& gate) { return gate->get_matrix(); });
}
} // namespace internal
#endif
Expand Down
62 changes: 32 additions & 30 deletions include/scaluq/gate/gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
namespace scaluq {
namespace internal {

template <FloatingPoint Fp>
class PauliGateImpl : public GateBase<Fp> {
const PauliOperator<Fp> _pauli;
template <Precision Prec>
class PauliGateImpl : public GateBase<Prec> {
const PauliOperator<Prec> _pauli;

public:
PauliGateImpl(std::uint64_t control_mask, const PauliOperator<Fp>& pauli)
: GateBase<Fp>(vector_to_mask<false>(pauli.target_qubit_list()), control_mask),
PauliGateImpl(std::uint64_t control_mask, const PauliOperator<Prec>& pauli)
: GateBase<Prec>(vector_to_mask<false>(pauli.target_qubit_list()), control_mask),
_pauli(pauli) {}

PauliOperator<Fp> pauli() const { return _pauli; };
PauliOperator<Prec> pauli() const { return _pauli; };
std::vector<std::uint64_t> pauli_id_list() const { return _pauli.pauli_id_list(); }

std::shared_ptr<const GateBase<Fp>> get_inverse() const override {
std::shared_ptr<const GateBase<Prec>> get_inverse() const override {
return this->shared_from_this();
}
internal::ComplexMatrix<Fp> get_matrix() const override { return this->_pauli.get_matrix(); }
internal::ComplexMatrix get_matrix() const override { return this->_pauli.get_matrix(); }

void update_quantum_state(StateVector<Fp>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Fp>& states) const override;
void update_quantum_state(StateVector<Prec>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Prec>& states) const override;

std::string to_string(const std::string& indent) const override;

Expand All @@ -37,30 +37,32 @@ class PauliGateImpl : public GateBase<Fp> {
}
};

template <FloatingPoint Fp>
class PauliRotationGateImpl : public GateBase<Fp> {
const PauliOperator<Fp> _pauli;
const Fp _angle;
template <Precision Prec>
class PauliRotationGateImpl : public GateBase<Prec> {
const PauliOperator<Prec> _pauli;
const Float<Prec> _angle;

public:
PauliRotationGateImpl(std::uint64_t control_mask, const PauliOperator<Fp>& pauli, Fp angle)
: GateBase<Fp>(vector_to_mask<false>(pauli.target_qubit_list()), control_mask),
PauliRotationGateImpl(std::uint64_t control_mask,
const PauliOperator<Prec>& pauli,
Float<Prec> angle)
: GateBase<Prec>(vector_to_mask<false>(pauli.target_qubit_list()), control_mask),
_pauli(pauli),
_angle(angle) {}

PauliOperator<Fp> pauli() const { return _pauli; }
PauliOperator<Prec> pauli() const { return _pauli; }
std::vector<std::uint64_t> pauli_id_list() const { return _pauli.pauli_id_list(); }
Fp angle() const { return _angle; }
double angle() const { return _angle; }

std::shared_ptr<const GateBase<Fp>> get_inverse() const override {
return std::make_shared<const PauliRotationGateImpl<Fp>>(
std::shared_ptr<const GateBase<Prec>> get_inverse() const override {
return std::make_shared<const PauliRotationGateImpl<Prec>>(
this->_control_mask, _pauli, -_angle);
}

internal::ComplexMatrix<Fp> get_matrix() const override;
internal::ComplexMatrix get_matrix() const override;

void update_quantum_state(StateVector<Fp>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Fp>& states) const override;
void update_quantum_state(StateVector<Prec>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Prec>& states) const override;

std::string to_string(const std::string& indent) const override;

Expand All @@ -73,10 +75,10 @@ class PauliRotationGateImpl : public GateBase<Fp> {
};
} // namespace internal

template <FloatingPoint Fp>
using PauliGate = internal::GatePtr<internal::PauliGateImpl<Fp>>;
template <FloatingPoint Fp>
using PauliRotationGate = internal::GatePtr<internal::PauliRotationGateImpl<Fp>>;
template <Precision Prec>
using PauliGate = internal::GatePtr<internal::PauliGateImpl<Prec>>;
template <Precision Prec>
using PauliRotationGate = internal::GatePtr<internal::PauliRotationGateImpl<Prec>>;

namespace internal {
#define DECLARE_GET_FROM_JSON_PAULIGATE_WITH_TYPE(Type) \
Expand Down Expand Up @@ -113,15 +115,15 @@ DECLARE_GET_FROM_JSON_PAULIGATE_WITH_TYPE(BF16)

#ifdef SCALUQ_USE_NANOBIND
namespace internal {
template <FloatingPoint Fp>
template <Precision Prec>
void bind_gate_gate_pauli_hpp(nb::module_& m) {
DEF_GATE(PauliGate,
Fp,
Prec,
"Specific class of multi-qubit pauli gate, which applies single-qubit Pauli "
"gate to "
"each of qubit.");
DEF_GATE(PauliRotationGate,
Fp,
Prec,
"Specific class of multi-qubit pauli-rotation gate, represented as "
"$e^{-i\\frac{\\mathrm{angle}}{2}P}$.");
}
Expand Down
30 changes: 15 additions & 15 deletions include/scaluq/gate/gate_probablistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
namespace scaluq {
namespace internal {

template <FloatingPoint Fp>
class ProbablisticGateImpl : public GateBase<Fp> {
template <Precision Prec>
class ProbablisticGateImpl : public GateBase<Prec> {
std::vector<double> _distribution;
std::vector<double> _cumulative_distribution;
std::vector<Gate<Fp>> _gate_list;
std::vector<Gate<Prec>> _gate_list;

public:
ProbablisticGateImpl(const std::vector<double>& distribution,
const std::vector<Gate<Fp>>& gate_list);
const std::vector<Gate<Fp>>& gate_list() const { return _gate_list; }
const std::vector<Gate<Prec>>& gate_list);
const std::vector<Gate<Prec>>& gate_list() const { return _gate_list; }
const std::vector<double>& distribution() const { return _distribution; }

std::vector<std::uint64_t> target_qubit_list() const override {
Expand Down Expand Up @@ -51,15 +51,15 @@ class ProbablisticGateImpl : public GateBase<Fp> {
"ProbablisticGateImpl.");
}

std::shared_ptr<const GateBase<Fp>> get_inverse() const override;
internal::ComplexMatrix<Fp> get_matrix() const override {
std::shared_ptr<const GateBase<Prec>> get_inverse() const override;
internal::ComplexMatrix get_matrix() const override {
throw std::runtime_error(
"ProbablisticGateImpl::get_matrix(): This function must not be used in "
"ProbablisticGateImpl.");
}

void update_quantum_state(StateVector<Fp>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Fp>& states) const override;
void update_quantum_state(StateVector<Prec>& state_vector) const override;
void update_quantum_state(StateVectorBatched<Prec>& states) const override;

std::string to_string(const std::string& indent) const override;

Expand All @@ -71,8 +71,8 @@ class ProbablisticGateImpl : public GateBase<Fp> {
};
} // namespace internal

template <FloatingPoint Fp>
using ProbablisticGate = internal::GatePtr<internal::ProbablisticGateImpl<Fp>>;
template <Precision Prec>
using ProbablisticGate = internal::GatePtr<internal::ProbablisticGateImpl<Prec>>;

namespace internal {

Expand Down Expand Up @@ -102,19 +102,19 @@ DECLARE_GET_FROM_JSON_PROBGATE_WITH_TYPE(BF16)

#ifdef SCALUQ_USE_NANOBIND
namespace internal {
template <FloatingPoint Fp>
template <Precision Prec>
void bind_gate_gate_probablistic(nb::module_& m) {
DEF_GATE(ProbablisticGate,
Fp,
Prec,
"Specific class of probablistic gate. The gate to apply is picked from a cirtain "
"distribution.")
.def(
"gate_list",
[](const ProbablisticGate<Fp>& gate) { return gate->gate_list(); },
[](const ProbablisticGate<Prec>& gate) { return gate->gate_list(); },
nb::rv_policy::reference)
.def(
"distribution",
[](const ProbablisticGate<Fp>& gate) { return gate->distribution(); },
[](const ProbablisticGate<Prec>& gate) { return gate->distribution(); },
nb::rv_policy::reference);
}
} // namespace internal
Expand Down
Loading

0 comments on commit 651958a

Please sign in to comment.