libqasm 1.0.0__cp39-cp39-macosx_11_0_arm64.whl → 1.1.0__cp39-cp39-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cqasm/v3x/semantic.py CHANGED
@@ -646,6 +646,8 @@ class Annotated(Node):
646
646
  return GateInstruction._deserialize(cbor, seq_to_ob, links)
647
647
  if typ == 'NonGateInstruction':
648
648
  return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
649
+ if typ == 'AsmDeclaration':
650
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
649
651
  raise ValueError('unknown or unexpected type (@t) found in node serialization')
650
652
 
651
653
  def _serialize(self, id_map):
@@ -972,6 +974,392 @@ class MultiAnnotationData(_Multiple):
972
974
 
973
975
  _typemap['AnnotationData'] = AnnotationData
974
976
 
977
+ class Statement(Annotated):
978
+ __slots__ = []
979
+
980
+ def __init__(
981
+ self,
982
+ annotations=None,
983
+ ):
984
+ super().__init__(annotations=annotations)
985
+
986
+ @staticmethod
987
+ def _deserialize(cbor, seq_to_ob, links):
988
+ """Attempts to deserialize the given cbor object (in Python primitive
989
+ representation) into a node of this type. All (sub)nodes are added to
990
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
991
+ registered in the links list by means of a two-tuple of the setter
992
+ function for the link field and the sequence number of the target node.
993
+ """
994
+ if not isinstance(cbor, dict):
995
+ raise TypeError('node description object must be a dict')
996
+ typ = cbor.get('@t', None)
997
+ if typ is None:
998
+ raise ValueError('type (@t) field is missing from node serialization')
999
+ if typ == 'GateInstruction':
1000
+ return GateInstruction._deserialize(cbor, seq_to_ob, links)
1001
+ if typ == 'NonGateInstruction':
1002
+ return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1003
+ if typ == 'AsmDeclaration':
1004
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
1005
+ raise ValueError('unknown or unexpected type (@t) found in node serialization')
1006
+
1007
+ def _serialize(self, id_map):
1008
+ """Serializes this node to the Python primitive representation of its
1009
+ CBOR serialization. The tree that the node belongs to must be
1010
+ well-formed. id_map must match Python id() calls for all nodes to unique
1011
+ integers, to use for the sequence number representation of links."""
1012
+ cbor = {'@i': id_map[id(self)], '@t': 'Statement'}
1013
+
1014
+ # Serialize the annotations field.
1015
+ field = {'@T': '*'}
1016
+ lst = []
1017
+ for el in self._attr_annotations:
1018
+ el = el._serialize(id_map)
1019
+ el['@T'] = '1'
1020
+ lst.append(el)
1021
+ field['@d'] = lst
1022
+ cbor['annotations'] = field
1023
+
1024
+ # Serialize annotations.
1025
+ for key, val in self._annot.items():
1026
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1027
+
1028
+ return cbor
1029
+
1030
+
1031
+ class MultiStatement(_Multiple):
1032
+ """Wrapper for an edge with multiple Statement objects."""
1033
+
1034
+ _T = Statement
1035
+
1036
+
1037
+ _typemap['Statement'] = Statement
1038
+
1039
+ class Instruction(Statement):
1040
+ __slots__ = []
1041
+
1042
+ def __init__(
1043
+ self,
1044
+ annotations=None,
1045
+ ):
1046
+ super().__init__(annotations=annotations)
1047
+
1048
+ @staticmethod
1049
+ def _deserialize(cbor, seq_to_ob, links):
1050
+ """Attempts to deserialize the given cbor object (in Python primitive
1051
+ representation) into a node of this type. All (sub)nodes are added to
1052
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
1053
+ registered in the links list by means of a two-tuple of the setter
1054
+ function for the link field and the sequence number of the target node.
1055
+ """
1056
+ if not isinstance(cbor, dict):
1057
+ raise TypeError('node description object must be a dict')
1058
+ typ = cbor.get('@t', None)
1059
+ if typ is None:
1060
+ raise ValueError('type (@t) field is missing from node serialization')
1061
+ if typ == 'GateInstruction':
1062
+ return GateInstruction._deserialize(cbor, seq_to_ob, links)
1063
+ if typ == 'NonGateInstruction':
1064
+ return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1065
+ if typ == 'AsmDeclaration':
1066
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
1067
+ raise ValueError('unknown or unexpected type (@t) found in node serialization')
1068
+
1069
+ def _serialize(self, id_map):
1070
+ """Serializes this node to the Python primitive representation of its
1071
+ CBOR serialization. The tree that the node belongs to must be
1072
+ well-formed. id_map must match Python id() calls for all nodes to unique
1073
+ integers, to use for the sequence number representation of links."""
1074
+ cbor = {'@i': id_map[id(self)], '@t': 'Instruction'}
1075
+
1076
+ # Serialize the annotations field.
1077
+ field = {'@T': '*'}
1078
+ lst = []
1079
+ for el in self._attr_annotations:
1080
+ el = el._serialize(id_map)
1081
+ el['@T'] = '1'
1082
+ lst.append(el)
1083
+ field['@d'] = lst
1084
+ cbor['annotations'] = field
1085
+
1086
+ # Serialize annotations.
1087
+ for key, val in self._annot.items():
1088
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1089
+
1090
+ return cbor
1091
+
1092
+
1093
+ class MultiInstruction(_Multiple):
1094
+ """Wrapper for an edge with multiple Instruction objects."""
1095
+
1096
+ _T = Instruction
1097
+
1098
+
1099
+ _typemap['Instruction'] = Instruction
1100
+
1101
+ class AsmDeclaration(Instruction):
1102
+ __slots__ = [
1103
+ '_attr_backend_name',
1104
+ '_attr_backend_code',
1105
+ ]
1106
+
1107
+ def __init__(
1108
+ self,
1109
+ backend_name=None,
1110
+ backend_code=None,
1111
+ annotations=None,
1112
+ ):
1113
+ super().__init__(annotations=annotations)
1114
+ self.backend_name = backend_name
1115
+ self.backend_code = backend_code
1116
+
1117
+ @property
1118
+ def backend_name(self):
1119
+ return self._attr_backend_name
1120
+
1121
+ @backend_name.setter
1122
+ def backend_name(self, val):
1123
+ if val is None:
1124
+ del self.backend_name
1125
+ return
1126
+ if not isinstance(val, cqasm.v3x.primitives.Str):
1127
+ # Try to "typecast" if this isn't an obvious mistake.
1128
+ if isinstance(val, Node):
1129
+ raise TypeError('backend_name must be of type cqasm.v3x.primitives.Str')
1130
+ val = cqasm.v3x.primitives.Str(val)
1131
+ self._attr_backend_name = val
1132
+
1133
+ @backend_name.deleter
1134
+ def backend_name(self):
1135
+ self._attr_backend_name = cqasm.v3x.primitives.Str()
1136
+
1137
+ @property
1138
+ def backend_code(self):
1139
+ return self._attr_backend_code
1140
+
1141
+ @backend_code.setter
1142
+ def backend_code(self, val):
1143
+ if val is None:
1144
+ del self.backend_code
1145
+ return
1146
+ if not isinstance(val, cqasm.v3x.primitives.Str):
1147
+ # Try to "typecast" if this isn't an obvious mistake.
1148
+ if isinstance(val, Node):
1149
+ raise TypeError('backend_code must be of type cqasm.v3x.primitives.Str')
1150
+ val = cqasm.v3x.primitives.Str(val)
1151
+ self._attr_backend_code = val
1152
+
1153
+ @backend_code.deleter
1154
+ def backend_code(self):
1155
+ self._attr_backend_code = cqasm.v3x.primitives.Str()
1156
+
1157
+ def __eq__(self, other):
1158
+ """Equality operator. Ignores annotations!"""
1159
+ if not isinstance(other, AsmDeclaration):
1160
+ return False
1161
+ if self.backend_name != other.backend_name:
1162
+ return False
1163
+ if self.backend_code != other.backend_code:
1164
+ return False
1165
+ if self.annotations != other.annotations:
1166
+ return False
1167
+ return True
1168
+
1169
+ def dump(self, indent=0, annotations=None, links=1):
1170
+ """Returns a debug representation of this tree as a multiline string.
1171
+ indent is the number of double spaces prefixed before every line.
1172
+ annotations, if specified, must be a set-like object containing the key
1173
+ strings of the annotations that are to be printed. links specifies the
1174
+ maximum link recursion depth."""
1175
+ s = [' '*indent]
1176
+ s.append('AsmDeclaration(')
1177
+ if annotations is None:
1178
+ annotations = []
1179
+ for key in annotations:
1180
+ if key in self:
1181
+ s.append(' # {}: {}'.format(key, self[key]))
1182
+ s.append('\n')
1183
+ indent += 1
1184
+ s.append(' '*indent)
1185
+ s.append('backend_name: ')
1186
+ s.append(str(self.backend_name) + '\n')
1187
+ s.append(' '*indent)
1188
+ s.append('backend_code: ')
1189
+ s.append(str(self.backend_code) + '\n')
1190
+ s.append(' '*indent)
1191
+ s.append('annotations: ')
1192
+ if not self.annotations:
1193
+ s.append('-\n')
1194
+ else:
1195
+ s.append('[\n')
1196
+ for child in self.annotations:
1197
+ s.append(child.dump(indent + 1, annotations, links) + '\n')
1198
+ s.append(' '*indent + ']\n')
1199
+ indent -= 1
1200
+ s.append(' '*indent)
1201
+ s.append(')')
1202
+ return ''.join(s)
1203
+
1204
+ __str__ = dump
1205
+ __repr__ = dump
1206
+
1207
+ def find_reachable(self, id_map=None):
1208
+ """Returns a dictionary mapping Python id() values to stable sequence
1209
+ numbers for all nodes in the tree rooted at this node. If id_map is
1210
+ specified, found nodes are appended to it."""
1211
+ if id_map is None:
1212
+ id_map = {}
1213
+ if id(self) in id_map:
1214
+ raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
1215
+ id_map[id(self)] = len(id_map)
1216
+ for el in self._attr_annotations:
1217
+ el.find_reachable(id_map)
1218
+ return id_map
1219
+
1220
+ def check_complete(self, id_map=None):
1221
+ """Raises NotWellFormed if the tree rooted at this node is not
1222
+ well-formed. If id_map is specified, this tree is only a subtree in the
1223
+ context of a larger tree, and id_map must be a dict mapping from Python
1224
+ id() codes to tree indices for all reachable nodes."""
1225
+ if id_map is None:
1226
+ id_map = self.find_reachable()
1227
+ for child in self._attr_annotations:
1228
+ child.check_complete(id_map)
1229
+
1230
+ def copy(self):
1231
+ """Returns a shallow copy of this node."""
1232
+ return AsmDeclaration(
1233
+ backend_name=self._attr_backend_name,
1234
+ backend_code=self._attr_backend_code,
1235
+ annotations=self._attr_annotations.copy()
1236
+ )
1237
+
1238
+ def clone(self):
1239
+ """Returns a deep copy of this node. This mimics the C++ interface,
1240
+ deficiencies with links included; that is, links always point to the
1241
+ original tree. If you're not cloning a subtree in a context where this
1242
+ is the desired behavior, you may want to use the copy.deepcopy() from
1243
+ the stdlib instead, which should copy links correctly."""
1244
+ return AsmDeclaration(
1245
+ backend_name=_cloned(self._attr_backend_name),
1246
+ backend_code=_cloned(self._attr_backend_code),
1247
+ annotations=_cloned(self._attr_annotations)
1248
+ )
1249
+
1250
+ @staticmethod
1251
+ def _deserialize(cbor, seq_to_ob, links):
1252
+ """Attempts to deserialize the given cbor object (in Python primitive
1253
+ representation) into a node of this type. All (sub)nodes are added to
1254
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
1255
+ registered in the links list by means of a two-tuple of the setter
1256
+ function for the link field and the sequence number of the target node.
1257
+ """
1258
+ if not isinstance(cbor, dict):
1259
+ raise TypeError('node description object must be a dict')
1260
+ typ = cbor.get('@t', None)
1261
+ if typ is None:
1262
+ raise ValueError('type (@t) field is missing from node serialization')
1263
+ if typ != 'AsmDeclaration':
1264
+ raise ValueError('found node serialization for ' + typ + ', but expected AsmDeclaration')
1265
+
1266
+ # Deserialize the backend_name field.
1267
+ field = cbor.get('backend_name', None)
1268
+ if not isinstance(field, dict):
1269
+ raise ValueError('missing or invalid serialization of field backend_name')
1270
+ if hasattr(cqasm.v3x.primitives.Str, 'deserialize_cbor'):
1271
+ f_backend_name = cqasm.v3x.primitives.Str.deserialize_cbor(field)
1272
+ else:
1273
+ f_backend_name = cqasm.v3x.primitives.deserialize(cqasm.v3x.primitives.Str, field)
1274
+
1275
+ # Deserialize the backend_code field.
1276
+ field = cbor.get('backend_code', None)
1277
+ if not isinstance(field, dict):
1278
+ raise ValueError('missing or invalid serialization of field backend_code')
1279
+ if hasattr(cqasm.v3x.primitives.Str, 'deserialize_cbor'):
1280
+ f_backend_code = cqasm.v3x.primitives.Str.deserialize_cbor(field)
1281
+ else:
1282
+ f_backend_code = cqasm.v3x.primitives.deserialize(cqasm.v3x.primitives.Str, field)
1283
+
1284
+ # Deserialize the annotations field.
1285
+ field = cbor.get('annotations', None)
1286
+ if not isinstance(field, dict):
1287
+ raise ValueError('missing or invalid serialization of field annotations')
1288
+ if field.get('@T') != '*':
1289
+ raise ValueError('unexpected edge type for field annotations')
1290
+ data = field.get('@d', None)
1291
+ if not isinstance(data, list):
1292
+ raise ValueError('missing serialization of Any/Many contents')
1293
+ f_annotations = MultiAnnotationData()
1294
+ for element in data:
1295
+ if element.get('@T') != '1':
1296
+ raise ValueError('unexpected edge type for Any/Many element')
1297
+ f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
1298
+
1299
+ # Construct the AsmDeclaration node.
1300
+ node = AsmDeclaration(f_backend_name, f_backend_code, f_annotations)
1301
+
1302
+ # Deserialize annotations.
1303
+ for key, val in cbor.items():
1304
+ if not (key.startswith('{') and key.endswith('}')):
1305
+ continue
1306
+ key = key[1:-1]
1307
+ node[key] = cqasm.v3x.primitives.deserialize(key, val)
1308
+
1309
+ # Register node in sequence number lookup.
1310
+ seq = cbor.get('@i', None)
1311
+ if not isinstance(seq, int):
1312
+ raise ValueError('sequence number field (@i) is not an integer or missing from node serialization')
1313
+ if seq in seq_to_ob:
1314
+ raise ValueError('duplicate sequence number %d' % seq)
1315
+ seq_to_ob[seq] = node
1316
+
1317
+ return node
1318
+
1319
+ def _serialize(self, id_map):
1320
+ """Serializes this node to the Python primitive representation of its
1321
+ CBOR serialization. The tree that the node belongs to must be
1322
+ well-formed. id_map must match Python id() calls for all nodes to unique
1323
+ integers, to use for the sequence number representation of links."""
1324
+ cbor = {'@i': id_map[id(self)], '@t': 'AsmDeclaration'}
1325
+
1326
+ # Serialize the backend_name field.
1327
+ if hasattr(self._attr_backend_name, 'serialize_cbor'):
1328
+ cbor['backend_name'] = self._attr_backend_name.serialize_cbor()
1329
+ else:
1330
+ cbor['backend_name'] = cqasm.v3x.primitives.serialize(cqasm.v3x.primitives.Str, self._attr_backend_name)
1331
+
1332
+ # Serialize the backend_code field.
1333
+ if hasattr(self._attr_backend_code, 'serialize_cbor'):
1334
+ cbor['backend_code'] = self._attr_backend_code.serialize_cbor()
1335
+ else:
1336
+ cbor['backend_code'] = cqasm.v3x.primitives.serialize(cqasm.v3x.primitives.Str, self._attr_backend_code)
1337
+
1338
+ # Serialize the annotations field.
1339
+ field = {'@T': '*'}
1340
+ lst = []
1341
+ for el in self._attr_annotations:
1342
+ el = el._serialize(id_map)
1343
+ el['@T'] = '1'
1344
+ lst.append(el)
1345
+ field['@d'] = lst
1346
+ cbor['annotations'] = field
1347
+
1348
+ # Serialize annotations.
1349
+ for key, val in self._annot.items():
1350
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1351
+
1352
+ return cbor
1353
+
1354
+
1355
+ class MultiAsmDeclaration(_Multiple):
1356
+ """Wrapper for an edge with multiple AsmDeclaration objects."""
1357
+
1358
+ _T = AsmDeclaration
1359
+
1360
+
1361
+ _typemap['AsmDeclaration'] = AsmDeclaration
1362
+
975
1363
  class Block(Node):
976
1364
  __slots__ = [
977
1365
  '_attr_statements',
@@ -1173,20 +1561,20 @@ class Gate(Annotated):
1173
1561
  __slots__ = [
1174
1562
  '_attr_name',
1175
1563
  '_attr_gate',
1176
- '_attr_parameter',
1564
+ '_attr_parameters',
1177
1565
  ]
1178
1566
 
1179
1567
  def __init__(
1180
1568
  self,
1181
1569
  name=None,
1182
1570
  gate=None,
1183
- parameter=None,
1571
+ parameters=None,
1184
1572
  annotations=None,
1185
1573
  ):
1186
1574
  super().__init__(annotations=annotations)
1187
1575
  self.name = name
1188
1576
  self.gate = gate
1189
- self.parameter = parameter
1577
+ self.parameters = parameters
1190
1578
 
1191
1579
  @property
1192
1580
  def name(self):
@@ -1229,24 +1617,24 @@ class Gate(Annotated):
1229
1617
  self._attr_gate = None
1230
1618
 
1231
1619
  @property
1232
- def parameter(self):
1233
- return self._attr_parameter
1620
+ def parameters(self):
1621
+ return self._attr_parameters
1234
1622
 
1235
- @parameter.setter
1236
- def parameter(self, val):
1623
+ @parameters.setter
1624
+ def parameters(self, val):
1237
1625
  if val is None:
1238
- del self.parameter
1626
+ del self.parameters
1239
1627
  return
1240
- if not isinstance(val, cqasm.v3x.values.ValueBase):
1628
+ if not isinstance(val, cqasm.v3x.values.MultiValueBase):
1241
1629
  # Try to "typecast" if this isn't an obvious mistake.
1242
1630
  if isinstance(val, Node):
1243
- raise TypeError('parameter must be of type cqasm.v3x.values.ValueBase')
1244
- val = cqasm.v3x.values.ValueBase(val)
1245
- self._attr_parameter = val
1631
+ raise TypeError('parameters must be of type cqasm.v3x.values.MultiValueBase')
1632
+ val = cqasm.v3x.values.MultiValueBase(val)
1633
+ self._attr_parameters = val
1246
1634
 
1247
- @parameter.deleter
1248
- def parameter(self):
1249
- self._attr_parameter = None
1635
+ @parameters.deleter
1636
+ def parameters(self):
1637
+ self._attr_parameters = cqasm.v3x.values.MultiValueBase()
1250
1638
 
1251
1639
  def __eq__(self, other):
1252
1640
  """Equality operator. Ignores annotations!"""
@@ -1256,7 +1644,7 @@ class Gate(Annotated):
1256
1644
  return False
1257
1645
  if self.gate != other.gate:
1258
1646
  return False
1259
- if self.parameter != other.parameter:
1647
+ if self.parameters != other.parameters:
1260
1648
  return False
1261
1649
  if self.annotations != other.annotations:
1262
1650
  return False
@@ -1289,13 +1677,14 @@ class Gate(Annotated):
1289
1677
  s.append(self.gate.dump(indent + 1, annotations, links) + '\n')
1290
1678
  s.append(' '*indent + '>\n')
1291
1679
  s.append(' '*indent)
1292
- s.append('parameter: ')
1293
- if self.parameter is None:
1680
+ s.append('parameters: ')
1681
+ if not self.parameters:
1294
1682
  s.append('-\n')
1295
1683
  else:
1296
- s.append('<\n')
1297
- s.append(self.parameter.dump(indent + 1, annotations, links) + '\n')
1298
- s.append(' '*indent + '>\n')
1684
+ s.append('[\n')
1685
+ for child in self.parameters:
1686
+ s.append(child.dump(indent + 1, annotations, links) + '\n')
1687
+ s.append(' '*indent + ']\n')
1299
1688
  s.append(' '*indent)
1300
1689
  s.append('annotations: ')
1301
1690
  if not self.annotations:
@@ -1324,8 +1713,8 @@ class Gate(Annotated):
1324
1713
  id_map[id(self)] = len(id_map)
1325
1714
  if self._attr_gate is not None:
1326
1715
  self._attr_gate.find_reachable(id_map)
1327
- if self._attr_parameter is not None:
1328
- self._attr_parameter.find_reachable(id_map)
1716
+ for el in self._attr_parameters:
1717
+ el.find_reachable(id_map)
1329
1718
  for el in self._attr_annotations:
1330
1719
  el.find_reachable(id_map)
1331
1720
  return id_map
@@ -1339,8 +1728,8 @@ class Gate(Annotated):
1339
1728
  id_map = self.find_reachable()
1340
1729
  if self._attr_gate is not None:
1341
1730
  self._attr_gate.check_complete(id_map)
1342
- if self._attr_parameter is not None:
1343
- self._attr_parameter.check_complete(id_map)
1731
+ for child in self._attr_parameters:
1732
+ child.check_complete(id_map)
1344
1733
  for child in self._attr_annotations:
1345
1734
  child.check_complete(id_map)
1346
1735
 
@@ -1349,7 +1738,7 @@ class Gate(Annotated):
1349
1738
  return Gate(
1350
1739
  name=self._attr_name,
1351
1740
  gate=self._attr_gate,
1352
- parameter=self._attr_parameter,
1741
+ parameters=self._attr_parameters.copy(),
1353
1742
  annotations=self._attr_annotations.copy()
1354
1743
  )
1355
1744
 
@@ -1362,7 +1751,7 @@ class Gate(Annotated):
1362
1751
  return Gate(
1363
1752
  name=_cloned(self._attr_name),
1364
1753
  gate=_cloned(self._attr_gate),
1365
- parameter=_cloned(self._attr_parameter),
1754
+ parameters=_cloned(self._attr_parameters),
1366
1755
  annotations=_cloned(self._attr_annotations)
1367
1756
  )
1368
1757
 
@@ -1402,16 +1791,20 @@ class Gate(Annotated):
1402
1791
  else:
1403
1792
  f_gate = Gate._deserialize(field, seq_to_ob, links)
1404
1793
 
1405
- # Deserialize the parameter field.
1406
- field = cbor.get('parameter', None)
1794
+ # Deserialize the parameters field.
1795
+ field = cbor.get('parameters', None)
1407
1796
  if not isinstance(field, dict):
1408
- raise ValueError('missing or invalid serialization of field parameter')
1409
- if field.get('@T') != '?':
1410
- raise ValueError('unexpected edge type for field parameter')
1411
- if field.get('@t', None) is None:
1412
- f_parameter = None
1413
- else:
1414
- f_parameter = cqasm.v3x.values.ValueBase._deserialize(field, seq_to_ob, links)
1797
+ raise ValueError('missing or invalid serialization of field parameters')
1798
+ if field.get('@T') != '*':
1799
+ raise ValueError('unexpected edge type for field parameters')
1800
+ data = field.get('@d', None)
1801
+ if not isinstance(data, list):
1802
+ raise ValueError('missing serialization of Any/Many contents')
1803
+ f_parameters = cqasm.v3x.values.MultiValueBase()
1804
+ for element in data:
1805
+ if element.get('@T') != '1':
1806
+ raise ValueError('unexpected edge type for Any/Many element')
1807
+ f_parameters.append(cqasm.v3x.values.ValueBase._deserialize(element, seq_to_ob, links))
1415
1808
 
1416
1809
  # Deserialize the annotations field.
1417
1810
  field = cbor.get('annotations', None)
@@ -1429,7 +1822,7 @@ class Gate(Annotated):
1429
1822
  f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
1430
1823
 
1431
1824
  # Construct the Gate node.
1432
- node = Gate(f_name, f_gate, f_parameter, f_annotations)
1825
+ node = Gate(f_name, f_gate, f_parameters, f_annotations)
1433
1826
 
1434
1827
  # Deserialize annotations.
1435
1828
  for key, val in cbor.items():
@@ -1469,73 +1862,15 @@ class Gate(Annotated):
1469
1862
  field.update(self._attr_gate._serialize(id_map))
1470
1863
  cbor['gate'] = field
1471
1864
 
1472
- # Serialize the parameter field.
1473
- field = {'@T': '?'}
1474
- if self._attr_parameter is None:
1475
- field['@t'] = None
1476
- else:
1477
- field.update(self._attr_parameter._serialize(id_map))
1478
- cbor['parameter'] = field
1479
-
1480
- # Serialize the annotations field.
1865
+ # Serialize the parameters field.
1481
1866
  field = {'@T': '*'}
1482
1867
  lst = []
1483
- for el in self._attr_annotations:
1868
+ for el in self._attr_parameters:
1484
1869
  el = el._serialize(id_map)
1485
1870
  el['@T'] = '1'
1486
1871
  lst.append(el)
1487
1872
  field['@d'] = lst
1488
- cbor['annotations'] = field
1489
-
1490
- # Serialize annotations.
1491
- for key, val in self._annot.items():
1492
- cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1493
-
1494
- return cbor
1495
-
1496
-
1497
- class MultiGate(_Multiple):
1498
- """Wrapper for an edge with multiple Gate objects."""
1499
-
1500
- _T = Gate
1501
-
1502
-
1503
- _typemap['Gate'] = Gate
1504
-
1505
- class Statement(Annotated):
1506
- __slots__ = []
1507
-
1508
- def __init__(
1509
- self,
1510
- annotations=None,
1511
- ):
1512
- super().__init__(annotations=annotations)
1513
-
1514
- @staticmethod
1515
- def _deserialize(cbor, seq_to_ob, links):
1516
- """Attempts to deserialize the given cbor object (in Python primitive
1517
- representation) into a node of this type. All (sub)nodes are added to
1518
- the seq_to_ob dict, indexed by their cbor sequence number. All links are
1519
- registered in the links list by means of a two-tuple of the setter
1520
- function for the link field and the sequence number of the target node.
1521
- """
1522
- if not isinstance(cbor, dict):
1523
- raise TypeError('node description object must be a dict')
1524
- typ = cbor.get('@t', None)
1525
- if typ is None:
1526
- raise ValueError('type (@t) field is missing from node serialization')
1527
- if typ == 'GateInstruction':
1528
- return GateInstruction._deserialize(cbor, seq_to_ob, links)
1529
- if typ == 'NonGateInstruction':
1530
- return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1531
- raise ValueError('unknown or unexpected type (@t) found in node serialization')
1532
-
1533
- def _serialize(self, id_map):
1534
- """Serializes this node to the Python primitive representation of its
1535
- CBOR serialization. The tree that the node belongs to must be
1536
- well-formed. id_map must match Python id() calls for all nodes to unique
1537
- integers, to use for the sequence number representation of links."""
1538
- cbor = {'@i': id_map[id(self)], '@t': 'Statement'}
1873
+ cbor['parameters'] = field
1539
1874
 
1540
1875
  # Serialize the annotations field.
1541
1876
  field = {'@T': '*'}
@@ -1554,73 +1889,13 @@ class Statement(Annotated):
1554
1889
  return cbor
1555
1890
 
1556
1891
 
1557
- class MultiStatement(_Multiple):
1558
- """Wrapper for an edge with multiple Statement objects."""
1559
-
1560
- _T = Statement
1561
-
1562
-
1563
- _typemap['Statement'] = Statement
1564
-
1565
- class Instruction(Statement):
1566
- __slots__ = []
1567
-
1568
- def __init__(
1569
- self,
1570
- annotations=None,
1571
- ):
1572
- super().__init__(annotations=annotations)
1573
-
1574
- @staticmethod
1575
- def _deserialize(cbor, seq_to_ob, links):
1576
- """Attempts to deserialize the given cbor object (in Python primitive
1577
- representation) into a node of this type. All (sub)nodes are added to
1578
- the seq_to_ob dict, indexed by their cbor sequence number. All links are
1579
- registered in the links list by means of a two-tuple of the setter
1580
- function for the link field and the sequence number of the target node.
1581
- """
1582
- if not isinstance(cbor, dict):
1583
- raise TypeError('node description object must be a dict')
1584
- typ = cbor.get('@t', None)
1585
- if typ is None:
1586
- raise ValueError('type (@t) field is missing from node serialization')
1587
- if typ == 'GateInstruction':
1588
- return GateInstruction._deserialize(cbor, seq_to_ob, links)
1589
- if typ == 'NonGateInstruction':
1590
- return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1591
- raise ValueError('unknown or unexpected type (@t) found in node serialization')
1592
-
1593
- def _serialize(self, id_map):
1594
- """Serializes this node to the Python primitive representation of its
1595
- CBOR serialization. The tree that the node belongs to must be
1596
- well-formed. id_map must match Python id() calls for all nodes to unique
1597
- integers, to use for the sequence number representation of links."""
1598
- cbor = {'@i': id_map[id(self)], '@t': 'Instruction'}
1599
-
1600
- # Serialize the annotations field.
1601
- field = {'@T': '*'}
1602
- lst = []
1603
- for el in self._attr_annotations:
1604
- el = el._serialize(id_map)
1605
- el['@T'] = '1'
1606
- lst.append(el)
1607
- field['@d'] = lst
1608
- cbor['annotations'] = field
1609
-
1610
- # Serialize annotations.
1611
- for key, val in self._annot.items():
1612
- cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1613
-
1614
- return cbor
1615
-
1616
-
1617
- class MultiInstruction(_Multiple):
1618
- """Wrapper for an edge with multiple Instruction objects."""
1892
+ class MultiGate(_Multiple):
1893
+ """Wrapper for an edge with multiple Gate objects."""
1619
1894
 
1620
- _T = Instruction
1895
+ _T = Gate
1621
1896
 
1622
1897
 
1623
- _typemap['Instruction'] = Instruction
1898
+ _typemap['Gate'] = Gate
1624
1899
 
1625
1900
  class GateInstruction(Instruction):
1626
1901
  """A gate, or a composition of gate modifiers and a gate"""
@@ -1967,13 +2242,13 @@ class MultiGateInstruction(_Multiple):
1967
2242
  _typemap['GateInstruction'] = GateInstruction
1968
2243
 
1969
2244
  class NonGateInstruction(Instruction):
1970
- """A measure instruction or a reset instruction"""
2245
+ """A non-gate instruction: init, measure, reset, barrier, wait..."""
1971
2246
 
1972
2247
  __slots__ = [
1973
2248
  '_attr_instruction_ref',
1974
2249
  '_attr_name',
1975
2250
  '_attr_operands',
1976
- '_attr_parameter',
2251
+ '_attr_parameters',
1977
2252
  ]
1978
2253
 
1979
2254
  def __init__(
@@ -1981,14 +2256,14 @@ class NonGateInstruction(Instruction):
1981
2256
  instruction_ref=None,
1982
2257
  name=None,
1983
2258
  operands=None,
1984
- parameter=None,
2259
+ parameters=None,
1985
2260
  annotations=None,
1986
2261
  ):
1987
2262
  super().__init__(annotations=annotations)
1988
2263
  self.instruction_ref = instruction_ref
1989
2264
  self.name = name
1990
2265
  self.operands = operands
1991
- self.parameter = parameter
2266
+ self.parameters = parameters
1992
2267
 
1993
2268
  @property
1994
2269
  def instruction_ref(self):
@@ -2051,24 +2326,24 @@ class NonGateInstruction(Instruction):
2051
2326
  self._attr_operands = cqasm.v3x.values.MultiValueBase()
2052
2327
 
2053
2328
  @property
2054
- def parameter(self):
2055
- return self._attr_parameter
2329
+ def parameters(self):
2330
+ return self._attr_parameters
2056
2331
 
2057
- @parameter.setter
2058
- def parameter(self, val):
2332
+ @parameters.setter
2333
+ def parameters(self, val):
2059
2334
  if val is None:
2060
- del self.parameter
2335
+ del self.parameters
2061
2336
  return
2062
- if not isinstance(val, cqasm.v3x.values.ValueBase):
2337
+ if not isinstance(val, cqasm.v3x.values.MultiValueBase):
2063
2338
  # Try to "typecast" if this isn't an obvious mistake.
2064
2339
  if isinstance(val, Node):
2065
- raise TypeError('parameter must be of type cqasm.v3x.values.ValueBase')
2066
- val = cqasm.v3x.values.ValueBase(val)
2067
- self._attr_parameter = val
2340
+ raise TypeError('parameters must be of type cqasm.v3x.values.MultiValueBase')
2341
+ val = cqasm.v3x.values.MultiValueBase(val)
2342
+ self._attr_parameters = val
2068
2343
 
2069
- @parameter.deleter
2070
- def parameter(self):
2071
- self._attr_parameter = None
2344
+ @parameters.deleter
2345
+ def parameters(self):
2346
+ self._attr_parameters = cqasm.v3x.values.MultiValueBase()
2072
2347
 
2073
2348
  def __eq__(self, other):
2074
2349
  """Equality operator. Ignores annotations!"""
@@ -2080,7 +2355,7 @@ class NonGateInstruction(Instruction):
2080
2355
  return False
2081
2356
  if self.operands != other.operands:
2082
2357
  return False
2083
- if self.parameter != other.parameter:
2358
+ if self.parameters != other.parameters:
2084
2359
  return False
2085
2360
  if self.annotations != other.annotations:
2086
2361
  return False
@@ -2117,13 +2392,14 @@ class NonGateInstruction(Instruction):
2117
2392
  s.append(child.dump(indent + 1, annotations, links) + '\n')
2118
2393
  s.append(' '*indent + ']\n')
2119
2394
  s.append(' '*indent)
2120
- s.append('parameter: ')
2121
- if self.parameter is None:
2395
+ s.append('parameters: ')
2396
+ if not self.parameters:
2122
2397
  s.append('-\n')
2123
2398
  else:
2124
- s.append('<\n')
2125
- s.append(self.parameter.dump(indent + 1, annotations, links) + '\n')
2126
- s.append(' '*indent + '>\n')
2399
+ s.append('[\n')
2400
+ for child in self.parameters:
2401
+ s.append(child.dump(indent + 1, annotations, links) + '\n')
2402
+ s.append(' '*indent + ']\n')
2127
2403
  s.append(' '*indent)
2128
2404
  s.append('annotations: ')
2129
2405
  if not self.annotations:
@@ -2152,8 +2428,8 @@ class NonGateInstruction(Instruction):
2152
2428
  id_map[id(self)] = len(id_map)
2153
2429
  for el in self._attr_operands:
2154
2430
  el.find_reachable(id_map)
2155
- if self._attr_parameter is not None:
2156
- self._attr_parameter.find_reachable(id_map)
2431
+ for el in self._attr_parameters:
2432
+ el.find_reachable(id_map)
2157
2433
  for el in self._attr_annotations:
2158
2434
  el.find_reachable(id_map)
2159
2435
  return id_map
@@ -2167,8 +2443,8 @@ class NonGateInstruction(Instruction):
2167
2443
  id_map = self.find_reachable()
2168
2444
  for child in self._attr_operands:
2169
2445
  child.check_complete(id_map)
2170
- if self._attr_parameter is not None:
2171
- self._attr_parameter.check_complete(id_map)
2446
+ for child in self._attr_parameters:
2447
+ child.check_complete(id_map)
2172
2448
  for child in self._attr_annotations:
2173
2449
  child.check_complete(id_map)
2174
2450
 
@@ -2178,7 +2454,7 @@ class NonGateInstruction(Instruction):
2178
2454
  instruction_ref=self._attr_instruction_ref,
2179
2455
  name=self._attr_name,
2180
2456
  operands=self._attr_operands.copy(),
2181
- parameter=self._attr_parameter,
2457
+ parameters=self._attr_parameters.copy(),
2182
2458
  annotations=self._attr_annotations.copy()
2183
2459
  )
2184
2460
 
@@ -2192,7 +2468,7 @@ class NonGateInstruction(Instruction):
2192
2468
  instruction_ref=_cloned(self._attr_instruction_ref),
2193
2469
  name=_cloned(self._attr_name),
2194
2470
  operands=_cloned(self._attr_operands),
2195
- parameter=_cloned(self._attr_parameter),
2471
+ parameters=_cloned(self._attr_parameters),
2196
2472
  annotations=_cloned(self._attr_annotations)
2197
2473
  )
2198
2474
 
@@ -2245,16 +2521,20 @@ class NonGateInstruction(Instruction):
2245
2521
  raise ValueError('unexpected edge type for Any/Many element')
2246
2522
  f_operands.append(cqasm.v3x.values.ValueBase._deserialize(element, seq_to_ob, links))
2247
2523
 
2248
- # Deserialize the parameter field.
2249
- field = cbor.get('parameter', None)
2524
+ # Deserialize the parameters field.
2525
+ field = cbor.get('parameters', None)
2250
2526
  if not isinstance(field, dict):
2251
- raise ValueError('missing or invalid serialization of field parameter')
2252
- if field.get('@T') != '?':
2253
- raise ValueError('unexpected edge type for field parameter')
2254
- if field.get('@t', None) is None:
2255
- f_parameter = None
2256
- else:
2257
- f_parameter = cqasm.v3x.values.ValueBase._deserialize(field, seq_to_ob, links)
2527
+ raise ValueError('missing or invalid serialization of field parameters')
2528
+ if field.get('@T') != '*':
2529
+ raise ValueError('unexpected edge type for field parameters')
2530
+ data = field.get('@d', None)
2531
+ if not isinstance(data, list):
2532
+ raise ValueError('missing serialization of Any/Many contents')
2533
+ f_parameters = cqasm.v3x.values.MultiValueBase()
2534
+ for element in data:
2535
+ if element.get('@T') != '1':
2536
+ raise ValueError('unexpected edge type for Any/Many element')
2537
+ f_parameters.append(cqasm.v3x.values.ValueBase._deserialize(element, seq_to_ob, links))
2258
2538
 
2259
2539
  # Deserialize the annotations field.
2260
2540
  field = cbor.get('annotations', None)
@@ -2272,7 +2552,7 @@ class NonGateInstruction(Instruction):
2272
2552
  f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
2273
2553
 
2274
2554
  # Construct the NonGateInstruction node.
2275
- node = NonGateInstruction(f_instruction_ref, f_name, f_operands, f_parameter, f_annotations)
2555
+ node = NonGateInstruction(f_instruction_ref, f_name, f_operands, f_parameters, f_annotations)
2276
2556
 
2277
2557
  # Deserialize annotations.
2278
2558
  for key, val in cbor.items():
@@ -2320,13 +2600,15 @@ class NonGateInstruction(Instruction):
2320
2600
  field['@d'] = lst
2321
2601
  cbor['operands'] = field
2322
2602
 
2323
- # Serialize the parameter field.
2324
- field = {'@T': '?'}
2325
- if self._attr_parameter is None:
2326
- field['@t'] = None
2327
- else:
2328
- field.update(self._attr_parameter._serialize(id_map))
2329
- cbor['parameter'] = field
2603
+ # Serialize the parameters field.
2604
+ field = {'@T': '*'}
2605
+ lst = []
2606
+ for el in self._attr_parameters:
2607
+ el = el._serialize(id_map)
2608
+ el['@T'] = '1'
2609
+ lst.append(el)
2610
+ field['@d'] = lst
2611
+ cbor['parameters'] = field
2330
2612
 
2331
2613
  # Serialize the annotations field.
2332
2614
  field = {'@T': '*'}