Skip to content

Commit fbc0b9c

Browse files
committed
Refactor building and pending of headers
1 parent 30518a0 commit fbc0b9c

File tree

1 file changed

+31
-64
lines changed

1 file changed

+31
-64
lines changed

cqc/pythonLib.py

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,18 +1996,14 @@ def _single_qubit_gate(self, command, notify, block):
19961996

19971997
if self._cqc.pend_messages:
19981998

1999-
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2000-
if self._cqc._inside_cqc_mix:
2001-
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH)
2002-
2003-
# Build the header
2004-
header = CQCCmdHeader()
2005-
header.setVals(qubit_id=self._qID, instr=command, notify=notify, block=block)
2006-
# Pend the header
2007-
self._cqc._pend_header(header)
1999+
self._build_and_pend_command(command, notify, block)
20082000

20092001
# print info
2010-
logging.debug("App {} pends header: {}".format(self._cqc.name, header.printable()))
2002+
logging.debug(
2003+
"App {} pends message: 'Perform command {} to qubit with ID {}'".format(
2004+
self._cqc.name, command, self._qID
2005+
)
2006+
)
20112007

20122008
else:
20132009
# print info
@@ -2104,6 +2100,26 @@ def K(self, notify=True, block=True):
21042100
"""
21052101
self._single_qubit_gate(CQC_CMD_K, notify, block)
21062102

2103+
def _build_and_pend_command(self, command, notify=False, block=False, subheader: Header=None, *subheader_values):
2104+
2105+
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2106+
if self._cqc._inside_cqc_mix:
2107+
self._cqc._pend_type_header(
2108+
CQCType.COMMAND,
2109+
CQCCmdHeader.HDR_LENGTH + (subheader.HDR_LENGTH if subheader is not None else 0)
2110+
)
2111+
2112+
# Build and pend the command header
2113+
command_header = CQCCmdHeader()
2114+
command_header.setVals(self._qID, command, notify, block)
2115+
self._cqc._pend_header(command_header)
2116+
2117+
# Build and pend the subheader, if there is one
2118+
if subheader is not None:
2119+
subheader.setVals(*subheader_values)
2120+
self._cqc._pend_header(subheader)
2121+
2122+
21072123
def _single_gate_rotation(self, command, step, notify, block):
21082124
"""
21092125
Perform a rotation on a qubit
@@ -2118,21 +2134,8 @@ def _single_gate_rotation(self, command, step, notify, block):
21182134

21192135
if self._cqc.pend_messages:
21202136

2121-
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2122-
if self._cqc._inside_cqc_mix:
2123-
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH + CQCRotationHeader.HDR_LENGTH)
2124-
2125-
# Build command header and rotation sub header
2126-
command_header = CQCCmdHeader()
2127-
command_header.setVals(self._qID, command, notify, block)
2128-
2129-
rot_sub_header = CQCRotationHeader()
2130-
rot_sub_header.setVals(step)
2131-
2132-
# Pend headers
2133-
self._cqc._pend_header(command_header)
2134-
self._cqc._pend_header(rot_sub_header)
2135-
2137+
self._build_and_pend_command(command, notify, block, CQCRotationHeader(), step)
2138+
21362139
# print info
21372140
logging.debug(
21382141
"App {} pends message: 'Perform rotation command {} (angle {}*2pi/256) to qubit with ID {}'".format(
@@ -2210,20 +2213,7 @@ def _two_qubit_gate(self, command, target, notify, block):
22102213

22112214
if self._cqc.pend_messages:
22122215

2213-
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2214-
if self._cqc._inside_cqc_mix:
2215-
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH + CQCXtraQubitHeader.HDR_LENGTH)
2216-
2217-
# Build command header and extra qubit sub header
2218-
command_header = CQCCmdHeader()
2219-
command_header.setVals(self._qID, command, notify, block)
2220-
2221-
extra_qubit_sub_header = CQCXtraQubitHeader()
2222-
extra_qubit_sub_header.setVals(target._qID)
2223-
2224-
# Pend headers
2225-
self._cqc._pend_header(command_header)
2226-
self._cqc._pend_header(extra_qubit_sub_header)
2216+
self._build_and_pend_command(command, notify, block, CQCXtraQubitHeader(), target._qID)
22272217

22282218
# print info
22292219
logging.debug(
@@ -2296,24 +2286,10 @@ def measure(self, inplace=False, block=True):
22962286

22972287
if self._cqc.pend_messages:
22982288

2299-
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2300-
if self._cqc._inside_cqc_mix:
2301-
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH + CQCAssignHeader.HDR_LENGTH)
2302-
23032289
# Create a CQC Variable that holds the reference id for the measurement outcome
23042290
cqc_variable = CQCVariable()
23052291

2306-
# Build header
2307-
header = CQCCmdHeader()
2308-
header.setVals(self._qID, command, block=block)
2309-
2310-
# Bild Assign sub header
2311-
assign_sub_header = CQCAssignHeader()
2312-
assign_sub_header.setVals(cqc_variable.ref_id)
2313-
2314-
# Pend headers
2315-
self._cqc._pend_header(header)
2316-
self._cqc._pend_header(assign_sub_header)
2292+
self._build_and_pend_command(command, False, block, CQCAssignHeader(), cqc_variable.ref_id)
23172293

23182294
# print info
23192295
logging.debug("App {} pends message: 'Measure qubit with ID {}'".format(self._cqc.name, self._qID))
@@ -2351,16 +2327,7 @@ def reset(self, notify=True, block=True):
23512327

23522328
if self._cqc.pend_messages:
23532329

2354-
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2355-
if self._cqc._inside_cqc_mix:
2356-
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH)
2357-
2358-
# Build header
2359-
header = CQCCmdHeader()
2360-
header.setVals(self._qID, CQC_CMD_RESET, notify, block)
2361-
2362-
# Pend header
2363-
self._cqc._pend_header(header)
2330+
self._build_and_pend_command(CQC_CMD_RESET, notify, block)
23642331

23652332
# print info
23662333
logging.debug("App {} pends message: 'Reset qubit with ID {}'".format(self._cqc.name, self._qID))

0 commit comments

Comments
 (0)