s2 = exatn.Tensor("S2", exatn.TensorShape([2, 2])) # Make a dictionary to feed to the tensor network tensor_dict = { z0.getName(): z0, t0.getName(): t0, t1.getName(): t1, t2.getName(): t2, h0.getName(): h0, s0.getName(): s0, s1.getName(): s1, s2.getName(): s2 } # Declare a tensor network: network = exatn.TensorNetwork( "{0,1} 3-site MPS closure", # network name "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)", # network specification tensor_dict) network.printIt() num_server = exatn.getNumServer() # Create participating ExaTN tensors: create_z0 = op_factory.createTensorOpShared(exatn.TensorOpCode.CREATE) create_z0.setTensorOperand(z0) num_server.submit(create_z0) create_t0 = op_factory.createTensorOpShared(exatn.TensorOpCode.CREATE) create_t0.setTensorOperand(t0) num_server.submit(create_t0) create_t1 = op_factory.createTensorOpShared(exatn.TensorOpCode.CREATE)
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]) circuit.appendTensorGate(5, 'H', [1]) circuit.appendTensorGate(6, 'H', [2]) circuit.appendTensorGate(7, 'CNOT', [1,2]) circuit.printIt() inverse = exatn.TensorNetwork(circuit) inverse.rename('InverseCircuit') inverse.appendTensorGate(8, 'CNOT', [1,2], True)
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())
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()))
shape_rank = shape.getRank() shape_dimext0 = shape.getDimExtent(0) shape_dimext1 = shape.getDimExtent(1) print("The example TensorShape rank is: " + str(shape_rank)) print("The example TensorShape DimExtent[0,1] are: [" + str(shape_dimext0) + "," + str(shape_dimext1) + "]") print("\n") leg = exatn.TensorLeg(1, 4) print("The tensor leg direction is: " + str(leg.getDirection())) print("The tensor leg ID is: " + str(leg.getTensorId())) print("\n\n") #build an example tensor network network = exatn.TensorNetwork("{0,1} 3-site MPS closure", exatn.Tensor("Z0"), []) network.appendTensor( 1, exatn.Tensor("T0", [2, 2]), [exatn.TensorLeg(4, 0), exatn.TensorLeg(2, 0)]) network.appendTensor( 2, exatn.Tensor("T1", [2, 2, 2]), [exatn.TensorLeg(1, 1), exatn.TensorLeg(4, 1), exatn.TensorLeg(3, 0)]) network.appendTensor( 3, exatn.Tensor("T2", [2, 2]), [exatn.TensorLeg(2, 2), exatn.TensorLeg(7, 1)]) network.appendTensor(4, exatn.Tensor("H0", [2, 2, 2, 2]), [ exatn.TensorLeg(1, 0), exatn.TensorLeg(2, 1),
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) ham.appendComponent(h23, [[2, 0], [3, 1]], [[2, 2], [3, 3]], 1.0) # Declare the ket MPS tensor network: # Q0----Q1----Q2----Q3 # | | | | mps_ket = exatn.TensorNetwork( 'MPS', 'Z0(i0,i1,i2,i3)+=Q0(i0,j0)*Q1(j0,i1,j1)*Q2(j1,i2,j2)*Q3(j2,i3)', { 'Z0': z0, 'Q0': q0, 'Q1': q1, 'Q2': q2, 'Q3': q3 }) # Declare the ket tensor network expansion: # Q0----Q1----Q2----Q3 # | | | | ket = exatn.TensorExpansion() ket.appendComponent(mps_ket, 1.0) ket.rename('MPSket') # Declare the bra tensor network expansion (conjugated ket): # | | | | # Q0----Q1----Q2----Q3 bra = exatn.TensorExpansion(ket)