Skip to content

Commit

Permalink
Restore native reporting precision
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Dec 7, 2024
1 parent 428f77b commit 3d2bdc2
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 55 deletions.
1 change: 0 additions & 1 deletion include/common/qrack_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ void mul2x2(const complex* left, const complex* right, complex* out);
void exp2x2(const complex* matrix2x2, complex* outMatrix2x2);
void log2x2(const complex* matrix2x2, complex* outMatrix2x2);
void inv2x2(const complex* matrix2x2, complex* outMatrix2x2);
void makeUnitary(const complex* matrix2x2, complex_h* outMatrix2x2);
bool isOverflowAdd(
bitCapIntOcl inOutInt, bitCapIntOcl inInt, const bitCapIntOcl& signMask, const bitCapIntOcl& lengthPower);
bool isOverflowSub(
Expand Down
2 changes: 0 additions & 2 deletions include/common/qrack_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ typedef double real1_s;
#endif
#endif

typedef double real1_h;
typedef std::complex<real1> complex;
typedef std::complex<real1_h> complex_h;
const bitCapInt ONE_BCI = 1U;
const bitCapInt ZERO_BCI = 0U;
constexpr bitLenInt bitsInCap = ((bitLenInt)1U) << ((bitLenInt)QBCAPPOW);
Expand Down
36 changes: 0 additions & 36 deletions src/common/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,42 +208,6 @@ void inv2x2(const complex* matrix2x2, complex* outMatrix2x2)
outMatrix2x2[3U] = det * matrix2x2[0U];
}

void makeUnitary(const complex* m, complex_h* u)
{
real1 th, ph, lm;
complex phase;
real1 nrm = norm(m[0U]);
if (nrm <= FP_NORM_EPSILON) {
phase = ONE_CMPLX;
th = PI_R1 / 2;
} else {
nrm = sqrt(nrm);
phase = m[0U] / nrm;
if (nrm > ONE_R1) {
nrm = ONE_R1;
}
th = acos(nrm);
}
if ((norm(m[1U]) <= FP_NORM_EPSILON) || (norm(m[2U]) <= FP_NORM_EPSILON)) {
ph = arg(m[3U] / phase);
lm = ZERO_R1;
} else {
ph = arg(m[2U] / phase);
lm = arg(-m[1U] / phase);
}

const real1_h th_h = (real1_h)th;
const real1_h c = cos(th_h / 2);
const real1_h s = sin(th_h / 2);
const complex_h el = exp(complex_h(0.0, (real1_h)lm));
const complex_h ep = exp(complex_h(0.0, (real1_h)ph));
const complex_h p = (complex_h)phase;
u[0U] = p * c;
u[1U] = -p * el * s;
u[2U] = p * ep * s;
u[3U] = p * ep * el * c;
}

/// Check if an addition with overflow sets the flag
bool isOverflowAdd(
bitCapIntOcl inOutInt, bitCapIntOcl inInt, const bitCapIntOcl& signMask, const bitCapIntOcl& lengthPower)
Expand Down
8 changes: 4 additions & 4 deletions src/pinvoke_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ MICROSOFT_QUANTUM_DECL void qstabilizer_out_to_file(_In_ uintq sid, _In_ char* f

std::ofstream ofile;
ofile.open(f);
ofile.precision(17);
ofile.precision(36);

try {
ofile << std::dynamic_pointer_cast<QStabilizerHybrid>(simulators[sid]);
Expand Down Expand Up @@ -3886,7 +3886,7 @@ MICROSOFT_QUANTUM_DECL void qcircuit_out_to_file(_In_ uintq cid, _In_ char* f)
CIRCUIT_LOCK_GUARD_VOID(cid)
std::ofstream ofile;
ofile.open(f);
ofile.precision(17);
ofile.precision(36);
ofile << circuit;
ofile.close();
}
Expand All @@ -3895,7 +3895,7 @@ MICROSOFT_QUANTUM_DECL void qcircuit_in_from_file(_In_ uintq cid, _In_ char* f)
CIRCUIT_LOCK_GUARD_VOID(cid)
std::ifstream ifile;
ifile.open(f);
ifile.precision(17);
ifile.precision(36);
ifile >> circuit;
ifile.close();
}
Expand All @@ -3905,7 +3905,7 @@ MICROSOFT_QUANTUM_DECL size_t qcircuit_out_to_string_length(_In_ uintq cid)
CIRCUIT_LOCK_GUARD_TYPED(cid, 0U)

std::stringstream ss;
ss.precision(17);
ss.precision(36);
ss << circuit;
circuitStrings[circuit.get()] = ss.str();

Expand Down
5 changes: 2 additions & 3 deletions src/qcircuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ std::ostream& operator<<(std::ostream& os, const QCircuitGatePtr g)
os << g->payloads.size() << " ";
for (const auto& p : g->payloads) {
os << (size_t)p.first << " ";
complex_h u[4U];
makeUnitary(p.second.get(), u);
const complex* mtrx = p.second.get();
for (size_t i = 0U; i < 4U; ++i) {
os << u[i] << " ";
os << mtrx[i] << " ";
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/qstabilizerhybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2144,12 +2144,10 @@ std::ostream& operator<<(std::ostream& os, const QStabilizerHybridPtr s)
const std::vector<MpsShardPtr>& shards = s->shards;
for (size_t i = 0U; i < shards.size(); ++i) {
const complex* mtrx = !shards[i] ? id : shards[i]->gate;
complex_h u[4U];
makeUnitary(mtrx, u);
for (size_t j = 0U; j < 3U; ++j) {
os << u[j] << " ";
os << mtrx[j] << " ";
}
os << u[3U] << std::endl;
os << mtrx[3U] << std::endl;
}

return os;
Expand Down
10 changes: 5 additions & 5 deletions src/wasm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ void qstabilizer_out_to_file(quid sid, std::string f)

std::ofstream ofile;
ofile.open(f.c_str());
ofile.precision(17);
ofile.precision(36);
ofile << std::dynamic_pointer_cast<QStabilizerHybrid>(simulators[sid]);
ofile.close();
}
Expand All @@ -869,7 +869,7 @@ void qstabilizer_in_from_file(quid sid, std::string f)

std::ifstream ifile;
ifile.open(f.c_str());
ifile.precision(17);
ifile.precision(36);
ifile >> std::dynamic_pointer_cast<QStabilizerHybrid>(simulators[sid]);
ifile.close();

Expand Down Expand Up @@ -2795,7 +2795,7 @@ void qcircuit_out_to_file(quid cid, std::string f)
CIRCUIT_LOCK_GUARD_VOID(cid)
std::ofstream ofile;
ofile.open(f.c_str());
ofile.precision(17);
ofile.precision(36);
ofile << circuit;
ofile.close();
}
Expand All @@ -2805,7 +2805,7 @@ void qcircuit_in_from_file(quid cid, std::string f)
CIRCUIT_LOCK_GUARD_VOID(cid)
std::ifstream ifile;
ifile.open(f.c_str());
ifile.precision(17);
ifile.precision(36);
ifile >> circuit;
ifile.close();
}
Expand All @@ -2814,7 +2814,7 @@ std::string qcircuit_out_to_string(quid cid)
{
CIRCUIT_LOCK_GUARD_TYPED(cid, "")
std::stringstream ss;
ss.precision(17);
ss.precision(36);
ss << circuit;
return ss.str();
}
Expand Down

0 comments on commit 3d2bdc2

Please sign in to comment.