Skip to content

Commit

Permalink
make types.hpp internal
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Aug 30, 2024
1 parent c91971c commit 2abdc61
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 92 deletions.
2 changes: 1 addition & 1 deletion scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class GateBase : public std::enable_shared_from_this<GateBase> {
}

[[nodiscard]] virtual Gate get_inverse() const = 0;
[[nodiscard]] virtual ComplexMatrix get_matrix() const = 0;
[[nodiscard]] virtual internal::ComplexMatrix get_matrix() const = 0;

virtual void update_quantum_state(StateVector& state_vector) const = 0;
};
Expand Down
11 changes: 5 additions & 6 deletions scaluq/gate/gate_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,15 @@ inline Gate PauliRotation(const PauliOperator& pauli,
internal::vector_to_mask(controls), pauli, angle);
}
inline Gate DenseMatrix(const std::vector<std::uint64_t>& targets,
const ComplexMatrix& matrix,
const internal::ComplexMatrix& matrix,
const std::vector<std::uint64_t>& controls = {}) {
std::uint64_t nqubits = targets.size();
std::uint64_t dim = 1ULL << nqubits;
if (static_cast<std::uint64_t>(matrix.rows()) != dim ||
static_cast<std::uint64_t>(matrix.cols()) != dim) {
throw std::runtime_error(
"gate::DenseMatrix(const std::vector<std::uint64_t>&, const ComplexMatrix&): matrix "
"size "
"must "
"be 2^{n_qubits} x 2^{n_qubits}.");
"gate::DenseMatrix(const std::vector<std::uint64_t>&, const internal::ComplexMatrix&): "
"matrix size must be 2^{n_qubits} x 2^{n_qubits}.");
}
if (targets.size() == 0) return I();
if (targets.size() == 1) {
Expand Down Expand Up @@ -204,7 +202,8 @@ inline Gate DenseMatrix(const std::vector<std::uint64_t>& targets,
controls);
}
throw std::runtime_error(
"gate::DenseMatrix(const std::vector<std::uint64_t>&, const ComplexMatrix&): DenseMatrix "
"gate::DenseMatrix(const std::vector<std::uint64_t>&, const internal::ComplexMatrix&): "
"DenseMatrix "
"gate "
"more "
"than two qubits is not implemented yet.");
Expand Down
8 changes: 4 additions & 4 deletions scaluq/gate/gate_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class OneTargetMatrixGateImpl : public GateBase {
Kokkos::conj(_matrix[0][1]),
Kokkos::conj(_matrix[1][1])});
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << this->_matrix[0][0], this->_matrix[0][1], this->_matrix[1][0], this->_matrix[1][1];
return mat;
}
Expand Down Expand Up @@ -84,8 +84,8 @@ class TwoTargetMatrixGateImpl : public GateBase {
return std::make_shared<const TwoTargetMatrixGateImpl>(
_target_mask, _control_mask, matrix_dag);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(4, 4);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(4, 4);
mat << this->_matrix[0][0], this->_matrix[0][1], this->_matrix[0][2], this->_matrix[0][3],
this->_matrix[1][0], this->_matrix[1][1], this->_matrix[1][2], this->_matrix[1][3],
this->_matrix[2][0], this->_matrix[2][1], this->_matrix[2][2], this->_matrix[2][3],
Expand Down
8 changes: 4 additions & 4 deletions scaluq/gate/gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PauliGateImpl : public GateBase {
std::vector<std::uint64_t> get_pauli_id_list() const { return _pauli.get_pauli_id_list(); }

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix 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& state_vector) const override {
pauli_gate(_control_mask, _pauli, state_vector);
Expand All @@ -45,12 +45,12 @@ class PauliRotationGateImpl : public GateBase {
return std::make_shared<const PauliRotationGateImpl>(_control_mask, _pauli, -_angle);
}

ComplexMatrix get_matrix() const override {
ComplexMatrix mat = this->_pauli.get_matrix_ignoring_coef();
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat = this->_pauli.get_matrix_ignoring_coef();
Complex true_angle = _angle * _pauli.get_coef();
StdComplex imag_unit(0, 1);
mat = (StdComplex)Kokkos::cos(-true_angle / 2) *
ComplexMatrix::Identity(mat.rows(), mat.cols()) +
internal::ComplexMatrix::Identity(mat.rows(), mat.cols()) +
imag_unit * (StdComplex)Kokkos::sin(-true_angle / 2) * mat;
return mat;
}
Expand Down
2 changes: 1 addition & 1 deletion scaluq/gate/gate_probablistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ProbablisticGateImpl : public GateBase {
});
return std::make_shared<const ProbablisticGateImpl>(_distribution, inv_gate_list);
}
ComplexMatrix get_matrix() const override {
internal::ComplexMatrix get_matrix() const override {
throw std::runtime_error(
"ProbablisticGateImpl::get_matrix(): This function must not be used in "
"ProbablisticGateImpl.");
Expand Down
92 changes: 47 additions & 45 deletions scaluq/gate/gate_standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class IGateImpl : public GateBase {
IGateImpl() : GateBase(0, 0) {}

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix get_matrix() const override { return ComplexMatrix::Identity(1, 1); }
internal::ComplexMatrix get_matrix() const override {
return internal::ComplexMatrix::Identity(1, 1);
}

void update_quantum_state(StateVector& state_vector) const override {
i_gate(_target_mask, _control_mask, state_vector);
Expand All @@ -31,8 +33,8 @@ class GlobalPhaseGateImpl : public GateBase {
Gate get_inverse() const override {
return std::make_shared<const GlobalPhaseGateImpl>(_control_mask, -_phase);
}
ComplexMatrix get_matrix() const override {
return ComplexMatrix::Identity(1, 1) * std::exp(std::complex<double>(0, _phase));
internal::ComplexMatrix get_matrix() const override {
return internal::ComplexMatrix::Identity(1, 1) * std::exp(std::complex<double>(0, _phase));
}

void update_quantum_state(StateVector& state_vector) const override {
Expand All @@ -57,8 +59,8 @@ class XGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0, 1, 1, 0;
return mat;
}
Expand All @@ -74,8 +76,8 @@ class YGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0, -1i, 1i, 0;
return mat;
}
Expand All @@ -91,8 +93,8 @@ class ZGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, -1;
return mat;
}
Expand All @@ -108,8 +110,8 @@ class HGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 1, 1, -1;
mat /= std::sqrt(2);
return mat;
Expand All @@ -135,8 +137,8 @@ class SGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override;
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, 1i;
return mat;
}
Expand All @@ -154,8 +156,8 @@ class SdagGateImpl : public GateBase {
Gate get_inverse() const override {
return std::make_shared<const SGateImpl>(_target_mask, _control_mask);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, -1i;
return mat;
}
Expand All @@ -175,8 +177,8 @@ class TGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override;
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, (1. + 1i) / std::sqrt(2);
return mat;
}
Expand All @@ -194,8 +196,8 @@ class TdagGateImpl : public GateBase {
Gate get_inverse() const override {
return std::make_shared<const TGateImpl>(_target_mask, _control_mask);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, (1. - 1.i) / std::sqrt(2);
return mat;
}
Expand All @@ -215,8 +217,8 @@ class SqrtXGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override;
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0.5 + 0.5i, 0.5 - 0.5i, 0.5 - 0.5i, 0.5 + 0.5i;
return mat;
}
Expand All @@ -234,8 +236,8 @@ class SqrtXdagGateImpl : public GateBase {
Gate get_inverse() const override {
return std::make_shared<const SqrtXGateImpl>(_target_mask, _control_mask);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0.5 - 0.5i, 0.5 + 0.5i, 0.5 + 0.5i, 0.5 - 0.5i;
return mat;
}
Expand All @@ -255,8 +257,8 @@ class SqrtYGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override;
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0.5 + 0.5i, -0.5 - 0.5i, 0.5 + 0.5i, 0.5 + 0.5i;
return mat;
}
Expand All @@ -274,8 +276,8 @@ class SqrtYdagGateImpl : public GateBase {
Gate get_inverse() const override {
return std::make_shared<const SqrtYGateImpl>(_target_mask, _control_mask);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0.5 - 0.5i, 0.5 - 0.5i, -0.5 + 0.5i, 0.5 - 0.5i;
return mat;
}
Expand All @@ -297,8 +299,8 @@ class P0GateImpl : public GateBase {
Gate get_inverse() const override {
throw std::runtime_error("P0::get_inverse: Projection gate doesn't have inverse gate");
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, 0;
return mat;
}
Expand All @@ -316,8 +318,8 @@ class P1GateImpl : public GateBase {
Gate get_inverse() const override {
throw std::runtime_error("P1::get_inverse: Projection gate doesn't have inverse gate");
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 0, 0, 0, 1;
return mat;
}
Expand All @@ -335,8 +337,8 @@ class RXGateImpl : public RotationGateBase {
Gate get_inverse() const override {
return std::make_shared<const RXGateImpl>(_target_mask, _control_mask, -_angle);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << std::cos(_angle / 2), -1i * std::sin(_angle / 2), -1i * std::sin(_angle / 2),
std::cos(_angle / 2);
return mat;
Expand All @@ -355,8 +357,8 @@ class RYGateImpl : public RotationGateBase {
Gate get_inverse() const override {
return std::make_shared<const RYGateImpl>(_target_mask, _control_mask, -_angle);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << std::cos(_angle / 2), -std::sin(_angle / 2), std::sin(_angle / 2),
std::cos(_angle / 2);
return mat;
Expand All @@ -375,8 +377,8 @@ class RZGateImpl : public RotationGateBase {
Gate get_inverse() const override {
return std::make_shared<const RZGateImpl>(_target_mask, _control_mask, -_angle);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << std::exp(-0.5i * _angle), 0, 0, std::exp(0.5i * _angle);
return mat;
}
Expand All @@ -399,8 +401,8 @@ class U1GateImpl : public GateBase {
Gate get_inverse() const override {
return std::make_shared<const U1GateImpl>(_target_mask, _control_mask, -_lambda);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << 1, 0, 0, std::exp(1i * _lambda);
return mat;
}
Expand All @@ -426,8 +428,8 @@ class U2GateImpl : public GateBase {
-_lambda - Kokkos::numbers::pi,
-_phi + Kokkos::numbers::pi);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << std::cos(Kokkos::numbers::pi / 4.),
-std::exp(1i * _lambda) * std::sin(Kokkos::numbers::pi / 4.),
std::exp(1i * _phi) * std::sin(Kokkos::numbers::pi / 4.),
Expand Down Expand Up @@ -460,8 +462,8 @@ class U3GateImpl : public GateBase {
return std::make_shared<const U3GateImpl>(
_target_mask, _control_mask, -_theta, -_lambda, -_phi);
}
ComplexMatrix get_matrix() const override {
ComplexMatrix mat(2, 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat(2, 2);
mat << std::cos(_theta / 2.), -std::exp(1i * _lambda) * std::sin(_theta / 2.),
std::exp(1i * _phi) * std::sin(_theta / 2.),
std::exp(1i * _phi) * std::exp(1i * _lambda) * std::cos(_theta / 2.);
Expand All @@ -479,8 +481,8 @@ class SwapGateImpl : public GateBase {
using GateBase::GateBase;

Gate get_inverse() const override { return shared_from_this(); }
ComplexMatrix get_matrix() const override {
ComplexMatrix mat = ComplexMatrix::Identity(1 << 2, 1 << 2);
internal::ComplexMatrix get_matrix() const override {
internal::ComplexMatrix mat = internal::ComplexMatrix::Identity(1 << 2, 1 << 2);
mat << 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1;
return mat;
}
Expand Down
2 changes: 1 addition & 1 deletion scaluq/gate/param_gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ParamGateBase {
}

[[nodiscard]] virtual ParamGate get_inverse() const = 0;
[[nodiscard]] virtual ComplexMatrix get_matrix(double param) const = 0;
[[nodiscard]] virtual internal::ComplexMatrix get_matrix(double param) const = 0;

virtual void update_quantum_state(StateVector& state_vector, double param) const = 0;
};
Expand Down
6 changes: 3 additions & 3 deletions scaluq/gate/param_gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class PPauliRotationGateImpl : public ParamGateBase {
ParamGate get_inverse() const override {
return std::make_shared<const PPauliRotationGateImpl>(_control_mask, _pauli, -_pcoef);
}
ComplexMatrix get_matrix(double param) const override {
internal::ComplexMatrix get_matrix(double param) const override {
double angle = _pcoef * param;
Complex true_angle = angle * this->_pauli.get_coef();
ComplexMatrix mat = this->_pauli.get_matrix_ignoring_coef();
internal::ComplexMatrix mat = this->_pauli.get_matrix_ignoring_coef();
StdComplex imag_unit(0, 1);
mat = (StdComplex)Kokkos::cos(-true_angle / 2) *
ComplexMatrix::Identity(mat.rows(), mat.cols()) +
internal::ComplexMatrix::Identity(mat.rows(), mat.cols()) +
imag_unit * (StdComplex)Kokkos::sin(-true_angle / 2) * mat;
return mat;
}
Expand Down
2 changes: 1 addition & 1 deletion scaluq/gate/param_gate_probablistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PProbablisticGateImpl : public ParamGateBase {
});
return std::make_shared<const PProbablisticGateImpl>(_distribution, inv_gate_list);
}
ComplexMatrix get_matrix(double) const override {
internal::ComplexMatrix get_matrix(double) const override {
throw std::runtime_error(
"PProbablisticGateImpl::get_matrix(): This function must not be used in "
"PProbablisticGateImpl.");
Expand Down
Loading

0 comments on commit 2abdc61

Please sign in to comment.