Skip to content

Commit

Permalink
float deduction ok
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Oct 22, 2024
1 parent 3953c6c commit b9ef075
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 110 deletions.
2 changes: 1 addition & 1 deletion scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ class GatePtr {
friend class GateFactory;
template <GateImpl U>
friend class GatePtr;
using Fp = typename T::Fp;

private:
std::shared_ptr<const T> _gate_ptr;
GateType _gate_type;

public:
using Fp = typename T::Fp;
GatePtr() : _gate_ptr(nullptr), _gate_type(get_gate_type<T>()) {}
template <GateImpl U>
GatePtr(const std::shared_ptr<const U>& gate_ptr) {
Expand Down
15 changes: 7 additions & 8 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 Fp>
class ProbablisticGateImpl : public GateBase<Fp> {
std::vector<Fp> _distribution;
std::vector<Fp> _cumlative_distribution;
std::vector<std::shared_ptr<const GateBase<Fp>>> _gate_list;
std::vector<Gate<Fp>> _gate_list;

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

std::vector<std::uint64_t> target_qubit_list() const override {
Expand Down Expand Up @@ -65,12 +65,11 @@ class ProbablisticGateImpl : public GateBase<Fp> {
}

std::shared_ptr<const GateBase<Fp>> get_inverse() const override {
std::vector<std::shared_ptr<const GateBase<Fp>>> inv_gate_list;
std::vector<Gate<Fp>> inv_gate_list;
inv_gate_list.reserve(_gate_list.size());
std::ranges::transform(
_gate_list,
std::back_inserter(inv_gate_list),
[](const std::shared_ptr<const GateBase<Fp>>& gate) { return gate->get_inverse(); });
std::ranges::transform(_gate_list,
std::back_inserter(inv_gate_list),
[](const Gate<Fp>& gate) { return gate->get_inverse(); });
return std::make_shared<const ProbablisticGateImpl>(_distribution, inv_gate_list);
}
internal::ComplexMatrix<Fp> get_matrix() const override {
Expand Down
31 changes: 18 additions & 13 deletions scaluq/gate/gate_standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class YGateImpl : public GateBase<Fp> {
}
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> mat(2, 2);
mat << 0, -1i, 1i, 0;
mat << 0, Complex<Fp>(0, -1), Complex<Fp>(0, 1), 0;
return mat;
}

Expand Down Expand Up @@ -378,7 +378,8 @@ class SqrtYGateImpl : public GateBase<Fp> {

internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> mat(2, 2);
mat << 0.5 + 0.5i, -0.5 - 0.5i, 0.5 + 0.5i, 0.5 + 0.5i;
mat << StdComplex<Fp>(0.5, 0.5), StdComplex<Fp>(-0.5, -0.5), StdComplex<Fp>(0.5, 0.5),
StdComplex<Fp>(0.5, 0.5);
return mat;
}

Expand All @@ -405,7 +406,8 @@ class SqrtYdagGateImpl : public GateBase<Fp> {
}
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> mat(2, 2);
mat << 0.5 - 0.5i, 0.5 - 0.5i, -0.5 + 0.5i, 0.5 - 0.5i;
mat << StdComplex<Fp>(0.5, -0.5), StdComplex<Fp>(0.5, -0.5), StdComplex<Fp>(-0.5, 0.5),
StdComplex<Fp>(0.5, -0.5);
return mat;
}

Expand Down Expand Up @@ -487,8 +489,8 @@ class RXGateImpl : public RotationGateBase<Fp> {
}
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> mat(2, 2);
mat << std::cos(this->_angle / 2), -1i * std::sin(this->_angle / 2),
-1i * std::sin(this->_angle / 2), std::cos(this->_angle / 2);
mat << std::cos(this->_angle / 2), Complex<Fp>(0, -std::sin(this->_angle / 2)),
Complex<Fp>(0, std::sin(this->_angle / 2)), std::cos(this->_angle / 2);
return mat;
}

Expand Down Expand Up @@ -547,7 +549,8 @@ class RZGateImpl : public RotationGateBase<Fp> {
}
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> mat(2, 2);
mat << std::exp(-0.5i * this->_angle), 0, 0, std::exp(0.5i * this->_angle);
mat << std::exp(StdComplex<Fp>(0, -0.5 * this->_angle)), 0, 0,
std::exp(StdComplex<Fp>(0, 0.5 * this->_angle));
return mat;
}

Expand Down Expand Up @@ -581,7 +584,7 @@ class U1GateImpl : public GateBase<Fp> {
}
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> mat(2, 2);
mat << 1, 0, 0, std::exp(1i * _lambda);
mat << 1, 0, 0, std::exp(StdComplex<Fp>(0, _lambda));
return mat;
}

Expand Down Expand Up @@ -617,9 +620,10 @@ class U2GateImpl : public GateBase<Fp> {
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> 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.),
std::exp(1i * _phi) * std::exp(1i * _lambda) * std::cos(Kokkos::numbers::pi / 4.);
-std::exp(StdComplex<Fp>(0, _lambda)) * std::sin((Fp)Kokkos::numbers::pi / 4),
std::exp(StdComplex<Fp>(0, _phi)) * std::sin((Fp)Kokkos::numbers::pi / 4),
std::exp(StdComplex<Fp>(0, _phi)) * std::exp(StdComplex<Fp>(0, _lambda)) *
std::cos((Fp)Kokkos::numbers::pi / 4);
return mat;
}

Expand Down Expand Up @@ -654,9 +658,10 @@ class U3GateImpl : public GateBase<Fp> {
}
internal::ComplexMatrix<Fp> get_matrix() const override {
internal::ComplexMatrix<Fp> 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.);
mat << std::cos(_theta / 2.), -std::exp(StdComplex<Fp>(0, _lambda)) * std::sin(_theta / 2),
std::exp(StdComplex<Fp>(0, _phi)) * std::sin(_theta / 2),
std::exp(StdComplex<Fp>(0, _phi)) * std::exp(StdComplex<Fp>(0, _lambda)) *
std::cos(_theta / 2);
return mat;
}

Expand Down
14 changes: 8 additions & 6 deletions scaluq/gate/update_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ inline void one_target_diagonal_matrix_gate(std::uint64_t target_mask,
}

template <std::floating_point Fp>
inline void i_gate(std::uint64_t target_mask, std::uint64_t control_mask, StateVector<Fp>& state) {}
inline void i_gate(std::uint64_t, std::uint64_t, StateVector<Fp>&) {}

template <std::floating_point Fp>
inline void global_phase_gate(std::uint64_t target_mask,
inline void global_phase_gate(std::uint64_t,
std::uint64_t control_mask,
Fp angle,
StateVector<Fp>& state) {
Complex<Fp> coef = Kokkos::polar(1., angle);
Complex<Fp> coef = Kokkos::polar<Fp>(1., angle);
Kokkos::parallel_for(
state.dim() >> std::popcount(control_mask), KOKKOS_LAMBDA(std::uint64_t i) {
state._raw[insert_zero_at_mask_positions(i, control_mask) | control_mask] *= coef;
Expand Down Expand Up @@ -250,7 +250,7 @@ inline void rz_gate(std::uint64_t target_mask,
StateVector<Fp>& state) {
const Fp cosval = std::cos(angle / 2.);
const Fp sinval = std::sin(angle / 2.);
DiagonalMatrix2x2<double> diag = {Complex<Fp>(cosval, -sinval), Complex<Fp>(cosval, sinval)};
DiagonalMatrix2x2<Fp> diag = {Complex<Fp>(cosval, -sinval), Complex<Fp>(cosval, sinval)};
one_target_diagonal_matrix_gate(target_mask, control_mask, diag, state);
}

Expand All @@ -277,8 +277,10 @@ inline void u2_gate(std::uint64_t target_mask,
Fp phi,
Fp lambda,
StateVector<Fp>& state) {
one_target_dense_matrix_gate(
target_mask, control_mask, get_IBMQ_matrix(Kokkos::numbers::pi / 2., phi, lambda), state);
one_target_dense_matrix_gate(target_mask,
control_mask,
get_IBMQ_matrix((Fp)Kokkos::numbers::pi / 2, phi, lambda),
state);
}

template <std::floating_point Fp>
Expand Down
2 changes: 1 addition & 1 deletion scaluq/operator/pauli_operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class PauliOperator {
flip_mask,
phase_mask,
rot90_count);
std::vector<StdComplex<Fp>> rot = {1, -1.i, -1, 1.i};
std::vector<StdComplex<Fp>> rot = {1, Complex<Fp>(0, -1), -1, Complex<Fp>(0, 1)};
std::uint64_t matrix_dim = 1ULL << _ptr->_pauli_id_list.size();
internal::ComplexMatrix<Fp> mat = internal::ComplexMatrix<Fp>::Zero(matrix_dim, matrix_dim);
for (std::uint64_t index = 0; index < matrix_dim; index++) {
Expand Down
Loading

0 comments on commit b9ef075

Please sign in to comment.