def get(network_name=None): """Get the current nodes of the network.""" networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) try: nodes = networks_config.get_node_names(network_name=network_name) except ValueError: print("No network {}".format(network_name)) else: print(("{} " * len(nodes))[:-1].format(*nodes))
def __init__(self, host, name, cqc_net, backend, network_name="default"): """ Initialize CQC Factory. lhost details of the local host (class host) """ self.host = host self.name = name self.cqcNet = cqc_net self.virtRoot = None self.qReg = None self.backend = backend(self) self.network_name = network_name # Dictionary that keeps qubit dictorionaries for each application self.qubitList = {} # Lock governing access to the qubitList self._lock = DeferredLock() # Read in topology, if specified. topology=None means fully connected # topology self.topology = None if simulaqron_settings.topology_file is not None and simulaqron_settings.topology_file != "": self._setup_topology(simulaqron_settings.topology_file) else: if simulaqron_settings.network_config_file is not None: networks_config = NetworksConfigConstructor( file_path=simulaqron_settings.network_config_file) self.topology = networks_config.networks[network_name].topology
def __init__(self, host, name, qnodeos_net, backend, network_name="default"): """ Initialize NetQASM Factory. lhost details of the local host (class host) """ self.host = host self.name = name self.qnodeos_net = qnodeos_net self.virtRoot = None self.qReg = None self.backend = backend(self) self.network_name = network_name # Dictionary that keeps qubit dictorionaries for each application self.qubitList = {} # Lock governing access to the qubitList self._lock = DeferredLock() self._logger = get_netqasm_logger(f"{self.__class__.__name__}({name})") # Read in topology, if specified. topology=None means fully connected # topology self.topology = None if simulaqron_settings.network_config_file is not None: networks_config = NetworksConfigConstructor(file_path=simulaqron_settings.network_config_file) self.topology = networks_config.networks[network_name].topology
def tearDownClass(cls): default_network_config_file = simulaqron_settings._default_config[ "network_config_file"] network_config = NetworksConfigConstructor(default_network_config_file) network_config.reset() network_config.write_to_file() simulaqron_settings.default_settings()
def read_config(self, filename, network_name="default", config_type="vnode"): """ Reads the configuration file in which each line has the form: node name, hostname, port number. For example: Alice, localhost, 8888 """ with open(filename) as confFile: if filename.endswith(".json"): if config_type not in ["vnode", "cqc", "app"]: raise ValueError( "Type needs to be either 'vnode', 'cqc' or 'app'") if network_name is None: network_name = "default" network_config = NetworksConfigConstructor( file_path=filename).networks[network_name] nodes = network_config.nodes for node_name, node_config in nodes.items(): hostname = getattr(node_config, "{}_hostname".format(config_type)) port = getattr(node_config, "{}_port".format(config_type)) self.hostDict[node_name] = host(node_name, hostname, port) elif filename.endswith(".cfg"): for line in confFile: if not line.startswith("#"): words = line.split(",") # We will simply ignore lines which are not of the right form if len(words) == 3: newHost = host(words[0].strip(), words[1].strip(), words[2].strip()) self.hostDict[words[0]] = newHost else: raise ValueError("Unknown file type {}".format( filename.split(".")[-1]))
def remove(name, network_name=None, force=False): """ Remove a node to the network. NAME: The name of the node, e.g. Alice """ if not force: answer = input( "Do you want to remove the node {} to the network {} in the file {}? (yes/no)." .format(name, network_name, simulaqron_settings.network_config_file)) if not _is_positive_answer(answer): print("Aborting!") return networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) networks_config.remove_node(node_name=name, network_name=network_name) networks_config.write_to_file()
def default(network_name=None, force=False): """ Sets the default nodes of the network. The default network consists of the five nodes: Alice, Bob, Charlie, David, Eve """ if not force: answer = input( "Do you want to set the network {} in the file {} to default," "i.e. with nodes Alice, Bob, Charlie, David and Eve? (yes/no).". format(network_name, simulaqron_settings.network_config_file)) if not _is_positive_answer(answer): print("Aborting!") return networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) node_names = ["Alice", "Bob", "Charlie", "David", "Eve"] networks_config.add_network(node_names=node_names, network_name=network_name) networks_config.write_to_file()
def add(name, network_name=None, hostname=None, app_port=None, cqc_port=None, vnode_port=None, neighbors=None, force=False): """ Add a node to the network. NAME: The name of the node, e.g. Alice HOSTNAME: The host name of the node, e.g. localhost or 192.168.0.1 """ if not force: answer = input( "Do you want to add the node {} to the network {} in the file {}? (yes/no)." .format(name, network_name, simulaqron_settings.network_config_file)) if not _is_positive_answer(answer): print("Aborting!") return if neighbors is not None: neighbors = neighbors.split(',') neighbors = [neighbor.strip() for neighbor in neighbors] networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) networks_config.add_node(node_name=name, network_name=network_name, app_hostname=hostname, cqc_hostname=hostname, vnode_hostname=hostname, app_port=app_port, cqc_port=cqc_port, vnode_port=vnode_port, neighbors=neighbors) networks_config.write_to_file()
import simulaqron from simulaqron.network import Network from simulaqron.settings import simulaqron_settings from simulaqron.toolbox.manage_nodes import NetworksConfigConstructor from simulaqron.toolbox.reset import main as reset_simulaqron CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) PID_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".simulaqron_pids") # Check that the default network_config_file exists default_network_config_file = simulaqron_settings._default_config[ "network_config_file"] if not os.path.exists(default_network_config_file): networks_config = NetworksConfigConstructor() networks_config.reset() networks_config.write_to_file(default_network_config_file) class SimulaQronDaemon(run.RunDaemon): def __init__(self, pidfile, name=None, nrnodes=None, nodes=None, topology=None, new=True): super().__init__(pidfile=pidfile) self.name = name self.nrnodes = nrnodes
def __init__(self, name=None, nodes=None, topology=None, network_config_file=None, force=False, new=True): """ Used to spin up a simulated network. If new=True then a fresh network with only the specified nodes (or the default Alice, Bob, Charlie, David and Eve) are created and overwriting the current network with the same name in the network config file. Otherwise only the specified nodes are started without changing the config file. Note that if the nodes does not currently exists and new=False, an ValueError is raised. If force=False an input to confirm the overwriting is issued. :param name: None or str (defualts to "default") :param nodes: None or list of str :param topology: None or dict :param network_config_file: None or str (defaults to simulaqron_settings.network_config_file :param force: bool :param new: bool """ self._running = False if name is None: self.name = "default" else: self.name = name if network_config_file is None: self._network_config_file = simulaqron_settings.network_config_file else: self._network_config_file = network_config_file networks_config = NetworksConfigConstructor( file_path=self._network_config_file) if new: if nodes is None: if isinstance(topology, dict): self.nodes = list(topology.keys()) else: self.nodes = ["Alice", "Bob", "Charlie", "David", "Eve"] else: self.nodes = nodes self.topology = construct_topology_config(topology, self.nodes) if not force: answer = input( "Do you want to add/replace the network {} in the file {}" "with a network constisting of the nodes {}? (yes/no)". format(self.name, self._network_config_file, self.nodes)) if answer not in ["yes", "y"]: raise RuntimeError( "User did not want to replace network in file") networks_config.add_network(node_names=self.nodes, network_name=self.name, topology=self.topology) networks_config.write_to_file(self._network_config_file) else: if topology is not None: raise ValueError("If new is False a topology cannot be used.") if self.name in networks_config.networks: node_names = networks_config.get_node_names(self.name) self.topology = networks_config.networks[self.name].topology else: raise ValueError( "Network {} is not in the file {}\n" "If you wish to add this network to the file, use the" "--new flag.".format(self.name, self._network_config_file)) if nodes is None: self.nodes = node_names else: self.nodes = nodes for node_name in self.nodes: if node_name not in node_names: raise ValueError( "Node {} is not in the current network {} in the file {}\n" "If you wish to overwrite the current network in the file, use the" "--new flag.".format(node_name, self.name, self._network_config_file)) self.processes = [] self._setup_processes()
def _create_default_network_config(): networks_config = NetworksConfigConstructor() networks_config.reset() networks_config.write_to_file(simulaqron_settings.network_config_file)
def main(): simulaqron_settings.default_settings() networks_config = NetworksConfigConstructor() networks_config.reset() networks_config.write_to_file(simulaqron_settings.network_config_file)