Skip to content

Commit 69ae775

Browse files
Minimize nullptr usage
1 parent fa474a7 commit 69ae775

23 files changed

+50
-51
lines changed

include/common/cudaengine.cuh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct InitCUDAResult {
128128

129129
InitCUDAResult()
130130
: all_dev_contexts()
131-
, default_dev_context(nullptr)
131+
, default_dev_context{ nullptr }
132132
{
133133
// Intentionally left blank
134134
}

include/common/oclengine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct InitOClResult {
229229

230230
InitOClResult()
231231
: all_dev_contexts()
232-
, default_dev_context(nullptr)
232+
, default_dev_context{ nullptr }
233233
{
234234
// Intentionally left blank
235235
}

include/hamiltonian.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct HamiltonianOp {
3636
: targetBit(0U)
3737
, anti(false)
3838
, uniform(false)
39-
, matrix(nullptr)
39+
, matrix{ nullptr }
4040
, controls()
4141
, toggles()
4242
{

include/qengine_cuda.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ class PoolItem {
126126
std::shared_ptr<real1> angleArray;
127127

128128
PoolItem()
129-
: probArray(nullptr)
130-
, angleArray(nullptr)
129+
: probArray{ nullptr }
130+
, angleArray{ nullptr }
131131
{
132132
cmplxBuffer = MakeBuffer(sizeof(complex) * CMPLX_NORM_LEN);
133133
realBuffer = MakeBuffer(sizeof(real1) * REAL_ARG_LEN);

include/qengine_opencl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ class PoolItem {
113113
std::shared_ptr<real1> angleArray;
114114

115115
PoolItem(cl::Context& context)
116-
: probArray(nullptr)
117-
, angleArray(nullptr)
116+
: probArray{ nullptr }
117+
, angleArray{ nullptr }
118118
{
119119
cmplxBuffer = MakeBuffer(context, sizeof(complex) * CMPLX_NORM_LEN);
120120
realBuffer = MakeBuffer(context, sizeof(real1) * REAL_ARG_LEN);

include/qengineshard.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class QEngineShard {
8383

8484
public:
8585
QEngineShard()
86-
: unit(nullptr)
86+
: unit{ nullptr }
8787
, mapped(0)
8888
, isProbDirty(false)
8989
, isPhaseDirty(false)
@@ -99,7 +99,7 @@ class QEngineShard {
9999
}
100100

101101
QEngineShard(const bool& set, const complex& rand_phase = ONE_CMPLX)
102-
: unit(nullptr)
102+
: unit{ nullptr }
103103
, mapped(0)
104104
, isProbDirty(false)
105105
, isPhaseDirty(false)

include/qinterface.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class QInterface : public ParallelFor {
248248
, amplitudeFloor(REAL1_EPSILON)
249249
, maxQPower(ONE_BCI)
250250
, rand_distribution(0.0, 1.0)
251-
, hardware_rand_generator(nullptr)
251+
, hardware_rand_generator{ nullptr }
252252
{
253253
// Intentionally left blank
254254
}
@@ -260,7 +260,7 @@ class QInterface : public ParallelFor {
260260

261261
void SetRandomSeed(uint32_t seed)
262262
{
263-
if (rand_generator != nullptr) {
263+
if (!!rand_generator) {
264264
rand_generator->seed(seed);
265265
}
266266
}
@@ -285,7 +285,7 @@ class QInterface : public ParallelFor {
285285
/** Generate a random real number between 0 and 1 */
286286
real1_f Rand()
287287
{
288-
if (hardware_rand_generator != nullptr) {
288+
if (!!hardware_rand_generator) {
289289
return hardware_rand_generator->Next();
290290
} else {
291291
return rand_distribution(*rand_generator);

include/qstabilizer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class QStabilizer : public QInterface {
119119

120120
void SetRandomSeed(uint32_t seed)
121121
{
122-
if (rand_generator != nullptr) {
122+
if (!!rand_generator) {
123123
rand_generator->seed(seed);
124124
}
125125
}
@@ -128,7 +128,7 @@ class QStabilizer : public QInterface {
128128

129129
bool Rand()
130130
{
131-
if (hardware_rand_generator != nullptr) {
131+
if (!!hardware_rand_generator) {
132132
if (!rawRandBoolsRemaining) {
133133
rawRandBools = hardware_rand_generator->NextRaw();
134134
rawRandBoolsRemaining = sizeof(unsigned) * bitsInByte;
@@ -400,7 +400,7 @@ class QStabilizer : public QInterface {
400400
}
401401

402402
QStabilizerPtr nQubits = std::make_shared<QStabilizer>(length, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG,
403-
false, randGlobalPhase, false, -1, hardware_rand_generator != nullptr);
403+
false, randGlobalPhase, false, -1, !!hardware_rand_generator);
404404
return Compose(nQubits, start);
405405
}
406406

include/qunitclifford.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ class QUnitClifford : public QInterface {
575575
}
576576

577577
QUnitCliffordPtr nQubits = std::make_shared<QUnitClifford>(length, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG,
578-
false, randGlobalPhase, false, -1, hardware_rand_generator != nullptr);
578+
false, randGlobalPhase, false, -1, !!hardware_rand_generator);
579579
return Compose(nQubits, start);
580580
}
581581

include/qunitmulti.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct QEngineInfo {
2828
size_t deviceIndex;
2929

3030
QEngineInfo()
31-
: unit(nullptr)
31+
: unit{ nullptr }
3232
, deviceIndex(0U)
3333
{
3434
}

src/common/cudaengine.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ std::vector<DeviceContextPtr> CUDAEngine::GetDeviceContextPtrVector() { return a
3939
void CUDAEngine::SetDeviceContextPtrVector(std::vector<DeviceContextPtr> vec, DeviceContextPtr dcp)
4040
{
4141
all_device_contexts = vec;
42-
if (dcp != nullptr) {
42+
if (!!dcp) {
4343
default_device_context = dcp;
4444
}
4545
}

src/common/oclengine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ std::vector<DeviceContextPtr> OCLEngine::GetDeviceContextPtrVector() { return al
154154
void OCLEngine::SetDeviceContextPtrVector(std::vector<DeviceContextPtr> vec, DeviceContextPtr dcp)
155155
{
156156
all_device_contexts = vec;
157-
if (dcp != nullptr) {
157+
if (!!dcp) {
158158
default_device_context = dcp;
159159
}
160160
}

src/pinvoke_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ MICROSOFT_QUANTUM_DECL void allocateQubit(_In_ uintq sid, _In_ uintq qid)
12961296
QInterfacePtr nQubit = CreateQuantumInterface(
12971297
simulatorTypes[sid], 1U, ZERO_BCI, randNumGen, CMPLX_DEFAULT_ARG, false, true, simulatorHostPointer[sid]);
12981298

1299-
if (simulators[sid] == nullptr) {
1299+
if (!simulators[sid]) {
13001300
simulators[sid] = nQubit;
13011301
shards[nQubit.get()] = {};
13021302
shards[nQubit.get()][qid] = 0;

src/qbdt/tree.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ QBdt::QBdt(std::vector<QInterfaceEngine> eng, bitLenInt qBitCount, const bitCapI
2727
bitLenInt qubitThreshold, real1_f sep_thresh)
2828
: QInterface(qBitCount, rgp, doNorm, useHardwareRNG, randomGlobalPhase, doNorm ? norm_thresh : ZERO_R1_F)
2929
, devID(deviceId)
30-
, root(nullptr)
30+
, root{ nullptr }
3131
, deviceIDs(devIds)
3232
, engines(eng)
3333
, shards(qubitCount)
@@ -58,9 +58,8 @@ void QBdt::Init()
5858

5959
QEnginePtr QBdt::MakeQEngine(bitLenInt qbCount, const bitCapInt& perm)
6060
{
61-
return std::dynamic_pointer_cast<QEngine>(
62-
CreateQuantumInterface(engines, qbCount, perm, rand_generator, ONE_CMPLX, doNormalize, false, false, devID,
63-
hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor, deviceIDs));
61+
return std::dynamic_pointer_cast<QEngine>(CreateQuantumInterface(engines, qbCount, perm, rand_generator, ONE_CMPLX,
62+
doNormalize, false, false, devID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor, deviceIDs));
6463
}
6564

6665
void QBdt::par_for_qbdt(const bitCapInt& end, bitLenInt maxQubit, BdtFunc fn, bool branch)

src/qbdthybrid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ QBdtHybrid::QBdtHybrid(std::vector<QInterfaceEngine> eng, bitLenInt qBitCount, c
2525
, thresholdQubits(qubitThreshold)
2626
, separabilityThreshold(sep_thresh)
2727
, devID(deviceId)
28-
, engine(nullptr)
28+
, engine{ nullptr }
2929
, phaseFactor(phaseFac)
3030
, deviceIDs(devList)
3131
, engines(eng)

src/qengine/cuda.cu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ bitLenInt QEngineCUDA::Allocate(bitLenInt start, bitLenInt length)
16081608
}
16091609

16101610
QEngineCUDAPtr nQubits = std::make_shared<QEngineCUDA>(length, 0U, rand_generator, ONE_CMPLX, doNormalize,
1611-
randGlobalPhase, useHostRam, deviceID, hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor);
1611+
randGlobalPhase, useHostRam, deviceID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor);
16121612

16131613
return Compose(nQubits, start);
16141614
}
@@ -3083,7 +3083,7 @@ QInterfacePtr QEngineCUDA::Clone()
30833083
}
30843084

30853085
QEngineCUDAPtr copyPtr = std::make_shared<QEngineCUDA>(qubitCount, 0U, rand_generator, ONE_CMPLX, doNormalize,
3086-
randGlobalPhase, useHostRam, deviceID, hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor);
3086+
randGlobalPhase, useHostRam, deviceID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor);
30873087

30883088
copyPtr->clFinish();
30893089
clFinish();
@@ -3101,7 +3101,7 @@ QInterfacePtr QEngineCUDA::Clone()
31013101
QEnginePtr QEngineCUDA::CloneEmpty()
31023102
{
31033103
QEngineCUDAPtr copyPtr = std::make_shared<QEngineCUDA>(0U, 0U, rand_generator, ONE_CMPLX, doNormalize,
3104-
randGlobalPhase, useHostRam, deviceID, hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor);
3104+
randGlobalPhase, useHostRam, deviceID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor);
31053105

31063106
copyPtr->SetQubitCount(qubitCount);
31073107

src/qengine/opencl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ void QEngineOCL::DecomposeDispose(bitLenInt start, bitLenInt length, QEngineOCLP
14411441
const bitLenInt nLength = qubitCount - length;
14421442

14431443
if (!nLength) {
1444-
if (destination != nullptr) {
1444+
if (!!destination) {
14451445
destination->stateVec = stateVec;
14461446
destination->stateBuffer = stateBuffer;
14471447
stateBuffer = nullptr;
@@ -1649,7 +1649,7 @@ bitLenInt QEngineOCL::Allocate(bitLenInt start, bitLenInt length)
16491649
}
16501650

16511651
QEngineOCLPtr nQubits = std::make_shared<QEngineOCL>(length, ZERO_BCI, rand_generator, ONE_CMPLX, doNormalize,
1652-
randGlobalPhase, useHostRam, deviceID, hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor);
1652+
randGlobalPhase, useHostRam, deviceID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor);
16531653

16541654
return Compose(nQubits, start);
16551655
}
@@ -3164,7 +3164,7 @@ QInterfacePtr QEngineOCL::Clone()
31643164
}
31653165

31663166
QEngineOCLPtr copyPtr = std::make_shared<QEngineOCL>(qubitCount, ZERO_BCI, rand_generator, ONE_CMPLX, doNormalize,
3167-
randGlobalPhase, useHostRam, deviceID, hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor);
3167+
randGlobalPhase, useHostRam, deviceID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor);
31683168

31693169
cl::Event copyEvent;
31703170

@@ -3185,7 +3185,7 @@ QInterfacePtr QEngineOCL::Clone()
31853185
QEnginePtr QEngineOCL::CloneEmpty()
31863186
{
31873187
QEngineOCLPtr copyPtr = std::make_shared<QEngineOCL>(0U, ZERO_BCI, rand_generator, ONE_CMPLX, doNormalize,
3188-
randGlobalPhase, useHostRam, deviceID, hardware_rand_generator != nullptr, false, (real1_f)amplitudeFloor);
3188+
randGlobalPhase, useHostRam, deviceID, !!hardware_rand_generator, false, (real1_f)amplitudeFloor);
31893189

31903190
copyPtr->SetQubitCount(qubitCount);
31913191

src/qinterface/qinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ QInterface::QInterface(
3333
, maxQPower(pow2(qubitCount))
3434
, rand_generator(rgp)
3535
, rand_distribution(ZERO_R1_F, ONE_R1_F)
36-
, hardware_rand_generator(nullptr)
36+
, hardware_rand_generator{ nullptr }
3737
{
3838
if (qubitCount > QRACK_MAX_QUBITS) {
3939
throw std::invalid_argument(

src/qstabilizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ QInterfacePtr QStabilizer::Clone()
7575
Finish();
7676

7777
QStabilizerPtr clone = std::make_shared<QStabilizer>(qubitCount, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG, false,
78-
randGlobalPhase, false, -1, hardware_rand_generator != nullptr);
78+
randGlobalPhase, false, -1, !!hardware_rand_generator);
7979
clone->Finish();
8080

8181
clone->x = x;
@@ -1609,7 +1609,7 @@ bitLenInt QStabilizer::Compose(QStabilizerPtr toCopy, bitLenInt start)
16091609
QInterfacePtr QStabilizer::Decompose(bitLenInt start, bitLenInt length)
16101610
{
16111611
QStabilizerPtr dest = std::make_shared<QStabilizer>(length, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG, false,
1612-
randGlobalPhase, false, -1, hardware_rand_generator != nullptr);
1612+
randGlobalPhase, false, -1, !!hardware_rand_generator);
16131613
Decompose(start, dest);
16141614

16151615
return dest;

src/qstabilizerhybrid.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ QStabilizerHybrid::QStabilizerHybrid(std::vector<QInterfaceEngine> eng, bitLenIn
6060
, devID(deviceId)
6161
, phaseFactor(phaseFac)
6262
, logFidelity(0.0)
63-
, engine(nullptr)
64-
, rdmClone(nullptr)
63+
, engine{ nullptr }
64+
, rdmClone{ nullptr }
6565
, deviceIDs(devList)
6666
, engineTypes(eng)
6767
, cloneEngineTypes(eng)
6868
, shards(qubitCount)
69-
, stateMapCache(nullptr)
69+
, stateMapCache{ nullptr }
7070
, prng(std::random_device{}())
7171
{
7272
#if ENABLE_OPENCL || ENABLE_CUDA
@@ -220,7 +220,7 @@ void QStabilizerHybrid::FlushIfBlocked(bitLenInt control, bitLenInt target, bool
220220
: stabilizer->Compose(std::make_shared<QUnitClifford>(
221221
1U, ZERO_BCI, rand_generator, CMPLX_DEFAULT_ARG, false, randGlobalPhase, false, -1, useRDRAND));
222222
++ancillaCount;
223-
shards.push_back(nullptr);
223+
shards.emplace_back(nullptr);
224224
if (deadAncillaCount) {
225225
--deadAncillaCount;
226226
}
@@ -1138,7 +1138,7 @@ void QStabilizerHybrid::Mtrx(const complex* lMtrx, bitLenInt target)
11381138
: stabilizer->Compose(std::make_shared<QUnitClifford>(1U, ZERO_BCI, rand_generator,
11391139
CMPLX_DEFAULT_ARG, false, randGlobalPhase, false, -1, useRDRAND));
11401140
++ancillaCount;
1141-
shards.push_back(nullptr);
1141+
shards.emplace_back(nullptr);
11421142
if (deadAncillaCount) {
11431143
--deadAncillaCount;
11441144
}
@@ -1997,9 +1997,10 @@ real1_f QStabilizerHybrid::ApproxCompareHelper(QStabilizerHybridPtr toCompare, b
19971997
return ONE_R1_F;
19981998
}
19991999

2000-
QStabilizerHybridPtr thisClone = stabilizer ? std::dynamic_pointer_cast<QStabilizerHybrid>(Clone()) : nullptr;
2001-
QStabilizerHybridPtr thatClone =
2002-
toCompare->stabilizer ? std::dynamic_pointer_cast<QStabilizerHybrid>(toCompare->Clone()) : nullptr;
2000+
QStabilizerHybridPtr thisClone{ stabilizer ? std::dynamic_pointer_cast<QStabilizerHybrid>(Clone()) : nullptr };
2001+
QStabilizerHybridPtr thatClone{
2002+
toCompare->stabilizer ? std::dynamic_pointer_cast<QStabilizerHybrid>(toCompare->Clone()) : nullptr
2003+
};
20032004

20042005
if (thisClone) {
20052006
thisClone->FlushBuffers();

src/qtensornetwork.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ void QTensorNetwork::MakeLayerStack(std::set<bitLenInt> qubits)
162162
}
163163

164164
// We need to prepare the layer stack (and cache it).
165-
layerStack =
166-
CreateQuantumInterface(engines, qubitCount, ZERO_BCI, rand_generator, ONE_CMPLX, doNormalize, randGlobalPhase,
167-
useHostRam, devID, hardware_rand_generator != nullptr, isSparse, (real1_f)amplitudeFloor, deviceIDs);
165+
layerStack = CreateQuantumInterface(engines, qubitCount, ZERO_BCI, rand_generator, ONE_CMPLX, doNormalize,
166+
randGlobalPhase, useHostRam, devID, !!hardware_rand_generator, isSparse, (real1_f)amplitudeFloor, deviceIDs);
168167
layerStack->SetReactiveSeparate(isReactiveSeparate);
169168
layerStack->SetTInjection(useTGadget);
170169

@@ -207,8 +206,8 @@ void QTensorNetwork::MakeLayerStack(std::set<bitLenInt> qubits)
207206
QInterfacePtr QTensorNetwork::Clone()
208207
{
209208
QTensorNetworkPtr clone = std::make_shared<QTensorNetwork>(engines, qubitCount, ZERO_BCI, rand_generator, ONE_CMPLX,
210-
doNormalize, randGlobalPhase, useHostRam, devID, hardware_rand_generator != nullptr, isSparse,
211-
(real1_f)amplitudeFloor, deviceIDs);
209+
doNormalize, randGlobalPhase, useHostRam, devID, !!hardware_rand_generator, isSparse, (real1_f)amplitudeFloor,
210+
deviceIDs);
212211

213212
clone->circuit.clear();
214213
for (size_t i = 0U; i < circuit.size(); ++i) {

src/qunit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ bool QUnit::Detach(bitLenInt start, bitLenInt length, QUnitPtr dest, bool isTry,
307307
}
308308
}
309309

310-
const QUnitPtr copy = isTry ? std::dynamic_pointer_cast<QUnit>(Copy()) : nullptr;
310+
const QUnitPtr copy{ isTry ? std::dynamic_pointer_cast<QUnit>(Copy()) : nullptr };
311311

312312
// After ordering all subunits contiguously, since the top level mapping is a contiguous array, all subunit sets are
313313
// also contiguous. From the lowest index bits, they are mapped simply for the length count of bits involved in the
@@ -317,7 +317,7 @@ bool QUnit::Detach(bitLenInt start, bitLenInt length, QUnitPtr dest, bool isTry,
317317
QEngineShard& shard = shards[start + i];
318318
QInterfacePtr unit = shard.unit;
319319

320-
if (unit == nullptr) {
320+
if (!unit) {
321321
continue;
322322
}
323323

src/wasm_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ void allocateQubit(quid sid, bitLenInt qid)
958958
QInterfacePtr nQubit = CreateQuantumInterface(
959959
simulatorTypes[sid], 1U, ZERO_BCI, randNumGen, CMPLX_DEFAULT_ARG, false, true, simulatorHostPointer[sid]);
960960

961-
if (simulators[sid] == nullptr) {
961+
if (!simulators[sid]) {
962962
simulators[sid] = nQubit;
963963
shards[nQubit.get()] = {};
964964
shards[nQubit.get()][qid] = 0;

0 commit comments

Comments
 (0)