Example #1
0
def test_pyquil_to_qubitop_pauliterm_conversion():
    """Test if a pauliterm is converted to a pauli sum"""
    pyquil_term = PauliTerm("X", 0) * PauliTerm("Y", 5)
    # implicit test of conversion from Term to Sum
    open_fermion_term = pyquilpauli_to_qubitop(pyquil_term)
    op_fermion_term = QubitOperator(((0, "X"), (5, "Y")))
    assert open_fermion_term.isclose(op_fermion_term)
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)
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_tpdm_build():
    """
    Check if constructing the 2-RDM (full) works.  This check uses openfermion
    data and thus requires openfermion to be installed
    """

    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)
    assert np.allclose(tpdm, molecule.fci_two_rdm)
Example #5
0
def test_pyquil_to_qubitop_type_enforced():
    """Enforce the appropriate type"""
    create_one = FermionOperator('1^')
    empty_one_body = np.zeros((2, 2))
    empty_two_body = np.zeros((2, 2, 2, 2))
    interact_one = InteractionOperator(1, empty_one_body, empty_two_body)
    interact_rdm = InteractionRDM(empty_one_body, empty_two_body)

    with pytest.raises(TypeError):
        pyquilpauli_to_qubitop(create_one)
    with pytest.raises(TypeError):
        pyquilpauli_to_qubitop(interact_one)
    with pytest.raises(TypeError):
        pyquilpauli_to_qubitop(interact_rdm)

    # don't accept anything other than pyquil PauliSum or PauliTerm
    with pytest.raises(TypeError):
        pyquilpauli_to_qubitop(create_one)
    with pytest.raises(TypeError):
        pyquilpauli_to_qubitop(interact_one)
    with pytest.raises(TypeError):
        pyquilpauli_to_qubitop(interact_rdm)
Example #6
0
def test_pyquil_to_qubitop():
    pyquil_term = PauliSum([PauliTerm("X", 0) * PauliTerm("Y", 5)])
    op_fermion_term = QubitOperator(((0, "X"), (5, "Y")))
    test_op_fermion_term = pyquilpauli_to_qubitop(pyquil_term)
    assert test_op_fermion_term.isclose(op_fermion_term)