Exemple #1
0
def test_parse_strings_to_tensor():
    energy_strings = [['+1.000000', 'f(i,i)'],
                      ['+1.000000', 'f(i,a)', 't1(a,i)'],
                      ['-0.500000', '<i,j||i,j>'],
                      ['-0.250000', '<i,j||a,b>', 't2(a,b,j,i)'],
                      ['+0.500000', '<i,j||a,b>', 't1(a,i)', 't1(b,j)']]

    energy_tensor_terms = contracted_strings_to_tensor_terms(energy_strings)
    i, j, a, b = Index('i',
                       'occ'), Index('j',
                                     'occ'), Index('a',
                                                   'virt'), Index('b', 'virt')
    h_ii = BaseTerm(indices=(i, i), name='f')
    f_ia = BaseTerm(indices=(i, a), name='f')
    t1 = T1amps(indices=(a, i))
    g_ijab = TwoBody(indices=(i, j, a, b), name='g')
    t2_abij = T2amps(indices=(a, b, j, i), name='t2')

    eterm1 = TensorTerm(base_terms=(h_ii, ))
    eterm2 = TensorTerm(base_terms=(
        f_ia,
        t1,
    ), coefficient=1)
    eterm3 = TensorTerm(base_terms=(g_ijab, t2_abij), coefficient=-0.25)
    assert eterm1.__repr__() == energy_tensor_terms[0].__repr__()
    assert eterm2.__repr__() == energy_tensor_terms[1].__repr__()
    assert eterm3.__repr__() == energy_tensor_terms[3].__repr__()
Exemple #2
0
def erpa_terms_to_einsum(tensor_terms: List[TensorTerm],
                         constant_terms=['r', 's', 'p', 'q'],
                         contract_d2_with='k2'):
    """
    Generate terms einsum contractions for the ERPA matrix

    This is the simplest contraction generation and should only be used to
    check if other simplified codes are correct.  The deltafunctions are not
    removed and thus the user must specify an identity matrix called kd.  To

    get the erpa matrix reshape the 4-tensor with labels (pqrs) into a matrix
    with row indices rs and column indices pq.
    """
    k2_idx = [
        Index('i', 'all'),
        Index('j', 'all'),
        Index('k', 'all'),
        Index('l', 'all')
    ]
    for tt in tensor_terms:
        # add the hamiltonian to contract with
        tt.base_terms += (BaseTerm(indices=tuple(k2_idx),
                                   name=contract_d2_with), )

        print("# ", tt)
        print(
            tt.einsum_string(update_val='erpa_val',
                             occupied=['i', 'j', 'k', 'l', 'r', 's', 'p', 'q'],
                             virtual=[],
                             output_variables=constant_terms,
                             optimize=False))
        print()
Exemple #3
0
def test_tensorterm():
    hij = BaseTerm(indices=(Index('i', 'occ'), Index('j', 'occ')),
                    name='h')
    t1ij = BaseTerm(indices=(Index('i', 'occ'), Index('j', 'occ')),
                    name='t')
    tensor_term = TensorTerm(base_terms=(hij, t1ij))
    assert tensor_term.coefficient == 1.0

    assert tensor_term.__repr__() == " 1.0000 h(i,j)*t(i,j)"
Exemple #4
0
def test_preset_tensor_terms():
    i, j, a, b = Index('i', 'occ'), Index('j', 'occ'), Index('a', 'virt'), \
                 Index('b', 'virt'),
    test_term = T1amps(indices=(i, a))
    assert test_term.name == 't1'
    test_term = T2amps(indices=(i, j, b, a))
    assert test_term.name == 't2'
    test_term = TwoBody(indices=(i, j, b, a))
    assert test_term.name == 'g'
    test_term = OneBody(indices=(i, j))
    assert test_term.name == 'h'
Exemple #5
0
def test_index():
    i_idx = Index('i', 'occ')
    assert i_idx.support == 'occ'
    assert i_idx.name == 'i'

    i2_idx = Index('i', 'occ')
    assert i2_idx == i_idx

    j_idx = Index('j', 'occ')
    assert i_idx != j_idx

    iv_idx = Index('i', 'virt')
    assert i_idx != j_idx

    assert i_idx != iv_idx
Exemple #6
0
def vacuum_normal_ordered_strings_to_tensor_terms(pdaggerq_list_of_strings):
    """
    Take the output of a normal ordering in pdaggerq and produce tensor terms

    Thus function enforces parsing normal ordered terms.  The name of the term
    parsed is equal to d{} half the length of the number of indices. d1 'i*','j'
    d2 'i*','j*','k','l', etc

    :param pdaggerq_list_of_strings: List[List[str]] where  first item is always
                                     a float.
    :return: List of algebra.TensorTerms
    """
    tensor_terms = []
    for pq_strings in pdaggerq_list_of_strings:
        coeff = float(pq_strings[0])
        delta_strings = list(
            filter(lambda xx: True if 'd(' in xx else False, pq_strings))
        delta_terms = []
        for d_str in delta_strings:
            delta_idx = d_str.replace('d(', '').replace(')', '').split(',')
            delta_idx = [Index(xx, 'all') for xx in delta_idx]
            delta_terms.append(Delta(indices=tuple(delta_idx)))
        rdm_strings = list(
            filter(lambda xx: False if 'd(' in xx else True, pq_strings[1:]))
        dagger_locations = [1 if '*' in xx else 0 for xx in rdm_strings]
        zero_location = dagger_locations.index(0)
        if not all(dagger_locations[:zero_location]):
            raise ValueError("Not in vacuum normal order")
        if any(dagger_locations[zero_location:]):
            raise ValueError("Not in vacuum normal order")
        rdm_idx = [
            xx.replace('*', '') if '*' in xx else xx for xx in rdm_strings
        ]
        g_idx = [Index(xx, 'all') for xx in rdm_idx]
        rdm_baseterm = BaseTerm(indices=tuple(g_idx),
                                name="d{}".format(len(g_idx) // 2))
        tensor_terms.append(
            TensorTerm(base_terms=tuple(delta_terms + [rdm_baseterm]),
                       coefficient=coeff))
    return tensor_terms
Exemple #7
0
def test_baseterm():
    term = BaseTerm(indices=(Index('i', 'occ'), Index('j', 'occ')),
                    name='h')
    assert term.indices[0] == Index('i', 'occ')
    assert term.indices[1] == Index('j', 'occ')

    term2 = BaseTerm(indices=(Index('k', 'occ'), Index('l', 'occ')),
                    name='t1')
    tensort = term2 * term
    assert isinstance(tensort, TensorTerm)

    tensort = term * term2
    assert isinstance(tensort, TensorTerm)

    assert term2 == term2
    assert term2 != term
Exemple #8
0
def test_tensor_multiply():
    hij = BaseTerm(indices=(Index('i', 'occ'), Index('j', 'occ')),
                    name='h')
    t1ij = BaseTerm(indices=(Index('i', 'occ'), Index('j', 'occ')),
                    name='t')
    tensor_term = TensorTerm(base_terms=(hij, t1ij))

    test_tensor_term = tensor_term * 4
    assert isinstance(test_tensor_term, TensorTerm)
    assert np.isclose(test_tensor_term.coefficient, 4)

    test_tensor_term = 4 * tensor_term
    assert isinstance(test_tensor_term, TensorTerm)
    assert np.isclose(test_tensor_term.coefficient, 4)

    t1kl = BaseTerm(indices=(Index('k', 'occ'), Index('l', 'virt')),
                    name='t')
    test_tensor_term = tensor_term * t1kl
    assert test_tensor_term.base_terms[2] == t1kl

    test_tensor_term = t1kl * tensor_term
    assert test_tensor_term.base_terms[2] == t1kl
Exemple #9
0
def string_to_baseterm(term_string,
                       occ_idx=OCC_INDICES,
                       virt_idx=VIRT_INDICES):
    if "||" in term_string:
        index_string = term_string.replace('<',
                                           '').replace('>',
                                                       '').replace('||', ',')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return TwoBody(indices=tuple(g_idx))
    if "g(" in term_string:
        index_string = term_string.replace('g(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return TwoBody(indices=tuple(g_idx))
    elif 'h' in term_string:
        index_string = term_string.replace('h(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return OneBody(indices=tuple(g_idx))
    elif 'f(' in term_string:
        index_string = term_string.replace('f(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return FockMat(indices=tuple(g_idx))
    elif 't4' in term_string:
        index_string = term_string.replace('t4(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return T4amps(indices=tuple(g_idx))
    elif 't3' in term_string:
        index_string = term_string.replace('t3(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return T3amps(indices=tuple(g_idx))
    elif 't2' in term_string:
        index_string = term_string.replace('t2(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return T2amps(indices=tuple(g_idx))
    elif 't1' in term_string:
        index_string = term_string.replace('t1(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return T1amps(indices=tuple(g_idx))
    elif 'd(' in term_string:
        index_string = term_string.replace('d(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Delta(indices=tuple(g_idx))
    elif 'l0' in term_string:
        return Left0amps()
    elif 'r0' in term_string:
        return Right0amps()
    elif 'l1' in term_string:
        index_string = term_string.replace('l1(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Left1amps(indices=tuple(g_idx))
    elif 'l2' in term_string:
        index_string = term_string.replace('l2(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Left2amps(indices=tuple(g_idx))
    elif 'l3' in term_string:
        index_string = term_string.replace('l3(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Left3amps(indices=tuple(g_idx))
    elif 'l4' in term_string:
        index_string = term_string.replace('l4(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Left4amps(indices=tuple(g_idx))
    elif 'r1' in term_string:
        index_string = term_string.replace('r1(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Right1amps(indices=tuple(g_idx))
    elif 'r2' in term_string:
        index_string = term_string.replace('r2(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Right2amps(indices=tuple(g_idx))
    elif 'r3' in term_string:
        index_string = term_string.replace('r3(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Right3amps(indices=tuple(g_idx))
    elif 'r4' in term_string:
        index_string = term_string.replace('r4(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return Right4amps(indices=tuple(g_idx))
    elif 'P(' in term_string:
        index_string = term_string.replace('P(', '').replace(')', '')
        g_idx = [
            Index(xx, 'occ') if xx in occ_idx else Index(xx, 'virt')
            for xx in index_string.split(',')
        ]
        return ContractionPermuter(indices=tuple(g_idx))
    else:
        raise TypeError("{} not recognized".format(term_string))