def set_tensornetwork_backend(backend: Optional[str] = None) -> None: """ set the runtime backend of tensornetwork :param backend: numpy, tensorflow, jax, pytorch :return: """ if not backend: backend = get_default_backend() backend_obj = get_backend(backend) for module in modules: if module in sys.modules: setattr(sys.modules[module], "backend", backend_obj) tn.set_default_backend(backend)
def test_set_default(backend): tensornetwork.set_default_backend(backend) assert tensornetwork.config.default_backend == backend net = tensornetwork.TensorNetwork() assert net.backend.name == backend
from tensornetwork.block_sparse.charge import U1Charge from tensornetwork.block_sparse.index import Index from tensornetwork.block_sparse import BlockSparseTensor as BT from tensornetwork.block_sparse import linalg as BLA from tensornetwork.block_sparse import initialization as BI from tensornetwork.block_sparse.caching import set_caching_status, clear_cache # tensornetwork and block_sparse modules from eigh import eigh from helper_functs import orthog, trunct_eigh, expand_dims, orthog_sym, eye_sym from sym_link_networks import (LiftHam, LowerDensity, RightLink, LeftLink, TopLink, CenterLink) # from link_networks import CenterLink from binaryMERA import binaryMERA tn.set_default_backend('symmetric') set_caching_status(True) clear_cache() # set simulation parameters chi_b = 4 chi = 4 chi_p = 4 chi_m = 16 n_levels = 3 n_iterations = 1000 n_sites = 3 * (2**(n_levels + 1)) left_on = 1 right_on = 1 center_on = 1
import numpy as np #import tensorflow as tf # tf.enable_v2_behavior() import tensornetwork as tn tn.set_default_backend("numpy") class TSpinSq(object): """Generate the vacuum S^2 operator.""" def __init__(self, n_hole_states, n_particle_states, ref=[], s=0.5): """Class constructor. Instantiate PairingHamiltonian2B object. Arguments: n_hole_states -- number of holes states in the single particle basis n_particle_states -- number of particles states in the single particle basis Keyword arguments: ref -- the reference state. must match dimensions imposed by arugments (default: [1,1,1,1,0,0,0,0]) d -- the energy level spacing (default: 1.0) g -- the pairing strength (default: 0.5) pb -- strength of the pair-breaking term (operates in double particle basis) (default: 0.0)""" self._s = s if ref == []: self._reference = np.append( np.ones(n_hole_states, dtype=np.float32), np.zeros(n_particle_states, dtype=np.float32)) else: self._reference = np.asarray(ref, dtype=np.float32)
def test_set_default(backend): tn.set_default_backend(backend) assert _default_backend_stack.default_backend == backend a = tn.Node(np.eye(2)) assert a.backend.name == backend
from tensorflow.python.framework.ops import disable_eager_execution from TNModel.utils import * import TNModel.tf_model as tf_model import tensornetwork as tn import tensorflow as tf import numpy as np import os os.environ['TF_DISABLE_MLC'] = '1' # If you want to run in eager mode, just comment those two lines. disable_eager_execution() tn.set_default_backend('tensorflow') # HyperParams hyper_params = { 'rank': 28 * 28, 'phys_dim': 10, 'bond_dim': 2, 'labels': 10, 'string_cnt': 4, # for 1d-sbs only 'sbs_op': 'mean', # mean or prod , alternative choice for 1d-sbs contraction 'model': 'peps', # mps (finished) or 1d-sbs (half-working) # vectorized_map is only supported in part of the machines. 'vectorized': True, 'batch_size': 16, 'max_singular_values': 64 } if __name__ == '__main__':
from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt import tensorflow as tf # tf.enable_v2_behavior() # Import tensornetwork import tensornetwork as tn # Settting backend to tensorflow otherwise numpy is usedB tn.set_default_backend("tensorflow") class TNLayer(tf.keras.layers.Layer): def __init__(self): super(TNLayer, self).__init__() # Creating variables for the layer. self.a_var = tf.Variable(tf.random.normal(shape=(8, 8, 2), stddev=1.0 / 16.0), name="a", trainable=True) self.b_var = tf.Variable(tf.random.normal(shape=(8, 8, 2), stddev=1.0 / 16.0), name="b", trainable=True) self.bias = tf.Variable(tf.zeros(shape=(8, 8)), name="bias", trainable=True) def call(self, inputs): # Define the contraction
def test_contextmanager_BaseBackend(): tn.set_default_backend("pytorch") a = tn.Node(np.ones((10, ))) with tn.DefaultBackend(a.backend): b = tn.Node(np.ones((10, ))) assert b.backend.name == "pytorch"
def test_set_default_backend_value_error(): tn.set_default_backend("pytorch") with pytest.raises(ValueError, match="Item passed to set_default_backend " "must be Text or BaseBackend"): tn.set_default_backend(-1) # pytype: disable=wrong-arg-types
def test_contextmanager_default_backend(): tn.set_default_backend("pytorch") with tn.DefaultBackend("numpy"): assert _default_backend_stack.default_backend == "pytorch"
def test_contextmanager_interruption(): tn.set_default_backend("pytorch") with pytest.raises(AssertionError): with tn.DefaultBackend("numpy"): tn.set_default_backend("tensorflow")
for jdx in range(num_batch): f(*args) acc += time.time() - start return acc / num_iter def foo(a, b): node1 = tn.Node(a) node2 = tn.Node(b) node1[1] ^ node2[0] return tn.contract_between(node1, node2) def run(n, num_iter, num_batch): a, b = np.random.randn(2, n, n) nops = 2 * n**3 / 1e9 elapsed = benchmark(num_iter, num_batch, foo, a, b) print(n, " ", nops / elapsed) if __name__ == "__main__": tn.set_default_backend(sys.argv[1]) for i in range(4096, 512, -256): run(i, 10, 1) for i in range(512, 64, -32): run(i, 50, 1000) for i in range(64, 16, -1): run(i, 50, 1000)
def reset_default_backend(): tensornetwork.set_default_backend("numpy") yield tensornetwork.set_default_backend("numpy")
A[N - 1]) A[N - 1] = B mps.tensors = A print("") ##### -ZZ+hZ+gX model ############# ####################################### ##### Set bond dimensions and simulation options h = 0.7 g = 1.05 tn.set_default_backend('jax') print('H = - ZZ + hZ + gX') print('g = {g}'.format(g=g)) print('h = {h}\n'.format(h=h)) num = 0 Narray = [10] prec = 1E-4 Dinit = 8 for N in Narray: print('Length of the chain = {N}\n'.format(N=N))
""" DMRG for XXZ model. """ from typing import Type, Text import tensornetwork as tn import numpy as np tn.set_default_backend('pytorch') def initialize_spin_mps(N: int, D: int, dtype: Type[np.number]): return tn.FiniteMPS.random([2] * N, [D] * (N - 1), dtype=dtype) def initialize_XXZ_mpo(Jz: np.ndarray, Jxy: np.ndarray, Bz: np.ndarray, dtype: Type[np.number]): return tn.FiniteXXZ(Jz, Jxy, Bz, dtype=dtype) def run_twosite_dmrg(N: int, D: int, dtype: Type[np.number], Jz: np.ndarray, Jxy: np.ndarray, Bz: np.ndarray, num_sweeps: int): mps = initialize_spin_mps(N, 32, dtype) mpo = initialize_XXZ_mpo(Jz, Jxy, Bz, dtype) dmrg = tn.FiniteDMRG(mps, mpo) result = dmrg.run_two_site(max_bond_dim=D, num_sweeps=num_sweeps, num_krylov_vecs=10, verbose=1) return result, mps
def test_bad_backend_name(): with pytest.raises(ValueError, match="Backend 'BAD_NAME' was not found."): tn.set_default_backend("BAD_NAME")
... [r_{1,n}, r_{2,n}, ..., d_n]] The diagonal entries here are precisely the input dimensions for each of the core tensors, while the symmetric nature of the matrix is necessary for different cores to contract with each other. Besides the above matrix format, the ranks are also sometimes specified in the following upper-triangular format: [[r_{1,2}, r_{1,3}, ..., r_{1,n}], [r_{2,3}, ..., r_{2,n}], ... ..., [r_{n-2,n-1}, r_{n-2,n}], [r_{n-1,n}]] """ # Set global defaults tn.set_default_backend("pytorch") torch.set_default_tensor_type(torch.DoubleTensor) def continuous_optim(tensor_list, train_data, loss_fun, epochs=10, val_data=None, other_args=dict()): """ Train a tensor network using gradient descent on input dataset Args: tensor_list: List of tensors encoding the network being trained train_data: The data used to train the network
def backend_tensorflow(): tn.set_default_backend("tensorflow") yield tn.set_default_backend("numpy")