stuff-from-scratch/plugins/quantum_computing/src/Qubit.cpp
2023-01-26 15:16:46 +00:00

38 lines
No EOL
752 B
C++

#include "Qubit.h"
#include <sstream>
Qubit::Qubit(const ComplexNumber& alpha, const ComplexNumber& beta)
: mAlpha(alpha),
mBeta(beta)
{
}
const ComplexNumber& Qubit::getAlpha() const
{
return mAlpha;
}
const ComplexNumber& Qubit::getBeta() const
{
return mBeta;
}
bool Qubit::isIn0State() const
{
return mAlpha.getReal() == 1.0 && mBeta.getMagnitude() == 0.0;
}
bool Qubit::isIn1State() const
{
return mBeta.getReal() == 1.0 && mAlpha.getMagnitude() == 0.0;
}
std::string Qubit::toString(std::size_t precision) const
{
std::stringstream sstr;
sstr.precision(precision);
sstr << "alpha " << mAlpha.getReal() << " " << mAlpha.getImaginary() << "i , beta " << mBeta.getReal() << " " << mBeta.getImaginary() << "i";
return sstr.str();
}