Introducing Arithmetica — A simple but useful math library with Quantum Computing Simulator

Deepak Battini
5 min readMay 11, 2019

Arithmetica is another hobby project started in love with Maths and Quantum Physics. It’s strange I use to run from these maths and physics subjects in school but now I think I find it more interesting when I am able to understand the simple and humbleness behind it. It always looks daunting when we talk about these subject but trust me it’s as easy as learning any language, only thing you have to think of the problems in numbers and equations 🙂

To summarise, arithmetica is developed to help students, professional or scientists to learn or prototype work related to applied mathematics and quantum physics.

Following are the features:

  • Full-featured implementation of mathematical data types including complex numbers’ quaternion and various sizes of matrices and vectors.
  • Support for most of the math functions for basic maths, linear algebra, rounding, comparison, log, exp, powering etc.
  • Support single-precision and double-precision floating point types.
  • Covers most of the functions required to use Complex vectors and matrix.
  • Geometric data types and algorithms for 2D and 3D — distance and intersection methods, bounding volumes etc.
  • Random number and noise generation with around 10 algorithms
  • Strong-Typed collections for the library’s data types.
  • Support for Multi-Dimensional array in case 2D, 3D are not sufficient.

How to use the library

  1. Create a new .NET core or Windows console project.
  2. Right-Click on the solution and select Managed NuGet References.
  3. Search for “Arithmetica”, select and install the latest version
  4. Open the class file and import namespace “using Arithmetica” to start using the library and its functions.

Let’s start with a basic example to work with vectors. According to wikipedia “In mathematics, physics, and engineering, a Euclidean vector (sometimes called a geometric or spatial vector, or-as here-simply a vector) is a geometric object that has magnitude (or length) and direction. Vectors can be added to other vectors according to vector algebra. A Euclidean vector is frequently represented by a line segment with a definite direction, or graphically as an arrow, connecting an initial point A with a terminal point B.”

Let’s define two vector in 2D space and find the Euclidean distance between them:

Start with importing the namespace. Since the library support Single and Double precision, you can use any one of them:

using Arithmetica.Geometry2D; using Arithmetica.LinearAlgebra.Single;

Now the function to calculate the distance

//Point A in the 2D space
Vector2 A = new Vector2(10, 20);

//Point B in the 2D space
Vector2 B = new Vector2(70, 80);

//Find the Euclidean distance
var distance = DistanceMethods.Distance(A, B);

Let see another example for Complex Matrix multiplication. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x2= −1. Because no real number satisfies this equation, i is called an imaginary number. For the complex number a + bi, a is called the real part, and b is called the imaginary part. Despite the historical nomenclature “imaginary”, complex numbers are regarded in the mathematical sciences as just as “real” as the real numbers, and are fundamental in many aspects of the scientific description of the natural world.

Lets define a 2 x 2 Matrix of complex number and a Vector of Complex number.

//Define a 2x2 complex matrix
ComplexMatrix matrix = new ComplexMatrix(2, 2);
matrix[0, 0] = new Complex(2, 2);
matrix[0, 1] = new Complex(1, 2);
matrix[1, 0] = new Complex(3, 1);
matrix[1, 1] = new Complex(2, 3);
Console.WriteLine("First Matrix");
Console.WriteLine(matrix.ToString());

//Define a comple vector of length 2
ComplexVector vector = new ComplexVector(2);
vector[0] = new Complex(2, 1);
vector[1] = new Complex(1, 3);
Console.WriteLine("Second Vector");
Console.WriteLine(vector.ToString());

// Multiplying a 2x2 matrix with a vector will result a vector
var result = matrix * vector;

Console.WriteLine("Result Vector");
Console.WriteLine(result.ToString());

Complex numbers is widely used in the quantum physics area to represent the values of the particles in certain state weather single or super-positioned. In the coming few blogs I will lay down a series of the post explaining the fundamental behavior of the mysterious quantum world and how to simulate using the “Arithmetica Quantum” library which is so far well implemented with most of the gates to design a circuit. Below is a quick view of the teleportation examples which is the most common example to explain Superposition and Entanglement behavior.

As per Wikipedia “Quantum teleportation is a process by which quantum information can be transmitted from one location to another, with the help of classical communication and previously shared quantum entanglement between the sending and receiving location”

//Create a register with 2 Qubits
QuantumRegister register = new QuantumRegister(2);

//Create a blank circuit with the regiter initialised
QuantumCircuit circuit = new QuantumCircuit(register);

//Initialize the tranported counter
int transported = 0;

//Let try to teleport 25 quantum information
for (int i = 0; i < 25; i++)
{
var send = GetRandom();

//Initial the first Qubit with the particle to be teleported
circuit.INIT(0, send);

//Hadamard gate to apply superposition to the first quantum bit which means the first qubit will have both 0 and 1
circuit.H(0);

//Controlled not gate to entangle the two qubit
circuit.CNOT(0, 1);

//Measure the first will collapse the quantum state and bring it to reality whic will be either one or zero
circuit.Measure(0);

//Store the first state
var res1 = register[0].QState;

//Now measue the second particle and store the value
circuit.Measure(1);
var res2 = register[1].QState;

Console.WriteLine("Send: {0}, Received: {1}", res1, res2);

//If you compare the result the two result will be same which states that the information is teleported.
if (res1 == res2)
transported++;

register.Reset();
}

//var result = circuit.Execute(1000);
Console.WriteLine("Teleported count: " + transported);

This is just an intro blog, and I will posting a learning series to build your knowledge combining both maths and physics 🙂

Here is the link to API docs: https://deepakkumar1984.github.io/Arithmetica/api/index.html

Github Project: https://github.com/deepakkumar1984/Arithmetica/

Originally published at https://www.tech-quantum.com on May 11, 2019.

--

--

Deepak Battini

Programmer and founder of blazorly.com. passionate open-source contributor, loves to combine cutting-edge tech expertise.