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)
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')
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')
# 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') # Compare to what numpy would have gotten print(np.dot(-s, r)) print('z0 = ', z0_data) print(exatn.getLocalTensor('S')) print(exatn.getLocalTensor('S').shape) # Clean up and destroy exatn.destroyTensor("S") exatn.destroyTensor("R") exatn.destroyTensor("Z0")
# 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()) pattern = pattern.replace("L", "X") pattern = pattern.replace("R", "Y") print(pattern) # Perform calculation exatn.createTensor(tNet.getTensor(4)) exatn.contractTensors(pattern) # Evaluate the tensor network (after merging two tensors) exatn.evaluate(tNet) # Print root tensor root = exatn.getLocalTensor(tNet.getTensor(0).getName()) print(root) # Evaluate the *Original* network to make sure it is the same. tNetOriginal.printIt() exatn.evaluate(tNetOriginal) rootOriginal = exatn.getLocalTensor(tNetOriginal.getTensor(0).getName()) print(rootOriginal) assert(np.allclose(root, rootOriginal))
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))] tensorCounter += nQubits assert(inverse.collapseIsometries()) inverse.printIt() bra = qubitReg bra.conjugate() bra.rename('QubitBra') pairings = [[i,i] for i in range(nQubits)] inverse.appendTensorNetwork(bra, pairings) inverse.printIt() assert(inverse.getRank() == 0) assert(exatn.evaluate(inverse)) print(exatn.getLocalTensor(inverse.getTensor(0).getName()))
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()))
def conjugate(tName): exatn.createTensor(tName + 'conj', np.conj(exatn.getLocalTensor(tName)))
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))) print('WI') print(wi) print('differences') print(np.linalg.norm(wi - np.transpose(np.conjugate(wi), (1, 0, 3, 2)))) print(np.linalg.norm(ei - np.transpose(np.conjugate(ei), (1, 0, 3, 2)))) wim = np.transpose(wi, (0, 2, 1, 3)).reshape(4, 4) eim = np.transpose(ei, (0, 2, 1, 3)).reshape(4, 4) svdw = np.linalg.svd(wim) svde = np.linalg.svd(eim) print('sigmaw, sigmae, nw, sw, ne, se:') print(svdw[1].round(5)) print(svde[1].round(5))
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)
closed_prod = exatn.TensorExpansion(ham_ket, bra) closed_prod.rename('MPSbraHamMPSket') closed_prod.printIt() # Declare the derivative tensor expansion with respect to tensor Q1+: # Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3 # | | | | | | | | | | | | # ==H01== | | + | ==H12== | + | | ==H23== # | | | | | | | | | | | | # Q0-- --Q2----Q3 Q0-- --Q2----Q3 Q0-- --Q2----Q3 deriv_q1 = exatn.TensorExpansion(closed_prod, 'Q1', True) deriv_q1.rename('DerivativeQ1') # Create the Accumulator tensor for the closed tensor expansion: exatn.createTensor('AC0') accumulator0 = exatn.getTensor('AC0') exatn.createTensor('AC1', [2, 2, 4], 0.0) accumulator1 = exatn.getTensor('AC1') # Evaluate the expectation value: exatn.evaluate(closed_prod, accumulator0) # Evaluate the derivative of the expectation value w.r.t. tensor Q1: exatn.evaluate(deriv_q1, accumulator1) # Print the expectation values print(exatn.getLocalTensor('AC0')) [ print(exatn.getLocalTensor(c.network.getTensor(0).getName())) for c in closed_prod ]