Beispiel #1
0
def test_purification_identity():
    n_density, rdm_gen, transform, molecule = h2_system()

    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm().real

    test_tpdm = purify_marginal(tpdm, molecule.n_electrons, molecule.n_qubits)
    assert np.allclose(test_tpdm, tpdm)
def test_number_expected():
    n_density, rdm_gen, transform, molecule = heh_system()

    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm().real
    opdm = density.construct_opdm().real

    # calculate on the fly
    np.testing.assert_almost_equal(number_expectation(opdm), opdm.trace())
Beispiel #3
0
def test_purification():
    n_density, rdm_gen, transform, molecule = h2_system()

    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm().real

    corrupted_tpdm = add_gaussian_noise_antisymmetric_four_tensor(
        tpdm.real, 0.0001)
    test_tpdm = purify_marginal(corrupted_tpdm, molecule.n_electrons,
                                molecule.n_qubits)
    assert np.allclose(test_tpdm, tpdm)
def test_mazziotti_opdm_purification():
    n_density, rdm_gen, transform, molecule = heh_system()
    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    opdm = density.construct_opdm()
    opdm_corrupted = add_gaussian_noise(opdm, 0.001)
    opdm_corrupted = 0.5 * (opdm_corrupted + np.conj(opdm_corrupted).T)
    purificed_opdm = mazziotti_opdm_purification(opdm_corrupted,
                                                 opdm.trace().real)
    assert np.isclose(purificed_opdm.trace(), opdm.trace())
    w, v = np.linalg.eigh(purificed_opdm)
    assert (1 >= w).all()
    assert (w >= 0).all()
def test_unitary_subspace_purification_fixed_opdm():
    n_density, rdm_gen, transform, molecule = heh_system()
    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm()
    tpdm_noise = add_gaussian_noise_antisymmetric_four_tensor(tpdm, 0.00001)
    check_antisymmetric_d2(tpdm_noise.real)
    tpdm_projected = unitary_subspace_purification_fixed_initial_trace(
        tpdm_noise.real, molecule.n_electrons, disp=True)

    d2_matrix = four_tensor2matrix(tpdm_projected)
    w, v = np.linalg.eigh(d2_matrix)
    assert all(w > -1.0E-6)
def test_sz_expected():
    n_density, rdm_gen, transform, molecule = heh_system()

    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm().real
    opdm = density.construct_opdm().real

    # calculate on the fly
    sz_eval = 0.0
    for i in range(opdm.shape[0]//2):
        sz_eval += opdm[2 * i, 2 * i] - opdm[2 * i + 1, 2 * i + 1]
    sz_eval *= 0.5

    np.testing.assert_almost_equal(sz_expected(opdm), sz_eval)
def test_map_d2_q2_with_wedge():
    n_density, rdm_gen, transform, molecule = heh_system()
    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm()
    tqdm = density.construct_thdm()
    opdm = density.construct_opdm()
    dim = opdm.shape[0]
    eye_wedge_eye = wedge_product(np.eye(dim), np.eye(dim))
    one_wedge_eye = wedge_product(opdm, np.eye(dim))
    tqdm_test = 2 * eye_wedge_eye - 4 * one_wedge_eye + tpdm
    for p, q, r, s in product(range(dim), repeat=4):
        np.testing.assert_allclose(tqdm_test[p, q, r, s],
                                   tqdm[p, q, r, s],
                                   atol=1.0E-10)
def test_s2_expected():
    n_density, rdm_gen, transform, molecule = heh_system()

    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm().real
    opdm = density.construct_opdm().real

    # calculate S^{2} by jordan-wigner of fermionic modes
    so_dim = int(opdm.shape[0])
    sp_dim = int(so_dim / 2)
    jw = JWTransform()
    sminus = sI(0) * 0
    splus = sI(0) * 0
    szop = sI(0) * 0
    for i in range(sp_dim):
        sminus += jw.product_ops([2 * i + 1, 2 * i], [-1, 1])
        splus += jw.product_ops([2 * i, 2 * i + 1], [-1, 1])
        szop += 0.5 * jw.product_ops([2 * i, 2 * i], [-1, 1])
        szop -= 0.5 * jw.product_ops([2 * i + 1, 2 * i + 1], [-1, 1])

    s2op = sminus * splus + szop + szop * szop
    s2op_matrix = tensor_up(s2op, so_dim)
    szop_matrix = tensor_up(szop, so_dim)
    szop2_matrix = tensor_up(szop * szop, so_dim)
    splus_matrix = tensor_up(splus, so_dim)
    sminus_matrix = tensor_up(sminus, so_dim)
    smsp_matrix = tensor_up(sminus * splus, so_dim)

    np.testing.assert_allclose(smsp_matrix, sminus_matrix.dot(splus_matrix))
    np.testing.assert_allclose(szop2_matrix, szop_matrix.dot(szop_matrix))
    np.testing.assert_allclose(szop2_matrix + szop_matrix,
                               szop_matrix.dot(szop_matrix) + szop_matrix)

    np.testing.assert_allclose(smsp_matrix + szop2_matrix + szop_matrix,
                               smsp_matrix + szop_matrix.dot(szop_matrix) + szop_matrix)

    np.testing.assert_allclose(smsp_matrix + szop2_matrix + szop_matrix,
                               s2op_matrix)

    # Now we can check if our two methods for computing the S^{2} value are
    # correct.
    s2_from_ndensity = np.trace(n_density.dot(s2op_matrix)).real
    trial_s2_expected = s2_expected(tpdm, opdm)
    np.testing.assert_almost_equal(s2_from_ndensity, trial_s2_expected)
def test_coleman_decomp():
    n_density, rdm_gen, transform, molecule = heh_system()
    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm()
    opdm = density.construct_opdm()
    dim = tpdm.shape[0]

    # check if we get a valid tpdm
    check_antisymmetric_d2(tpdm)

    # now test the Coleman-Absar decomposition
    d2_0, d2_1, d2_2 = coleman_decomposition(tpdm)

    # now check some properties.
    # 1. check d2_2 contracts to zero
    d1_zero = np.einsum('ikjk', d2_2)
    assert np.allclose(d1_zero, 0.0)

    # 2. check orthogonality of tenors
    assert np.isclose(np.einsum('ijkl, lkji', d2_0, d2_1), 0.0)
    assert np.isclose(np.einsum('ijkl, lkji', d2_1, d2_2), 0.0)
    assert np.isclose(np.einsum('ijkl, lkji', d2_2, d2_0), 0.0)

    # 3. check reconstruct to tpdm
    assert np.allclose(d2_0 + d2_1 + d2_2, tpdm)

    # 4. check D1 looks like a contracted d2 up to coefficients
    one_one_tensor = np.einsum('ikjk', tpdm)
    N = molecule.n_electrons
    # why are we missing the factor of 2? Is it because of normalization on D2?
    assert np.allclose(one_one_tensor, (N - 1) * opdm)

    # 5. Check trace of d2_0 is same as tpdm
    assert np.isclose(np.einsum('ijij', tpdm), np.einsum('ijij', d2_0))

    # 6. Check one-trace (d2_0 + d2_1) = a1
    assert np.allclose(np.einsum('ikjk', d2_0 + d2_1), one_one_tensor)
def heh_system():
    basis = 'sto-3g'
    multiplicity = 1
    charge = 1
    geometry = [('He', [0.0, 0.0, 0.0]), ('H', [0, 0, 0.75])]
    molecule = MolecularData(geometry, basis, multiplicity, charge)
    # Run Psi4.
    molecule = run_psi4(molecule,
                        run_scf=True,
                        run_mp2=False,
                        run_cisd=False,
                        run_ccsd=False,
                        run_fci=True,
                        delete_input=False)

    molecule, gs_wf, n_density, eigen_val = get_molecule_openfermion(molecule, eigen_index=2)
    rdm_generator = SpinOrbitalDensity(n_density, molecule.n_qubits)
    transform = jordan_wigner
    return n_density, rdm_generator, transform, molecule
Beispiel #11
0
def test_mappings():
    n_density, rdm_gen, transform, molecule = h2_system()

    density = SpinOrbitalDensity(n_density, molecule.n_qubits)
    tpdm = density.construct_tpdm().real
    opdm = density.construct_opdm().real
    oqdm = density.construct_ohdm().real
    tqdm = density.construct_thdm().real
    phdm = density.construct_phdm().real

    test_opdm = map_tpdm_to_opdm(tpdm, molecule.n_electrons)
    assert np.allclose(test_opdm, opdm)

    test_tqdm = map_tpdm_to_tqdm(tpdm, opdm)
    assert np.allclose(test_tqdm, tqdm)

    test_phdm = map_tpdm_to_phdm(tpdm, opdm)
    assert np.allclose(test_phdm, phdm)

    test_tpdm = map_tqdm_to_tpdm(tqdm, opdm)
    assert np.allclose(test_tpdm, tpdm)

    test_tpdm = map_phdm_to_tpdm(phdm, opdm)
    assert np.allclose(test_tpdm, tpdm)

    test_oqdm = map_tqdm_to_oqdm(tqdm,
                                 molecule.n_qubits - molecule.n_electrons)
    assert np.allclose(test_oqdm, oqdm)

    test_opdm = map_oqdm_to_opdm(oqdm)
    assert np.allclose(test_opdm, opdm)

    test_oqdm = map_opdm_to_oqdm(opdm)
    assert np.allclose(test_oqdm, oqdm)

    eta = molecule.n_qubits - molecule.n_electrons
    N = molecule.n_electrons
    opdm_test = np.einsum('ijkj', phdm) / ((N + 1))
    assert np.allclose(opdm, opdm_test)