Skip to content

Commit 5206a10

Browse files
committed
A) bugfixes, B) compatible with Python V <3.7, C) renamed CQCIfHeader
1 parent 246a7c2 commit 5206a10

File tree

4 files changed

+69
-78
lines changed

4 files changed

+69
-78
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
/cqc/settings.ini
99

1010
.idea/*
11+
12+
.vscode/

cqc/MessageHandler.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
CQCType,
6868
CQCTypeHeader,
6969
CQCAssignHeader,
70-
CQCIFHeader,
70+
CQCIfHeader,
7171
CQCLogicalOperator
7272
)
7373
from twisted.internet.defer import DeferredLock, inlineCallbacks
@@ -314,7 +314,6 @@ def _process_command(self, cqc_header, length, data, is_locked=False):
314314
logging.debug("CQC %s: Read XTRA Header: %s", self.name, xtra.printable())
315315

316316
# Run this command
317-
print(cmd.printable())
318317
logging.debug("CQC %s: Executing command: %s", self.name, cmd.printable())
319318
if cmd.instr not in self.commandHandlers:
320319
logging.debug("CQC {}: Unknown command {}".format(self.name, cmd.instr))
@@ -436,12 +435,12 @@ def handle_conditional(self, header: CQCHeader, data: bytes):
436435
Handler for messages of TP_IF.
437436
"""
438437
# Strategy for handling TP_IF:
439-
# We extract the CQCIFHeader from the data. We then extract all necessary variables from the header.
438+
# We extract the CQCIfHeader from the data. We then extract all necessary variables from the header.
440439
# We then evaluate the conditional. If the conditional evaluates to FALSE, then we return the bodylength of
441440
# the IF. The mix handler will then skip this bodylength.
442441
# If the conditional evaluates to True, then we return 0.
443442

444-
if_header = CQCIFHeader(data[:CQCIFHeader.HDR_LENGTH])
443+
if_header = CQCIfHeader(data[:CQCIfHeader.HDR_LENGTH])
445444

446445
try:
447446
first_operand_value = self.references[header.app_id][if_header.first_operand]
@@ -450,7 +449,7 @@ def handle_conditional(self, header: CQCHeader, data: bytes):
450449
self.create_return_message(header.app_id, CQC_ERR_GENERAL, cqc_version=header.version)
451450
)
452451

453-
if if_header.type_of_second_operand is CQCIFHeader.TYPE_VALUE:
452+
if if_header.type_of_second_operand is CQCIfHeader.TYPE_VALUE:
454453
second_operand_value = if_header.second_operand
455454
else:
456455
try:

cqc/cqcHeader.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2828
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
31-
# This import allows to use type hints for classes that have not been defined yet.
32-
# See https://stackoverflow.com/questions/33533148/
33-
# This import must be the very first import in this file, otherwise an error is raised
34-
from __future__ import annotations
35-
3630
import warnings
3731

3832
import struct
@@ -150,15 +144,15 @@ class CQCLogicalOperator(IntEnum):
150144
NEQ = 1 # Not equal
151145

152146
@staticmethod
153-
def opposite_of(operator: CQCLogicalOperator):
147+
def opposite_of(operator: 'CQCLogicalOperator'):
154148
opposites = {
155149
CQCLogicalOperator.EQ: CQCLogicalOperator.NEQ,
156150
CQCLogicalOperator.NEQ: CQCLogicalOperator.EQ
157151
}
158152
return opposites[operator]
159153

160154
@staticmethod
161-
def is_true(first_operand: int, operator: CQCLogicalOperator, second_operand: int):
155+
def is_true(first_operand: int, operator: 'CQCLogicalOperator', second_operand: int):
162156
comparison_method = {
163157
CQCLogicalOperator.EQ: first_operand.__eq__,
164158
CQCLogicalOperator.NEQ: first_operand.__ne__
@@ -380,7 +374,7 @@ def make_equivalent_CQCHeader(self, version: int, app_id: int) -> CQCHeader:
380374
return cqc_header
381375

382376

383-
class CQCIFHeader(Header):
377+
class CQCIfHeader(Header):
384378
"""
385379
Definition of the CQC IF header.
386380
"""

cqc/pythonLib.py

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2828
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
31-
# This import allows to use type hints for classes that have not been defined yet.
32-
# See https://stackoverflow.com/questions/33533148/
33-
# This import must be the very first import in this file, otherwise an error is raised
34-
from __future__ import annotations
35-
3630
import math
3731
import os
3832
import sys
@@ -94,7 +88,7 @@
9488
CQC_TP_NEW_OK,
9589
CQC_TP_EXPIRE,
9690
CQCLogicalOperator,
97-
CQCIFHeader,
91+
CQCIfHeader,
9892
CQCTypeHeader,
9993
CQCType,
10094
CQCAssignHeader
@@ -169,8 +163,10 @@ def get_remote_from_directory_or_address(cqcNet, name, remote_socket):
169163
return remote_ip, remote_port
170164

171165

172-
# !! Deprecated. Do not use this method
166+
# Deprecated. Do not use this method
173167
def createXtraHeader(command, values):
168+
warnings.warn("Method 'createXtraHeader' is deprecated.", DeprecationWarning)
169+
174170
if command == CQC_CMD_SEND or command == CQC_CMD_EPR:
175171
header = CQCCommunicationHeader()
176172
header.setVals(remote_app_id=values[0], remote_node=values[1], remote_port=values[2])
@@ -585,7 +581,7 @@ def sendCmdXtra(
585581
elif command == CQC_CMD_ROT_X or command == CQC_CMD_ROT_Y or command == CQC_CMD_ROT_Z:
586582
xtra_hdr = CQCRotationHeader()
587583
xtra_hdr.setVals(step)
588-
elif command == CQC_CMD_MEASURE:
584+
elif command == CQC_CMD_MEASURE or CQC_CMD_MEASURE_INPLACE:
589585
xtra_hdr = CQCAssignHeader()
590586
xtra_hdr.setVals(ref_id)
591587

@@ -1046,6 +1042,9 @@ def sendQubit(self, q, name, remote_appID=0, remote_socket=None, notify=True, bl
10461042
self._pend_header(command_header)
10471043
self._pend_header(comm_sub_header)
10481044

1045+
# Deactivate qubit
1046+
q._set_active(False)
1047+
10491048
# print info
10501049
logging.debug(
10511050
"App {} pends message: 'Send qubit with ID {} to {} and appID {}'".format(
@@ -1071,8 +1070,8 @@ def sendQubit(self, q, name, remote_appID=0, remote_socket=None, notify=True, bl
10711070
if notify:
10721071
message = self.readMessage()
10731072
self.print_CQC_msg(message)
1074-
1075-
# Deactivate qubit
1073+
1074+
# Deactivate qubit
10761075
q._set_active(False)
10771076

10781077
def recvQubit(self, notify=True, block=True):
@@ -1088,21 +1087,17 @@ def recvQubit(self, notify=True, block=True):
10881087
:block: Do we want the qubit to be blocked
10891088
"""
10901089

1091-
# initialize the qubit
1092-
q = qubit(self, createNew=False)
1093-
10941090
if self.pend_messages:
10951091
# print info
10961092
logging.debug("App {} pends message: 'Receive qubit'".format(self.name))
10971093

10981094
# Build header
10991095
header = CQCCmdHeader()
1100-
header.setVals(q._qID, CQC_CMD_RECV, notify, block)
1096+
header.setVals(0, CQC_CMD_RECV, notify, block)
11011097

11021098
# Pend header
11031099
self._pend_header(header)
11041100

1105-
return q
11061101
else:
11071102
# print info
11081103
logging.debug("App {} tells CQC: 'Receive qubit'".format(self.name))
@@ -1119,7 +1114,8 @@ def recvQubit(self, notify=True, block=True):
11191114
message = self.readMessage()
11201115
self.print_CQC_msg(message)
11211116

1122-
# initialize the qubit
1117+
# initialize the qubit
1118+
q = qubit(self, createNew=False)
11231119
q._qID = q_id
11241120

11251121
# Activate and return qubit
@@ -1141,14 +1137,11 @@ def createEPR(self, name, remote_appID=0, remote_socket=None, notify=True, block
11411137

11421138
remote_ip, remote_port = get_remote_from_directory_or_address(self._cqcNet, name, remote_socket)
11431139

1144-
# initialize the qubit
1145-
q = qubit(self, createNew=False)
1146-
11471140
if self.pend_messages:
11481141

11491142
# Build command header and communication sub header
11501143
command_header = CQCCmdHeader()
1151-
command_header.setVals(q._qID, CQC_CMD_EPR, notify, block)
1144+
command_header.setVals(0, CQC_CMD_EPR, notify, block)
11521145

11531146
comm_sub_header = CQCCommunicationHeader()
11541147
comm_sub_header.setVals(remote_appID, remote_ip, remote_port)
@@ -1161,7 +1154,7 @@ def createEPR(self, name, remote_appID=0, remote_socket=None, notify=True, block
11611154
logging.debug(
11621155
"App {} pends message: 'Create EPR-pair with {} and appID {}'".format(self.name, name, remote_appID)
11631156
)
1164-
return q
1157+
11651158
else:
11661159
# print info
11671160
logging.debug(
@@ -1189,6 +1182,9 @@ def createEPR(self, name, remote_appID=0, remote_socket=None, notify=True, block
11891182
message = self.readMessage()
11901183
self.print_CQC_msg(message)
11911184

1185+
# initialize the qubit
1186+
q = qubit(self, createNew=False)
1187+
11921188
q.set_entInfo(entInfoHdr)
11931189
q._qID = q_id
11941190
# Activate and return qubit
@@ -1205,20 +1201,18 @@ def recvEPR(self, notify=True, block=True):
12051201
:block: Do we want the qubit to be blocked
12061202
"""
12071203

1208-
# initialize the qubit
1209-
q = qubit(self, createNew=False)
12101204
if self.pend_messages:
12111205

12121206
# Build header
12131207
header = CQCCmdHeader()
1214-
header.setVals(q._qID, CQC_CMD_EPR_RECV, notify, block)
1208+
header.setVals(0, CQC_CMD_EPR_RECV, notify, block)
12151209

12161210
# Pend header
12171211
self._pend_header(header)
12181212

12191213
# print info
12201214
logging.debug("App {} pends message: 'Receive half of EPR'".format(self.name))
1221-
return q
1215+
12221216
else:
12231217
# print info
12241218
logging.debug("App {} tells CQC: 'Receive half of EPR'".format(self.name))
@@ -1236,7 +1230,9 @@ def recvEPR(self, notify=True, block=True):
12361230
message = self.readMessage()
12371231
self.print_CQC_msg(message)
12381232

1239-
# initialize the qubit
1233+
# initialize the qubit
1234+
q = qubit(self, createNew=False)
1235+
12401236
q.set_entInfo(entInfoHdr)
12411237
q._qID = q_id
12421238

@@ -1257,6 +1253,21 @@ def set_pending(self, pend_messages):
12571253
self.flush()
12581254
self.pend_messages = pend_messages
12591255

1256+
def create_qubits(self, nb_of_qubits: int) -> List['qubit']:
1257+
"""
1258+
Creates (i.e. allocates) multiple qubits, and returns a list with qubit objects.
1259+
:nb_of_qubits: The amount of qubits to be created.
1260+
"""
1261+
# First, flush all pending headers
1262+
self.flush()
1263+
1264+
# Build and insert the new qubit Command header
1265+
cmd_header = CQCCmdHeader()
1266+
cmd_header.setVals(qubit_id=0, instr=CQC_CMD_NEW, notify=1, block=1)
1267+
self._pend_header(cmd_header)
1268+
1269+
return self.flush_factory(nb_of_qubits)
1270+
12601271
def flush(self, do_sequence=False):
12611272
"""
12621273
Flush all pending messages to the backend.
@@ -1505,22 +1516,22 @@ def __init__(
15051516
self.operator = operator
15061517
self.operand_two = operand_two
15071518

1508-
def get_negation(self) -> LogicalFunction:
1519+
def get_negation(self) -> 'LogicalFunction':
15091520
return LogicalFunction(self.operand_one, CQCLogicalOperator.opposite_of(self.operator), self.operand_two)
15101521

1511-
def get_CQCIFHeader(self) -> CQCIFHeader:
1522+
def get_CQCIfHeader(self) -> CQCIfHeader:
15121523
"""
15131524
Builds the If header corresponding to this logical function.
15141525
"""
15151526

15161527
if isinstance(self.operand_two, int):
1517-
type_of_operand_two = CQCIFHeader.TYPE_VALUE
1528+
type_of_operand_two = CQCIfHeader.TYPE_VALUE
15181529
operand_two = self.operand_two
15191530
else:
1520-
type_of_operand_two = CQCIFHeader.TYPE_REF_ID
1531+
type_of_operand_two = CQCIfHeader.TYPE_REF_ID
15211532
operand_two = self.operand_two._ref_id
15221533

1523-
header = CQCIFHeader()
1534+
header = CQCIfHeader()
15241535
header.setVals(
15251536
self.operand_one.ref_id,
15261537
self.operator,
@@ -1702,10 +1713,10 @@ def __init__(self, cqc_connection: CQCConnection, is_else: bool, logical_functio
17021713

17031714
def __enter__(self):
17041715
# Pend CQC Type header
1705-
self._conn._pend_type_header(CQCType.IF, CQCIFHeader.HDR_LENGTH)
1716+
self._conn._pend_type_header(CQCType.IF, CQCIfHeader.HDR_LENGTH)
17061717

17071718
# Build the IF header, and store it so we can modify its length at __exit__
1708-
self.header = self._logical_function.get_CQCIFHeader()
1719+
self.header = self._logical_function.get_CQCIfHeader()
17091720

17101721
# Pend the IF header
17111722
self._conn._pend_header(self.header)
@@ -1836,40 +1847,25 @@ def __init__(self, cqc, notify=True, block=True, createNew=True, q_id=None, entI
18361847
self.scope_of_deactivation = None
18371848

18381849
if createNew:
1839-
if cqc.pend_messages:
1840-
# Set q id, None by default
1841-
self._qID = q_id
1842-
self._set_active(False)
1843-
1844-
# Build header
1845-
header = CQCCmdHeader()
1846-
header.setVals(0, CQC_CMD_NEW, notify, block)
1847-
1848-
# Pend header
1849-
self._cqc._pend_header(header)
1850+
# print info
1851+
logging.debug("App {} tells CQC: 'Create qubit'".format(self._cqc.name))
18501852

1851-
# print info
1852-
logging.debug("App {} pends message:'Create qubit'".format(self._cqc.name))
1853-
else:
1854-
# print info
1855-
logging.debug("App {} tells CQC: 'Create qubit'".format(self._cqc.name))
1853+
# Create new qubit at the cqc server
1854+
self._cqc.sendCommand(0, CQC_CMD_NEW, notify=int(notify), block=int(block))
18561855

1857-
# Create new qubit at the cqc server
1858-
self._cqc.sendCommand(0, CQC_CMD_NEW, notify=int(notify), block=int(block))
1856+
# Get qubit id
1857+
message = self._cqc.readMessage()
1858+
try:
1859+
otherHdr = message[1]
1860+
self._qID = otherHdr.qubit_id
1861+
except AttributeError:
1862+
raise CQCGeneralError("Didn't receive the qubit id")
1863+
# Activate qubit
1864+
self._set_active(True)
18591865

1860-
# Get qubit id
1866+
if notify:
18611867
message = self._cqc.readMessage()
1862-
try:
1863-
otherHdr = message[1]
1864-
self._qID = otherHdr.qubit_id
1865-
except AttributeError:
1866-
raise CQCGeneralError("Didn't receive the qubit id")
1867-
# Activate qubit
1868-
self._set_active(True)
1869-
1870-
if notify:
1871-
message = self._cqc.readMessage()
1872-
self._cqc.print_CQC_msg(message)
1868+
self._cqc.print_CQC_msg(message)
18731869
else:
18741870
self._qID = q_id
18751871
self._set_active(False) # Why?

0 commit comments

Comments
 (0)