3535import socket
3636import warnings
3737
38- from cqc .settings import get_cqc_file , get_app_file
3938from cqc .cqcHeader import (
4039 Header ,
4140 CQCCmdHeader ,
9089 CQC_TP_EXPIRE ,
9190)
9291from cqc .entInfoHeader import EntInfoHeader
93- from cqc .hostConfig import cqc_node_id_from_addrinfo , networkConfig
92+ from cqc .hostConfig import cqc_node_id_from_addrinfo
93+
94+ try :
95+ import simulaqron
96+ from simulaqron .general .hostConfig import socketsConfig
97+ from simulaqron .settings import simulaqron_settings
98+ _simulaqron_version = simulaqron .__version__
99+ _simulaqron_major = int (_simulaqron_version .split ('.' )[0 ])
100+ except ModuleNotFoundError :
101+ _simulaqron_major = - 1
94102
95103
96104def shouldReturn (command ):
@@ -171,16 +179,19 @@ def createXtraHeader(command, values):
171179class CQCConnection :
172180 _appIDs = {}
173181
174- def __init__ (self , name , socket_address = None , cqcFile = None , appFile = None , appID = None , pend_messages = False ,
175- retry_connection = True , conn_retry_time = 0.1 , log_level = None ):
182+ def __init__ (self , name , socket_address = None , appID = None , pend_messages = False ,
183+ retry_connection = True , conn_retry_time = 0.1 , log_level = None , backend = None ,
184+ use_classical_communication = True , network_name = None ):
176185 """
177186 Initialize a connection to the cqc server.
178187
188+ Since version 3.0.0: If socket_address is None or use_classical_communication is True, the CQC connection
189+ needs some way of finding the correct socket addresses. If backend is None or "simulaqron" the connection
190+ will try to make use of the network config file setup in simulaqron. If simulaqron is not installed
191+
179192 - **Arguments**
180193 :param name: Name of the host.
181194 :param socket_address: tuple (str, int) of ip and port number.
182- :param cqcFile: Path to cqcFile. If None, 'Setting.CONF_CQC_FILE' is used, unless socket_address
183- :param appFile: Path to appFile. If None, 'Setting.CONF_APP_FILE' is used.
184195 :param appID: Application ID. If set to None, defaults to a nonused ID.
185196 :param pend_messages: True if you want to wait with sending messages to the back end.
186197 Use flush() to send all pending messages in one go as a sequence to the server
@@ -190,6 +201,14 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
190201 How many seconds to wait between each connection retry
191202 :param log_level: int or None
192203 The log-level, for example logging.DEBUG (default: logging.WARNING)
204+ :param backend: None or str
205+ If socket_address is None or use_classical_communication is True, If None or "simulaqron" is used
206+ the cqc library tries to use the network config file setup in simulaqron if network_config_file is None.
207+ If network_config_file is None and simulaqron is not installed a ValueError is raised.
208+ :param use_classical_communication: bool
209+ Whether to use the built-in classical communication or not.
210+ :param network_name: None or str
211+ Used if simulaqron is used to load socket addresses for the backend
193212 """
194213 self ._setup_logging (log_level )
195214
@@ -233,19 +252,21 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
233252 # Classical connections in the application network
234253 self ._classicalConn = {}
235254
236- if socket_address is None :
237- # This file defines the network of CQC servers interfacing to virtual quantum nodes
238- if cqcFile is None :
239- self .cqcFile = get_cqc_file ()
240- else :
241- self .cqcFile = cqcFile
242-
243- # Read configuration files for the cqc network
244- if os .path .exists (self .cqcFile ):
245- self ._cqcNet = networkConfig (self .cqcFile )
255+ if socket_address is None or use_classical_communication :
256+ if backend is None or backend == "simulaqron" :
257+ if _simulaqron_major < 3 :
258+ raise ValueError ("If (socket_address is None or use_classical_communication is True)"
259+ "and (backend is None or 'simulaqron'\n "
260+ "you need simulaqron>=3.0.0 installed." )
261+ else :
262+ network_config_file = simulaqron_settings .network_config_file
263+ self ._cqcNet = socketsConfig (network_config_file , network_name = network_name , config_type = "cqc" )
264+ if use_classical_communication :
265+ self ._appNet = socketsConfig (network_config_file , network_name = network_name , config_type = "app" )
266+ else :
267+ self ._appNet = None
246268 else :
247- raise ValueError ("There was no path specified for the cqc file containing addresses"
248- "and port numbers to the cqc nodes in the backend." )
269+ raise ValueError ("Unknown backend" )
249270
250271 # Host data
251272 if self .name in self ._cqcNet .hostDict :
@@ -255,7 +276,7 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
255276
256277 # Get IP and port number
257278 addr = myHost .addr
258- else :
279+ if socket_address is not None :
259280 try :
260281 hostname , port = socket_address
261282 if not isinstance (hostname , str ):
@@ -287,20 +308,7 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
287308 self ._s .close ()
288309 raise err
289310
290- # This file defines the application network
291- if appFile is None :
292- self .appFile = get_app_file ()
293- else :
294- self .appFile = appFile
295-
296- # Read configuration files for the application network
297- if os .path .exists (self .appFile ):
298- self ._appNet = networkConfig (self .appFile )
299- else :
300- logging .warning ("Since there is no appFile was found the built-in classical commmunication cannot be used." )
301- self ._appNet = None
302-
303- # List of pending messages waiting to be send to the back-end
311+ # List of pending messages waiting to be send to the back-end
304312 self .pend_messages = pend_messages
305313 self .pending_messages = []
306314
@@ -362,7 +370,8 @@ def startClassicalServer(self):
362370 """
363371 if self ._appNet is None :
364372 raise ValueError (
365- "Since there is no appFile was found the built-in classical commmunication cannot be used."
373+ "Since use_classical_communication was set to False upon init, the built-in classical communication"
374+ "cannot be used."
366375 )
367376
368377 if not self ._classicalServer :
@@ -412,7 +421,8 @@ def openClassicalChannel(self, name):
412421 """
413422 if self ._appNet is None :
414423 raise ValueError (
415- "Since there is no appFile was found the built-in classical commmunication cannot be used."
424+ "Since use_classical_communication was set to False upon init, the built-in classical communication"
425+ "cannot be used."
416426 )
417427 if name not in self ._classicalConn :
418428 logging .debug ("App {}: Opening classical channel to {}" .format (self .name , name ))
0 commit comments