コード例 #1
0
ファイル: test_peptide.py プロジェクト: 1ucian0/qiskit-nature
    def test_peptide_hot_vector_longer_chain(self):
        """Tests that a Peptide is created."""
        main_chain_residue_seq = "SAAAAAAAA"
        side_chain_residue_sequences = ["", "", "A", "", "", "A", "", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)

        side_chain_hot_vector = peptide.get_side_chain_hot_vector()

        self.assertEqual(len(peptide.get_main_chain.beads_list), 9)
        self.assertEqual(side_chain_hot_vector, [0, 0, 1, 0, 0, 1, 0, 1, 0])
コード例 #2
0
    def test_peptide_constructor(self):
        """Tests that a Peptide is created."""
        main_chain_residue_seq = "SAAR"
        side_chain_residue_sequences = ["", "", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)

        side_chain_hot_vector = peptide.get_side_chain_hot_vector()

        self.assertEqual(len(peptide.get_main_chain.beads_list), 4)
        self.assertEqual(len(peptide.get_main_chain[2].side_chain.beads_list), 1)
        assert peptide.get_main_chain[0].side_chain is None
        assert peptide.get_main_chain[1].side_chain is None
        assert peptide.get_main_chain[3].side_chain is None
        self.assertEqual(side_chain_hot_vector, [0, 0, 1, 0])
コード例 #3
0
    def test_get_result_binary_vector_compressed(self):
        """Tests if a protein folding result returns a correct expanded best sequence."""
        lambda_back = 10
        lambda_chiral = 10
        lambda_1 = 10
        penalty_terms = PenaltyParameters(lambda_chiral, lambda_back, lambda_1)

        main_chain_residue_seq = "SAASSASAA"
        side_chain_residue_sequences = [
            "", "", "A", "A", "A", "A", "A", "A", ""
        ]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)

        mj_interaction = MiyazawaJerniganInteraction()

        protein_folding_problem = ProteinFoldingProblem(
            peptide, mj_interaction, penalty_terms)
        best_sequence = "101110010"
        protein_folding_problem._unused_qubits = [0, 1, 2, 5, 7]

        protein_folding_result = ProteinFoldingResult(protein_folding_problem,
                                                      best_sequence)

        result = protein_folding_result.get_result_binary_vector()
        expected_sequence = "101110*0*10***"

        self.assertEqual(result, expected_sequence)
コード例 #4
0
    def test_second_neighbor(self):
        """
        Tests that Pauli operators for 2nd neighbor interactions are created correctly.
        """
        main_chain_residue_seq = "SAASS"
        side_chain_residue_sequences = ["", "", "A", "", ""]
        mj_interaction = MiyazawaJerniganInteraction()
        pair_energies = mj_interaction.calculate_energy_matrix(
            main_chain_residue_seq)

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        lambda_1 = 2
        lower_main_bead_index = 1
        upper_main_bead_index = 4
        side_chain_lower_main_bead = 0
        side_chain_upper_main_bead = 0
        distance_map = DistanceMap(peptide)
        second_neighbor = distance_map._second_neighbor(
            peptide,
            lower_main_bead_index,
            side_chain_upper_main_bead,
            upper_main_bead_index,
            side_chain_lower_main_bead,
            lambda_1,
            pair_energies,
        )
        expected_path = self.get_resource_path(
            "test_second_neighbor",
            PATH,
        )
        expected = read_expected_file(expected_path)
        self.assertEqual(second_neighbor, expected)
コード例 #5
0
    def __init__(
        self,
        main_chain_turns: List[int],
        side_chain_turns: List[Union[None, int]],
        peptide: Peptide,
    ) -> None:
        """
        Args:
            main_chain_turns: A list of integers encoding the turns of the main chain.
            side_chain_turns: A list of integers and None encoding the turns of the main chain
                or None.
            peptide: The peptide we are getting the positions for.

        """
        self._main_chain_turns = main_chain_turns
        self._side_chain_turns = side_chain_turns

        self._main_chain_aminoacid_list = np.array(
            list(peptide.get_main_chain.main_chain_residue_sequence))
        self._side_chain_aminoacid_list = np.array([
            aminoacid.residue_sequence[0] if (aminoacid is not None) else None
            for aminoacid in peptide.get_side_chains()
        ])
        self._main_positions = self.generate_main_positions()
        self._side_positions = self.generate_side_positions()
コード例 #6
0
    def test_protein_folding_problem_2_second_bead_side_chain(self):
        """Tests if a protein folding problem is created and returns a correct qubit operator if
        a second main bead has a side chain."""
        lambda_back = 10
        lambda_chiral = 10
        lambda_1 = 10
        penalty_terms = PenaltyParameters(lambda_chiral, lambda_back, lambda_1)

        main_chain_residue_seq = "SAAS"
        side_chain_residue_sequences = ["", "A", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)

        mj_interaction = MiyazawaJerniganInteraction()

        protein_folding_problem = ProteinFoldingProblem(
            peptide, mj_interaction, penalty_terms)
        qubit_op = protein_folding_problem._qubit_op_full()

        expected_path = self.get_resource_path(
            "test_protein_folding_problem_2_second_bead_side_chain",
            PATH,
        )
        expected = read_expected_file(expected_path)
        self.assertEqual(qubit_op, expected)
コード例 #7
0
def create_protein_folding_result(main_chain: str, side_chains: List[str],
                                  turn_sequence: str) -> ProteinFoldingResult:
    """
    Creates a protein_folding_problem, solves it and uses the result
    to create a protein_folding_result instance.
    Args:
        main_chain: The desired main_chain for the molecules to be optimized
        side_chains: The desired side_chains for the molecules to be optimized
        turn_sequence: The best sequence found by ProteinFoldingResult pre-computed
    Returns:
        Protein Folding Result
    """
    algorithm_globals.random_seed = 23
    peptide = Peptide(main_chain, side_chains)
    mj_interaction = MiyazawaJerniganInteraction()

    penalty_back = 10
    penalty_chiral = 10
    penalty_1 = 10

    penalty_terms = PenaltyParameters(penalty_chiral, penalty_back, penalty_1)

    protein_folding_problem = ProteinFoldingProblem(peptide, mj_interaction,
                                                    penalty_terms)
    protein_folding_problem.qubit_op()

    return ProteinFoldingResult(
        unused_qubits=protein_folding_problem.unused_qubits,
        peptide=protein_folding_problem.peptide,
        turn_sequence=turn_sequence,
    )
コード例 #8
0
    def test_create_pauli_for_contacts_2(self):
        """
        Tests that Pauli operators for contact qubits are created correctly.
        """
        main_chain_residue_seq = "SAASSS"
        side_chain_residue_sequences = ["", "", "A", "A", "S", ""]
        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        (
            lower_main_upper_main,
            lower_side_upper_main,
            lower_main_upper_side,
            lower_side_upper_side,
            r_contact,
        ) = contact_map_builder._create_contact_qubits(peptide)
        expected_path = self.get_resource_path(
            "test_create_pauli_for_contacts_2_expected_1",
            PATH,
        )

        expected_1 = read_expected_file(expected_path)
        expected_path = self.get_resource_path(
            "test_create_pauli_for_contacts_2_expected_2",
            PATH,
        )

        expected_2 = read_expected_file(expected_path)

        self.assertEqual(lower_main_upper_main[1][6], expected_1)
        self.assertEqual(lower_main_upper_side[1][5], expected_2)
        self.assertEqual(lower_side_upper_main, {})
        self.assertEqual(lower_side_upper_side, {})
        self.assertEqual(r_contact, 2)
コード例 #9
0
    def setUp(self):
        super().setUp()
        main_chain_residue_seq = "SAAAA"
        side_chain_residue_sequences = ["", "", "A", "", ""]

        self.peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        self.distance_map_builder = DistanceMapBuilder()
コード例 #10
0
ファイル: test_peptide.py プロジェクト: 1ucian0/qiskit-nature
    def test_peptide_get_side_chains(self):
        """Tests that a side chains are provided."""
        main_chain_residue_seq = "SAAAAAAAA"
        side_chain_residue_sequences = ["", "", "A", "", "", "A", "", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)

        side_chains = peptide.get_side_chains()
        self.assertEqual(side_chains[0], None)
        self.assertEqual(side_chains[1], None)
        self.assertEqual(side_chains[2].residue_sequence, ["A"])
        self.assertEqual(side_chains[3], None)
        self.assertEqual(side_chains[4], None)
        self.assertEqual(side_chains[5].residue_sequence, ["A"])
        self.assertEqual(side_chains[6], None)
        self.assertEqual(side_chains[7].residue_sequence, ["A"])
        self.assertEqual(side_chains[8], None)
コード例 #11
0
    def test_check_turns(self):
        """
        Tests that check turns operators are generate correctly.
        """
        main_chain_residue_seq = "SAASSA"
        side_chain_residue_sequences = ["", "", "A", "A", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        bead_2 = peptide.get_main_chain[2]
        bead_3 = peptide.get_main_chain[3]
        bead_4 = peptide.get_main_chain[4]
        side_bead_2 = bead_2.side_chain[0]
        side_bead_3 = bead_3.side_chain[0]
        side_bead_4 = bead_4.side_chain[0]

        lambda_back = 10
        lambda_chiral = 10
        lambda_1 = 10
        penalty_params = PenaltyParameters(lambda_chiral, lambda_back, lambda_1)
        mj_interaction = MiyazawaJerniganInteraction()
        pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)

        qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)

        t_23 = qubit_op_builder._create_turn_operators(bead_2, bead_3)
        t_34 = qubit_op_builder._create_turn_operators(bead_3, bead_4)
        t_2s3 = qubit_op_builder._create_turn_operators(side_bead_2, bead_3)
        t_3s4s = qubit_op_builder._create_turn_operators(side_bead_3, side_bead_4)

        expected_path_t23 = self.get_resource_path(
            "test_check_turns_expected_t23",
            PATH,
        )
        expected_t23 = read_expected_file(expected_path_t23)
        expected_path_t34 = self.get_resource_path(
            "test_check_turns_expected_t34",
            PATH,
        )
        expected_t34 = read_expected_file(expected_path_t34)
        expected_path_t2s3 = self.get_resource_path(
            "test_check_turns_expected_t2s3",
            PATH,
        )
        expected_t_2s3 = read_expected_file(expected_path_t2s3)
        expected_path_t3s4s = self.get_resource_path(
            "test_check_turns_expected_t3s4s",
            PATH,
        )
        expected_t_3s4s = read_expected_file(expected_path_t3s4s)
        self.assertEqual(t_23, expected_t23)
        self.assertEqual(t_34, expected_t34)
        self.assertEqual(t_2s3, expected_t_2s3)
        self.assertEqual(t_3s4s, expected_t_3s4s)
コード例 #12
0
 def test_create_h_short(self):
     """
     Tests that the Hamiltonian to back-overlaps is created correctly.
     """
     main_chain_residue_seq = "APRLAAAA"
     side_chain_residue_sequences = ["", "", "A", "A", "A", "A", "A", ""]
     mj_interaction = MiyazawaJerniganInteraction()
     pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
     peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
     penalty_params = PenaltyParameters()
     qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)
     h_short = qubit_op_builder._create_h_short()
     expected = PauliSumOp.from_list([("IIIIIIIIIIIIIIIIIIIIIIIIIIII", 0)])
     self.assertEqual(h_short, expected)
コード例 #13
0
 def test_create_h_bbsc_and_h_scbb_3(self):
     """
     Tests if H_BBBB Hamiltonian qubit operator is built correctly.
     """
     main_chain_residue_seq = "SAACS"
     side_chain_residue_sequences = ["", "", "A", "A", ""]
     mj_interaction = MiyazawaJerniganInteraction()
     pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
     peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
     penalty_params = PenaltyParameters()
     qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)
     h_bbsc, h_scbb = qubit_op_builder._create_h_bbsc_and_h_scbb()
     self.assertEqual(h_bbsc, 0)
     self.assertEqual(h_scbb, 0)
コード例 #14
0
    def test_shape(
        self,
        main_chain_turns,
        side_chain_turns,
        main_chain_residue_sequence,
        side_chain_residue_sequences,
        side_positions,
        main_positions,
        xyz,
        name_file,
    ):
        """Tests if ProteinShapeFileGen is properly initialized and its attributes are properly set."""
        peptide = Peptide(
            main_chain_residue_sequence=main_chain_residue_sequence,
            side_chain_residue_sequences=side_chain_residue_sequences,
        )
        filegen = ProteinShapeFileGen(
            main_chain_turns=main_chain_turns,
            side_chain_turns=side_chain_turns,
            peptide=peptide,
        )
        with self.subTest("Side Positions"):
            for result, expected in zip(filegen.side_positions,
                                        side_positions):
                if expected is None:
                    self.assertIsNone(result)
                else:
                    np.testing.assert_almost_equal(result, expected, decimal=6)

        with self.subTest("Main Positions"):
            np.testing.assert_almost_equal(
                filegen.main_positions,
                main_positions,
                decimal=6,
            )
        with self.subTest("XYZ file data"):
            np.testing.assert_equal(filegen.get_xyz_data(), xyz)

        with self.subTest("Write file"):
            current_dir = os.path.dirname(__file__)
            test_path = os.path.join(current_dir, "resources")
            file_test = os.path.join(test_path, name_file + "_test.xyz")
            with tempfile.TemporaryDirectory() as tmpdirname:
                filegen.save_xyz_file(name=name_file + "_temp",
                                      path=tmpdirname,
                                      comment="This is a dummy comment.")

                file_temp = os.path.join(tmpdirname, name_file + "_temp.xyz")
                self.assertTrue(filecmp.cmp(file_temp, file_test))
コード例 #15
0
 def test_create_h_bbbb(self):
     """Tests if H_BBBB Hamiltonian qubit operator is built correctly."""
     main_chain_residue_seq = "SAASSASA"
     side_chain_residue_sequences = ["", "", "A", "", "", "A", "A", ""]
     mj_interaction = MiyazawaJerniganInteraction()
     pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
     peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
     penalty_params = PenaltyParameters()
     qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)
     h_bbbb = qubit_op_builder._create_h_bbbb()
     expected_path = self.get_resource_path(
         "test_create_h_bbbb_expected",
         PATH,
     )
     expected = read_expected_file(expected_path)
     self.assertEqual(h_bbbb, expected)
コード例 #16
0
 def test_create_h_chiral_2(self):
     """
     Tests that the Hamiltonian chirality constraints is created correctly.
     """
     main_chain_residue_seq = "SAACS"
     side_chain_residue_sequences = ["", "", "A", "A", ""]
     peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
     mj_interaction = MiyazawaJerniganInteraction()
     pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
     penalty_params = PenaltyParameters()
     qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)
     h_chiral = qubit_op_builder._create_h_chiral()
     expected_path = self.get_resource_path(
         "test_create_h_chiral_2_expected",
         PATH,
     )
     expected = read_expected_file(expected_path)
     self.assertEqual(h_chiral, expected)
コード例 #17
0
    def test_create_pauli_for_contacts(self):
        """
        Tests that Pauli operators for contact qubits are created correctly.
        """
        main_chain_residue_seq = "SAASS"
        side_chain_residue_sequences = ["", "", "A", "A", ""]
        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        (
            lower_main_upper_main,
            lower_side_upper_main,
            lower_main_upper_side,
            lower_side_upper_side,
            r_contact,
        ) = contact_map_builder._create_contact_qubits(peptide)

        self.assertEqual(lower_main_upper_main, {})
        self.assertEqual(lower_side_upper_main, {})
        self.assertEqual(lower_main_upper_side, {})
        self.assertEqual(lower_side_upper_side, {})
        self.assertEqual(r_contact, 0)
コード例 #18
0
    def test_create_h_back_side_chains(self):
        """
        Tests that the Hamiltonian to back-overlaps is created correctly in the presence of side
        chains which should not have any influence in this case.
        """
        main_chain_residue_seq = "SAASS"
        side_chain_residue_sequences = ["", "", "A", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        mj_interaction = MiyazawaJerniganInteraction()
        pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
        penalty_params = PenaltyParameters()
        qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)
        h_back = qubit_op_builder._create_h_back()
        expected_path = self.get_resource_path(
            "test_create_h_back_side_chains_expected",
            PATH,
        )
        expected = read_expected_file(expected_path)
        self.assertEqual(h_back, expected)
コード例 #19
0
    def test_build_qubit_op(self):
        """Tests if a total Hamiltonian qubit operator is built correctly."""
        lambda_back = 10
        lambda_chiral = 10
        lambda_1 = 10
        main_chain_residue_seq = "SAASSASAA"
        side_chain_residue_sequences = ["", "", "A", "A", "A", "A", "A", "A", ""]

        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        mj_interaction = MiyazawaJerniganInteraction()
        penalty_params = PenaltyParameters(lambda_chiral, lambda_back, lambda_1)
        pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
        qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)
        qubit_op = qubit_op_builder._build_qubit_op()
        expected_path = self.get_resource_path(
            "test_build_qubit_op_expected",
            PATH,
        )
        expected = read_expected_file(expected_path)
        self.assertEqual(qubit_op, expected)
コード例 #20
0
    def test_create_h_bbsc_and_h_scbb(self):
        """Tests if H_BBSC and H_SCBB Hamiltonians qubit operators are built correctly."""
        main_chain_residue_seq = "APRLAAA"
        side_chain_residue_sequences = ["", "", "A", "", "", "A", ""]
        mj_interaction = MiyazawaJerniganInteraction()
        pair_energies = mj_interaction.calculate_energy_matrix(main_chain_residue_seq)
        peptide = Peptide(main_chain_residue_seq, side_chain_residue_sequences)
        penalty_params = PenaltyParameters()
        qubit_op_builder = QubitOpBuilder(peptide, pair_energies, penalty_params)

        h_bbsc, h_scbb = qubit_op_builder._create_h_bbsc_and_h_scbb()

        expected_path_h_bbsc = self.get_resource_path(
            "test_create_h_bbsc_and_h_scbb_expected_h_bbsc",
            PATH,
        )
        expected_path_h_scbb = self.get_resource_path(
            "test_create_h_bbsc_and_h_scbb_expected_h_scbb",
            PATH,
        )
        expected_h_bbsc = read_expected_file(expected_path_h_bbsc)
        expected_h_scbb = read_expected_file(expected_path_h_scbb)
        self.assertEqual(h_bbsc, expected_h_bbsc)
        self.assertEqual(h_scbb, expected_h_scbb)