Skip to content

Commit

Permalink
.git/refs/COMMIT_EDITMSG
Browse files Browse the repository at this point in the history
Signed-off-by: slowy07 <[email protected]>
  • Loading branch information
slowy07 committed Aug 21, 2023
1 parent 443601b commit 9d42922
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 42 deletions.
26 changes: 26 additions & 0 deletions clara_test/tests/classFunction/reversible_testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,29 @@ TEST(clara_reversibe_test, TOF) {
circuit.TOF({0, 1, 2});
EXPECT_EQ(circuit.get(2), true);
}

TEST(clara_reversibe_test, SetAndGet) {
Dynamic_bitset bitset(8);
bitset.set(3);
EXPECT_TRUE(bitset.get(3));
EXPECT_FALSE(bitset.get(2));
}

TEST(clara_reversibe_test, InitializationCircuit) {
Dynamic_bitset bitset(8);
Bit_circuit circuit(bitset);
EXPECT_EQ(circuit.size(), 8);
}

TEST(clara_reversibe_test, InitializationCircuiTenBitset) {
Dynamic_bitset bitset(10);
EXPECT_EQ(bitset.size(), 10);
EXPECT_EQ(bitset.storage_size(), 1);
}

TEST(clara_reversibe_test, XGate) {
Dynamic_bitset bitset(8);
Bit_circuit circuit(bitset);
circuit.X(3);
EXPECT_TRUE(circuit.get(3));
}
29 changes: 29 additions & 0 deletions clara_test/tests/classFunction/states_testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ TEST(clara_states_testing, SingletonInstance) {
EXPECT_EQ(&states, &states2);
}

TEST(clara_states_testing, MesState) {
const States& states = States::get_instance();
ket mes_state = states.mes();

ket expected_mes_state = ket::Zero(4);
expected_mes_state(0) = expected_mes_state(3) = 1.0 / std::sqrt(2.0);

EXPECT_EQ(mes_state, expected_mes_state);
}

TEST(clara_states_testing, TestWStates) {
const States& states = States::get_instance();
ket w_state = states.W;
ket expected_w_state = ket::Zero(8);
expected_w_state(1) = expected_w_state(2) = expected_w_state(4) = 1.0 / std::sqrt(3.0);
EXPECT_EQ(w_state, expected_w_state);
}

TEST(clara_states_testing, TestTwoQubit) {
const States& states = States::get_instance();
ket plus_state = states.plus(2);
ket expected_plus_state = ket::Zero(4);
std::complex<double> value = 0.5;
expected_plus_state(0) = expected_plus_state(1) = expected_plus_state(2) =
expected_plus_state(3) = value;

EXPECT_EQ(plus_state, expected_plus_state);
}

TEST(clara_states_testing, OtherTest) {
idx n = 1;
EXPECT_NEAR(0, norm(clara::st.z0 - clara::st.zero(n)), 1e-7);
Expand Down
30 changes: 27 additions & 3 deletions clara_test/tests/classFunction/timer_testing.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
#include <chrono>
#include <sstream>
#include <thread>

#include "../../../include/clara.h"
#include "gtest/gtest.h"

using namespace clara;

TEST(clara_timer_test, DefaultConstructor) {
Timer<> timer;
EXPECT_GE(timer.tics(), 0);
TEST(clara_timer_test, TimerTestMiliseconds) {
Timer<std::chrono::steady_clock, std::chrono::duration<double>> timer;
timer.tic();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
timer.toc();
EXPECT_EQ(timer.get_milliseconds(), 1000);
}

TEST(clara_timer_test, OperatorShout) {
Timer<std::chrono::steady_clock, std::chrono::duration<double>> timer;
timer.tic();
std::this_thread::sleep_for(std::chrono::seconds(1));
timer.toc();

std::ostringstream os;
os << timer;
EXPECT_NE(os.str(), "1.00007");
}

TEST(clara_test, TimerTestDuration) {
Timer<std::chrono::steady_clock, std::chrono::duration<double>> timer;

timer.tic();
std::this_thread::sleep_for(std::chrono::seconds(1));
timer.toc();
EXPECT_NE(timer.get_duration().count(), 1.0);
}

TEST(clara_timer_test, TicToc) {
Expand Down
8 changes: 3 additions & 5 deletions include/classFunction/reversible.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef CLASSFUNCTION_REVESIBLE_H_
#define CLASSFUNCTION_REVESIBLE_H_

#include <strings.h>

#include <bitset>
#include <cassert>
#include <climits>
Expand Down Expand Up @@ -36,7 +34,8 @@ class Dynamic_bitset : public IDisplay {
// total number of bits
idx N_;
// storage vector for the bitset
std::vector<value_type> v_;
// std::vector<value_type>
storage_type v_;

/**
* @brief calculate the index of the storage element containing a bit at the given position
Expand Down Expand Up @@ -313,8 +312,7 @@ class Dynamic_bitset : public IDisplay {
* to be used as an override for the IDisplay::display function
*/
std::ostream& display(std::ostream& os) const override {
idx bitset_size = this->size();
for (idx i = bitset_size; i-- > 0;) {
for (idx i = this -> size(); i-- > 0;) {
os << this->get(i);
}
return os;
Expand Down
77 changes: 43 additions & 34 deletions include/classFunction/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,88 @@ namespace clara {
* @tparam T the type of duration to be used to measuring time
* @param CLOCK_T clock type to be used
*/
template <typename T = std::chrono::duration<double>, typename CLOCK_T = std::chrono::steady_clock>
template <typename Clock = std::chrono::steady_clock, typename Duration = std::chrono::duration<double>>
class Timer : public IDisplay {
protected:
typename CLOCK_T::time_point start_, end_;
typename Clock::time_point start_, end_;

public:
/**
* @brief construct an instance with the current time
* as starting point
* @brief Construct an instance with the current time as starting point
*/
Timer() noexcept : start_{CLOCK_T::now()}, end_{start_} {}
Timer() noexcept : start_{Clock::now()}, end_{start_} {}

/**
* @brief reset the chronometer
* reset the starting/ending point to the current time
* @brief Reset the chronometer
* Reset the starting/ending point to the current time
*/
void tic() noexcept { start_ = end_ = CLOCK_T::now(); }
void tic() noexcept { start_ = end_ = Clock::now(); }

/**
* @brief stops the chronometer
* set the current time as the ending point
* @return current instance
* @brief Stops the chronometer
* Set the current time as the ending point
* @return Current instance
*/
const Timer& toc() noexcept {
end_ = CLOCK_T::now();
end_ = Clock::now();
return *this;
}

/**
* @brief Get the duration of the time interval
* @return Duration that has elapsed since the last call to tic() or reset()
*/
Duration get_duration() const noexcept {
return std::chrono::duration_cast<Duration>(end_ - start_);
}

/**
* @brief time passsed in the duration specified by T
* @return number of tics (specified by T) that passed between the
* instantiation/rest and invocation of clara::Timer::toc()
* @brief Get the duration in seconds
* @return Number of seconds that have elapsed since the last call to tic() or reset()
*/
double tics() const noexcept { return std::chrono::duration_cast<T>(end_ - start_).count(); }
double get_seconds() const noexcept { return get_duration().count(); }

/**
* @brief get the duration specified by U
*
* get the duration that passed between the reset and invocation of clara::Timer::toc()
* @tparam U the type of duration to be returned
* @return Duration that passed between that reset and invocation clara::Timer::toc()
* @brief Get the duration in milliseconds
* @return Number of milliseconds that have elapsed since the last call to tic() or reset()
*/
template <typename U = T>
U get_duration() const noexcept {
return std::chrono::duration_cast<U>(end_ - start_);
double get_milliseconds() const noexcept {
return std::chrono::duration_cast<std::chrono::milliseconds>(get_duration()).count();
}

/**
* @brief default copy constructor
* @brief Default copy constructor
*/
Timer(const Timer&) = default;

/**
* @brief default move constructor
* @brief Default move constructor
*/
Timer(Timer&&) = default;

/**
* @brief default copy assignment operator
* @brief Default copy assignment operator
*/
Timer& operator=(const Timer&) = default;

/**
* @brief default move assignment operator
* @brief Default move assignment operator
*/
Timer& operator=(Timer&&) = default;

/**
* @brief default virtual destructor
* @brief Default virtual destructor
*/
virtual ~Timer() = default;

private:
/**
* @brief override of clara::IDisplay::display()
* writes the output strem the number of tics that passed between the reset and invocation of
* clara::TImer::toc()
* @return the output stream
* @brief Override of clara::IDisplay::display()
* Writes the output stream the number of seconds that have elapsed since the last call to
* clara::Timer::toc()
* @return The output stream
*/
std::ostream& display(std::ostream& os) const override { return os << tics(); }
std::ostream& display(std::ostream& os) const override { return os << get_seconds(); }
};

} // namespace clara
Expand Down

0 comments on commit 9d42922

Please sign in to comment.