Giter Club home page Giter Club logo

syngular's Introduction

Tensor Network in Python

version development maintenance launched


Introduction

Syngular emerged in the context of my quantum computing project at CentraleSuépelec. I was working on the efficient simulation of quantum circuits on classical computers. The last work on the subject that tackled this issue was using Matrix Product State as a base to represent the 2^N parameters quantum state of a quantum system (What limits the simulation of quantum computers? Yiqing Zhou, E. Miles Stoudenmire, Xavier Waintal) with only a fraction of parameters at the price of approximation on the singular values. I dived into this world of Matrix Product State and Operator leading me to the theorisation of Tensor Network with Yvan Osedelets works and I created a simple simulator (too simple). Then, in my second year at CentraleSupélec in the same team project, I had the opportunity to work on how to use tensor networks that came from the quantum world to machine learning as to compress efficiently neural network.

As so, I developed this Python package to create easily Tensor Network and simulate as well Quantum Circuit as Neural Networks and optimization of function.


Getting Started

Installation

First, you will have to install the package

pip install syngular

MatrixProductState & MatrixProductOperator

from syngular.tensor import MatrixProductState
from syngular.tensor import MatrixProductOperator

import numpy as np

tensor_W = np.arange(16**6).reshape((16,16,16, 16,16,16))
tensor_X = np.arange(16**3).reshape((16,16,16))

W = MatrixProductOperator(tensor_W, bond_shape=(16,16,))
W.decompose()

X = MatrixProductState(tensor_X, bond_shape=(4,4,))
X.decompose()

T = MatrixProductOperator.random((16,16,16), (16,16,16), (8,8,))
O = MatrixProductOperator.zeros((16,16,16), (16,16,16), (4,4,))
U = MatrixProductState.random((16,16,16), (8,8,))

W = W >> 4
T = T >> 2

Z = ((T + W) @ T) @ X

print(X | U)
print(Z | X)

Z = Z >> 16
Z.left_orthonormalization()

print(np.diag(Z.left_orthogonality(0)))
print(np.diag(Z.left_orthogonality(1)))

Quantum Simulation

from syngular.quantum import Circuit, Qbit
import syngular.quantum.gate as gate

Qbit.LSB = False

circ = Circuit(size=15, structure=[
    (gate.X, 0),
    (gate.X, 2),
    (gate.H, 0),
    (gate.H, 2),
])

circ.run()
circ.add((gate.H, 1))
circ.run()

#########################################################

def verity_table(g, name):
    import itertools

    size = int(len(g.shape) // 2)
    length = ((3+size)*2+1)
    
    print('------------- Vertity Table --------------')
    print(f' > Gate : {name}')
    print("="*length)
    print("| inp > out |")
    print("="*length)

    for b in itertools.product([0, 1], repeat=size):
        qbit = Qbit(size)
        for i in range(len(b)): if b[i] == 1: qbit @= (gate.X, i)
        output = qbit @ (g, 0)

        print("|",qbit.to_binary(), '|', output.to_binary(), "|")
        print("-"*length)

verity_table(gate.TOFFOLI, "Toffoli")

######################################################

Qbit.LSB = True

qbit = Qbit(4)
qbit @= (gate.X, 0)
qbit @= (gate.X, 2)
print(qbit.to_binary())

qbit = qbit.swap(0,3)
print(qbit.to_binary())

qbit = qbit.swap(0,2)
print(qbit.to_binary())

qbit = qbit.swap(3,2)
print(qbit.to_binary())

syngular's People

Contributors

antoine311200 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.