def graph(self): """The allowed circuit topology of the backend device as a directed acyclic graph. This property is optional; if arbitrary topologies are allowed by the device, this will simply return ``None``. Returns: networkx.DiGraph: a directed acyclic graph """ if self.circuit is None: return None # returned DAG has all parameters set to 0 bb = blackbird.loads(self.circuit) if bb.is_template(): params = bb.parameters kwargs = {p: 0 for p in params} # initialize the topology with all template # parameters set to zero topology = to_DiGraph(bb(**kwargs)) else: topology = to_DiGraph(bb) return topology
def test_args(self): """Test case where operations have arguments""" program = loads( dedent("""\ name prog version 0.0 Sgate(0.54) | 1 BSgate(0.432, phi=0.54) | [1, 2] """)) res = to_DiGraph(program) assert len(res) == 2 nodes = res.nodes().data() assert nodes[0]['name'] == 'Sgate' assert nodes[0]['modes'] == (1, ) assert nodes[0]['args'] == [0.54] assert nodes[0]['kwargs'] == {} assert nodes[1]['name'] == 'BSgate' assert nodes[1]['modes'] == (1, 2) assert nodes[1]['args'] == [0.432] assert nodes[1]['kwargs'] == {'phi': 0.54} assert list(res.edges()) == [(0, 1)]
def test_no_dependence(self): """Test case where operations do not depend on each other. This should result in a graph with no edges.""" program = loads( dedent("""\ name prog version 0.0 Sgate(0.43) | 0 Dgate(0.543) | 1 """)) res = to_DiGraph(program) assert len(res) == 2 nodes = res.nodes().data() assert len(res.edges()) == 0
def test_regrefs(self): """Test case where operations have regref arguments""" program = loads( dedent("""\ name prog version 0.0 Dgate(0.54) | 0 MeasureX | 0 Sgate(2*q0) | 1 """)) res = to_DiGraph(program) assert len(res) == 3 assert list(res.edges()) == [(0, 1), (1, 2)] nodes = res.nodes().data() assert isinstance(nodes[2]['args'][0], RegRefTransform)
def test_no_args(self): """Test case where operations have no arguments""" program = loads( dedent("""\ name prog version 0.0 Vac | 0 """)) res = to_DiGraph(program) assert len(res) == 1 nodes = res.nodes().data() assert nodes[0]['name'] == 'Vac' assert nodes[0]['modes'] == (0, ) assert nodes[0]['args'] == [] assert nodes[0]['kwargs'] == {}
def graph(self) -> Optional[nx.DiGraph]: """The allowed circuit topologies or connectivity of the class, modelled as a directed acyclic graph. This property is optional; if arbitrary topologies are allowed in the circuit class, this will simply return ``None``. Returns: networkx.DiGraph: a directed acyclic graph """ if self.circuit is None: return None if self._graph is not None: return self._graph # returned DAG has all parameters set to 0 bb = blackbird.loads(self.circuit) topology = to_DiGraph(bb) self._set_graph(topology) return topology