def test_cvqnnlayers_uniform_dimensions(self, n_subsystems, n_layers): """Confirm that pennylane.init.cvqnn_layers_uniform() returns an array with the right dimensions.""" a = (n_layers, n_subsystems) b = (n_layers, n_subsystems * (n_subsystems - 1) // 2) p = cvqnn_layers_uniform(n_wires=n_subsystems, n_layers=n_layers, seed=0) dims = [p_.shape for p_ in p] assert dims == [b, b, a, a, a, b, b, a, a, a, a]
def test_cvqnnlayers_uniform_edgecase(self, seed, tol): """Test sampling edge case of pennylane.init.cvqnn_layers_uniform().""" p = cvqnn_layers_uniform(n_layers=2, n_wires=10, low=1, high=1, mean_active=1, std_active=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.)
def test_cvqnnlayers_uniform_interval(self, seed): """Confirm that no uniform sample in pennylane.init.cvqnn_layers_uniform() lies outside of interval.""" low = -2 high = 1 p = cvqnn_layers_uniform(n_layers=2, n_wires=10, low=low, high=high, seed=seed) p_uni = [p[i] for i in [0, 1, 2, 4, 5, 6, 7, 9]] assert all([(p_ <= high).all() and (p_ >= low).all() for p_ in p_uni])
def test_cvqnnlayers_uniform_seed(self, seed, tol): """Confirm that pennylane.init.cvqnn_layers_uniform() invokes the correct np.random sampling function for a given seed.""" low = -2 high = 1 mean_a = 0.5 std_a = 2 n_wires = 3 n_layers = 2 n_if = n_wires * (n_wires - 1) // 2 p = cvqnn_layers_uniform(n_layers=n_layers, n_wires=n_wires, low=low, high=high, mean_active=mean_a, std_active=std_a, seed=seed) np.random.seed(seed) theta_1 = np.random.uniform(low=low, high=high, size=(n_layers, n_if)) phi_1 = np.random.uniform(low=low, high=high, size=(n_layers, n_if)) varphi_1 = np.random.uniform(low=low, high=high, size=(n_layers, n_wires)) r = np.random.normal(loc=mean_a, scale=std_a, size=(n_layers, n_wires)) phi_r = np.random.uniform(low=low, high=high, size=(n_layers, n_wires)) theta_2 = np.random.uniform(low=low, high=high, size=(n_layers, n_if)) phi_2 = np.random.uniform(low=low, high=high, size=(n_layers, n_if)) varphi_2 = np.random.uniform(low=low, high=high, size=(n_layers, n_wires)) a = np.random.normal(loc=mean_a, scale=std_a, size=(n_layers, n_wires)) phi_a = np.random.uniform(low=low, high=high, size=(n_layers, n_wires)) k = np.random.normal(loc=mean_a, scale=std_a, size=(n_layers, n_wires)) p_target = [ theta_1, phi_1, varphi_1, r, phi_r, theta_2, phi_2, varphi_2, a, phi_a, k ] assert np.allclose(p, p_target, atol=tol, rtol=0.)