Пример #1
0
def test_spin_adapt_lih():
    """
    Test if we accurately generate the fci spin-adapted d2-matrices
    """
    lih_file = os.path.join(DATA_DIRECTORY, "H1-Li1_sto-3g_singlet_1.45.hdf5")
    molecule = MolecularData(filename=lih_file)
    d2aa, d2bb, d2ab = get_sz_spin_adapted(molecule.fci_two_rdm)
    assert np.allclose(d2aa, d2bb)
    assert np.isclose(np.trace(d2ab), (molecule.n_electrons / 2)**2)

    test_tpdm = unspin_adapt(d2aa, d2bb, d2ab)
    assert np.allclose(molecule.fci_two_rdm, test_tpdm)
Пример #2
0
def test_spin_adapt_h2():
    """
    Test if we accurately generate the fci spin-adapted d2-matrices
    """
    h2_file = os.path.join(DATA_DIRECTORY, "H2_sto-3g_singlet_0.7.hdf5")
    molecule = MolecularData(filename=h2_file)
    d2aa, d2bb, d2ab = get_sz_spin_adapted(molecule.fci_two_rdm)
    assert np.allclose(d2aa, np.zeros((1, 1)))
    assert np.allclose(d2bb, np.zeros((1, 1)))

    true_d2ab = np.array([[0.98904311, 0., 0., -0.10410015], [0., 0., 0., 0.],
                          [0., 0., 0., 0.], [-0.10410015, 0., 0., 0.01095689]])
    assert np.allclose(d2ab, true_d2ab)

    test_tpdm = unspin_adapt(d2aa, d2bb, d2ab)
    assert np.allclose(molecule.fci_two_rdm, test_tpdm)
def test_lih_tpdm_ab_build():
    """
    Check if 2-RDM construction from pauli terms works for the ab block
    """
    lih_file = os.path.join(DATA_DIRECTORY, "H1-Li1_sto-3g_singlet_1.45.hdf5")
    molecule = MolecularData(filename=lih_file)
    rdms = molecule.get_molecular_rdm(use_fci=True)
    dim = molecule.n_qubits
    d2aa, d2bb, d2ab = get_sz_spin_adapted(molecule.fci_two_rdm)
    paulis_to_measure = pauli_terms_for_tpdm_ab(dim // 2)
    pauli_to_coeff = {}
    for term in paulis_to_measure:
        # convert back to FermionOperator
        qubit_op = pyquilpauli_to_qubitop(term_with_coeff(term, 1.0))
        pauli_to_coeff[term.id()] = rdms.expectation(qubit_op)

    tpdm_ab = pauli_to_tpdm_ab(dim // 2, pauli_to_coeff)
    assert np.allclose(tpdm_ab, d2ab)
def test_h2_spin_adapted_ab():
    h2_file = os.path.join(DATA_DIRECTORY, "H2_sto-3g_singlet_0.7.hdf5")
    molecule = MolecularData(filename=h2_file)
    rdms = molecule.get_molecular_rdm(use_fci=True)
    dim = molecule.n_qubits
    paulis_to_measure = pauli_terms_for_tpdm(dim)
    pauli_to_coeff = {}
    for term in paulis_to_measure:
        # convert back to FermionOperator
        qubit_op = pyquilpauli_to_qubitop(term_with_coeff(term, 1.0))
        pauli_to_coeff[term.id()] = rdms.expectation(qubit_op)
    tpdm = pauli_to_tpdm(dim, pauli_to_coeff)
    d2aa, d2bb, d2ab = get_sz_spin_adapted(tpdm)

    paulis_to_measure_ab = pauli_terms_for_tpdm_ab(dim // 2)
    pauli_to_coeff = {}
    for term in paulis_to_measure_ab:
        # convert back to FermionOperator
        qubit_op = pyquilpauli_to_qubitop(term_with_coeff(term, 1.0))
        pauli_to_coeff[term.id()] = rdms.expectation(qubit_op)
    tpdm_ab = pauli_to_tpdm_ab(dim // 2, pauli_to_coeff)
    assert np.allclose(d2ab, tpdm_ab)