13
13
from qlinklayer .mhp import SimulatedNodeCentricMHPService
14
14
from easysquid .toolbox import logger
15
15
import random
16
+ import bitstring
16
17
17
18
18
19
class EGPRequest :
19
- def __init__ (self , otherID , num_pairs , min_fidelity , max_time , purpose_id = 0 , priority = None , store = True ,
20
- measure_directly = False ):
20
+
21
+ package_format = 'uint:32=other_ip, ' \
22
+ 'uint:16=other_port, ' \
23
+ 'uint:16=purpose_id, ' \
24
+ 'float:32=create_time, ' \
25
+ 'float:32=min_fidelity, ' \
26
+ 'float:32=max_time, ' \
27
+ 'uint:16=create_id, ' \
28
+ 'uint:8=num_pairs, ' \
29
+ 'uint:4=priority' , \
30
+ 'uint:1=store, ' \
31
+ 'uint:1=measure_directly'
32
+ HDR_LENGTH = 24
33
+
34
+ def __init__ (self , other_ip = 0 , other_port = 0 , num_pairs = 0 , min_fidelity = 0 , max_time = 0 ,
35
+ purpose_id = 0 , priority = 0 , store = True , measure_directly = False ):
21
36
"""
22
37
Stores required parameters of Entanglement Generation Protocol Request
23
- :param otherID: int
24
- ID of the other node we are attempting to generate entanglement with
38
+ :param other_ip: int
39
+ IP of the other node we are attempting to generate entanglement with
40
+ :param other_port: int
41
+ Port number of other node.
25
42
:param num_pairs: int
26
43
The number of entangled pairs we are trying to generate
27
44
:param min_fidelity: float
@@ -38,7 +55,9 @@ def __init__(self, otherID, num_pairs, min_fidelity, max_time, purpose_id=0, pri
38
55
:param measure_directly: bool
39
56
Specifies whether to measure the communication qubit directly after the photon is emitted
40
57
"""
41
- self .otherID = otherID
58
+ self .other_ip = other_ip
59
+ self .other_port = other_port
60
+ self .otherID = (self .other_ip , self .other_port )
42
61
self .num_pairs = num_pairs
43
62
self .min_fidelity = min_fidelity
44
63
self .max_time = max_time
@@ -57,9 +76,9 @@ def __copy__(self):
57
76
:return: obj `~qlinklayer.egp.EGPRequest`
58
77
A copy of the EGPRequest object
59
78
"""
60
- c = type (self )(otherID = self .otherID , num_pairs = self .num_pairs , min_fidelity = self .min_fidelity ,
61
- max_time = self .max_time , purpose_id = self .purpose_id , priority = self .priority , store = self . store ,
62
- measure_directly = self .measure_directly )
79
+ c = type (self )(other_ip = self .other_ip , other_port = self .other_port , num_pairs = self .num_pairs ,
80
+ min_fidelity = self .min_fidelity , max_time = self .max_time , purpose_id = self .purpose_id ,
81
+ priority = self . priority , store = self . store , measure_directly = self .measure_directly )
63
82
c .assign_create_id (self .create_id , self .create_time )
64
83
return c
65
84
@@ -75,6 +94,52 @@ def assign_create_id(self, create_id, create_time):
75
94
def get_create_info (self ):
76
95
return self .create_id , self .create_time
77
96
97
+ def pack (self ):
98
+ """
99
+ Pack the data in packet form.
100
+ :return: str
101
+ """
102
+ if self .create_time is None :
103
+ raise ValueError ("Cannot pack if create_time is None" )
104
+ if self .create_id is None :
105
+ raise ValueError ("Cannot pack if create_id is None" )
106
+ to_pack = {"other_ip" : self .other_ip ,
107
+ "other_port" : self .other_port ,
108
+ "purpose_id" : self .purpose_id ,
109
+ "create_time" : self .create_time ,
110
+ "min_fidelity" : self .min_fidelity ,
111
+ "max_time" : self .max_time ,
112
+ "create_id" : self .create_id ,
113
+ "num_pairs" : self .num_pairs ,
114
+ "priority" : self .priority ,
115
+ "store" : self .store ,
116
+ "measure_directly" : self .measure_directly }
117
+ request_Bitstring = bitstring .pack (self .package_format , ** to_pack )
118
+ requestH = request_Bitstring .tobytes ()
119
+
120
+ return requestH
121
+
122
+ def unpack (self , headerBytes ):
123
+ """
124
+ Unpack data.
125
+ :param headerBytes: str
126
+ :return:
127
+ """
128
+ request_Bitstring = bitstring .BitString (headerBytes )
129
+ request_fields = request_Bitstring .unpack (self .package_format )
130
+ self .other_ip = request_fields [0 ]
131
+ self .other_port = request_fields [1 ]
132
+ self .otherID = (self .other_ip , self .other_port )
133
+ self .purpose_id = request_fields [2 ]
134
+ self .create_time = request_fields [3 ]
135
+ self .min_fidelity = request_fields [4 ]
136
+ self .max_time = request_fields [5 ]
137
+ self .create_id = request_fields [6 ]
138
+ self .num_pairs = request_fields [7 ]
139
+ self .priority = request_fields [8 ]
140
+ self .store = bool (request_fields [9 ])
141
+ self .measure_directly = bool (request_fields [10 ])
142
+
78
143
79
144
class EGP (EasyProtocol ):
80
145
def __init__ (self , node , conn = None , err_callback = None , ok_callback = None ):
0 commit comments