Simple C++ implementation of qubits and it's operations.
Preamble · Introduction · Class fields · Constructors and destructor · Operators · Quantum logic gates · Methods
The purpose of this project is to create Qubit
class that emulates working with qubits.
Qubit quantum register is presented via Matrix of complex numbers.
Qubit is the quantum version of classic binary bit, but it is in superposition of 0 and 1 state.
Qubit register is the vector of probability amplitudes: a|0〉 + b|1〉 there a
and b
are complex numbers.
It means that qubit can has value 0
or 1
with
Operations with qubit register is provided due to different quantum logic gates.
All of Qubit
class fields are private
qubitNumber
- number of qubits in the register.
unsigned qubitNumber;
amplitudes
- probability amplitudes of the register.
Matrix amplitudes;
generator
- random generator for qubit state measuring.
std::default_random_engine generator;
- The Constructor receiving the number of qubits in the register.
By default has value0
.
Probability amplitudes are uniformly distributed between the states of the qubit.
Qubit(unsigned = 0) noexcept;
- Copy constructor.
Qubit(const Qubit&) noexcept;
- Destructor.
virtual ~Qubit();
- Call operator for non-const objects receiving
index
of the qubit state.
Indices are cycled, so there is no out-of-range error.
std::complex<double>& operator () (const unsigned&);
- Call operator for const objects receiving
index
of the qubit state.
Indices are cycled, so there is no out-of-range error.
const std::complex<double>& operator () (const unsigned&) const;
- Copy assignment operator.
Qubit& operator = (const Qubit&);
- Multiplication operator.
Merges qubits registers.
Qubit operator * (const Qubit&);
- Multiplication assignment operator.
Merges qubits registers.
Qubit& operator *= (const Qubit&);
- Equal operator.
Determines if qubits are equal.
bool operator == (const Qubit&);
- Non-equal operator.
Determines if qubits are not-equal.
bool operator != (const Qubit&);
- Multiplication operator.
Multipliesthis->amplitudes
by givenMatrix
.
Qubit operator * (Matrix&);
- Multiplication assignment operator.
Multipliesthis->amplitudes
by givenMatrix
.
Qubit& operator *= (Matrix&);
- Equal operator.
Determines ifthis->amplitudes
is equal to givenMatrix
.
bool operator == (const Matrix&);
- Non-equal operator.
Determines ifthis->amplitudes
is not equal to givenMatrix
.
bool operator != (const Matrix&);
- Output operator.
Prints qubit register to output.
friend std::ostream& operator << (std::ostream&, Qubit&);
Identity gate
.
Returns an identity matrix with the size of a qubit register.
static Matrix identity();
Identity gate
.
Applies an identity matrix tothis->amplitudes
.
Returnstrue
because this is always possible.
static bool identity(Qubit&);
Pauli X gate
.
Returns matrix representing Pauli X gate.
static Matrix pauliX();
Pauli X gate
.
Applies Pauli X gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool pauliX(Qubit&);
Pauli Y gate
.
Returns matrix representing Pauli Y gate.
static Matrix pauliY();
Pauli Y gate
.
Applies Pauli Y gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool pauliY(Qubit&);
Pauli Z gate
.
Returns matrix representing Pauli Z gate.
static Matrix pauliZ();
Pauli Z gate
.
Applies Pauli Z gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool pauliZ(Qubit&);
Hadamard gate
.
Returns matrix representing Hadamard gate.
static Matrix hadamard();
Hadamard gate
.
Applies Hadamard gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool hadamard(Qubit&);
Phase shift gate
.
Returns matrix representing phase shift gate with givenangle
.
static Matrix phaseShift(double);
Phase shift gate
.
Applies phase shift gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool phaseShift(Qubit&, double);
Swap gate
.
Returns matrix representing swap gate.
static Matrix swap();
Swap gate
.
Applies swap gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool swap(Qubit&);
Rotation X gate
.
Returns matrix representing rotation X gate with givenangle
.
static Matrix rotX(double);
Rotation X gate
.
Applies rotation X gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool rotX(Qubit&, double);
Rotation Y gate
.
Returns matrix representing rotation Y gate with givenangle
.
static Matrix rotY(double);
Rotation Y gate
.
Applies rotation Y gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool rotY(Qubit&, double);
Rotation Z gate
.
Returns matrix representing rotation Z gate with givenangle
.
static Matrix rotZ(double);
Rotation Z gate
.
Applies rotation Z gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool rotZ(Qubit&, double);
Controlled gate
.
Returns necessary controlled gate.
First parameter is the total number of qubits in controlled gate.
Second parameter is the name of the controlled gate, remainig first qubits become controling.
Third parameter is the string representingangle
for the gates where it is necessary.
static Matrix controlled(unsigned, std::string, std::string = "");
For example:
controlled(3, "swap")
will produce CSWAP gate,
conctrolled(3, "NOT")
will produce CCNOT gate,
controlled(2, "phaseShift", "1.5")
will produce C-phaseShift gate with angle 1.5 .
Controlled gate
.
Applies given controlled gate tothis->amplitudes
.
Returnstrue
if this is possible, otherwise returnsfalse
.
static bool controlled(Qubit&, unsigned, std::string, std::string = "");
distribute
.
Uniformly distributes the probability amplitudes between given states of the qubit.
If given index of state is negative, then this state's probability amplitude becomes negative.
void distribute(std::vector<int>);
concentrate
.
Sets the probability amplitude of the state under the given index to 1.
Others become 0.
void concentrate(unsigned);
reset
.
Uniformly distributes the probability amplitudes between all states of the qubit.
void reset();
getAmplitudes
.
Returnsthis->amplitudes
.
Matrix getAmplitudes();
getQubitNumber
.
Returnsthis->qubitNumber
.
unsigned getQubitNumber();
getProbabilities
.
Returns vector of qubit states probabilities.
std::vector<double> getProbabilities();
measure
.
Prints measured state of qubit.
void measure();
printProbabilities
.
Prints qubit states probabilities.
void printProbabilities();