Exemplo n.º 1
0
    def calc_residuals(self, h, a):
        """
        Updates right hand side of the CC equations, commonly referred as G
        """
        t2names = ['x1', 'x2', 'x3', 'x4']
        t2x = [a.t2[key] for key in t2names]

        t3names = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6']
        t3x = [a.t3[key] for key in t3names]

        # symmetrize t2 before feeding into res
        t2x_sym = cpd_symmetrize(t2x, {(1, 0, 3, 2): ('ident',)})

        # symmetrize t3 before feeding into res
        t3x_sym = cpd_symmetrize(t3x, {(1, 2, 0, 4, 5, 3): ('ident',),
                                       (2, 0, 1, 5, 3, 4): ('ident',),
                                       (0, 2, 1, 3, 5, 4): ('ident',),
                                       (2, 1, 0, 5, 4, 3): ('ident',),
                                       (1, 0, 2, 4, 3, 5): ('ident',)})

        # return _rccsdt_cpd_ls_t_calc_residuals(h, a)
        return _rccsdt_mul_ri_calc_residuals(
            h,
            Tensors(t1=a.t1,
                    t2=cpd_rebuild(t2x_sym),
                    t3=cpd_rebuild(t3x_sym))
        )
Exemplo n.º 2
0
    def calculate_update(self, h, a):
        """
        Calculate new amplitudes
        """
        names_abij = sorted(a.t2.keys())
        xs = [elem for elem in a.t2.to_generator()]

        # symmetrize t2 before feeding into res
        xs_sym = cpd_symmetrize(xs, {(1, 0, 3, 2): ('ident', )})
        a_sym = Tensors(t1=a.t1, t2=Tensors(zip(names_abij, xs_sym)))

        # Calculate residuals
        r1 = _rccsd_cpd_ls_t_true_calc_r1(h, a_sym)

        # Solve T1
        t1 = a.t1 - r1 * (cc_denom(h.f, 2, 'dir', 'full'))

        namesd_abij = ['d1', 'd2', 'd3', 'd4']
        d = Tensors(
            t2=Tensors(zip(namesd_abij, cc_denom(h.f, 4, 'dir', 'cpd'))))

        new_a = Tensors(t1=t1, t2=Tensors(zip(names_abij, xs)))

        for idx, name in enumerate(sorted(a_sym.t2.keys())):
            g = (-self._calculate_r2d_projection(name, h, a_sym, new_a, d) +
                 als_contract_cpd(new_a.t2.to_list(),
                                  a_sym.t2.to_list(),
                                  idx,
                                  tensor_format='cpd'))
            s = als_pseudo_inverse(new_a.t2.to_list(), new_a.t2.to_list(), idx)
            f = np.dot(g, s)
            new_a.t2[name] = f

        return new_a
Exemplo n.º 3
0
    def calculate_update(self, h, a):
        """
        Calculate new amplitudes
        """

        names_abij = ['x1', 'x2', 'x3', 'x4']
        xs = [a.t2[key] for key in names_abij]

        # symmetrize t2 before feeding into res
        xs_sym = cpd_symmetrize(xs, {(1, 0, 3, 2): ('ident', )})

        r = self.calc_residuals(
            h, Tensors(t1=a.t1, t2=Tensors(zip(names_abij, xs_sym))))

        # Solve T1
        t1 = a.t1 - r.t1 * (cc_denom(h.f, 2, 'dir', 'full'))

        # Symmetrize T2 residuals
        r2 = 1 / 2 * (r.t2 + r.t2.transpose([1, 0, 3, 2]))

        # Solve T2
        r2_d = -r2 * cc_denom(h.f, 4, 'dir', 'full')

        t2 = [f for f in xs]
        for idx in range(len(t2)):
            g = (als_contract_dense(t2, r2_d, idx, tensor_format='cpd') +
                 als_contract_cpd(t2, xs_sym, idx, tensor_format='cpd'))
            s = als_pseudo_inverse(t2, t2, idx)
            f = np.dot(g, s)
            t2[idx] = f

        return Tensors(t1=t1, t2=Tensors(zip(names_abij, t2)))
Exemplo n.º 4
0
    def calc_residuals(self, h, a):
        """
        Calculates CC residuals for CC equations
        """
        # Symmetrize T2 before feeding into res
        names = ['x1', 'x2', 'x3', 'x4']
        xs_sym = cpd_symmetrize([a.t2.x1, a.t2.x2, a.t2.x3, a.t2.x4],
                                {(1, 0, 3, 2): ('ident', )})

        return _rccsd_cpd_ls_t_calc_residuals(
            h, Tensors(t1=a.t1, t2=Tensors(zip(names, xs_sym))))
Exemplo n.º 5
0
    def calc_residuals(self, h, a):
        """
        Calculates CC residuals for CC equations
        """

        # Symmetrize T2 before feeding into res
        xs_sym = cpd_symmetrize([a.t2.x1, a.t2.x2, a.t2.x3, a.t2.x4],
                                {(1, 0, 3, 2): ('ident', )})

        amps = Tensors(t1=a.t1, t2=cpd_rebuild(xs_sym))

        # residuals are calculated with full amps for speed
        return _rccsd_ri_calc_residuals(h, amps)
Exemplo n.º 6
0
    def calculate_update(self, h, a):
        """
        Calculate new amplitudes
        """

        t2names = ['x1', 'x2', 'x3', 'x4']
        t2x = [a.t2[key] for key in t2names]

        t3names = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6']
        t3x = [a.t3[key] for key in t3names]

        # Running residuals with symmetrized amplitudes
        # symmetrization is done inside calc_residuals
        r = self.calc_residuals(
            h,
            Tensors(t1=a.t1, t2=Tensors(zip(t2names, t2x)),
                    t3=Tensors(zip(t3names, t3x)))
        )

        t1 = a.t1 - r.t1 * (cc_denom(h.f, 2, 'dir', 'full'))

        # Build symmetric T2 residual
        # TODO: implement unit residual for T2?
        r2 = 1 / 2 * (r.t2 + r.t2.transpose([1, 0, 3, 2]))
        # Solve
        r2_d = - r2 * cc_denom(h.f, 4, 'dir', 'full')

        # symmetrize initial T2
        t2x_sym = cpd_symmetrize(t2x, {(1, 0, 3, 2): ('ident',)})

        t2 = [f for f in t2x]
        for idx in range(len(t2)):
            g = (als_contract_dense(t2, r2_d, idx,
                                    tensor_format='cpd')
                 + als_contract_cpd(t2, t2x_sym, idx,
                                    tensor_format='cpd'))
            s = als_pseudo_inverse(t2, t2, idx)
            f = np.dot(g, s)
            t2[idx] = f

        # Build unit T3 residual
        r3s = (+ r.t3
               - r.t3.transpose([0, 1, 2, 3, 5, 4])
               + r.t3.transpose([0, 1, 2, 4, 5, 3]))
        r3u = ((+ r.t3
                + r.t3.transpose([1, 2, 0, 4, 5, 3])
                + r.t3.transpose([2, 0, 1, 5, 3, 4])
                + r.t3.transpose([0, 2, 1, 3, 5, 4])
                + r.t3.transpose([2, 1, 0, 5, 4, 3])
                + r.t3.transpose([1, 0, 2, 4, 3, 5])
                + 2 * r3s) / 12)

        # Solve
        r3_d = - r3u * cc_denom(h.f, 6, 'dir', 'full')

        # symmetrize inital T3
        t3x_sym = cpd_symmetrize(t3x, {(1, 2, 0, 4, 5, 3): ('ident',),
                                       (2, 0, 1, 5, 3, 4): ('ident',),
                                       (0, 2, 1, 3, 5, 4): ('ident',),
                                       (2, 1, 0, 5, 4, 3): ('ident',),
                                       (1, 0, 2, 4, 3, 5): ('ident',)})

        # 0 1 2 4 3 5 permutation of t3
        t3x_sym_a = [t3x_sym[0], t3x_sym[1], t3x_sym[2],
                     t3x_sym[4], t3x_sym[3], t3x_sym[5]]

        # Build combinations of T for unitary residual
        t3x_u = cpd_symmetrize(
            t3x_sym,
            {(0, 1, 2, 4, 3, 5): ('neg',),
             (0, 1, 2, 5, 4, 3): ('neg',),
             (0, 1, 2, 3, 5, 4): ('neg',),
             (0, 1, 2, 5, 3, 4): ('ident',),
             (0, 1, 2, 4, 5, 3): ('ident',), },
            weights=[2 / 3, 1 / 3, 1 / 3, 1 / 3, 1 / 6, 1 / 6])

        t3x_au = cpd_symmetrize(
            t3x_sym_a,
            {(0, 1, 2, 4, 3, 5): ('neg',),
             (0, 1, 2, 5, 4, 3): ('neg',),
             (0, 1, 2, 3, 5, 4): ('neg',),
             (0, 1, 2, 5, 3, 4): ('ident',),
             (0, 1, 2, 4, 5, 3): ('ident',), },
            weights=[2 / 3, 1 / 3, 1 / 3, 1 / 3, 1 / 6, 1 / 6])

        t3 = [f for f in t3x]
        for idx in range(len(t2)):
            g = (als_contract_dense(t3, r3_d, idx,
                                    tensor_format='cpd')
                 + als_contract_cpd(t3, t3x_u, idx,
                                    tensor_format='cpd')
                 - als_contract_cpd(t3, t3x_au, idx,
                                    tensor_format='cpd')
                 )

            s = als_pseudo_inverse(t3, t3, idx)
            f = np.dot(g, s)
            t3[idx] = f

        return Tensors(t1=t1, t2=Tensors(zip(t2names, t2)),
                       t3=Tensors(zip(t3names, t3)))