Skip to content

Commit

Permalink
一部のget_from_jsonを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Dec 13, 2024
1 parent 79b8c62 commit a7d0644
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 7 deletions.
15 changes: 12 additions & 3 deletions exe/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,18 @@ int main() {
std::cout << Json(paramprobgate) << std::endl;
}
{
Gate<double> g;
XGate<double> x;
g = x;
auto x = gate::X<double>(1, {2});
Json j = x;
std::cout << j << std::endl;
Gate<double> gate = j;
std::cout << gate << std::endl;
}
{
auto x = gate::RX<double>(1, 0.5, {2});
Json j = x;
std::cout << j << std::endl;
Gate<double> gate = j;
std::cout << gate << std::endl;
}

Kokkos::finalize();
Expand Down
35 changes: 31 additions & 4 deletions include/scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ class GateBase : public std::enable_shared_from_this<GateBase<_FloatType>> {
template <typename T>
concept GateImpl = std::derived_from<T, GateBase<typename T::Fp>>;

template <GateImpl T>
inline std::shared_ptr<const T> get_from_json(const Json&);

template <GateImpl T>
class GatePtr {
friend class GateFactory;
Expand Down Expand Up @@ -289,10 +292,34 @@ class GatePtr {
friend void to_json(Json& j, const GatePtr& gate) { gate->get_as_json(j); }
friend void from_json(const Json& j, GatePtr& gate) {
std::string type = j.at("type");
if (type == "X") {
auto target = j.at("target");
auto control = j.at("control");
gate = std::shared_ptr<const XGateImpl>();
if (type == "I") {
gate = get_from_json<IGateImpl<Fp>>(j);
} else if (type == "GlobalPhase") {
gate = get_from_json<GlobalPhaseGateImpl<Fp>>(j);
} else if (type == "X") {
gate = get_from_json<XGateImpl<Fp>>(j);
} else if (type == "Y") {
gate = get_from_json<YGateImpl<Fp>>(j);
} else if (type == "Z") {
gate = get_from_json<ZGateImpl<Fp>>(j);
} else if (type == "H") {
gate = get_from_json<HGateImpl<Fp>>(j);
} else if (type == "S") {
gate = get_from_json<SGateImpl<Fp>>(j);
} else if (type == "Sdag") {
gate = get_from_json<SdagGateImpl<Fp>>(j);
} else if (type == "T") {
gate = get_from_json<TGateImpl<Fp>>(j);
} else if (type == "Tdag") {
gate = get_from_json<TdagGateImpl<Fp>>(j);
}

else if (type == "RX") {
gate = get_from_json<RXGateImpl<Fp>>(j);
} else if (type == "RY") {
gate = get_from_json<RYGateImpl<Fp>>(j);
} else if (type == "RZ") {
gate = get_from_json<RZGateImpl<Fp>>(j);
}
}
};
Expand Down
125 changes: 125 additions & 0 deletions include/scaluq/gate/gate_standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,131 @@ class SwapGateImpl : public GateBase<Fp> {

} // namespace internal

template <std::floating_point Fp>
using IGate = internal::GatePtr<internal::IGateImpl<Fp>>;
template <std::floating_point Fp>
using GlobalPhaseGate = internal::GatePtr<internal::GlobalPhaseGateImpl<Fp>>;
template <std::floating_point Fp>
using XGate = internal::GatePtr<internal::XGateImpl<Fp>>;
template <std::floating_point Fp>
using YGate = internal::GatePtr<internal::YGateImpl<Fp>>;
template <std::floating_point Fp>
using ZGate = internal::GatePtr<internal::ZGateImpl<Fp>>;
template <std::floating_point Fp>
using HGate = internal::GatePtr<internal::HGateImpl<Fp>>;
template <std::floating_point Fp>
using SGate = internal::GatePtr<internal::SGateImpl<Fp>>;
template <std::floating_point Fp>
using SdagGate = internal::GatePtr<internal::SdagGateImpl<Fp>>;
template <std::floating_point Fp>
using TGate = internal::GatePtr<internal::TGateImpl<Fp>>;
template <std::floating_point Fp>
using TdagGate = internal::GatePtr<internal::TdagGateImpl<Fp>>;
template <std::floating_point Fp>
using SqrtXGate = internal::GatePtr<internal::SqrtXGateImpl<Fp>>;
template <std::floating_point Fp>
using SqrtXdagGate = internal::GatePtr<internal::SqrtXdagGateImpl<Fp>>;
template <std::floating_point Fp>
using SqrtYGate = internal::GatePtr<internal::SqrtYGateImpl<Fp>>;
template <std::floating_point Fp>
using SqrtYdagGate = internal::GatePtr<internal::SqrtYdagGateImpl<Fp>>;
template <std::floating_point Fp>
using P0Gate = internal::GatePtr<internal::P0GateImpl<Fp>>;
template <std::floating_point Fp>
using P1Gate = internal::GatePtr<internal::P1GateImpl<Fp>>;
template <std::floating_point Fp>
using RXGate = internal::GatePtr<internal::RXGateImpl<Fp>>;
template <std::floating_point Fp>
using RYGate = internal::GatePtr<internal::RYGateImpl<Fp>>;
template <std::floating_point Fp>
using RZGate = internal::GatePtr<internal::RZGateImpl<Fp>>;
template <std::floating_point Fp>
using U1Gate = internal::GatePtr<internal::U1GateImpl<Fp>>;
template <std::floating_point Fp>
using U2Gate = internal::GatePtr<internal::U2GateImpl<Fp>>;
template <std::floating_point Fp>
using U3Gate = internal::GatePtr<internal::U3GateImpl<Fp>>;
template <std::floating_point Fp>
using SwapGate = internal::GatePtr<internal::SwapGateImpl<Fp>>;

namespace internal { // for json implemention
template <>
inline std::shared_ptr<const IGateImpl<double>> get_from_json(const Json&) {
return std::make_shared<const IGateImpl<double>>();
}
template <>
inline std::shared_ptr<const IGateImpl<float>> get_from_json(const Json&) {
return std::make_shared<const IGateImpl<float>>();
}

template <>
inline std::shared_ptr<const GlobalPhaseGateImpl<double>> get_from_json(const Json& j) {
auto controls = j.at("control").get<std::vector<std::uint64_t>>();
double phase = j.at("phase").get<double>();
return std::make_shared<const GlobalPhaseGateImpl<double>>(vector_to_mask(controls), phase);
}
template <>
inline std::shared_ptr<const GlobalPhaseGateImpl<float>> get_from_json(const Json& j) {
auto controls = j.at("control").get<std::vector<std::uint64_t>>();
float phase = j.at("phase").get<float>();
return std::make_shared<const GlobalPhaseGateImpl<float>>(vector_to_mask(controls), phase);
}

#define DECLARE_GET_FROM_JSON(Impl) \
template <> \
inline std::shared_ptr<const Impl<double>> get_from_json(const Json& j) { \
auto targets = j.at("target").get<std::vector<std::uint64_t>>(); \
auto controls = j.at("control").get<std::vector<std::uint64_t>>(); \
return std::make_shared<const Impl<double>>(vector_to_mask(targets), \
vector_to_mask(controls)); \
} \
template <> \
inline std::shared_ptr<const Impl<float>> get_from_json(const Json& j) { \
auto targets = j.at("target").get<std::vector<std::uint64_t>>(); \
auto controls = j.at("control").get<std::vector<std::uint64_t>>(); \
return std::make_shared<const Impl<float>>(vector_to_mask(targets), \
vector_to_mask(controls)); \
}

DECLARE_GET_FROM_JSON(XGateImpl);
DECLARE_GET_FROM_JSON(YGateImpl);
DECLARE_GET_FROM_JSON(ZGateImpl);
DECLARE_GET_FROM_JSON(HGateImpl);
DECLARE_GET_FROM_JSON(SGateImpl);
DECLARE_GET_FROM_JSON(SdagGateImpl);
DECLARE_GET_FROM_JSON(TGateImpl);
DECLARE_GET_FROM_JSON(TdagGateImpl);
DECLARE_GET_FROM_JSON(SqrtXGateImpl);
DECLARE_GET_FROM_JSON(SqrtXdagGateImpl);
DECLARE_GET_FROM_JSON(SqrtYGateImpl);
DECLARE_GET_FROM_JSON(SqrtYdagGateImpl);
DECLARE_GET_FROM_JSON(P0GateImpl);
DECLARE_GET_FROM_JSON(P1GateImpl);

#define DECLARE_GET_FROM_JSON_RGATE(Impl) \
template <> \
inline std::shared_ptr<const Impl<double>> get_from_json(const Json& j) { \
auto targets = j.at("target").get<std::vector<std::uint64_t>>(); \
auto controls = j.at("control").get<std::vector<std::uint64_t>>(); \
double angle = j.at("angle").get<double>(); \
return std::make_shared<const Impl<double>>( \
vector_to_mask(targets), vector_to_mask(controls), angle); \
} \
template <> \
inline std::shared_ptr<const Impl<float>> get_from_json(const Json& j) { \
auto targets = j.at("target").get<std::vector<std::uint64_t>>(); \
auto controls = j.at("control").get<std::vector<std::uint64_t>>(); \
float angle = j.at("angle").get<float>(); \
return std::make_shared<const Impl<float>>( \
vector_to_mask(targets), vector_to_mask(controls), angle); \
}

DECLARE_GET_FROM_JSON_RGATE(RXGateImpl);
DECLARE_GET_FROM_JSON_RGATE(RYGateImpl);
DECLARE_GET_FROM_JSON_RGATE(RZGateImpl);

} // namespace internal

#ifdef SCALUQ_USE_NANOBIND
namespace internal {
void bind_gate_gate_standard_hpp(nb::module_& m) {
Expand Down

0 comments on commit a7d0644

Please sign in to comment.