stuff-from-scratch/plugins/quantum_computing/src/Qubit.cpp
2023-12-27 12:20:02 +00:00

38 lines
No EOL
737 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;
}
String Qubit::toString(size_t precision) const
{
Stringstream sstr;
sstr.precision(precision);
sstr << "alpha " << mAlpha.getReal() << " " << mAlpha.getImaginary() << "i , beta " << mBeta.getReal() << " " << mBeta.getImaginary() << "i";
return sstr.str();
}