Beispiel #1
0
 def create_batch_input(self, batchSize):
     # randomly choose indice
     imageIndice = np.random.random_integers(0, self.dataNum, batchSize)
     # create input tensor
     inst = Tensor(batchSize, self.inputChanel, self.inputRow, self.inputCol)
     for batchIndex, imageIndex in enumerate(imageIndice):
         im = np.asarray(Image.open(self.dataPair[batchIndex][0]))
         im = resize_image() if flagResize else pass ## TO DO
         new_im = np.zeros(shape=(self.inputChannel, self.inputRow, self.inputCol))
         for color in xrange(self.inputChannel):
             new_im[color,:,:] = im[:,:,color]
         inst.set_image(batchIndex, new_im)
     return inst.get_value()
def get_KW_tensor(pars):
    """ The Kramers-Wannier duality defect of the classical 2D
    square lattice Ising model.
    """
    eye = np.eye(2, dtype=np.complex_)
    ham = hamiltonians["ising"](pars)
    B = np.exp(-pars["beta"] * ham)
    H = np.array([[1, 1], [1, -1]], dtype=np.complex_) / np.sqrt(2)
    y_trigged = np.ndarray((2, 2, 2), dtype=np.complex_)
    y_trigged[:, :, 0] = eye
    y_trigged[:, :, 1] = sigma('y')
    D_sigma = np.sqrt(2) * np.einsum('ab,abi,ic,ad,adk,kc->abcd', B, y_trigged,
                                     H, B, y_trigged.conjugate(), H)

    u = symmetry_bases["ising"]
    u_dg = u.T.conjugate()
    D_sigma = ncon((D_sigma, u, u, u_dg, u_dg),
                   ([1, 2, 3, 4], [-1, 1], [-2, 2], [3, -3], [4, -4]))
    if pars["symmetry_tensors"]:
        D_sigma = TensorZ2.from_ndarray(D_sigma,
                                        shape=[[1, 1]] * 4,
                                        qhape=[[0, 1]] * 4,
                                        dirs=[1, 1, -1, -1])
    else:
        D_sigma = Tensor.from_ndarray(D_sigma, dirs=[1, 1, -1, -1])
    return D_sigma
def get_initial_sixvertex_tensor(pars):
    try:
        a = pars["sixvertex_a"]
        b = pars["sixvertex_b"]
        c = pars["sixvertex_c"]
    except KeyError:
        u = pars["sixvertex_u"]
        lmbd = pars["sixvertex_lambda"]
        rho = pars["sixvertex_rho"]
        a = rho * np.sin(lmbd - u)
        b = rho * np.sin(u)
        c = rho * np.sin(lmbd)
    A_0 = np.zeros((2, 2, 2, 2), dtype=pars["dtype"])
    A_0[1, 0, 0, 1] = a
    A_0[0, 1, 1, 0] = a
    A_0[0, 0, 1, 1] = b
    A_0[1, 1, 0, 0] = b
    A_0[0, 1, 0, 1] = c
    A_0[1, 0, 1, 0] = c
    if pars["symmetry_tensors"]:
        dim = [1, 1]
        qim = [-1, 1]
        A_0 = TensorU1.from_ndarray(A_0,
                                    shape=[dim] * 4,
                                    qhape=[qim] * 4,
                                    dirs=[1, 1, 1, 1])
        A_0 = A_0.flip_dir(2)
        A_0 = A_0.flip_dir(3)
    else:
        A_0 = Tensor.from_ndarray(A_0)
    return A_0
def get_initial_tensor_ising_3d(pars):
    beta = pars["beta"]
    ham = ising3d_ham(beta)
    A_0 = np.einsum('ai,aj,ak,al,am,an -> ijklmn', ham, ham, ham, ham, ham,
                    ham)
    if pars["symmetry_tensors"]:
        cls, dim, qim = TensorZ2, [1, 1], [0, 1]
        A_0 = cls.from_ndarray(A_0,
                               shape=[dim] * 6,
                               qhape=[qim] * 6,
                               dirs=[1, 1, -1, -1, 1, -1])
    else:
        A_0 = Tensor.from_ndarray(A_0)
    return A_0
def get_initial_tensor_potts33d(pars):
    beta = pars["beta"]
    Q = potts_Q(beta, 3)
    A = np.einsum('ai,aj,ak,al,am,an -> ijklmn', Q, Q, Q.conjugate(),
                  Q.conjugate(), Q, Q.conjugate())
    if np.linalg.norm(np.imag(A)) < 1e-12:
        A = np.real(A)
    if pars["symmetry_tensors"]:
        cls, dim, qim = symmetry_classes_dims_qims["potts3"]
        A = cls.from_ndarray(A,
                             shape=[dim] * 6,
                             qhape=[qim] * 6,
                             dirs=[1, 1, -1, -1, 1, -1])
    else:
        A = Tensor.from_ndarray(A)
    return A
def get_KW_unitary(pars):
    """ The unitary that moves the Kramers-Wannier duality defect of the
    classical 2D square lattice Ising model.
    """
    eye = np.eye(2, dtype=np.complex_)
    CZ = Csigma_np("z")
    U = ncon((CZ, R(np.pi / 4, 'z'), R(np.pi / 4, 'x'), R(np.pi / 4, 'y')),
             ([-1, -2, 5, 6], [-3, 5], [3, 6], [-4, 3]))
    u = symmetry_bases["ising"]
    u_dg = u.T.conjugate()
    U = ncon((U, u, u_dg, u_dg, u),
             ([1, 2, 3, 4], [-1, 1], [-2, 2], [3, -3], [4, -4]))
    U *= -1j
    if pars["symmetry_tensors"]:
        U = TensorZ2.from_ndarray(U,
                                  shape=[[1, 1]] * 4,
                                  qhape=[[0, 1]] * 4,
                                  dirs=[1, 1, -1, -1])
    else:
        U = Tensor.from_ndarray(U, dirs=[1, 1, 1, -1, -1, -1])
    return U
def get_initial_tensor(pars, **kwargs):
    if kwargs:
        pars = pars.copy()
        pars.update(kwargs)
    model_name = pars["model"].strip().lower()
    if model_name == "sixvertex":
        return get_initial_sixvertex_tensor(pars)
    elif model_name == "ising3d":
        return get_initial_tensor_ising_3d(pars)
    elif model_name == "potts33d":
        return get_initial_tensor_potts33d(pars)
    elif model_name == "complexion_qising":
        ham = get_ham(pars, model="qising")
        complexion = build_complexion(ham, pars)
        return complexion
    elif model_name == "complexion_qising_tricrit":
        ham = get_ham(pars, model="qising_tricrit")
        complexion = build_complexion(ham, pars)
        return complexion
    elif model_name == "complexion_sq_qising":
        ham = get_ham(pars, model="qising")
        complexion = build_complexion(ham, pars, square_hamiltonian=True)
        return complexion
    else:
        ham = hamiltonians[model_name](pars)
        boltz = np.exp(-pars["beta"] * ham)
        A_0 = np.einsum('ab,bc,cd,da->abcd', boltz, boltz, boltz, boltz)
        u = symmetry_bases[model_name]
        u_dg = u.T.conjugate()
        A_0 = ncon((A_0, u, u, u_dg, u_dg),
                   ([1, 2, 3, 4], [-1, 1], [-2, 2], [3, -3], [4, -4]))
        if pars["symmetry_tensors"]:
            cls, dim, qim = symmetry_classes_dims_qims[model_name]
            A_0 = cls.from_ndarray(A_0,
                                   shape=[dim] * 4,
                                   qhape=[qim] * 4,
                                   dirs=[1, 1, -1, -1])
        else:
            A_0 = Tensor.from_ndarray(A_0)
    return A_0
def get_initial_impurity(pars, legs=(3, ), factor=3, **kwargs):
    if kwargs:
        pars = pars.copy()
        pars.update(kwargs)
    A_pure = get_initial_tensor(pars)
    model = pars["model"]
    impurity = pars["impurity"]
    try:
        impurity_matrix = impurity_dict[model][impurity](pars)
    except KeyError:
        msg = ("Unknown (model, impurity) combination: ({}, {})".format(
            model, impurity))
        raise ValueError(msg)
    # TODO The expectation that everything is in the symmetry basis
    # clashes with how 2D ising and potts initial tensors are generated.
    if not pars["symmetry_tensors"]:
        impurity_matrix = Tensor.from_ndarray(impurity_matrix.to_ndarray())
    impurity_matrix *= -1
    A_impure = 0
    if 0 in legs:
        A_impure += ncon((A_pure, impurity_matrix),
                         ([1, -2, -3, -4, -5, -6], [1, -1]))
    if 1 in legs:
        A_impure += ncon((A_pure, impurity_matrix),
                         ([-1, 2, -3, -4, -5, -6], [2, -2]))
    if 2 in legs:
        A_impure += ncon((A_pure, impurity_matrix.transpose()),
                         ([-1, -2, 3, -4, -5, -6], [3, -3]))
    if 3 in legs:
        A_impure += ncon((A_pure, impurity_matrix.transpose()),
                         ([-1, -2, -3, 4, -5, -6], [4, -4]))
    if 4 in legs:
        A_impure += ncon((A_pure, impurity_matrix),
                         ([-1, -2, -3, -4, 5, -6], [5, -5]))
    if 5 in legs:
        A_impure += ncon((A_pure, impurity_matrix.transpose()),
                         ([-1, -2, -3, -4, -5, 6], [6, -6]))
    A_impure *= factor
    return A_impure
Beispiel #9
0
from itertools import product
from os.path import dirname, abspath, join as path_join
import sys
mydir = abspath(dirname(__file__))
sys.path.append(abspath(path_join(mydir, "..", "..")))
from tensors import Tensor
from tensors.numpy_interface import NumPyInterface
from tensors.numpy_interface.interface import _check_released
from tensors.indices import DeclareIndexRange, IndexRange, split_indices
from unittest import TestCase
import tensors
import unittest

tensors.type_checking_enabled = True
tensors.sanity_checking_enabled = True
Tensor.set_interface(NumPyInterface)

xproduct = lambda *args: product(*[xrange(*arg) if isinstance(arg, tuple) else xrange(arg) for arg in args])

# Note: in the future, we should have a not_implemented decorator
#   that requires a NotImplementedError to be raised
not_implemented = unittest.skip("not implemented")

class TestNumPyInterface(TestCase):

    interface = NumPyInterface

    def assertTensorEqual(self, first, second, msg=None):
        if hasattr(first, "_impl"):
            # allows handling of either Tensor or implementation types
            first, second = first._impl, second._impl
            else:
                dirs2 = None
            T2 = rtensor(shape=shp2, qhape=qhp2, dirs=dirs2)
            T2_orig = T2.copy()
            T1_np = T1.to_ndarray()
            T2_np = T2.to_ndarray()
            T = T1.dot(T2, (i_list, j_list))
            assert ((T1 == T1_orig).all())
            assert ((T2 == T2_orig).all())
            test_internal_consistency(T)
            i_list_compl = sorted(set(range(len(shp1))) - set(i_list))
            j_list_compl = sorted(set(range(len(shp2))) - set(j_list))
            product_shp = [shp1[i] for i in i_list_compl]\
                          + [shp2[j] for j in j_list_compl]
            if type(T) == Tensor:
                product_shp = Tensor.flatten_shape(product_shp)
            assert (T.shape == product_shp)
            T_np = np.tensordot(T1_np, T2_np, (i_list, j_list))
            assert (np.allclose(T_np, T.to_ndarray()))

            # Products of non-invariant vectors.
            n1 = np.random.randint(1, 3)
            T1 = rtensor(n=n1, chilow=1, invar=(n1 != 1))

            n2 = np.random.randint(1, 3)
            shp2 = rshape(n=n2, chilow=1)
            qhp2 = rqhape(shape=shp2)
            dirs2 = rdirs(shape=shp2)
            c2 = rcharge()
            shp2[0] = T1.shape[-1]
            if T1.qhape is not None:
def get_initial_tensor_CQL_3d(pars):
    delta = np.array([[[1, 0], [0, 0]], [[0, 0], [0, 1]]])
    A = np.einsum(('aeu,fiv,gjq,hbr,mcw,nxk,ols,ptd '
                   '-> abcdefghijklmnopqrstuvwx'), delta, delta, delta, delta,
                  delta, delta, delta, delta)
    return Tensor.from_ndarray(A.reshape((16, 16, 16, 16, 16, 16)))
def get_initial_tensor_CDL_3d_v2(pars):
    delta = np.eye(2, dtype=pars["dtype"])
    A = ncon((delta, ) * 12, ([-11, -21], [-12, -41], [-13, -51], [-14, -61],
                              [-31, -22], [-32, -42], [-33, -52], [-34, -62],
                              [-23, -63], [-64, -43], [-44, -53], [-54, -24]))
    return Tensor.from_ndarray(A.reshape((16, 16, 16, 16, 16, 16)))
def get_initial_tensor_CDL_3d(pars):
    delta = np.eye(2, dtype=pars["dtype"])
    A = np.einsum(('ae,fi,jm,nb,cq,rk,lu,vd,gs,to,pw,xh '
                   '-> abcdefghijklmnopqrstuvwx'), delta, delta, delta, delta,
                  delta, delta, delta, delta, delta, delta, delta, delta)
    return Tensor.from_ndarray(A.reshape((16, 16, 16, 16, 16, 16)))