aromanro / QCSim

Quantum computing simulator
GNU General Public License v3.0
36 stars 5 forks source link
computational-physics eigen physics physics-simulation quantum quantum-algorithms quantum-computing quantum-computing-algorithms quantum-information simulation simulator

QCSim

Quantum computation simulator

Codacy Badge CodeFactor

Build on Ubuntu

Blog entry on the Computational Physics Blog: Quantum Computing Simulator

Implemented algorithms:

Quantum error correction:

Quantum adders:

Simulation of quantum simulation:

Paradoxes (although some of the above might be considered paradoxes as well):

Quantum games:

Distributed quantum computing:

Quantum Machine Learning

Optimization

First, a brief description about usage, if somebody wants to use it in a separate project. The minimal thing to do is to add to include directories the Eigen directory and this project directory (the one that contains the source files). That's about it. Here is some minimal code that can be extended to execute a quantum algorithm:

#include <iostream>

#include "QubitRegister.h"

int main()
{
    QC::QubitRegister reg(3);
    QC::Gates::PauliXGate x;
    reg.ApplyGate(x, 0);
    unsigned int m = reg.MeasureAll();

    std::cout << "Result should be 1, it's: " << m << std::endl;

    return 0;
}

This is the statevector simulator, which is used in all examples so far. The project also has a Matrix Product States (the simplest Tensor Network, the 1D variant that has qubit sites on a chain, see the TEBD project for more details) simulator implementation, but it's work in progress started five days ago (when writing this), I still have to do some proper tests and fix the issues. If you try it and it doesn't work, it's not my fault, it's yours :)

You should compile it with c++ 17 (at least). Of course, if one wants to use some other stuff in there, more headers might need to be included. The 'test' cpp files are intended for both tests and examples, not necesarily for usage in some other app, including the headers should usually be enough.

I would recommend trying to use the cmake file for compiling on linux or mac (or something similar), turning on openmp increases the speed a lot (as in several times faster, depending on the processor) and on top of that turning on AVX2 (not available for apple 'metal') reduces the execution time again (something like 35 seconds instead of over 50 on my computer for all examples/tests).

The most important code to look into are in QuantumGate.h (namespace QC::Gates), where the quantum gates are implemented. The code is quite straightforward, a little bit more complex being the implementation of getOperatorMatrix (obsoleted now, it's not used anymore for 1, 2 or 3 qubits gates, creation and multiplication with the big operator matrices for those gates is optimized out now), which extends the operator matrix from the matrix that involves only the qubits on which the operator is applied, to all qubits from the register. Another header to look into is QubitRegister.h. The code is again straightforward, a little bit more complex being the one that does the measurement on a part of the register only. Also since the usage of the big operator matrices obtained by tensor product for 1, 2, 3 qubits gates is obsoleted, the code from QubitRegister::ApplyGate also got more complex so it might not be so easy to grasp. One used everywhere is the QuantumAlgorithm.h although that one is very simple, mostly a proxy for the qubit register. Another source file worth looking into is Utils.h, since BellState and MeasurementBasis are used in several algorithms.

For general operators/gates applied on the register, see the NQubitsQuantumGate and NQubitsControlledQuantumGate "subalgorithm" classes (for implementations with a big matrix) or NControlledGateWithAncilla.h and NControlledNotWithAncilla.h (for implementations with simple gates and ancilla qubits).

To see various algorithms in action, look into the Test files.

I intend to come from time to time to add more algorithms, but for now I think I have more here than I intended when I started the project, so I'll leave it like this for a while (except maybe some improvements of the existing code and bug fixes if necessary).

Required libraries

Dealing with matrices is done with the help of Eigen. The quantum fourier transform is checked against FFTW for Schrodinger quantum simulation. I guess using this library could be avoided, I actually provided several methods of solving the equation in the code, maybe I'll switch to some other one in the tests in the future. For now the check is done with FFTW.

Bibliograpy

[!TIP] I mentioned some things on the corresponding blog page, so please check it out, but I'll also refer here the tutorials papers I mentioned there and the code for them, which I also provided in another repository. If you want to learn qiskit, I higly recommend those.