Skip to content

Simulation of qubit and quantum gates written in C++

Notifications You must be signed in to change notification settings

Haimasker/Qubit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Qubit

Simple C++ implementation of qubits and it's operations.

GitHub code size in bytes Number of lines of code Code language count GitHub top language


Preamble

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.


Introduction

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 $|a|^2$ and $|b|^2$ probabilities respectively.
$|a|^2 + |b|^2$ (sum of squared magnitudes of probability amplitudes) is equal to 1.

Operations with qubit register is provided due to different quantum logic gates.


Class fields

All of Qubit class fields are private

  1. qubitNumber - number of qubits in the register.
unsigned qubitNumber;
  1. amplitudes - probability amplitudes of the register.
Matrix amplitudes;
  1. generator - random generator for qubit state measuring.
std::default_random_engine generator;

Constructors and destructor

  1. The Constructor receiving the number of qubits in the register.
    By default has value 0.
    Probability amplitudes are uniformly distributed between the states of the qubit.
Qubit(unsigned = 0) noexcept;

  1. Copy constructor.
Qubit(const Qubit&) noexcept;

  1. Destructor.
virtual ~Qubit();

Operators

  1. 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&);

  1. 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;

  1. Copy assignment operator.
Qubit& operator = (const Qubit&);

  1. Multiplication operator.
    Merges qubits registers.
Qubit operator * (const Qubit&);

  1. Multiplication assignment operator.
    Merges qubits registers.
Qubit& operator *= (const Qubit&);

  1. Equal operator.
    Determines if qubits are equal.
bool operator == (const Qubit&);

  1. Non-equal operator.
    Determines if qubits are not-equal.
bool operator != (const Qubit&);

  1. Multiplication operator.
    Multiplies this->amplitudes by given Matrix.
Qubit operator * (Matrix&);

  1. Multiplication assignment operator.
    Multiplies this->amplitudes by given Matrix.
Qubit& operator *= (Matrix&);
  1. Equal operator.
    Determines if this->amplitudes is equal to given Matrix.
bool operator == (const Matrix&);

  1. Non-equal operator.
    Determines if this->amplitudes is not equal to given Matrix.
bool operator != (const Matrix&);

  1. Output operator.
    Prints qubit register to output.
friend std::ostream& operator << (std::ostream&, Qubit&);

Quantum logic gates

  1. Identity gate.
    Returns an identity matrix with the size of a qubit register.
static Matrix identity();

  1. Identity gate.
    Applies an identity matrix to this->amplitudes.
    Returns true because this is always possible.
static bool identity(Qubit&);

  1. Pauli X gate.
    Returns matrix representing Pauli X gate.
static Matrix pauliX();

  1. Pauli X gate.
    Applies Pauli X gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool pauliX(Qubit&);

  1. Pauli Y gate.
    Returns matrix representing Pauli Y gate.
static Matrix pauliY();

  1. Pauli Y gate.
    Applies Pauli Y gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool pauliY(Qubit&);

  1. Pauli Z gate.
    Returns matrix representing Pauli Z gate.
static Matrix pauliZ();

  1. Pauli Z gate.
    Applies Pauli Z gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool pauliZ(Qubit&);

  1. Hadamard gate.
    Returns matrix representing Hadamard gate.
static Matrix hadamard();

  1. Hadamard gate.
    Applies Hadamard gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool hadamard(Qubit&);

  1. Phase shift gate.
    Returns matrix representing phase shift gate with given angle.
static Matrix phaseShift(double);

  1. Phase shift gate.
    Applies phase shift gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool phaseShift(Qubit&, double);

  1. Swap gate.
    Returns matrix representing swap gate.
static Matrix swap();

  1. Swap gate.
    Applies swap gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool swap(Qubit&);

  1. Rotation X gate.
    Returns matrix representing rotation X gate with given angle.
static Matrix rotX(double);

  1. Rotation X gate.
    Applies rotation X gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool rotX(Qubit&, double);

  1. Rotation Y gate.
    Returns matrix representing rotation Y gate with given angle.
static Matrix rotY(double);

  1. Rotation Y gate.
    Applies rotation Y gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool rotY(Qubit&, double);

  1. Rotation Z gate.
    Returns matrix representing rotation Z gate with given angle.
static Matrix rotZ(double);

  1. Rotation Z gate.
    Applies rotation Z gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool rotZ(Qubit&, double);

  1. 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 representing angle 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 .


  1. Controlled gate.
    Applies given controlled gate to this->amplitudes.
    Returns true if this is possible, otherwise returns false.
static bool controlled(Qubit&, unsigned, std::string, std::string = "");

Methods

  1. 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>);

  1. concentrate.
    Sets the probability amplitude of the state under the given index to 1.
    Others become 0.
void concentrate(unsigned);

  1. reset.
    Uniformly distributes the probability amplitudes between all states of the qubit.
void reset();

  1. getAmplitudes.
    Returns this->amplitudes.
Matrix getAmplitudes();

  1. getQubitNumber.
    Returns this->qubitNumber.
unsigned getQubitNumber();

  1. getProbabilities.
    Returns vector of qubit states probabilities.
std::vector<double> getProbabilities();

  1. measure.
    Prints measured state of qubit.
void measure();

  1. printProbabilities.
    Prints qubit states probabilities.
void printProbabilities();