Esempio n. 1
0
 def test_randomlayers_normal_edgecase(self, seed, tol):
     """Test sampling edge case of pennylane.init.random_layers_normal()."""
     n_layers = 3
     p = random_layers_normal(n_layers=n_layers,
                              n_wires=10,
                              mean=1,
                              std=0,
                              seed=seed)
     p_mean = np.mean(np.array([np.mean(pp) for p_ in p for pp in p_]))
     assert np.allclose(p_mean, 1, atol=tol, rtol=0.)
Esempio n. 2
0
 def test_randomlayers_normal_dimensions(self, n_subsystems, n_layers,
                                         n_rots):
     """Confirm that the pennylane.init.random_layers_normal()
      returns an array with the right dimensions."""
     if n_rots is None:
         n_rots = n_subsystems
     a = (n_layers, n_rots)
     p = random_layers_normal(n_layers=n_layers,
                              n_wires=n_subsystems,
                              n_rots=n_rots,
                              seed=0)
     dims = [p_.shape for p_ in p]
     assert dims == [a]
Esempio n. 3
0
 def test_randomlayers_normal_seed(self, seed, tol):
     """Confirm that pennylane.init.random_layers_normal() invokes the correct np.random sampling function
     for a given seed."""
     mean = -2
     std = 1
     n_wires = 3
     n_rots = 5
     n_layers = 3
     p = random_layers_normal(n_layers=n_layers,
                              n_wires=n_wires,
                              n_rots=n_rots,
                              mean=mean,
                              std=std,
                              seed=seed)
     np.random.seed(seed)
     p_target = np.random.normal(loc=mean,
                                 scale=std,
                                 size=(n_layers, n_rots))
     assert np.allclose(p[0], p_target, atol=tol, rtol=0.)
Esempio n. 4
0
class TestInitializationIntegration:
    """Tests integration with the parameter initialization functions from pennylane.init"""

    # TODO: Combine CV and Qubit tests, since the only difference is the device

    def make_n_features(self, n):
        """Helper to prepare dummy feature inputs for templates that have
        as many features as number of wires."""
        return [i for i in range(n)]

    QUBIT_INIT = [(StronglyEntanglingLayers,
                   {'weights': strong_ent_layers_uniform(n_layers=3, n_wires=2), 'wires': range(2)}),
                  (StronglyEntanglingLayers,
                   {'weights': strong_ent_layers_uniform(n_layers=2, n_wires=3), 'wires': range(3)}),
                  (StronglyEntanglingLayers,
                   {'weights': strong_ent_layers_normal(n_layers=3, n_wires=2), 'wires': range(2)}),
                  (StronglyEntanglingLayers,
                   {'weights': strong_ent_layers_normal(n_layers=2, n_wires=3), 'wires': range(3)}),
                  (RandomLayers,
                   {'weights': random_layers_uniform(n_layers=3, n_rots=2, n_wires=2), 'wires': range(2)}),
                  (RandomLayers,
                   {'weights': random_layers_uniform(n_layers=3, n_rots=2, n_wires=2), 'wires': range(2)}),
                  (RandomLayers,
                   {'weights': random_layers_normal(n_layers=2, n_rots=2, n_wires=3), 'wires': range(3)}),
                  (RandomLayers,
                   {'weights': random_layers_normal(n_layers=2, n_rots=2, n_wires=3), 'wires': range(3)}),
                  (QAOAEmbedding,
                   {'features': [1., 2.], 'weights': qaoa_embedding_uniform(n_layers=3, n_wires=2), 'wires': range(2)}),
                  (QAOAEmbedding,
                   {'features': [1., 2.], 'weights': qaoa_embedding_uniform(n_layers=3, n_wires=2), 'wires': range(2)}),
                  (QAOAEmbedding,
                   {'features': [1., 2.], 'weights': qaoa_embedding_normal(n_layers=2, n_wires=3), 'wires': range(3)}),
                  (QAOAEmbedding,
                   {'features': [1., 2.], 'weights': qaoa_embedding_normal(n_layers=2, n_wires=3), 'wires': range(3)}),
                  (QAOAEmbedding,
                   {'features': [1., 2.], 'weights': qaoa_embedding_normal(n_layers=2, n_wires=1), 'wires': range(1)}),
                  (QAOAEmbedding,
                   {'features': [1., 2.], 'weights': qaoa_embedding_uniform(n_layers=2, n_wires=1), 'wires': range(1)})
                  ]

    CV_INIT = [(CVNeuralNetLayers,
                {'theta_1': cvqnn_layers_theta_uniform(n_layers=3, n_wires=2),
                 'phi_1': cvqnn_layers_phi_uniform(n_layers=3, n_wires=2),
                 'varphi_1': cvqnn_layers_varphi_uniform(n_layers=3, n_wires=2),
                 'r': cvqnn_layers_r_uniform(n_layers=3, n_wires=2),
                 'phi_r': cvqnn_layers_phi_r_uniform(n_layers=3, n_wires=2),
                 'theta_2': cvqnn_layers_theta_uniform(n_layers=3, n_wires=2),
                 'phi_2': cvqnn_layers_phi_uniform(n_layers=3, n_wires=2),
                 'varphi_2': cvqnn_layers_varphi_uniform(n_layers=3, n_wires=2),
                 'a': cvqnn_layers_a_uniform(n_layers=3, n_wires=2),
                 'phi_a': cvqnn_layers_phi_a_uniform(n_layers=3, n_wires=2),
                 'k': cvqnn_layers_kappa_uniform(n_layers=3, n_wires=2),
                 'wires': range(2)}),
               (CVNeuralNetLayers,
                {'theta_1': cvqnn_layers_theta_normal(n_layers=3, n_wires=2),
                 'phi_1': cvqnn_layers_phi_normal(n_layers=3, n_wires=2),
                 'varphi_1': cvqnn_layers_varphi_normal(n_layers=3, n_wires=2),
                 'r': cvqnn_layers_r_normal(n_layers=3, n_wires=2),
                 'phi_r': cvqnn_layers_phi_r_normal(n_layers=3, n_wires=2),
                 'theta_2': cvqnn_layers_theta_normal(n_layers=3, n_wires=2),
                 'phi_2': cvqnn_layers_phi_normal(n_layers=3, n_wires=2),
                 'varphi_2': cvqnn_layers_varphi_normal(n_layers=3, n_wires=2),
                 'a': cvqnn_layers_a_normal(n_layers=3, n_wires=2),
                 'phi_a': cvqnn_layers_phi_a_normal(n_layers=3, n_wires=2),
                 'k': cvqnn_layers_kappa_normal(n_layers=3, n_wires=2),
                 'wires': range(2)}),
               (Interferometer,
                {'phi': interferometer_phi_uniform(n_wires=2), 'varphi': interferometer_varphi_uniform(n_wires=2),
                 'theta': interferometer_theta_uniform(n_wires=2), 'wires': range(2)}),
               (Interferometer,
                {'phi': interferometer_phi_normal(n_wires=2), 'varphi': interferometer_varphi_normal(n_wires=2),
                 'theta': interferometer_theta_normal(n_wires=2), 'wires': range(2)}),
               (Interferometer,
                {'phi': interferometer_phi_uniform(n_wires=3), 'varphi': interferometer_varphi_uniform(n_wires=3),
                 'theta': interferometer_theta_uniform(n_wires=3), 'wires': range(3)}),
               (Interferometer,
                {'phi': interferometer_phi_normal(n_wires=3), 'varphi': interferometer_varphi_normal(n_wires=3),
                 'theta': interferometer_theta_normal(n_wires=3), 'wires': range(3)})
               ]

    @pytest.mark.parametrize("template, dict", QUBIT_INIT)
    def test_integration_qubit_init(self, template, dict):
        """Checks parameter initialization compatible with qubit templates."""

        n_wires = len(dict['wires'])
        dev = qml.device('default.qubit', wires=n_wires)

        @qml.qnode(dev)
        def circuit():
            template(**dict)
            return qml.expval(qml.Identity(0))

        # Check that execution does not throw error
        circuit()

    @pytest.mark.parametrize("template, dict", CV_INIT)
    def test_integration_qubit_init(self, template, dict, gaussian_dummy):
        """Checks parameter initialization compatible with qubit templates."""

        n_wires = len(dict['wires'])
        dev = gaussian_dummy(n_wires)

        @qml.qnode(dev)
        def circuit():
            template(**dict)
            return qml.expval(qml.Identity(0))

        # Check that execution does not throw error
        circuit()