Skip to content

Commit

Permalink
一旦gateテストok
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Oct 11, 2024
1 parent e4a0582 commit eee5326
Show file tree
Hide file tree
Showing 11 changed files with 689 additions and 649 deletions.
5 changes: 4 additions & 1 deletion exe/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ int main() {
state.load({0, 1, 2, 3, 4, 5, 6, 7});
auto x_gate = scaluq::gate::X<double>(1, {0, 2});
x_gate->update_quantum_state(state);
// auto sqrtx = scaluq::gate::SqrtX<double>(1, {0, 3});
auto sqrtx_gate = scaluq::gate::SqrtX<double>(1, {0});
sqrtx_gate->update_quantum_state(state);
auto sqrtxdag_gate = scaluq::gate::SqrtXdag<double>(0);
sqrtxdag_gate->update_quantum_state(state);

std::cout << state << std::endl;
}
Expand Down
15 changes: 4 additions & 11 deletions scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,7 @@ class GateBase : public std::enable_shared_from_this<GateBase<_FloatType>> {
};

template <typename T>
concept GateImpl = std::derived_from<T, internal::GateBase<typename T::FloatType>>;

template <GateImpl T>
class GatePtr;

} // namespace internal

template <std::floating_point FloatType>
using Gate = internal::GatePtr<internal::GateBase<FloatType>>;

namespace internal {
concept GateImpl = std::derived_from<T, GateBase<typename T::FloatType>>;

template <GateImpl T>
class GatePtr {
Expand Down Expand Up @@ -316,6 +306,9 @@ class GatePtr {

} // namespace internal

template <std::floating_point FloatType>
using Gate = internal::GatePtr<internal::GateBase<FloatType>>;

#ifdef SCALUQ_USE_NANOBIND
namespace internal {
#define DEF_GATE_BASE(GATE_TYPE, DESCRIPTION) \
Expand Down
12 changes: 6 additions & 6 deletions scaluq/gate/gate_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OneTargetMatrixGateImpl : public GateBase<FloatType> {
return {_matrix[0][0], _matrix[0][1], _matrix[1][0], _matrix[1][1]};
}

Gate<FloatType> get_inverse() const override {
std::shared_ptr<const GateBase<FloatType>> get_inverse() const override {
return std::make_shared<const OneTargetMatrixGateImpl>(
this->_target_mask,
this->_control_mask,
Expand All @@ -46,14 +46,14 @@ class OneTargetMatrixGateImpl : public GateBase<FloatType> {
}

void update_quantum_state(StateVector<FloatType>& state_vector) const override {
check_qubit_mask_within_bounds(state_vector);
this->check_qubit_mask_within_bounds(state_vector);
one_target_dense_matrix_gate(
this->_target_mask, this->_control_mask, _matrix, state_vector);
}

std::string to_string(const std::string& indent) const override {
std::ostringstream ss;
ss << indent << "Gate<FloatType> Type: OneTargetMatrix\n";
ss << indent << "std::shared_ptr<const GateBase<FloatType>> Type: OneTargetMatrix\n";
ss << this->get_qubit_info_as_string(indent);
return ss.str();
}
Expand Down Expand Up @@ -85,7 +85,7 @@ class TwoTargetMatrixGateImpl : public GateBase<FloatType> {
return matrix;
}

Gate<FloatType> get_inverse() const override {
std::shared_ptr<const GateBase<FloatType>> get_inverse() const override {
std::array<std::array<Complex, 4>, 4> matrix_dag;
for (std::uint64_t i : std::views::iota(0, 4)) {
for (std::uint64_t j : std::views::iota(0, 4)) {
Expand All @@ -105,14 +105,14 @@ class TwoTargetMatrixGateImpl : public GateBase<FloatType> {
}

void update_quantum_state(StateVector<FloatType>& state_vector) const override {
check_qubit_mask_within_bounds(state_vector);
this->check_qubit_mask_within_bounds(state_vector);
two_target_dense_matrix_gate(
this->_target_mask, this->_control_mask, _matrix, state_vector);
}

std::string to_string(const std::string& indent) const override {
std::ostringstream ss;
ss << indent << "Gate<FloatType> Type: TwoTargetMatrix\n";
ss << indent << "std::shared_ptr<const GateBase<FloatType>> Type: TwoTargetMatrix\n";
ss << this->get_qubit_info_as_string(indent);
return ss.str();
}
Expand Down
6 changes: 4 additions & 2 deletions scaluq/gate/gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class PauliGateImpl : public GateBase<FloatType> {
PauliOperator<FloatType> pauli() const { return _pauli; };
std::vector<std::uint64_t> pauli_id_list() const { return _pauli.pauli_id_list(); }

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

void update_quantum_state(StateVector<FloatType>& state_vector) const override {
Expand Down Expand Up @@ -61,7 +63,7 @@ class PauliRotationGateImpl : public GateBase<FloatType> {
std::vector<std::uint64_t> pauli_id_list() const { return _pauli.pauli_id_list(); }
FloatType angle() const { return _angle; }

Gate<FloatType> get_inverse() const override {
std::shared_ptr<const GateBase<FloatType>> get_inverse() const override {
return std::make_shared<const PauliRotationGateImpl<FloatType>>(
this->_control_mask, _pauli, -_angle);
}
Expand Down
16 changes: 10 additions & 6 deletions scaluq/gate/gate_probablistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ template <std::floating_point FloatType>
class ProbablisticGateImpl : public GateBase<FloatType> {
std::vector<FloatType> _distribution;
std::vector<FloatType> _cumlative_distribution;
std::vector<Gate<FloatType>> _gate_list;
std::vector<std::shared_ptr<const GateBase<FloatType>>> _gate_list;

public:
ProbablisticGateImpl(const std::vector<FloatType>& distribution,
const std::vector<Gate<FloatType>>& gate_list)
const std::vector<std::shared_ptr<const GateBase<FloatType>>>& gate_list)
: GateBase<FloatType>(0, 0), _distribution(distribution), _gate_list(gate_list) {
std::uint64_t n = distribution.size();
if (n == 0) {
Expand All @@ -30,7 +30,9 @@ class ProbablisticGateImpl : public GateBase<FloatType> {
throw std::runtime_error("Sum of distribution must be equal to 1.");
}
}
const std::vector<Gate<FloatType>>& gate_list() const { return _gate_list; }
const std::vector<std::shared_ptr<const GateBase<FloatType>>>& gate_list() const {
return _gate_list;
}
const std::vector<FloatType>& distribution() const { return _distribution; }

std::vector<std::uint64_t> target_qubit_list() const override {
Expand Down Expand Up @@ -64,12 +66,14 @@ class ProbablisticGateImpl : public GateBase<FloatType> {
"ProbablisticGateImpl.");
}

Gate<FloatType> get_inverse() const override {
std::vector<Gate<FloatType>> inv_gate_list;
std::shared_ptr<const GateBase<FloatType>> get_inverse() const override {
std::vector<std::shared_ptr<const GateBase<FloatType>>> inv_gate_list;
inv_gate_list.reserve(_gate_list.size());
std::ranges::transform(_gate_list,
std::back_inserter(inv_gate_list),
[](const Gate<FloatType>& gate) { return gate->get_inverse(); });
[](const std::shared_ptr<const GateBase<FloatType>>& gate) {
return gate->get_inverse();
});
return std::make_shared<const ProbablisticGateImpl>(_distribution, inv_gate_list);
}
internal::ComplexMatrix get_matrix() const override {
Expand Down
Loading

0 comments on commit eee5326

Please sign in to comment.