Skip to content

Commit

Permalink
普通のgateはok
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Dec 10, 2024
1 parent bbf16b3 commit f3e8bf9
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 9 deletions.
48 changes: 39 additions & 9 deletions exe/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main() {
scaluq::StateVector<double> state(n_qubits);
state.load({0, 1, 2, 3, 4, 5, 6, 7});
Json j = state;
std::cout << j.dump(2) << std::endl;
std::cout << j << std::endl;
state = j;
std::cout << state << std::endl;
}
Expand All @@ -37,7 +37,7 @@ int main() {
scaluq::StateVectorBatched<double> states(batch_size, n_qubits);
states.set_Haar_random_state(batch_size, n_qubits, false);
Json j = states;
std::cout << j.dump(2) << std::endl;
std::cout << j << std::endl;
states = j;
std::cout << states << std::endl;
}
Expand All @@ -46,7 +46,7 @@ int main() {
std::string pauli_string = "X 0 Z 1 Y 2";
PauliOperator<double> pauli(pauli_string, coef);
Json j = pauli;
std::cout << j.dump(2) << std::endl;
std::cout << j << std::endl;
pauli = j;
}
{
Expand All @@ -55,14 +55,44 @@ int main() {
op.add_operator({0b001, 0b010, Complex<double>(2)});
op.add_operator({"X 2 Y 1", 1});
Json j = op;
std::cout << j.dump(2) << std::endl;
std::cout << j << std::endl;
op = j;
}
// {
// XGate<double> x = gate::X<double>(2);
// Json j = x;
// std::cout << j << std::endl;
// }
{
std::cout << Json(gate::I<double>()) << std::endl;
std::cout << Json(gate::X<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::Y<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::Z<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::H<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::S<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::Sdag<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::T<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::Tdag<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::SqrtX<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::SqrtXdag<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::SqrtY<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::SqrtYdag<double>(2, {0, 3})) << std::endl;
std::cout << Json(gate::RX<double>(2, 0.5, {0, 3})) << std::endl;
std::cout << Json(gate::RY<double>(2, 0.5, {0, 3})) << std::endl;
std::cout << Json(gate::RZ<double>(2, 0.5, {0, 3})) << std::endl;
std::cout << Json(gate::U1<double>(2, 0.5, {0, 3})) << std::endl;
std::cout << Json(gate::U2<double>(2, 0.5, 0.3, {0, 3})) << std::endl;
std::cout << Json(gate::U3<double>(2, 0.5, 0.3, 0.1, {0, 3})) << std::endl;
std::cout << Json(gate::Swap<double>(1, 2, {0, 3})) << std::endl;

PauliOperator<double> pauli("X 2 Y 1");
std::cout << Json(gate::Pauli<double>(pauli)) << std::endl;
std::cout << Json(gate::PauliRotation<double>(pauli, 0.5)) << std::endl;

std::cout << Json(gate::OneTargetMatrix<double>(2, {0, 1, 2, 3})) << std::endl;
std::cout << Json(gate::TwoTargetMatrix<double>(
2, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}))
<< std::endl;

auto probgate =
gate::Probablistic<double>({.1, .9}, {gate::X<double>(0), gate::I<double>()});
std::cout << Json(probgate) << std::endl;
}

Kokkos::finalize();
}
5 changes: 5 additions & 0 deletions include/scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class GateBase : public std::enable_shared_from_this<GateBase<_FloatType>> {
virtual void update_quantum_state(StateVector<Fp>& state_vector) const = 0;

[[nodiscard]] virtual std::string to_string(const std::string& indent = "") const = 0;

virtual void get_info_as_json(Json& j) const { j = Json{{"type", "Unknown"}}; }
};

template <typename T>
Expand Down Expand Up @@ -283,6 +285,9 @@ class GatePtr {
os << gate->to_string();
return os;
}

friend void to_json(Json& j, const GatePtr& gate) { gate->get_info_as_json(j); }
friend void from_json(const Json& j, GatePtr& gate) {}
};

} // namespace internal
Expand Down
35 changes: 35 additions & 0 deletions include/scaluq/gate/gate_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class OneTargetMatrixGateImpl : public GateBase<Fp> {

void update_quantum_state(StateVector<Fp>& state_vector) const override;
std::string to_string(const std::string& indent) const override;

void get_info_as_json(Json& j) const override {
j = Json{{"type", "OneTargetMatrix"},
{"target", this->target_qubit_list()},
{"control", this->control_qubit_list()},
{"matrix",
std::vector<std::vector<Kokkos::complex<Fp>>>{{_matrix[0][0], _matrix[0][1]},
{_matrix[1][0], _matrix[1][1]}}}};
}
};

template <std::floating_point Fp>
Expand Down Expand Up @@ -87,6 +96,18 @@ class TwoTargetMatrixGateImpl : public GateBase<Fp> {
void update_quantum_state(StateVector<Fp>& state_vector) const override;

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

void get_info_as_json(Json& j) const override {
j = Json{{"type", "TwoTargetMatrix"},
{"target", this->target_qubit_list()},
{"control", this->control_qubit_list()},
{"matrix",
std::vector<std::vector<Kokkos::complex<Fp>>>{
{_matrix[0][0], _matrix[0][1], _matrix[0][2], _matrix[0][3]},
{_matrix[1][0], _matrix[1][1], _matrix[1][2], _matrix[1][3]},
{_matrix[2][0], _matrix[2][1], _matrix[2][2], _matrix[2][3]},
{_matrix[3][0], _matrix[3][1], _matrix[3][2], _matrix[3][3]}}}};
}
};

template <std::floating_point Fp>
Expand All @@ -109,6 +130,13 @@ class DenseMatrixGateImpl : public GateBase<Fp> {
void update_quantum_state(StateVector<Fp>& state_vector) const override;

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

void get_info_as_json(Json& j) const override {
j = Json{{"type", "DensetMatrix"},
{"target", this->target_qubit_list()},
{"control", this->control_qubit_list()},
{"matrix", "Not inplemented yet"}};
}
};

template <std::floating_point Fp>
Expand All @@ -132,6 +160,13 @@ class SparseMatrixGateImpl : public GateBase<Fp> {
void update_quantum_state(StateVector<Fp>& state_vector) const override;

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

void get_info_as_json(Json& j) const override {
j = Json{{"type", "SparseMatrix"},
{"target", this->target_qubit_list()},
{"control", this->control_qubit_list()},
{"matrix", "Not inplemented yet"}};
}
};

} // namespace internal
Expand Down
12 changes: 12 additions & 0 deletions include/scaluq/gate/gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class PauliGateImpl : public GateBase<Fp> {
void update_quantum_state(StateVector<Fp>& state_vector) const override;

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

void get_info_as_json(Json& j) const override {
j = Json{
{"type", "Pauli"}, {"control", this->control_qubit_list()}, {"pauli", this->pauli()}};
}
};

template <std::floating_point Fp>
Expand Down Expand Up @@ -56,6 +61,13 @@ class PauliRotationGateImpl : public GateBase<Fp> {
void update_quantum_state(StateVector<Fp>& state_vector) const override;

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

void get_info_as_json(Json& j) const override {
j = Json{{"type", "PauliRotation"},
{"control", this->control_qubit_list()},
{"pauli", this->pauli()},
{"angle", this->angle()}};
}
};
} // namespace internal

Expand Down
6 changes: 6 additions & 0 deletions include/scaluq/gate/gate_probablistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class ProbablisticGateImpl : public GateBase<Fp> {
void update_quantum_state(StateVector<Fp>& state_vector) const override;

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

void get_info_as_json(Json& j) const override {
j = Json{{"type", "ProbablisticGate"},
{"gate_list", this->gate_list()},
{"distribution", this->distribution()}};
}
};
} // namespace internal

Expand Down
Loading

0 comments on commit f3e8bf9

Please sign in to comment.