35
35
import socket
36
36
import warnings
37
37
38
- from cqc .settings import get_cqc_file , get_app_file
39
38
from cqc .cqcHeader import (
40
39
Header ,
41
40
CQCCmdHeader ,
90
89
CQC_TP_EXPIRE ,
91
90
)
92
91
from 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
94
102
95
103
96
104
def shouldReturn (command ):
@@ -171,16 +179,19 @@ def createXtraHeader(command, values):
171
179
class CQCConnection :
172
180
_appIDs = {}
173
181
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 ):
176
185
"""
177
186
Initialize a connection to the cqc server.
178
187
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
+
179
192
- **Arguments**
180
193
:param name: Name of the host.
181
194
: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.
184
195
:param appID: Application ID. If set to None, defaults to a nonused ID.
185
196
:param pend_messages: True if you want to wait with sending messages to the back end.
186
197
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=
190
201
How many seconds to wait between each connection retry
191
202
:param log_level: int or None
192
203
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
193
212
"""
194
213
self ._setup_logging (log_level )
195
214
@@ -233,19 +252,21 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
233
252
# Classical connections in the application network
234
253
self ._classicalConn = {}
235
254
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
246
268
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" )
249
270
250
271
# Host data
251
272
if self .name in self ._cqcNet .hostDict :
@@ -255,7 +276,7 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
255
276
256
277
# Get IP and port number
257
278
addr = myHost .addr
258
- else :
279
+ if socket_address is not None :
259
280
try :
260
281
hostname , port = socket_address
261
282
if not isinstance (hostname , str ):
@@ -287,20 +308,7 @@ def __init__(self, name, socket_address=None, cqcFile=None, appFile=None, appID=
287
308
self ._s .close ()
288
309
raise err
289
310
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
304
312
self .pend_messages = pend_messages
305
313
self .pending_messages = []
306
314
@@ -362,7 +370,8 @@ def startClassicalServer(self):
362
370
"""
363
371
if self ._appNet is None :
364
372
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."
366
375
)
367
376
368
377
if not self ._classicalServer :
@@ -412,7 +421,8 @@ def openClassicalChannel(self, name):
412
421
"""
413
422
if self ._appNet is None :
414
423
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."
416
426
)
417
427
if name not in self ._classicalConn :
418
428
logging .debug ("App {}: Opening classical channel to {}" .format (self .name , name ))
0 commit comments