Exemplo n.º 1
0
def test_planar_mps2d_contract():
    code = PlanarCode(3, 3)
    sample = code.new_pauli().site('Y', (2, 0), (2, 4))
    prob_dist = DepolarizingErrorModel().probability_distribution(0.1)
    tnc = PlanarMPSDecoder.TNC()
    tn = tnc.create_tn(prob_dist, sample)
    result = tt.mps2d.contract(tn)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    assert 0 <= result <= 1, 'Contracted tensor network not within bounds'
Exemplo n.º 2
0
def test_planar_mps2d_contract_mask():
    code = PlanarCode(3, 4)
    sample = code.new_pauli().site('Y', (2, 0), (2, 4))
    prob_dist = DepolarizingErrorModel().probability_distribution(0.1)
    tnc = PlanarMPSDecoder.TNC()
    tn = tnc.create_tn(prob_dist, sample)
    rng = np.random.default_rng()

    # tn_contract exact
    exact_result = tt.mps2d.contract(tn)
    assert isinstance(exact_result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    assert 0 <= exact_result <= 1, 'Contracted tensor network not within bounds'
    print('#exact_result=', exact_result)

    # tn_contract approx
    approx_result = tt.mps2d.contract(tn, chi=2)
    assert isinstance(approx_result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    assert 0 <= approx_result <= 1, 'Contracted tensor network not within bounds'
    print('#approx_result=', approx_result, '#rtol=',
          abs(approx_result - exact_result) / abs(exact_result))
    assert _is_close(exact_result, approx_result, rtol=1e-4,
                     atol=0), 'tn_contract(chi=2) not as expected'

    # tn_contract with truncate (chi, mask=0)
    stp = 0  # skip truncate probability
    mask = rng.choice((True, False), size=tn.shape, p=(1 - stp, stp))
    result = tt.mps2d.contract(tn, chi=2, mask=mask)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    assert 0 <= result <= 1, 'Contracted tensor network not within bounds'
    print('#result (chi=2, mask=0)=', result, '#rtol=',
          abs(result - approx_result) / abs(approx_result))
    assert approx_result == result, 'tn_contract(chi=2, mask=0) not same as approx_result'

    # tn_contract with truncate (chi, mask=1)
    stp = 1  # skip truncate probability
    mask = rng.choice((True, False), size=tn.shape, p=(1 - stp, stp))
    result = tt.mps2d.contract(tn, chi=2, mask=mask)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    assert 0 <= result <= 1, 'Contracted tensor network not within bounds'
    print('#result (chi=2, mask=1)=', result, '#rtol=',
          abs(result - exact_result) / abs(exact_result))
    assert exact_result == result, 'tn_contract(chi=2, mask=1) not same as exact_result'

    # tn_contract with truncate (chi, mask=0.5)
    stp = 0.5  # skip truncate probability
    mask = rng.choice((True, False), size=tn.shape, p=(1 - stp, stp))
    result = tt.mps2d.contract(tn, chi=2, mask=mask)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    assert 0 <= result <= 1, 'Contracted tensor network not within bounds'
    print('#result (chi=2, mask=0.5)=', result, '#rtol=',
          abs(result - exact_result) / abs(exact_result))
    assert exact_result != result, 'tn_contract(chi=2, mask=0.5) should not equal exact_result'
    assert approx_result != result, 'tn_contract(chi=2, mask=0.5) should not equal approx_result'
    assert _is_close(
        exact_result, result, rtol=1e-4,
        atol=0), 'tn_contract(chi=2, mask=0.5) not close to exact_result'
    print('#result (chi=2, mask=0.5)=', result, '#rtol=',
          abs(result - approx_result) / abs(approx_result))
    assert _is_close(
        approx_result, result, rtol=1e-4,
        atol=0), ('tn_contract(chi=2, mask=0.5) not close to approx_result')
Exemplo n.º 3
0
def test_planar_mps_functions():
    prob_dist = DepolarizingErrorModel().probability_distribution(0.1)
    rng = np.random.default_rng()
    tnc = PlanarMPSDecoder.TNC()
    # column bra, mpo and ket
    bra = [
        tnc.create_h_node(prob_dist, 'X', 'nw'),
        tnc.create_s_node('w'),
        tnc.create_h_node(prob_dist, 'Z', 'sw'),
    ]
    mpo = [
        tnc.create_s_node('n'),
        tnc.create_v_node(prob_dist, 'Y'),
        tnc.create_s_node('s'),
    ]
    ket = [
        tnc.create_h_node(prob_dist, 'Z', 'ne'),
        tnc.create_s_node('e'),
        tnc.create_h_node(prob_dist, 'I', 'se'),
    ]
    # tensor network corresponding to column bra, mpo and ket
    tn = np.array([bra, mpo, ket], dtype=object).transpose()
    expected = 0.00096790123456790152

    # exact contraction for expected
    result = tt.mps.contract_pairwise(bra, mpo)
    result = tt.mps.inner_product(result, ket)
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14,
                     atol=0), 'Exact contraction not as expected'

    # exact contraction with contract_ladder
    result = tt.mps.contract_pairwise(bra, mpo)
    result = tt.mps.contract_pairwise(result, ket)
    result = tt.mps.contract_ladder(result)[0, 0, 0, 0]
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14,
                     atol=0), 'Exact contraction not as expected'

    # exact contraction with lcf
    result = tt.mps.left_canonical_form(bra)
    result = tt.mps.contract_pairwise(result, mpo)
    result = tt.mps.left_canonical_form(result)
    result = tt.mps.inner_product(result, ket)
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14,
                     atol=0), 'Exact contraction with lcf not as expected'

    # exact contraction with rcf
    result = tt.mps.right_canonical_form(bra)
    result = tt.mps.contract_pairwise(result, mpo)
    result = tt.mps.right_canonical_form(result)
    result = tt.mps.inner_product(result, ket)
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14,
                     atol=0), 'Exact contraction with rcf not as expected'

    # exact contraction with truncate
    result = tt.mps.contract_pairwise(bra, mpo)
    result, norm = tt.mps.truncate(result)
    result = tt.mps.inner_product(result, ket)
    result *= norm
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14,
                     atol=0), 'Exact contraction with truncate not as expected'

    # exact contraction with normalise lcf
    result, norm1 = tt.mps.left_canonical_form(bra, normalise=True)
    result = tt.mps.contract_pairwise(result, mpo)
    result, norm2 = tt.mps.left_canonical_form(result, normalise=True)
    result = tt.mps.inner_product(result, ket)
    result *= norm1 * norm2
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(
        expected, result, rtol=1e-14,
        atol=0), 'Exact contraction with normalise lcf not as expected'

    # exact contraction with normalise rcf
    result, norm1 = tt.mps.right_canonical_form(bra, normalise=True)
    result = tt.mps.contract_pairwise(result, mpo)
    result, norm2 = tt.mps.right_canonical_form(result, normalise=True)
    result = tt.mps.inner_product(result, ket)
    result *= norm1 * norm2
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(
        expected, result, rtol=1e-14,
        atol=0), 'Exact contraction with normalise rcf not as expected'

    # approximate contraction with truncate (chi)
    result = tt.mps.contract_pairwise(bra, mpo)
    result, norm = tt.mps.truncate(result, chi=2)
    result = tt.mps.inner_product(result, ket)
    result *= norm
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(
        expected, result, rtol=1e-14,
        atol=0), 'Approx. contraction with truncate (chi) not as expected'

    # approximate contraction with truncate (tol)
    result = tt.mps.contract_pairwise(bra, mpo)
    result, norm = tt.mps.truncate(result, tol=1e-8)
    result = tt.mps.inner_product(result, ket)
    result *= norm
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(
        expected, result, rtol=1e-14,
        atol=0), 'Approx. contraction with truncate (tol) not as expected'

    # tn_contract
    result = tt.mps2d.contract(tn)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14,
                     atol=0), 'Exact tn contraction not as expected'

    # tn_contract with truncate (chi)
    result = tt.mps2d.contract(tn, chi=2)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(
        expected, result, rtol=1e-14,
        atol=0), 'Approx. tn contraction with truncate (chi) not as expected'

    # tn_contract with truncate (tol)
    result = tt.mps2d.contract(tn, tol=1e-8)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(
        expected, result, rtol=1e-14,
        atol=0), 'Approx. tn contraction with truncate (tol) not as expected'

    # tn_contract with truncate (chi, mask)
    stp = 0.2  # skip truncate probability
    mask = rng.choice((True, False), size=tn.shape, p=(1 - stp, stp))
    result = tt.mps2d.contract(tn, chi=2, mask=mask)
    assert isinstance(result,
                      mp.mpf), 'Contracted tensor network is not an mp.mpf'
    print('#result=', result, '#rtol=', abs(result - expected) / abs(expected))
    assert _is_close(expected, result, rtol=1e-14, atol=0), (
        'Approx. tn contraction with truncate (chi, mask) not as expected')