Skip to content

Commit

Permalink
Exposed MeasureNoCollapse as public, I might need it in a private p…
Browse files Browse the repository at this point in the history
…roject.

Changed `addseed` to an `unsigned int` and set it to a random value if the passed value is zero.
  • Loading branch information
aromanro committed Jan 23, 2025
1 parent 3348aa9 commit 13742c9
Show file tree
Hide file tree
Showing 29 changed files with 71 additions and 57 deletions.
4 changes: 2 additions & 2 deletions QCSim/BernsteinVazirani.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace BernsteinVazirani {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

BernsteinVaziraniAlgorithm(size_t N = 3, int addseed = 0)
BernsteinVaziraniAlgorithm(size_t N = 3, unsigned int addseed = 0)
: BaseClass(N, addseed)
{
assert(N >= 1);
Expand Down Expand Up @@ -149,7 +149,7 @@ namespace BernsteinVazirani {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

BernsteinVaziraniAlgorithmWithGatesOracle(size_t N = 3, int addseed = 0)
BernsteinVaziraniAlgorithmWithGatesOracle(size_t N = 3, unsigned int addseed = 0)
: BaseClass(2 * N - 1, addseed),
oracle(2 * N - 1, 0, N - 1, N, N + 1)
{
Expand Down
2 changes: 1 addition & 1 deletion QCSim/CheckCHSHInequality.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace BellInequalities {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

explicit CheckCHSHInequality(int addseed = 0)
explicit CheckCHSHInequality(unsigned int addseed = 0)
: BaseClass(2, addseed),
S((-Q.getRawOperatorMatrix() - R.getRawOperatorMatrix()) / sqrt(2.)), // (-Z-X)/sqrt(2)=-H
T((Q.getRawOperatorMatrix() - R.getRawOperatorMatrix()) / sqrt(2.)), // (Z-X)/sqrt(2)
Expand Down
2 changes: 1 addition & 1 deletion QCSim/CoinFlipping.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Games {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

CoinFlipping(int addseed = 0)
CoinFlipping(unsigned int addseed = 0)
: BaseClass(1, addseed)
{
BaseClass::setToBasisState(0);
Expand Down
4 changes: 2 additions & 2 deletions QCSim/DeutschJozsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace DeutschJozsa {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

DeutschJozsaAlgorithm(size_t N = 2, int addseed = 0)
DeutschJozsaAlgorithm(size_t N = 2, unsigned int addseed = 0)
: BaseClass(N, addseed),
functionType(FunctionType::constantZero)
{
Expand Down Expand Up @@ -155,7 +155,7 @@ namespace DeutschJozsa {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

DeutschJozsaAlgorithmWithGatesOracle(size_t N = 2, int addseed = 0)
DeutschJozsaAlgorithmWithGatesOracle(size_t N = 2, unsigned int addseed = 0)
: BaseClass(N == 2 ? 2 : 2 * N - 3, addseed), oracle(N == 2 ? 2 : 2 * N - 3, 0, N - 2, N - 1, N)

{
Expand Down
4 changes: 2 additions & 2 deletions QCSim/DistributedCNOT.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Distributed {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

DistributedCU(size_t nrQubits = 4, size_t ctrlq = 0, size_t targetq = 3, size_t entq1 = 1, size_t entq2 = 2, int addseed = 0)
DistributedCU(size_t nrQubits = 4, size_t ctrlq = 0, size_t targetq = 3, size_t entq1 = 1, size_t entq2 = 2, unsigned int addseed = 0)
: BaseClass(nrQubits, addseed), ctrlQubit(ctrlq), targetQubit(targetq), entQubit1(entq1), entQubit2(entq2)
{
BaseClass::setToBasisState(0);
Expand Down Expand Up @@ -76,7 +76,7 @@ namespace Distributed {
public:
using BaseClass = DistributedCU<VectorClass, MatrixClass>;

DistributedCNOT(int addseed = 0)
DistributedCNOT(unsigned int addseed = 0)
: BaseClass(4, 0, 3, 1, 2, addseed)
{
}
Expand Down
4 changes: 2 additions & 2 deletions QCSim/DraperAdder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Adders {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

DraperAdder(size_t N = 3, int addseed = 0)
DraperAdder(size_t N = 3, unsigned int addseed = 0)
: BaseClass(2 * N, addseed), n(N), fourier(2 * N, N)
{
BaseClass::setToBasisState(0);
Expand Down Expand Up @@ -64,7 +64,7 @@ namespace Adders {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

DraperAdderWithCarry(size_t N = 3, int addseed = 0)
DraperAdderWithCarry(size_t N = 3, unsigned int addseed = 0)
: BaseClass(2 * N + 1, addseed), n(N), fourier(2 * N + 1, N)
{
BaseClass::setToBasisState(0);
Expand Down
2 changes: 1 addition & 1 deletion QCSim/EntanglementSwapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Teleportation {
using BaseClass = QuantumTeleportation<VectorClass, MatrixClass>;
using AlgorithmClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

EntanglementSwapping(int addseed = 0)
EntanglementSwapping(unsigned int addseed = 0)
: BaseClass(6, addseed)
{
}
Expand Down
6 changes: 3 additions & 3 deletions QCSim/ErrorCorrection3Qubits.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ErrorCorrection {
using BaseClass = ErrorCorrectionBase<VectorClass, MatrixClass>;
using AlgorithmClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

ErrorCorrection3Qubits(int addseed = 0)
ErrorCorrection3Qubits(unsigned int addseed = 0)
: BaseClass(3, addseed)
{
}
Expand All @@ -39,7 +39,7 @@ namespace ErrorCorrection {
using BaseClass = ErrorCorrection3Qubits<VectorClass, MatrixClass>;
using AlgorithmClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

ErrorCorrection3QubitsFlip(int addseed = 0)
ErrorCorrection3QubitsFlip(unsigned int addseed = 0)
: BaseClass(addseed)
{
}
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace ErrorCorrection {
using BaseClass = ErrorCorrection3Qubits<VectorClass, MatrixClass>;
using AlgorithmClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

ErrorCorrection3QubitsSign(int addseed = 0)
ErrorCorrection3QubitsSign(unsigned int addseed = 0)
: BaseClass(addseed)
{
}
Expand Down
2 changes: 1 addition & 1 deletion QCSim/ErrorCorrectionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ErrorCorrection {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

ErrorCorrectionBase(size_t N, int addseed = 0)
ErrorCorrectionBase(size_t N, unsigned int addseed = 0)
: BaseClass(N, addseed), errorQubit(N)
{
BaseClass::setToBasisState(0);
Expand Down
2 changes: 1 addition & 1 deletion QCSim/GeneralElitzurVaidmanBomb.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Paradoxes {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

GeneralElitzurVaidmanBomb(size_t maxStages, int addseed = 0)
GeneralElitzurVaidmanBomb(size_t maxStages, unsigned int addseed = 0)
: BaseClass(maxStages + 1, addseed), stages(maxStages), theta(M_PI / (maxStages + 1.))
{
assert((BaseClass::getNrQubits() >= 2));
Expand Down
4 changes: 2 additions & 2 deletions QCSim/GroverAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Grover {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

GroverAlgorithm(size_t N = 3, int addseed = 0)
GroverAlgorithm(size_t N = 3, unsigned int addseed = 0)
: BaseClass(N, addseed)
{
assert(N >= 1);
Expand Down Expand Up @@ -131,7 +131,7 @@ namespace Grover {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

GroverAlgorithmWithGatesOracle(size_t N = 3, int addseed = 0)
GroverAlgorithmWithGatesOracle(size_t N = 3, unsigned int addseed = 0)
: BaseClass(2 * N - 1, addseed), nControlledNOT(INT_MAX), correctQuestionState(0)
{
assert(N >= 1);
Expand Down
2 changes: 1 addition & 1 deletion QCSim/HardyParadox.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Paradoxes {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

HardyParadox(int addseed = 0)
HardyParadox(unsigned int addseed = 0)
: BaseClass(3, addseed)
{
const double theta = 0.575 * M_PI;
Expand Down
2 changes: 1 addition & 1 deletion QCSim/MPSSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace QC
public:
MPSSimulator() = delete;

MPSSimulator(size_t N, int addseed = 0)
MPSSimulator(size_t N, unsigned int addseed = 0)
: impl(N, addseed)
{
InitQubitsMap();
Expand Down
2 changes: 1 addition & 1 deletion QCSim/MPSSimulatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace QC {
public:
MPSSimulatorBase() = delete;

MPSSimulatorBase(size_t N, int addseed = 0)
MPSSimulatorBase(size_t N, unsigned int addseed = 0)
: lambdas(N - 1, LambdaType::Ones(1)), gammas(N, GammaType(1, 2, 1))
{
for (auto& gamma : gammas)
Expand Down
2 changes: 1 addition & 1 deletion QCSim/MPSSimulatorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace QC {
class MPSSimulatorImpl : public MPSSimulatorBase
{
public:
MPSSimulatorImpl(size_t N, int addseed = 0)
MPSSimulatorImpl(size_t N, unsigned int addseed = 0)
: MPSSimulatorBase(N, addseed)
{
}
Expand Down
2 changes: 1 addition & 1 deletion QCSim/MagicSquare.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Games {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

MagicSquare(int addseed = 0)
MagicSquare(unsigned int addseed = 0)
: BaseClass(4, addseed), distInd(1, 3), nrPlays(100)
{
const uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count() + addseed;
Expand Down
2 changes: 1 addition & 1 deletion QCSim/QMeansClustering2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace MachineLearning {
int cluster = 0;
};

QMeansClustering2D(int addseed = 0)
QMeansClustering2D(unsigned int addseed = 0)
: BaseClass(3, addseed)
{
const uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count() + addseed;
Expand Down
2 changes: 1 addition & 1 deletion QCSim/QuantumAdder.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace Adders {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

NQubitsAdderAlgorithm(size_t N = 3, int addseed = 0)
NQubitsAdderAlgorithm(size_t N = 3, unsigned int addseed = 0)
: BaseClass(3*N + 1, addseed) // the qubits for the two N inputs, N outputs and one for carry
{
BaseClass::setToBasisState(0);
Expand Down
2 changes: 1 addition & 1 deletion QCSim/QuantumAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace QC {
using RegisterClass = QubitRegister<VectorClass, MatrixClass>;
using GateClass = Gates::QuantumGateWithOp<MatrixClass>;

QuantumAlgorithm(size_t N = 3, int addseed = 0)
QuantumAlgorithm(size_t N = 3, unsigned int addseed = 0)
: reg(N, addseed)
{
assert(N > 0);
Expand Down
2 changes: 1 addition & 1 deletion QCSim/QuantumCountingAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace QuantumCounting {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

QuantumCountingAlgorithm(size_t PrecisionQubits = 6, size_t GroverQubits = 4, int addseed = 0)
QuantumCountingAlgorithm(size_t PrecisionQubits = 6, size_t GroverQubits = 4, unsigned int addseed = 0)
: BaseClass(PrecisionQubits + 2 * GroverQubits, addseed), precisionQubits(PrecisionQubits), groverQubits(GroverQubits), fourier(PrecisionQubits, 0, PrecisionQubits - 1)
{
assert(PrecisionQubits >= 3);
Expand Down
2 changes: 1 addition & 1 deletion QCSim/QuantumCryptograpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace QuantumCryptograpy {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

explicit BB84Protocol(int addseed = 0)
explicit BB84Protocol(unsigned int addseed = 0)
: BaseClass(1, addseed),
eavesdropping(false), randomEavesdropping(false)
{
Expand Down
2 changes: 1 addition & 1 deletion QCSim/QuantumEraser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Paradoxes {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

QuantumEraser(int addseed = 0)
QuantumEraser(unsigned int addseed = 0)
: BaseClass(2, addseed), eraser(false)
{
}
Expand Down
50 changes: 32 additions & 18 deletions QCSim/QubitRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace QC {
using GateClass = Gates::QuantumGateWithOp<MatrixClass>;
using BaseClass = QubitRegisterCalculator<VectorClass, MatrixClass>;

QubitRegister(size_t N = 3, int addseed = 0)
QubitRegister(size_t N = 3, unsigned int addseed = 0)
: NrQubits(N), NrBasisStates(1ULL << NrQubits),
registerStorage(VectorClass::Zero(NrBasisStates)),
uniformZeroOne(0, 1), recordGates(false)
Expand All @@ -23,6 +23,12 @@ namespace QC {

resultsStorage.resize(NrBasisStates);

if (addseed == 0)
{
std::random_device rd;
addseed = rd();
}

const uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count() + addseed;
std::seed_seq seed{ uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed >> 32) };
rng.seed(seed);
Expand All @@ -31,14 +37,20 @@ namespace QC {
}

// this is a special constructor, I need it for something in a derived work (closed source)
QubitRegister(size_t N, VectorClass& v, int addseed = 0)
QubitRegister(size_t N, VectorClass& v, unsigned int addseed = 0)
: NrQubits(N), NrBasisStates(1ULL << NrQubits),
uniformZeroOne(0, 1), recordGates(false)
{
assert(N > 0);
resultsStorage.resize(NrBasisStates);
registerStorage.swap(v);

if (addseed == 0)
{
std::random_device rd;
addseed = rd();
}

const uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count() + addseed;
std::seed_seq seed{ uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed >> 32) };
rng.seed(seed);
Expand Down Expand Up @@ -508,22 +520,6 @@ namespace QC {
registerStorage = savedSatateStorage;
}

protected:
inline void CheckQubits(const GateClass& gate, size_t qubit, size_t controllingQubit1, size_t controllingQubit2, size_t gateQubits) const
{
if (NrQubits == 0) throw std::invalid_argument("Qubit number is zero");
else if (NrQubits <= qubit) throw std::invalid_argument("Qubit number is too high");
else if (gateQubits == 2) {
if (NrQubits <= controllingQubit1) throw std::invalid_argument("Controlling qubit number is too high");
else if (qubit == controllingQubit1) throw std::invalid_argument("Qubit and controlling qubit are the same");
}
else if (gateQubits == 3)
{
if (NrQubits <= controllingQubit1 || NrQubits <= controllingQubit2) throw std::invalid_argument("Controlling qubit number is too high");
else if (qubit == controllingQubit1 || qubit == controllingQubit2 || controllingQubit1 == controllingQubit2) throw std::invalid_argument("Qubits must be different");
}
}

// the following ones should be used for 'repeated measurements' that avoid reexecuting the circuit each time
size_t MeasureNoCollapse()
{
Expand All @@ -543,6 +539,24 @@ namespace QC {
return state;
}

protected:
inline void CheckQubits(const GateClass& gate, size_t qubit, size_t controllingQubit1, size_t controllingQubit2, size_t gateQubits) const
{
if (NrQubits == 0) throw std::invalid_argument("Qubit number is zero");
else if (NrQubits <= qubit) throw std::invalid_argument("Qubit number is too high");
else if (gateQubits == 2) {
if (NrQubits <= controllingQubit1) throw std::invalid_argument("Controlling qubit number is too high");
else if (qubit == controllingQubit1) throw std::invalid_argument("Qubit and controlling qubit are the same");
}
else if (gateQubits == 3)
{
if (NrQubits <= controllingQubit1 || NrQubits <= controllingQubit2) throw std::invalid_argument("Controlling qubit number is too high");
else if (qubit == controllingQubit1 || qubit == controllingQubit2 || controllingQubit1 == controllingQubit2) throw std::invalid_argument("Qubits must be different");
}
}



// shortcut for measuring a single qubit
size_t MeasureNoCollapse(size_t qubit)
{
Expand Down
6 changes: 3 additions & 3 deletions QCSim/ShorAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace Shor {
public:
using BaseClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

ShorAlgorithmBase(size_t C = 15, size_t N = 7, size_t L = 3, size_t M = 4, int addseed = 0)
ShorAlgorithmBase(size_t C = 15, size_t N = 7, size_t L = 3, size_t M = 4, unsigned int addseed = 0)
: BaseClass(N, addseed),
Number(C), fx(L, M, C)
{
Expand Down Expand Up @@ -293,7 +293,7 @@ namespace Shor {
public:
using BaseClass = ShorAlgorithmBase<ShorAlgorithm<VectorClass, MatrixClass, ShorFunction>, VectorClass, MatrixClass>;

ShorAlgorithm(size_t C = 15, size_t N = 7, size_t L = 3, int addseed = 0)
ShorAlgorithm(size_t C = 15, size_t N = 7, size_t L = 3, unsigned int addseed = 0)
: BaseClass(C, N, L, N - L, addseed),
phaseEstimation(BaseClass::fx, N, L)
{
Expand Down Expand Up @@ -402,7 +402,7 @@ namespace Shor {
public:
using BaseClass = ShorAlgorithmBase<ShorAlgorithmWithoutTensorProduct<VectorClass, MatrixClass, ShorFunction>, VectorClass, MatrixClass, ShorFunction>;

ShorAlgorithmWithoutTensorProduct(size_t C = 15, size_t L = 3, int M = 4, int addseed = 0)
ShorAlgorithmWithoutTensorProduct(size_t C = 15, size_t L = 3, int M = 4, unsigned int addseed = 0)
: BaseClass(C, 2 * L + M - 1, L, M, addseed),
fourier(2 * L + M - 1, 0, L - 1)
{
Expand Down
2 changes: 1 addition & 1 deletion QCSim/ShorCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ErrorCorrection {
using BaseClass = ErrorCorrectionBase<VectorClass, MatrixClass>;
using AlgorithmClass = QC::QuantumAlgorithm<VectorClass, MatrixClass>;

ShorCode(int addseed = 0)
ShorCode(unsigned int addseed = 0)
: BaseClass(9, addseed), errorType(Flip), iy(std::complex<double>(0., 1.) * y.getRawOperatorMatrix())
{
}
Expand Down
Loading

0 comments on commit 13742c9

Please sign in to comment.