Skip to content

Commit f12fcca

Browse files
committed
Write comments and give CQCMix support to all qubit commands
1 parent 82d94c0 commit f12fcca

File tree

1 file changed

+85
-7
lines changed

1 file changed

+85
-7
lines changed

cqc/pythonLib.py

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,17 +1334,11 @@ def send_pending_headers(self) -> List[Any]:
13341334
After sending, self._pending_headers is emptied.
13351335
"""
13361336

1337-
print('========================================================')
13381337
# Send all pending headers
13391338
for header in self._pending_headers:
13401339
self._s.send(header.pack())
1341-
print('--------------------------------')
1342-
print("SENT: " + header.printable())
13431340
logging.debug("App {} sends CQC: {}".format(self.name, header.printable()))
13441341

1345-
1346-
print('========================================================')
1347-
13481342
# Reset _pending_headers to an empty list after all headers are sent
13491343
self._pending_headers = []
13501344

@@ -1461,15 +1455,26 @@ def test_preparation(self, preparation, exp_values, conf=2, iterations=100, prog
14611455

14621456

14631457
class CQCVariable:
1458+
"""
1459+
Instances of this class are returned by measure command, if executed inside a CQCMix context.
1460+
A CQCVariable holds a reference ID with which one can refer to the outcome of the measurement.
1461+
"""
14641462
_next_ref_id = 0
14651463

14661464
def __init__(self):
1465+
"""
1466+
Increments the reference ID, and assigns the new unique reference ID to this CQCVariable.
1467+
This system ensures no two CQCVariable instances have the same reference ID.
1468+
"""
14671469
self._ref_id = CQCVariable._next_ref_id
14681470
CQCVariable._next_ref_id += 1
14691471

14701472
# make ref_id a read-only variable
14711473
@property
14721474
def ref_id(self):
1475+
"""
1476+
Get the refernce ID of this CQCVariable. This is a read-only property.
1477+
"""
14731478
return self._ref_id
14741479

14751480
# override the == operator
@@ -1483,12 +1488,27 @@ def __ne__(self, other: Union['CQCVariable', int]):
14831488

14841489

14851490
class LogicalFunction:
1491+
"""
1492+
Private helper class. This class should never be used outside this pythonLib.
1493+
"""
14861494

14871495
def __init__(self,
14881496
operand_one: CQCVariable,
14891497
operator: CQCLogicalOperator,
14901498
operand_two: Union[CQCVariable, int]
14911499
):
1500+
"""
1501+
Stores all information necessary to create a logical comparison
1502+
1503+
- **Arguments**
1504+
1505+
:operand_one: The CQCVariable that stores the measurement outcome that must be compared
1506+
:operator: One of the CQCLogicalOperator types that CQC supports.
1507+
At present, equality and inequality are supported.
1508+
:operand_two: Either a CQCVariable or an integer.
1509+
If a CQCVariable, then the value behind this variable will be compared to operand_one.
1510+
If an integer, then the value behind operand_one will be compared to this integer.
1511+
"""
14921512

14931513
self.operand_one = operand_one
14941514
self.operator = operator
@@ -1498,6 +1518,9 @@ def get_negation(self) -> LogicalFunction:
14981518
return LogicalFunction(self.operand_one, CQCLogicalOperator.opposite_of(self.operator), self.operand_two)
14991519

15001520
def get_CQCIFHeader(self) -> CQCIFHeader:
1521+
"""
1522+
Builds the If header corresponding to this logical function.
1523+
"""
15011524

15021525
if isinstance(self.operand_two, int):
15031526
type_of_operand_two = CQCIFHeader.TYPE_VALUE
@@ -1519,7 +1542,20 @@ def get_CQCIFHeader(self) -> CQCIFHeader:
15191542

15201543

15211544
class CQCMix(NodeMixin):
1545+
"""
1546+
This Python Context Manager Type can be used to create CQC programs that consist of more than a single type.
1547+
Hence the name CQC Mix. Programs of this type can consist of any number and mix of the other CQC types.
1548+
"""
1549+
15221550
def __init__(self, cqc_connection: CQCConnection):
1551+
"""
1552+
Initializes the Mix context.
1553+
1554+
- **Arguments**
1555+
1556+
:cqc_connection: The CQCConnection to which this CQC Program must be sent.
1557+
"""
1558+
15231559
self._conn = cqc_connection
15241560

15251561
# Set the current scope to self
@@ -1564,16 +1600,45 @@ def __exit__(self, exc_type, exc_val, exc_tb):
15641600

15651601

15661602
def cqc_if(self, logical_function: LogicalFunction):
1603+
"""
1604+
Open a Python Context Manager Type to start an if-statement block.
1605+
1606+
- **Arguments**
1607+
1608+
:logical_function: A LogicalFunction instance. Never instantiate this explicitely; instead
1609+
use the following: CQCVariable == 1 OR CQCVariable == CQCVariable.
1610+
CQCVariable can be any instance that you want to test to a value, or to another
1611+
CQCVariable. The operator can be == or !=.
1612+
The value can be any integer (though only 1 and 0 make sense).
1613+
1614+
"""
15671615
return CQCConditional(self._conn, False, logical_function)
15681616

15691617
def cqc_else(self):
1618+
"""
1619+
Open a Python Context Manager Type to start an else-statement block.
1620+
This will be an else-block of the last closed cqc_if-block.
1621+
"""
15701622
# Find out to which if this else belongs
15711623
return CQCConditional(self._conn, True)
15721624

15731625
def loop(self, times: int):
1626+
"""
1627+
Open a Python Context Manager Type to start a factory (i.e. repeated sequence of commands).
1628+
1629+
- **Arguments**
1630+
1631+
:times: The number of times the commands inside body of this context should be repeated.
1632+
1633+
"""
15741634
return CQCFactory(self._conn, times)
15751635

1576-
class CQCFactory():
1636+
class CQCFactory:
1637+
"""
1638+
Private class to create factories inside CQCMix contexts. Never explicitely instantiate this class outside
1639+
the source code of this library.
1640+
Instead, use CQCMix.loop(x), where x is the amount of times to repeat.
1641+
"""
15771642

15781643
def __init__(self, cqc_connection: CQCConnection, repetition_amount: int):
15791644
self._conn = cqc_connection
@@ -1617,6 +1682,11 @@ def __exit__(self, exc_type, exc_val, exc_tb):
16171682

16181683

16191684
class CQCConditional(NodeMixin):
1685+
"""
1686+
Private helper class. Never explicitely instantiate this class outside the source code of this library.
1687+
This Context Manager class is instantiated by CQCMix.cqc_if() and CQCMix.cqc_else(). Its
1688+
function is to build and pend CQC If headers.
1689+
"""
16201690

16211691
# This private class variable holds the last CQCConditional that
16221692
# functioned as an IF (as opposed to an ELSE) on which __exit__ is invoked.
@@ -2062,6 +2132,10 @@ def _single_gate_rotation(self, command, step, notify, block):
20622132

20632133
if self._cqc.pend_messages:
20642134

2135+
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2136+
if self._cqc._inside_cqc_mix:
2137+
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH + CQCRotationHeader.HDR_LENGTH)
2138+
20652139
# Build command header and rotation sub header
20662140
command_header = CQCCmdHeader()
20672141
command_header.setVals(self._qID, command, notify, block)
@@ -2291,6 +2365,10 @@ def reset(self, notify=True, block=True):
22912365

22922366
if self._cqc.pend_messages:
22932367

2368+
# If we are inside a TP_MIX, then insert the CQC Type header before the command header
2369+
if self._cqc._inside_cqc_mix:
2370+
self._cqc._pend_type_header(CQCType.COMMAND, CQCCmdHeader.HDR_LENGTH)
2371+
22942372
# Build header
22952373
header = CQCCmdHeader()
22962374
header.setVals(self._qID, CQC_CMD_RESET, notify, block)

0 commit comments

Comments
 (0)