Exemple #1
0
def test_comparision():
    # sequence: sq_op, number of tensors, tensors, absolute value of coeff, coeff
    a = Term([make_tensor('H', 'g0,h1', 'g1,p1')], make_sq('g1,p1', 'g0,h1'))
    b = Term(
        [make_tensor('H', 'g0,h1', 'g1,p1'),
         make_tensor('t', 'h1', 'p1')], make_sq('g1', 'g0'))
    assert a > b

    b = Term([
        make_tensor('H', 'g0,h1,c0', 'g1,p1,v0'),
        make_tensor('t', 'c0', 'v0')
    ], make_sq('g1,p1', 'g0,h1'))
    assert a < b

    c = Term([
        make_tensor('H', 'g0,v0,c0', 'g1,p1,c0'),
        make_tensor('t', 'h1', 'v0')
    ], make_sq('g1,p1', 'g0,h1'))
    assert c > b
    assert c.list_of_tensors > b.list_of_tensors

    b = Term.from_term(a, flip_sign=True)
    c = Term.from_term(a)
    d = sorted([a, b, c])
    assert d[0] == b
    assert d[1] == d[2] == a == c
Exemple #2
0
def test_copy_1():
    # OK to have diagonal indices, but attributes not fully functional
    a = Term([make_tensor('H', 'g0', 'g0')], make_sq("g0", "g0"))
    b = Term.from_term(a, flip_sign=True)
    assert a.coeff == -b.coeff
    b.coeff = 1
    assert a == b
Exemple #3
0
def test_almost_eq():
    a = Term([make_tensor('H', 'g0, h1', 'g1, p1')],
             make_sq('g1, p1', 'g0, h1'))
    b = Term.from_term(a, flip_sign=True)
    assert a != b
    assert a.almost_equal(b)
    assert a.coeff + b.coeff == 0
Exemple #4
0
def test_copy_2():
    # OK to have diagonal indices, but attributes not fully functional
    a = Term([make_tensor('H', 'g0', 'g1')], make_sq("g1", "g0"))
    b = Term.from_term(a, flip_sign=True, hc=True)
    assert b.sq_op == make_sq("g0", "g1")
    assert b.list_of_tensors == a.list_of_tensors
def bch_cc_rsc(nested_level,
               cluster_levels,
               max_cu_levels,
               n_opens,
               for_commutator=True,
               expand_hole=True,
               single_reference=False,
               unitary=False,
               n_process=1,
               hermitian_tensor=True):
    """
    Compute the BCH nested commutator in coupled cluster theory using recursive commutator formalism.
    :param nested_level: the level of nested commutator
    :param cluster_levels: a list of integers for cluster operator, e.g., [1,2,3] for T1 + T2 + T3
    :param max_cu_levels: a list of integers for max cumulant level of each level of commutator
    :param n_opens: a list of tuple [(min, max)] for numbers of open indices of each level of commutator
    :param for_commutator: compute only non-zero terms for commutators if True
    :param expand_hole: expand HoleDensity to Kronecker minus Cumulant if True
    :param single_reference: use single-reference amplitudes if True
    :param unitary: use unitary formalism if True
    :param n_process: number of processes launched for tensor canonicalization
    :param hermitian_tensor: assume tensors being Hermitian if True
    :return: a map of nested level to a list of contracted canonicalized Term objects
    """
    if not isinstance(nested_level, int):
        raise ValueError("Invalid nested_level (must be an integer)")

    if not all(isinstance(t, int) for t in cluster_levels):
        raise ValueError(
            "Invalid content in cluster_operator (must be all integers)")

    if isinstance(max_cu_levels, int):
        max_cu_levels = [max_cu_levels] * nested_level
    if len(max_cu_levels) != nested_level:
        raise ValueError(
            f"Inconsistent size of max_cu_levels ({max_cu_levels}), required {nested_level}"
        )
    if any(not isinstance(i, int) for i in max_cu_levels):
        raise ValueError(
            f"Invalid max_cu_levels ({max_cu_levels}): not all integers")

    if isinstance(n_opens, tuple):
        n_opens = [n_opens] * nested_level
    if len(n_opens) != nested_level:
        raise ValueError(
            f"Inconsistent size of n_opens ({n_opens}), required {nested_level}"
        )
    for n in n_opens:
        if len(n) != 2:
            raise ValueError(
                f"Invalid element in n_opens: {n} cannot be used for min/max numbers of open indices."
            )
        if not (isinstance(n[0], int) and isinstance(n[1], int)):
            raise ValueError(
                f"Invalid element in n_opens: {n} contains non-integer elements"
            )

    max_amp = max(cluster_levels) * 2
    amps = [
        cluster_operators(cluster_levels,
                          start=(i * max_amp) + 4,
                          unitary=unitary,
                          single_reference=single_reference)
        for i in range(nested_level)
    ]

    out = defaultdict(list)
    out[0] = combine_terms([x for x in hamiltonian_operator(1).expand_composite_indices(True)]) + \
             combine_terms([x for x in hamiltonian_operator(2).expand_composite_indices(True)])

    left_pool = combine_terms([x for x in hamiltonian_operator(1).expand_composite_indices(True)]) + \
                combine_terms([x for x in hamiltonian_operator(2).expand_composite_indices(True)])

    for i in range(1, nested_level + 1):
        factor = 1.0 / i

        max_cu = max_cu_levels[i - 1]
        min_n_open, max_n_open = n_opens[i - 1]

        for left in left_pool:
            for right in amps[i - 1]:
                out[i] += single_commutator(left, right, max_cu, max_n_open,
                                            min_n_open, factor, for_commutator,
                                            expand_hole, n_process,
                                            hermitian_tensor)

        out[i] = combine_terms([Term.from_term(term) for term in out[i]])
        left_pool = [Term.from_term(term) for term in out[i]]

    return out