Ejemplo n.º 1
0
    def setLehmannTermsDynamicFermionic(self, dynamicObservable):
        for statePair, operatorPair in dynamicObservable.operatorPairs.items():
            operator1 = operatorPair[0]
            operator2 = operatorPair[1]
            if type(operator1) != ndarray:
                operator1 = operator1.toarray()
                operator2 = operator2.toarray()
            e = self.getEnergyEigenvalues()
            ve = self.getEnergyEigenstates().toarray()
            fockstates = range(self.fockspaceSize)

            n_inds_p = array(scatter_list(fockstates))
            nominators_p = []
            denominators_p = []
            for n in n_inds_p:
                n_op1 = ve[:,n].dot(operator1)
                op2_n = operator2.dot(ve[:,n])
                expn = exp(-self.beta*e[n])
                for m in fockstates:
                    if dynamicObservable.species == 'bosonic':
                        if equals(e[n], e[m]):
                            continue
                    el1 = n_op1.dot(ve[:,m])
                    if not equals(el1, 0):
                        el2 = ve[:,m].dot(op2_n)
                        if not equals(el2, 0):
                            expm = exp(-self.beta*e[m])
                            el = el1*el2
                            nominators_p.append([])
                            nominators_p[-1].append(expn*el)
                            nominators_p[-1].append(expm*el)
                            denominators_p.append(e[n]-e[m])
            dynamicObservable.lehmannNominators.update({statePair: allgather_list(nominators_p)})
            dynamicObservable.lehmannDenominators.update({statePair: allgather_list(denominators_p)})
        dynamicObservable.partitionFunction = self.getPartitionFunction()
Ejemplo n.º 2
0
    def setZeroFrequencyTerms(self, dynamicObservable):
        for statePair, operatorPair in dynamicObservable.operatorPairs.items():
            operator1 = operatorPair[0]
            operator2 = operatorPair[1]
            if type(operator1) != ndarray:
                operator1 = operator1.toarray()
                operator2 = operator2.toarray()
            e = self.getEnergyEigenvalues()
            ve = self.getEnergyEigenstates().toarray()
            fockstates = range(self.fockspaceSize)

            n_inds_p = array(scatter_list(fockstates))
            zeroFrequencyTerms_p = []
            for n in n_inds_p:
                n_op1 = ve[:,n].dot(operator1)
                op2_n = operator2.dot(ve[:,n])
                expn = exp(-self.beta*e[n])
                for m in fockstates:
                    if equals(e[n], e[m]):
                        el1 = n_op1.dot(ve[:,m])
                        if not equals(el1, 0):
                            el2 = ve[:,m].dot(op2_n)
                            if not equals(el2, 0):
                                el = el1*el2
                                zeroFrequencyTerms_p.append(-self.beta*expn*el)
            dynamicObservable.zeroFrequencyTerms.update({statePair: allgather_list(zeroFrequencyTerms_p)})
Ejemplo n.º 3
0
 def setLehmannSumStatic(self, staticObservable):
     assert type(staticObservable) == StaticObservable, 'StaticObservable expected.'
     e = self.getEnergyEigenvalues()
     ve = self.getEnergyEigenstates().toarray()
     z = self.getPartitionFunction()
     fockstates = range(self.fockspaceSize)
     for index, matrix in staticObservable.operators.items():
         if type(matrix) != ndarray:
             m = matrix.toarray()
         else:
             m = matrix
         n_inds_p = array(scatter_list(fockstates))
         terms_p = list()
         for n in n_inds_p:
             el = ve[:,n].dot(m.dot(ve[:,n]))
             expn = exp(-self.beta*e[n])
             if not equals(el, 0) and not equals(expn, 0):
                 terms_p.append(expn * el)
         staticObservable.expectationValue.update({index: sumScatteredLists(terms_p)/z})
Ejemplo n.º 4
0
 def __eq__(self, other):
     if isnan(self.w) and isnan(other.w):
         return bool(
             equals(self.red, other.red)
             and equals(self.green, other.green)
             and equals(self.blue, other.blue)
         )
     else:
         return bool(
             equals(self.x, other.x)
             and equals(self.y, other.y)
             and equals(self.z, other.z)
             and equals(self.w, other.w)
         )
Ejemplo n.º 5
0
def embedV(subspaceVectors, blocksizes):
    fockspaceSize = nsum(blocksizes)
    assert fockspaceSize == len(subspaceVectors), 'embedding will fail'
    iBlock = 0
    iBlockOrigin = 0
    vectors = zeros([len(subspaceVectors), fockspaceSize])
    x = list()
    y = list()
    data = list()
    for i, v in enumerate(subspaceVectors):
        for j, vj in enumerate(v):
            if not equals(vj, 0):
                y.append(j + iBlockOrigin) # TODO understand row/col exchange
                x.append(i)
                data.append(vj)
        if i == iBlockOrigin + blocksizes[iBlock] - 1:
            iBlockOrigin += blocksizes[iBlock]
            iBlock += 1
    return coo_matrix((data, (x,y)), [fockspaceSize]*2)
Ejemplo n.º 6
0
def step_impl(context):
    assert equals(context.reflectance, 0.48873)
Ejemplo n.º 7
0
def step_impl(context):
    assert equals(context.xs[0].t, 6.80798)
Ejemplo n.º 8
0
def step_impl(context):
    assert equals(context.xs[1].t, 7.08872)
Ejemplo n.º 9
0
def step_impl(context):
    assert equals(context.xs[1].t, 49.44994)
Ejemplo n.º 10
0
def step_impl(context):
    assert equals(context.xs[0].t, 0.35355)
Ejemplo n.º 11
0
def step_impl(context):
    assert equals(context.xs[0].t, 4.55006)
Ejemplo n.º 12
0
def step_impl(context):
    assert equals(context.xs[1].t, 8.66025)
Ejemplo n.º 13
0
    def __init__(
            self,
            dim,
            depth,
            heads=8,
            cross_attend=False,
            only_cross=False,
            position_infused_attn=True,
            custom_layers=None,
            sandwich_coef=None,
            residual_attn=False,
            pre_norm=True,
            causal=False,
            use_scalenorm=True,
            #dim_out=None,
            **kwargs):
        super().__init__()
        self.dim = dim
        self.depth = depth
        self.layers = nn.ModuleList([])
        self.dim_out = dim

        self.pia_pos_emb = FixedPositionalEmbedding(dim) if position_infused_attn else \
            None

        # To do: Implement relative position bias
        # To do: Implement residual attention

        self.pre_norm = pre_norm

        # ScaleNorm from paper: https://arxiv.org/abs/1910.05895
        norm_class = ScaleNorm if use_scalenorm else nn.LayerNorm
        norm_fn = partial(norm_class, dim)

        ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs)
        attn_kwargs, _ = groupby_prefix_and_trim('attn_', kwargs)

        if cross_attend and not only_cross:
            default_block = ('a', 'c', 'f'
                             )  # Attention -> cross -> Feedforward
        elif cross_attend and only_cross:
            default_block = ('c', 'f')  # Cross -> Feedforward
        else:
            default_block = ('a', 'f')  # Attention -> Feedforward

        if exists(custom_layers):
            layer_types = custom_layers
        elif exists(sandwich_coef):
            # TO DO
            pass
        else:
            layer_types = default_block * depth

        self.layer_types = layer_types
        self.num_attn_layers = len(list(filter(equals('a'), layer_types)))

        for layer_type in self.layer_types:
            if layer_type == 'a':
                layer = Attention(dim,
                                  heads=heads,
                                  causal=causal,
                                  **attn_kwargs)
            elif layer_type == 'c':
                layer = Attention(dim, heads=heads, **attn_kwargs)
            elif layer_type == 'f':
                layer = FeedForward(dim, **ff_kwargs)
            else:
                raise Exception(f'invalid layer type {layer_type}')

            self.layers.append(nn.ModuleList([norm_fn(), layer, Residual()]))
Ejemplo n.º 14
0
def step_impl(context):
    assert equals(context.c.pixel_size, 0.01)
Ejemplo n.º 15
0
 def __eq__(self, other):
     if (equals(self.matrix[0][0], other.matrix[0][0])
             and equals(self.matrix[0][1], other.matrix[0][1])
             and equals(self.matrix[0][2], other.matrix[0][2])
             and equals(self.matrix[0][3], other.matrix[0][3])
             and equals(self.matrix[1][0], other.matrix[1][0])
             and equals(self.matrix[1][1], other.matrix[1][1])
             and equals(self.matrix[1][2], other.matrix[1][2])
             and equals(self.matrix[1][3], other.matrix[1][3])
             and equals(self.matrix[2][0], other.matrix[2][0])
             and equals(self.matrix[2][1], other.matrix[2][1])
             and equals(self.matrix[2][2], other.matrix[2][2])
             and equals(self.matrix[2][3], other.matrix[2][3])
             and equals(self.matrix[3][0], other.matrix[3][0])
             and equals(self.matrix[3][1], other.matrix[3][1])
             and equals(self.matrix[3][2], other.matrix[3][2])
             and equals(self.matrix[3][3], other.matrix[3][3])):
         return True
     else:
         return False