Example #1
0
def test_contraction_categorized_1():
    h = SQ("g0", "g1")
    t = SQ("p0", "h0")
    ref = list(
        compute_operator_contractions([h, t],
                                      max_cu=2,
                                      max_n_open=4,
                                      for_commutator=False))
    a = list(
        compute_operator_contractions([h, t],
                                      max_cu=2,
                                      max_n_open=4,
                                      for_commutator=True))
    for i in a:
        assert i in ref
    assert len(a) == (len(ref) - 2)  # minus pure L2 and un-contracted
Example #2
0
def test_contraction_categorized_4():
    from timeit import default_timer as timer

    h = SQ("g0, g1", "g2, g3")
    t2e = SQ("v0, v1", "c0, c1")
    t2d = SQ("c2, c3", "v2, v3")
    t2ee = SQ("v4, v5", "c4, c5")

    start = timer()
    a = [
        i for con in compute_operator_contractions([t2d, h, t2e, t2ee],
                                                   max_cu=1,
                                                   for_commutator=True,
                                                   n_process=4,
                                                   batch_size=0) for i in con
    ]
    print(f"Time to compute T2^+ * H * T2 * T2: {timer() - start:.3f} s")
    assert len(
        a
    ) == 56128  # note that there are 72832 contractions in total from test_contraction_4()
Example #3
0
def test_contraction_categorized_2():
    h = SQ("g0", "g0")
    t2e = SQ("p0, p1", "h0, h1")

    ref = list(
        compute_operator_contractions_general([h, t2e],
                                              max_cu=3,
                                              n_process=2,
                                              batch_size=0))
    ref = [(sign, sorted(densities), sq_op) for con in ref
           for sign, densities, sq_op in con]

    a = list(
        compute_operator_contractions([h, t2e],
                                      max_cu=3,
                                      n_process=2,
                                      batch_size=0,
                                      for_commutator=True))
    a = [(sign, sorted(densities), sq_op) for con in a
         for sign, densities, sq_op in con]

    for i in a:
        assert i in ref
    assert len(a) == (len(ref) - 1 - 9)  # 1 un-contracted, 9 pure cumulant
Example #4
0
def test_contraction_categorized_3():
    MT = Tensor.make_tensor

    h = SQ("g0", "g0")
    t1d = SQ("h0", "p0")
    t1e = SQ("p1", "h1")

    # 13 double pairwise, 9 single pairwise with 2-cumulant, 6 triple pairwise
    ref = [
        (1, [MT('L', 'g0', 'h1'), MT('L', 'h0', 'g0')], SQ('p1', 'p0')),
        (-1, [MT('K', 'p1', 'p0'), MT('L', 'h0', 'g0')], SQ('g0', 'h1')),
        (1, [MT('L', 'p1', 'p0'), MT('L', 'h0', 'g0')], SQ('g0', 'h1')),
        (-1, [MT('K', 'g0', 'p0'), MT('L', 'h0', 'h1')], SQ('p1', 'g0')),
        (1, [MT('L', 'g0', 'p0'), MT('L', 'h0', 'h1')], SQ('p1', 'g0')),
        (-1, [MT('K', 'p1', 'g0'), MT('L', 'h0', 'h1')], SQ('g0', 'p0')),
        (1, [MT('L', 'p1', 'g0'), MT('L', 'h0', 'h1')], SQ('g0', 'p0')),
        (1, [MT('K', 'g0', 'p0'), MT('K', 'p1', 'g0')], SQ('h0', 'h1')),
        (-1, [MT('K', 'p1', 'g0'), MT('L', 'g0', 'p0')], SQ('h0', 'h1')),
        (-1, [MT('K', 'g0', 'p0'), MT('L', 'p1', 'g0')], SQ('h0', 'h1')),
        (1, [MT('L', 'g0', 'p0'), MT('L', 'p1', 'g0')], SQ('h0', 'h1')),
        (-1, [MT('K', 'p1', 'p0'), MT('L', 'g0', 'h1')], SQ('h0', 'g0')),
        (1, [MT('L', 'g0', 'h1'), MT('L', 'p1', 'p0')], SQ('h0', 'g0')),
        (-1, [MT('L', 'h0', 'g0'),
              MT('L', 'g0, p1', 'p0, h1')], SQ.make_empty()),
        (1, [MT('L', 'h0', 'h1'),
             MT('L', 'g0, p1', 'p0, g0')], SQ.make_empty()),
        (1, [MT('K', 'g0', 'p0'),
             MT('L', 'h0, p1', 'g0, h1')], SQ.make_empty()),
        (-1, [MT('L', 'g0', 'p0'),
              MT('L', 'h0, p1', 'g0, h1')], SQ.make_empty()),
        (-1, [MT('L', 'g0', 'h1'),
              MT('L', 'h0, p1', 'p0, g0')], SQ.make_empty()),
        (-1, [MT('K', 'p1', 'p0'),
              MT('L', 'h0, g0', 'g0, h1')], SQ.make_empty()),
        (1, [MT('L', 'p1', 'p0'),
             MT('L', 'h0, g0', 'g0, h1')], SQ.make_empty()),
        (1, [MT('K', 'p1', 'g0'),
             MT('L', 'h0, g0', 'p0, h1')], SQ.make_empty()),
        (-1, [MT('L', 'p1', 'g0'),
              MT('L', 'h0, g0', 'p0, h1')], SQ.make_empty()),
        (-1, [MT('K', 'p1', 'p0'),
              MT('L', 'g0', 'h1'),
              MT('L', 'h0', 'g0')], SQ.make_empty()),
        (1, [MT('L', 'g0', 'h1'),
             MT('L', 'p1', 'p0'),
             MT('L', 'h0', 'g0')], SQ.make_empty()),
        (1, [MT('K', 'g0', 'p0'),
             MT('K', 'p1', 'g0'),
             MT('L', 'h0', 'h1')], SQ.make_empty()),
        (-1, [MT('K', 'p1', 'g0'),
              MT('L', 'g0', 'p0'),
              MT('L', 'h0', 'h1')], SQ.make_empty()),
        (-1, [MT('K', 'g0', 'p0'),
              MT('L', 'p1', 'g0'),
              MT('L', 'h0', 'h1')], SQ.make_empty()),
        (1, [MT('L', 'g0', 'p0'),
             MT('L', 'p1', 'g0'),
             MT('L', 'h0', 'h1')], SQ.make_empty())
    ]

    a = list(
        compute_operator_contractions([t1d, h, t1e],
                                      max_cu=3,
                                      n_process=2,
                                      batch_size=0,
                                      for_commutator=True))
    a = [(sign, sorted(densities), sq_op) for con in a
         for sign, densities, sq_op in con]

    for i in a:
        assert i in ref
    assert len(a) == len(ref)
def contract_terms(terms,
                   max_cu=3,
                   max_n_open=6,
                   min_n_open=0,
                   scale_factor=1.0,
                   for_commutator=False,
                   expand_hole=True,
                   n_process=1,
                   hermitian_tensor=True):
    """
    Contract a list of Term objects.
    :param terms: a list of Term objects to be contracted
    :param max_cu: max level of cumulant
    :param max_n_open: max number of open indices for contractions kept for return
    :param min_n_open: min number of open indices for contractions kept for return
    :param scale_factor: a scaling factor for the results
    :param for_commutator: compute only non-zero terms for commutators if True
    :param expand_hole: expand HoleDensity to Kronecker - Cumulant if True
    :param n_process: number of processes launched for tensor canonicalization
    :param hermitian_tensor: assume tensors being Hermitian if True
    :return: a list of contracted and canonicalized Term objects
    """
    if len(terms) == 0:
        raise ValueError("size of terms cannot be zero.")

    n_process = min(n_process, multiprocessing.cpu_count())

    coeff = float(scale_factor)
    tensors = []
    sq_ops_to_be_contracted = []
    for term in terms:
        if not isinstance(term, Term):
            raise TypeError(f"{term} if not of Term type.")
        coeff *= term.coeff
        tensors += term.list_of_tensors
        if not term.sq_op.is_empty():
            sq_ops_to_be_contracted.append(term.sq_op)

    if len(sq_ops_to_be_contracted) < 2:
        sq_op = sq_ops_to_be_contracted[0] if len(
            sq_ops_to_be_contracted) == 1 else terms[0].sq_op
        return [Term(tensors, sq_op, coeff)]
    else:
        # contractions = [i for con in compute_operator_contractions(sq_ops_to_be_contracted, max_cu,
        #                                                            max_n_open, min_n_open, for_commutator,
        #                                                            expand_hole, n_process, batch_size=0)
        #                 for i in con]
        # chunk_size = int(sqrt(len(contractions)) / n_process) + 1
        # out = canonicalize_contractions_batch(n_process, contractions, tensors, coeff, False, chunk_size)
        # return combine_terms(out)

        count = 0
        out = []
        contractions = []
        batch_size = 10000 * max(1, n_process // 2)
        chunk_size = int(sqrt(batch_size) / n_process) + 1

        for batch in compute_operator_contractions(sq_ops_to_be_contracted,
                                                   max_cu,
                                                   max_n_open,
                                                   min_n_open,
                                                   for_commutator,
                                                   expand_hole,
                                                   1,
                                                   batch_size=0):
            count += len(batch)
            contractions += batch
            print(count, len(contractions))

            if count < batch_size:
                continue

            out += canonicalize_contractions_batch(n_process, contractions,
                                                   tensors, coeff, True,
                                                   chunk_size,
                                                   hermitian_tensor)
            contractions = []
            count = 0

        chunk_size = int(sqrt(len(contractions)) / n_process) + 1
        out += canonicalize_contractions_batch(n_process, contractions,
                                               tensors, coeff, False,
                                               chunk_size, hermitian_tensor)

        return combine_terms(out)