Skip to content

Commit

Permalink
Simplified some code
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanro committed Nov 24, 2024
1 parent 5114322 commit aa24170
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
24 changes: 6 additions & 18 deletions QCSim/QubitRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace QC {

if (firstQubit == secondQubit)
{
if (!enableMultithreading || NrBasisStates < BaseClass::OneQubitOmpLimit)
if (!BaseClass::GetMultithreading() || NrBasisStates < BaseClass::OneQubitOmpLimit)
return BaseClass::MeasureQubit(NrBasisStates, registerStorage, firstQubit, prob);

return BaseClass::MeasureQubitOmp(NrBasisStates, registerStorage, firstQubit, prob);
Expand Down Expand Up @@ -272,7 +272,7 @@ namespace QC {
bool swapStorage = true;
if (gateQubits == 1)
{
if (!enableMultithreading || NrBasisStates < BaseClass::OneQubitOmpLimit)
if (!BaseClass::GetMultithreading() || NrBasisStates < BaseClass::OneQubitOmpLimit)
BaseClass::ApplyOneQubitGate(gate, registerStorage, resultsStorage, gateMatrix, qubitBit, NrBasisStates, swapStorage);
else
BaseClass::ApplyOneQubitGateOmp(gate, registerStorage, resultsStorage, gateMatrix, qubitBit, NrBasisStates, swapStorage);
Expand All @@ -281,7 +281,7 @@ namespace QC {
{
const size_t ctrlQubitBit = 1ULL << controllingQubit1;

if (!enableMultithreading || NrBasisStates < BaseClass::TwoQubitOmpLimit)
if (!BaseClass::GetMultithreading() || NrBasisStates < BaseClass::TwoQubitOmpLimit)
BaseClass::ApplyTwoQubitsGate(gate, registerStorage, resultsStorage, gateMatrix, qubitBit, ctrlQubitBit, NrBasisStates);
else
BaseClass::ApplyTwoQubitsGateOmp(gate, registerStorage, resultsStorage, gateMatrix, qubitBit, ctrlQubitBit, NrBasisStates);
Expand All @@ -291,7 +291,7 @@ namespace QC {
const size_t qubitBit2 = 1ULL << controllingQubit1;
const size_t ctrlQubitBit = 1ULL << controllingQubit2;

if (!enableMultithreading || NrBasisStates < BaseClass::ThreeQubitOmpLimit)
if (!BaseClass::GetMultithreading() || NrBasisStates < BaseClass::ThreeQubitOmpLimit)
BaseClass::ApplyThreeQubitsGate(gate, registerStorage, resultsStorage, gateMatrix, qubitBit, qubitBit2, ctrlQubitBit, NrBasisStates);
else
BaseClass::ApplyThreeQubitsGateOmp(gate, registerStorage, resultsStorage, gateMatrix, qubitBit, qubitBit2, ctrlQubitBit, NrBasisStates);
Expand Down Expand Up @@ -487,22 +487,12 @@ namespace QC {

double GetQubitProbability(size_t qubit) const
{
if (!enableMultithreading || NrBasisStates < BaseClass::OneQubitOmpLimit)
if (!BaseClass::GetMultithreading() || NrBasisStates < BaseClass::OneQubitOmpLimit)
return BaseClass::GetQubitProbability(NrBasisStates, registerStorage, qubit);

return BaseClass::GetQubitProbabilityOmp(NrBasisStates, registerStorage, qubit);
}

void SetMultithreading(bool enable = true)
{
enableMultithreading = enable;
}

bool GetMultithreading() const
{
return enableMultithreading;
}

protected:
inline void CheckQubits(const GateClass& gate, size_t qubit, size_t controllingQubit1, size_t controllingQubit2, size_t gateQubits) const
{
Expand Down Expand Up @@ -555,7 +545,7 @@ namespace QC {

if (firstQubit == secondQubit)
{
if (!enableMultithreading || NrBasisStates < BaseClass::OneQubitOmpLimit)
if (!BaseClass::GetMultithreading() || NrBasisStates < BaseClass::OneQubitOmpLimit)
return BaseClass::MeasureQubitNoCollapse(NrBasisStates, registerStorage, firstQubit, prob);

return BaseClass::MeasureQubitNoCollapseOmp(NrBasisStates, registerStorage, firstQubit, prob);
Expand All @@ -575,8 +565,6 @@ namespace QC {

std::vector<Gates::AppliedGate<MatrixClass>> computeGates;
bool recordGates;

bool enableMultithreading = true;
};

}
Expand Down
13 changes: 13 additions & 0 deletions QCSim/QubitRegisterCalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,17 @@ namespace QC {
return static_cast<int>(threads ? threads : GetCpuInfoNrThreads());
}


void SetMultithreading(bool enable = true)
{
enableMultithreading = enable;
}

bool GetMultithreading() const
{
return enableMultithreading;
}

//constexpr static auto cone = std::complex<double>(1.0, 0.0);

constexpr static int divSchedule = 4;
Expand All @@ -574,6 +585,8 @@ namespace QC {
return std::count(std::istream_iterator<std::string>(cpuinfo), std::istream_iterator<std::string>(), std::string("processor"));
#endif
}

bool enableMultithreading = true;
};

}
Expand Down
21 changes: 13 additions & 8 deletions QCSim/StabilizerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ namespace QC {
{
for (size_t q = 0; q < nrQubits; ++q)
{
const int x1 = j.X[q] ? 1 : 0;
const int z1 = j.Z[q] ? 1 : 0;
const int x2 = h.X[q] ? 1 : 0;
const int z2 = h.Z[q] ? 1 : 0;
const int x1 = BoolToInt(j.X[q]);
const int z1 = BoolToInt(j.Z[q]);
const int x2 = BoolToInt(h.X[q]);
const int z2 = BoolToInt(h.Z[q]);

// add up all the exponents of i that contribute to the sign of the product
m += g(x1, z1, x2, z2);
Expand All @@ -321,10 +321,10 @@ namespace QC {
#pragma omp parallel for reduction(+:mloc) num_threads(processor_count) schedule(static, 256)
for (long long int q = 0; q < static_cast<long long int>(nrQubits); ++q)
{
const int x1 = j.X[q] ? 1 : 0;
const int z1 = j.Z[q] ? 1 : 0;
const int x2 = h.X[q] ? 1 : 0;
const int z2 = h.Z[q] ? 1 : 0;
const int x1 = BoolToInt(j.X[q]);
const int z1 = BoolToInt(j.Z[q]);
const int x2 = BoolToInt(h.X[q]);
const int z2 = BoolToInt(h.Z[q]);

// add up all the exponents of i that contribute to the sign of the product
mloc += g(x1, z1, x2, z2);
Expand All @@ -343,6 +343,11 @@ namespace QC {
h.PhaseSign = m % 4 != 0;
}

static inline int BoolToInt(bool b)
{
return b ? 1 : 0;
}

std::vector<Generator> destabilizerGenerators;
std::vector<Generator> stabilizerGenerators;

Expand Down

0 comments on commit aa24170

Please sign in to comment.