def test_insufficient_samples(self): """Test if function returns ``ValueError`` when user specifies a number of samples less than 1 """ with pytest.raises( ValueError, match="Number of samples must be greater than zero"): sample.uniform_sampler(modes=2, sampled_modes=2, samples=0)
def test_output_dimensions(self): """Tests if function returns list of correct dimensions""" samples = np.array( sample.uniform_sampler(modes=2, sampled_modes=1, samples=3)) dims = samples.shape assert len(dims) == 2 assert dims[0] == 3 assert dims[1] == 2
def test_random_seed(dim, adj): """Test for the function ``strawberryfields.gbs.sample.random_seed``. Checks that samples are identical after repeated initialization of ``random_seed``.""" sample.random_seed(1968) q_s_1 = sample.quantum_sampler(A=adj, n_mean=2, samples=10, backend_options={"threshold": False}) u_s_1 = sample.uniform_sampler(modes=dim, sampled_modes=2, samples=10) sample.random_seed(1968) q_s_2 = sample.quantum_sampler(A=adj, n_mean=2, samples=10, backend_options={"threshold": False}) u_s_2 = sample.uniform_sampler(modes=dim, sampled_modes=2, samples=10) assert np.array_equal(q_s_1, q_s_2) assert np.array_equal(u_s_1, u_s_2)
def test_output_sampled_modes(self): """Tests if samples from function are only zeros and ones and have a number of ones equal to ``sampled_modes``""" samples = np.array( sample.uniform_sampler(modes=2, sampled_modes=1, samples=3)) only_zero_one = True for samp in samples: if not np.allclose(np.unique(samp), [0, 1]): only_zero_one = False assert only_zero_one assert np.unique(np.sum(samples, axis=1)) == 1
def test_sampled_modes_exceeds_modes(self): """Test if function returns ``ValueError`` when user specifies a number of ``sampled_modes`` that exceeds ``modes`` """ with pytest.raises(ValueError, match="Modes and sampled_modes"): sample.uniform_sampler(modes=2, sampled_modes=3, samples=2)
def test_insufficient_sampled_modes(self): """Tests if function returns ``ValueError`` when user specifies a number of ``sampled_modes`` less than 1 """ with pytest.raises(ValueError, match="Modes and sampled_modes"): sample.uniform_sampler(modes=2, sampled_modes=0, samples=2)
def sample_subgraphs( graph: nx.Graph, nodes: int, samples: int = 1, sample_options: Optional[dict] = None, backend_options: Optional[dict] = None, ) -> list: """Functionality for sampling subgraphs. The optional ``sample_options`` argument can be used to specify the type of sampling. It should be a dict that contains any of the following: .. glossary:: key: ``"distribution"``, value: *str* Subgraphs can be sampled according to the following distributions: - ``"gbs"``: for generating subgraphs according to the Gaussian boson sampling distribution. In this distribution, subgraphs are sampled with a variable size ( default). - ``"uniform"``: for generating subgraphs uniformly at random. When using this distribution, subgraphs are of a fixed size. Note that ``backend_options`` are not used and that remote sampling is unavailable due to the simplicity of the distribution. key: ``"postselect_ratio"``, value: *float* Ratio of ``nodes`` used to determine the minimum size of subgraph sampled; defaults to 0.75. Args: graph (nx.Graph): the input graph nodes (int): the mean size of subgraph samples samples (int): number of samples; defaults to 1 sample_options (dict[str, Any]): dictionary specifying options used by :func:`sample_subgraphs`; defaults to :const:`SAMPLE_DEFAULTS` backend_options (dict[str, Any]): dictionary specifying options used by backends during sampling; defaults to ``None`` Returns: list[list[int]]: a list of length ``samples`` whose elements are subgraphs given by a list of nodes """ sample_options = {**SAMPLE_DEFAULTS, **(sample_options or {})} distribution = sample_options["distribution"] if distribution == "uniform": s = sample.uniform_sampler(modes=graph.order(), sampled_modes=nodes, samples=samples) elif distribution == "gbs": postselect = int(sample_options["postselect_ratio"] * nodes) backend_options = {**(backend_options or {}), "postselect": postselect} s = sample.quantum_sampler( A=nx.to_numpy_array(graph), n_mean=nodes, samples=samples, backend_options=backend_options, ) else: raise ValueError("Invalid distribution selected") return to_subgraphs(graph, s)