def to_subgraphs(graph: nx.Graph, samples: list) -> list: """Converts a list of samples to a list of subgraphs. Given a list of samples, with each sample of ``len(nodes)`` being a list of zeros and ones, this function returns a list of subgraphs selected by, for each sample, picking the nodes corresponding to ones and creating the induced subgraph. The subgraph is specified as a list of selected nodes. For example, given an input 6-node graph, a sample :math:`[0, 1, 1, 0, 0, 1]` is converted to a subgraph :math:`[1, 2, 5]`. Args: graph (nx.Graph): the input graph samples (list): a list of samples, each a binary sequence of ``len(nodes)`` Returns: list[list[int]]: a list of subgraphs, where each subgraph is represented by a list of its nodes """ graph_nodes = list(graph.nodes) node_number = len(graph_nodes) subgraph_samples = [sample.modes_from_counts(s) for s in samples] if graph_nodes != list(range(node_number)): return [sorted([graph_nodes[i] for i in s]) for s in subgraph_samples] return subgraph_samples
def test_modes_from_counts(): """Test if the function ``strawberryfields.gbs.sample.modes_from_counts`` returns the correct mode samples when input a set of photon count samples.""" counts = [[0, 0, 0, 0], [1.0, 0.0, 0.0, 2.0], [1, 1, 1, 0], [1, 2, 1, 0], [0, 1, 0, 2, 4]] modes = [[], [0, 3, 3], [0, 1, 2], [0, 1, 1, 2], [1, 3, 3, 4, 4, 4, 4]] assert [sample.modes_from_counts(s) for s in counts] == modes