Ejemplo n.º 1
0
def test_exatn():

    a1 = np.random.randn(2, 3, 4)
    print('A1 shape: ', a1.shape)

    b1 = np.random.randn(2, 3, 4, 5)
    print('B1 shape: ', b1.shape)

    exatn.createTensor('C1', [3, 4, 5], 0.0)
    exatn.createTensor('A1', a1.copy(order='F'))
    exatn.createTensor('B1', b1.copy(order='F'))

    exatn.contractTensors('C1(b,c,d)=A1(a,b,c)*B1(a,b,c,d)', 1.0)

    c1 = exatn.getLocalTensor('C1')
    print('C1 shape: ', c1.shape)
    print('ExaTN result c1 = a1 * b1:\n', c1)

    d1 = np.einsum('abc, abcd->bcd', a1, b1)
    print('D1 shape: ', d1.shape)
    print('NumPy result c1 = a1 * b1:\n', d1)

    exatn.destroyTensor('B1')
    exatn.destroyTensor('A1')
    exatn.destroyTensor('C1')
Ejemplo n.º 2
0
def test_exatn():
    a1 = np.array([
         [1., 0., 0.],
         [0., 1., 1.]])
    print('A1 shape: ',a1.shape)

    b1 = np.array([
         [ 1., 0.,  3., 0.],
         [ 1., 1.,  2., 2.],
         [-1., 1., -2., 2.]])
    print('B1 shape: ',b1.shape)

    exatn.createTensor('C1', [2, 4], 0.0)
    exatn.createTensor('A1', a1.copy(order='F'))
    exatn.createTensor('B1', b1.copy(order='F'))

    exatn.contractTensors('C1(a,c)=A1(a,b)*B1(b,c)',1.0)

    c1 = exatn.getLocalTensor('C1')
    print('C1 shape: ',c1.shape)

    d1 = np.dot(a1, b1)
    print('D1 shape: ',d1.shape)

    print('NumPy result c1 = a1 * b1:\n', d1)
    print('ExaTN result c1 = a1 * b1:\n', c1)

    exatn.destroyTensor('B1')
    exatn.destroyTensor('A1')
    exatn.destroyTensor('C1')
Ejemplo n.º 3
0
def run(n, num_iter, num_batch):
    tn.createTensor("C", np.zeros((n, n)))
    tn.createTensor("A", np.random.rand(n, n))
    tn.createTensor("B", np.random.rand(n, n))
    nops = 2 * n**3 / 1e9
    elapsed = benchmark(num_iter, num_batch, foo)
    tn.getLocalTensor("C")
    tn.destroyTensor("A")
    tn.destroyTensor("B")
    tn.destroyTensor("C")
    print(n, ", ", nops / elapsed)
import exatn, numpy as np

exatn.createTensor("Sx", np.array([[0., 1.], [1., 0.]]))
exatn.createTensor("Sy", np.array([[0., -1.j], [1.j, 0.]]))
exatn.print("Sx")
exatn.print("Sy")


def negate(data):
    data *= -1.


exatn.transformTensor("Sx", negate)
exatn.print("Sx")

exatn.transformTensor("Sx", negate)
exatn.print("Sx")

exatn.transformTensor("Sy", negate)
exatn.print("Sy")
Ejemplo n.º 5
0
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn, numpy as np

#Quantum Circuit:
#Q0----H---------
#Q1----H----C----
#Q2----H----N----

#Define the initial qubit state vector:
qzero = np.array([1.0, 0.0], dtype=complex)
hadamard = np.array([[1., 1.],[1., -1.]], dtype=complex)

cnot = np.reshape(np.array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 0, 1],[0, 0, 1, 0]], dtype=complex), (2,2,2,2))

exatn.createTensor('Q0', qzero)
exatn.createTensor('Q1', qzero)
exatn.createTensor('Q2', qzero)

exatn.createTensor('H', hadamard)
exatn.createTensor('CNOT', cnot)

exatn.registerTensorIsometry('H', [0], [1])
exatn.registerTensorIsometry('CNOT', [0,1], [2,3])

circuit = exatn.TensorNetwork('QuantumCircuit')
circuit.appendTensor(1, 'Q0')
circuit.appendTensor(2, 'Q1')
circuit.appendTensor(3, 'Q2')

circuit.appendTensorGate(4, 'H', [0])
Ejemplo n.º 6
0
import exatn, numpy as np

# Create some numpy arrays
s = np.random.rand(2)
r = np.random.rand(2)

# Create the ExaTN tensors
exatn.createTensor("Z0", 0.0)
exatn.createTensor("S", s)
exatn.createTensor("R", r)

# Print S, R
exatn.print("S")
exatn.print("R")


# Demonstrate transformTensor interface by
# negating the tensor data in S
def negate(data):
    data *= -1.


exatn.transformTensor("S", negate)
exatn.print("S")

# Compute the dot product, store in Z0
exatn.evaluateTensorNetwork('MyTN', 'Z0() = S(a) * R(a)')
exatn.print('Z0')

z0_data = exatn.getLocalTensor('Z0')
Ejemplo n.º 7
0
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn
import numpy as np

# Demonstrate simple tensor network manipulation

exatn.createTensor('X', [2, 2], 0)
exatn.createTensor('Y', [2, 2], 0)
exatn.createTensor('Z', [2, 2], 0)
exatn.initTensorRnd('X')
exatn.initTensorRnd('Y')
exatn.initTensorRnd('Z')

tNet = exatn.TensorNetwork('test')
tNet.appendTensor(1, 'X')
tNet.appendTensor(2, 'Y')
tNet.appendTensor(3, 'Z')
# print tensor network
tNet.printIt()
tNetOriginal = exatn.TensorNetwork(tNet)

# Merge X and Y
pattern = tNet.mergeTensors(1, 2, 4)
print("After merge:")
tNet.printIt()
# Print the generic merge pattern
print(pattern)
# Create the merged tensor
pattern = pattern.replace("D", tNet.getTensor(4).getName())
Ejemplo n.º 8
0
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn, numpy as np

qzero = np.array([1.0, 0.0], dtype=complex)
hadamard = np.array([[1., 1.],[1., -1.]], dtype=complex)

nQubits = 10
[exatn.createTensor('Q'+str(i), qzero) for i in range(nQubits)]

exatn.createTensor('H', hadamard)
exatn.registerTensorIsometry('H', [0], [1])

tensorCounter = 1
circuit = exatn.TensorNetwork('QuantumCircuit')
[circuit.appendTensor(tensorCounter+c, 'Q'+str(i)) for i,c in enumerate(range(nQubits))]
tensorCounter += nQubits

qubitReg = exatn.TensorNetwork(circuit)
qubitReg.rename('QubitKet')

[circuit.appendTensorGate(tensorCounter+c, 'H', [i]) for i, c in enumerate(range(nQubits))]
tensorCounter += nQubits

circuit.printIt()

inverse = exatn.TensorNetwork(circuit)
inverse.rename('InverseCircuit')

[inverse.appendTensorGate(tensorCounter+c, 'H', [nQubits - i - 1], True) for i,c in enumerate(range(nQubits))]
Ejemplo n.º 9
0
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn, numpy as np

qzero = np.array([1.0, 0.0], dtype=complex)
unitary = np.reshape(np.array([1, -1j, -1j, 1], dtype=complex), (2, 2))

exatn.createTensor('Q0', qzero)
exatn.createTensor('U', unitary)
exatn.registerTensorIsometry('U', [0], [1])

circuit = exatn.TensorNetwork('QuantumCircuit')
circuit.appendTensor(1, 'Q0')
circuit.appendTensorGate(2, 'U', [0])
circuit.printIt()

conj_circuit = exatn.TensorNetwork(circuit)
conj_circuit.rename('ConjugatedCircuit')
conj_circuit.conjugate()
conj_circuit.printIt()

assert (exatn.evaluate(circuit))
assert (exatn.evaluate(conj_circuit))

print(exatn.getLocalTensor(circuit.getTensor(0).getName()))
print(exatn.getLocalTensor(conj_circuit.getTensor(0).getName()))
Ejemplo n.º 10
0
def conjugate(tName):
    exatn.createTensor(tName + 'conj', np.conj(exatn.getLocalTensor(tName)))
Ejemplo n.º 11
0
chosen = np.transpose(chosen, (0, 2, 1, 3))
print(np.reshape(chosen, (4, 4)))

#isometryGuess = np.array([[[1,0],[0,0]],[[0,1/np.sqrt(2)],[1/np.sqrt(2),0]],[[0,0],[0,0]],[[0,0],[0,1]]]) + np.array([[[0,0],[0,0]],[[0,0],[0,0]],[[0,1/np.sqrt(2)],[-1/np.sqrt(2),0]],[[0,0],[0,0]]])*1j
#isometryGuess = np.array([[[1,0],[0,0]],[[0,.5],[.5,0]],[[0,.5],[.5,0]],[[0,0],[0,1]]]) + np.array([[[0,0],[0,0]],[[0,-.5],[.5,0]],[[0,.5],[-.5,0]],[[0,0],[0,0]]])*1j
# isometryGuess = np.array([[[1,0],[0,0]],[[0,1],[0,0]],[[0,0],[1,0]],[[0,0],[0,1]]]) + np.array([[[0,0],[0,0]],[[0,0],[0,0]],[[0,0],[0,0]],[[0,0],[0,0]]])*1j
angles = [np.random.uniform(0, np.pi) for i in range(15)]
SSU = anglesToTensor(angles)
SSU = anglesToTensor([
    -0.020491765812750733, 1.0849813938928623, 0.1823094915565751,
    -0.01892176877947449, 1.8492804753843204, 0.16753058604914656,
    1.5707963267913112, -7.266906333088912e-13, 1.5707963267988387,
    2.4032056784753717, 0.13338369000216244, 0.5760507158714833,
    0.28699706946011383, 0.3217444561240923, -0.11511191774971147
])
I = exatn.createTensor('I', np.reshape(SSU, (4, 2, 2)))
conjugate('I')
#test I is isometry, II should be identity
exatn.createTensor('II', [4, 4], 0 + 0j)
exatn.contractTensors('II(a,b)=I(a,i,j)*Iconj(b,i,j)')
print('I doubled')
exatn.print('II')

rho = np.reshape(chosen, (4, 4))
w, sig1, e = np.linalg.svd(rho)[0], np.linalg.svd(rho)[1], np.linalg.svd(
    rho)[2]
w, e = np.reshape(w, (2, 2, 4)), np.reshape(e, (4, 2, 2))

wi = np.tensordot(w.copy(), exatn.getLocalTensor('I').copy(), axes=((2), (0)))
ei = np.tensordot(e.copy(), exatn.getLocalTensor('I').copy(), axes=((0), (0)))
Ejemplo n.º 12
0
    dimVars = dimVars[:-1]
    dimVarList = dimVars.split("; ")
    vardict = {}
    for var in dimVarList:
        varName, varVal = var.split(":")
        vardict[varName] = varVal
        gFlops *= float(varVal)
    exprSplits = re.split(' = | * ', expr)
    rhsTensor = exprSplits[0]
    lhsTensorOperand1 = exprSplits[1]
    lhsTensorOperand2 = exprSplits[3]
    #print("{}:{}:{}".format(rhsTensor, lhsTensorOperand1, lhsTensorOperand2))

    # LHS (result) 'C' tensor
    assert (rhsTensor[0] == "C")
    exatn.createTensor("C", getTensorDimArray(rhsTensor, vardict), 0.0)

    # RHS A tensor
    assert (lhsTensorOperand1[0] == "A")
    exatn.createTensor("A", getTensorDimArray(lhsTensorOperand1, vardict), 0.0)
    # Initialize random tensor body
    exatn.initTensorRnd("A")

    # RHS B tensor
    assert (lhsTensorOperand2[0] == "B")
    exatn.createTensor("B", getTensorDimArray(lhsTensorOperand2, vardict), 0.0)
    # Initialize random tensor body
    exatn.initTensorRnd("B")

    # Convert [] to () (ExaTN convention)
    exatnExpr = expr.replace('[', '(').replace(']', ')')
Ejemplo n.º 13
0
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn

exatn.createTensor('Z0')
exatn.createTensor('T0', [2,2], .01)
exatn.createTensor('T1', [2,2,2], .01)
exatn.createTensor('T2', [2,2], .01)
exatn.createTensor('H0', [2,2,2,2], .01)
exatn.createTensor('S0', [2,2], .01)
exatn.createTensor('S1', [2,2,2], .01)
exatn.createTensor('S2', [2,2], .01)

exatn.evaluateTensorNetwork('{0,1} 3-site MPS closure', 'Z0() = T0(a,b) * T1(b,c,d) * T2(d,e) * H0(a,c,f,g) * S0(f,h) * S1(h,g,i) * S2(i,e)')

z0 = exatn.getLocalTensor('Z0')
assert(abs(z0 - 5.12e-12) < 1e-12)
print(z0)
Ejemplo n.º 14
0
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn, numpy as np

# Declare MPS tensors:
exatn.createTensor('Q0', [2, 2], 1e-2)
exatn.createTensor('Q1', [2, 2, 4], 1e-2)
exatn.createTensor('Q2', [4, 2, 2], 1e-2)
exatn.createTensor('Q3', [2, 2], 1e-2)

# Declare Hamiltonian Tensors
exatn.createTensor('H01', [2, 2, 2, 2], 1e-2)
exatn.createTensor('H12', [2, 2, 2, 2], 1e-2)
exatn.createTensor('H23', [2, 2, 2, 2], 1e-2)
exatn.createTensor('Z0', [2, 2, 2, 2], 1e-2)

# Get them as exatn.Tensor
q0 = exatn.getTensor('Q0')
q1 = exatn.getTensor('Q1')
q2 = exatn.getTensor('Q2')
q3 = exatn.getTensor('Q3')
h01 = exatn.getTensor('H01')
h12 = exatn.getTensor('H12')
h23 = exatn.getTensor('H23')
z0 = exatn.getTensor('Z0')

# Declare the Hamiltonian Operator
ham = exatn.TensorOperator('Hamiltonian')
ham.appendComponent(h01, [[0, 0], [1, 1]], [[0, 2], [1, 3]], 1.0)
ham.appendComponent(h12, [[1, 0], [2, 1]], [[1, 2], [2, 3]], 1.0)