libqasm 0.6.6__cp310-cp310-win_amd64.whl → 0.6.8__cp310-cp310-win_amd64.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/__init__.py +71 -22
- cqasm/v3x/ast.py +665 -377
- cqasm/v3x/primitives.py +5 -0
- cqasm/v3x/semantic.py +771 -25
- libqasm/_libqasm.cp310-win_amd64.pyd +0 -0
- libqasm/libqasm.py +8 -8
- libqasm-0.6.8.dist-info/METADATA +128 -0
- libqasm-0.6.8.dist-info/RECORD +16 -0
- {libqasm-0.6.6.dist-info → libqasm-0.6.8.dist-info}/WHEEL +1 -1
- libqasm-0.6.6.dist-info/METADATA +0 -224
- libqasm-0.6.6.dist-info/RECORD +0 -16
- {libqasm-0.6.6.dist-info → libqasm-0.6.8.dist-info}/LICENSE.md +0 -0
- {libqasm-0.6.6.dist-info → libqasm-0.6.8.dist-info}/top_level.txt +0 -0
cqasm/v3x/ast.py
CHANGED
@@ -1166,12 +1166,14 @@ class Annotated(Node):
|
|
1166
1166
|
typ = cbor.get('@t', None)
|
1167
1167
|
if typ is None:
|
1168
1168
|
raise ValueError('type (@t) field is missing from node serialization')
|
1169
|
-
if typ == 'Variable':
|
1170
|
-
return Variable._deserialize(cbor, seq_to_ob, links)
|
1171
1169
|
if typ == 'Gate':
|
1172
1170
|
return Gate._deserialize(cbor, seq_to_ob, links)
|
1173
|
-
if typ == '
|
1174
|
-
return
|
1171
|
+
if typ == 'Variable':
|
1172
|
+
return Variable._deserialize(cbor, seq_to_ob, links)
|
1173
|
+
if typ == 'GateInstruction':
|
1174
|
+
return GateInstruction._deserialize(cbor, seq_to_ob, links)
|
1175
|
+
if typ == 'NonGateInstruction':
|
1176
|
+
return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
|
1175
1177
|
raise ValueError('unknown or unexpected type (@t) found in node serialization')
|
1176
1178
|
|
1177
1179
|
def _serialize(self, id_map):
|
@@ -2522,10 +2524,10 @@ class Statement(Annotated):
|
|
2522
2524
|
raise ValueError('type (@t) field is missing from node serialization')
|
2523
2525
|
if typ == 'Variable':
|
2524
2526
|
return Variable._deserialize(cbor, seq_to_ob, links)
|
2525
|
-
if typ == '
|
2526
|
-
return
|
2527
|
-
if typ == '
|
2528
|
-
return
|
2527
|
+
if typ == 'GateInstruction':
|
2528
|
+
return GateInstruction._deserialize(cbor, seq_to_ob, links)
|
2529
|
+
if typ == 'NonGateInstruction':
|
2530
|
+
return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
|
2529
2531
|
raise ValueError('unknown or unexpected type (@t) found in node serialization')
|
2530
2532
|
|
2531
2533
|
def _serialize(self, id_map):
|
@@ -2584,10 +2586,10 @@ class BlockStatement(Statement):
|
|
2584
2586
|
raise ValueError('type (@t) field is missing from node serialization')
|
2585
2587
|
if typ == 'Variable':
|
2586
2588
|
return Variable._deserialize(cbor, seq_to_ob, links)
|
2587
|
-
if typ == '
|
2588
|
-
return
|
2589
|
-
if typ == '
|
2590
|
-
return
|
2589
|
+
if typ == 'GateInstruction':
|
2590
|
+
return GateInstruction._deserialize(cbor, seq_to_ob, links)
|
2591
|
+
if typ == 'NonGateInstruction':
|
2592
|
+
return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
|
2591
2593
|
raise ValueError('unknown or unexpected type (@t) found in node serialization')
|
2592
2594
|
|
2593
2595
|
def _serialize(self, id_map):
|
@@ -4977,81 +4979,27 @@ class MultiFunctionCall(_Multiple):
|
|
4977
4979
|
|
4978
4980
|
_typemap['FunctionCall'] = FunctionCall
|
4979
4981
|
|
4980
|
-
class
|
4981
|
-
|
4982
|
-
|
4983
|
-
def __init__(
|
4984
|
-
self,
|
4985
|
-
annotations=None,
|
4986
|
-
):
|
4987
|
-
super().__init__(annotations=annotations)
|
4988
|
-
|
4989
|
-
@staticmethod
|
4990
|
-
def _deserialize(cbor, seq_to_ob, links):
|
4991
|
-
"""Attempts to deserialize the given cbor object (in Python primitive
|
4992
|
-
representation) into a node of this type. All (sub)nodes are added to
|
4993
|
-
the seq_to_ob dict, indexed by their cbor sequence number. All links are
|
4994
|
-
registered in the links list by means of a two-tuple of the setter
|
4995
|
-
function for the link field and the sequence number of the target node.
|
4996
|
-
"""
|
4997
|
-
if not isinstance(cbor, dict):
|
4998
|
-
raise TypeError('node description object must be a dict')
|
4999
|
-
typ = cbor.get('@t', None)
|
5000
|
-
if typ is None:
|
5001
|
-
raise ValueError('type (@t) field is missing from node serialization')
|
5002
|
-
if typ == 'Gate':
|
5003
|
-
return Gate._deserialize(cbor, seq_to_ob, links)
|
5004
|
-
if typ == 'MeasureInstruction':
|
5005
|
-
return MeasureInstruction._deserialize(cbor, seq_to_ob, links)
|
5006
|
-
raise ValueError('unknown or unexpected type (@t) found in node serialization')
|
5007
|
-
|
5008
|
-
def _serialize(self, id_map):
|
5009
|
-
"""Serializes this node to the Python primitive representation of its
|
5010
|
-
CBOR serialization. The tree that the node belongs to must be
|
5011
|
-
well-formed. id_map must match Python id() calls for all nodes to unique
|
5012
|
-
integers, to use for the sequence number representation of links."""
|
5013
|
-
cbor = {'@i': id_map[id(self)], '@t': 'Instruction'}
|
5014
|
-
|
5015
|
-
# Serialize the annotations field.
|
5016
|
-
field = {'@T': '*'}
|
5017
|
-
lst = []
|
5018
|
-
for el in self._attr_annotations:
|
5019
|
-
el = el._serialize(id_map)
|
5020
|
-
el['@T'] = '1'
|
5021
|
-
lst.append(el)
|
5022
|
-
field['@d'] = lst
|
5023
|
-
cbor['annotations'] = field
|
5024
|
-
|
5025
|
-
# Serialize annotations.
|
5026
|
-
for key, val in self._annot.items():
|
5027
|
-
cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
|
5028
|
-
|
5029
|
-
return cbor
|
5030
|
-
|
5031
|
-
|
5032
|
-
class MultiInstruction(_Multiple):
|
5033
|
-
"""Wrapper for an edge with multiple Instruction objects."""
|
5034
|
-
|
5035
|
-
_T = Instruction
|
5036
|
-
|
5037
|
-
|
5038
|
-
_typemap['Instruction'] = Instruction
|
4982
|
+
class Gate(Annotated):
|
4983
|
+
"""A gate can be a named gate or a composition of gate modifiers acting on a
|
4984
|
+
gate."""
|
5039
4985
|
|
5040
|
-
class Gate(Instruction):
|
5041
4986
|
__slots__ = [
|
5042
4987
|
'_attr_name',
|
5043
|
-
'
|
4988
|
+
'_attr_gate',
|
4989
|
+
'_attr_parameter',
|
5044
4990
|
]
|
5045
4991
|
|
5046
4992
|
def __init__(
|
5047
4993
|
self,
|
5048
4994
|
name=None,
|
5049
|
-
|
4995
|
+
gate=None,
|
4996
|
+
parameter=None,
|
5050
4997
|
annotations=None,
|
5051
4998
|
):
|
5052
4999
|
super().__init__(annotations=annotations)
|
5053
5000
|
self.name = name
|
5054
|
-
self.
|
5001
|
+
self.gate = gate
|
5002
|
+
self.parameter = parameter
|
5055
5003
|
|
5056
5004
|
@property
|
5057
5005
|
def name(self):
|
@@ -5074,24 +5022,44 @@ class Gate(Instruction):
|
|
5074
5022
|
self._attr_name = None
|
5075
5023
|
|
5076
5024
|
@property
|
5077
|
-
def
|
5078
|
-
return self.
|
5025
|
+
def gate(self):
|
5026
|
+
return self._attr_gate
|
5079
5027
|
|
5080
|
-
@
|
5081
|
-
def
|
5028
|
+
@gate.setter
|
5029
|
+
def gate(self, val):
|
5082
5030
|
if val is None:
|
5083
|
-
del self.
|
5031
|
+
del self.gate
|
5084
5032
|
return
|
5085
|
-
if not isinstance(val,
|
5033
|
+
if not isinstance(val, Gate):
|
5086
5034
|
# Try to "typecast" if this isn't an obvious mistake.
|
5087
5035
|
if isinstance(val, Node):
|
5088
|
-
raise TypeError('
|
5089
|
-
val =
|
5090
|
-
self.
|
5036
|
+
raise TypeError('gate must be of type Gate')
|
5037
|
+
val = Gate(val)
|
5038
|
+
self._attr_gate = val
|
5091
5039
|
|
5092
|
-
@
|
5093
|
-
def
|
5094
|
-
self.
|
5040
|
+
@gate.deleter
|
5041
|
+
def gate(self):
|
5042
|
+
self._attr_gate = None
|
5043
|
+
|
5044
|
+
@property
|
5045
|
+
def parameter(self):
|
5046
|
+
return self._attr_parameter
|
5047
|
+
|
5048
|
+
@parameter.setter
|
5049
|
+
def parameter(self, val):
|
5050
|
+
if val is None:
|
5051
|
+
del self.parameter
|
5052
|
+
return
|
5053
|
+
if not isinstance(val, Expression):
|
5054
|
+
# Try to "typecast" if this isn't an obvious mistake.
|
5055
|
+
if isinstance(val, Node):
|
5056
|
+
raise TypeError('parameter must be of type Expression')
|
5057
|
+
val = Expression(val)
|
5058
|
+
self._attr_parameter = val
|
5059
|
+
|
5060
|
+
@parameter.deleter
|
5061
|
+
def parameter(self):
|
5062
|
+
self._attr_parameter = None
|
5095
5063
|
|
5096
5064
|
def __eq__(self, other):
|
5097
5065
|
"""Equality operator. Ignores annotations!"""
|
@@ -5099,7 +5067,9 @@ class Gate(Instruction):
|
|
5099
5067
|
return False
|
5100
5068
|
if self.name != other.name:
|
5101
5069
|
return False
|
5102
|
-
if self.
|
5070
|
+
if self.gate != other.gate:
|
5071
|
+
return False
|
5072
|
+
if self.parameter != other.parameter:
|
5103
5073
|
return False
|
5104
5074
|
if self.annotations != other.annotations:
|
5105
5075
|
return False
|
@@ -5129,12 +5099,20 @@ class Gate(Instruction):
|
|
5129
5099
|
s.append(self.name.dump(indent + 1, annotations, links) + '\n')
|
5130
5100
|
s.append(' '*indent + '>\n')
|
5131
5101
|
s.append(' '*indent)
|
5132
|
-
s.append('
|
5133
|
-
if self.
|
5134
|
-
s.append('
|
5102
|
+
s.append('gate: ')
|
5103
|
+
if self.gate is None:
|
5104
|
+
s.append('-\n')
|
5135
5105
|
else:
|
5136
5106
|
s.append('<\n')
|
5137
|
-
s.append(self.
|
5107
|
+
s.append(self.gate.dump(indent + 1, annotations, links) + '\n')
|
5108
|
+
s.append(' '*indent + '>\n')
|
5109
|
+
s.append(' '*indent)
|
5110
|
+
s.append('parameter: ')
|
5111
|
+
if self.parameter is None:
|
5112
|
+
s.append('-\n')
|
5113
|
+
else:
|
5114
|
+
s.append('<\n')
|
5115
|
+
s.append(self.parameter.dump(indent + 1, annotations, links) + '\n')
|
5138
5116
|
s.append(' '*indent + '>\n')
|
5139
5117
|
s.append(' '*indent)
|
5140
5118
|
s.append('annotations: ')
|
@@ -5164,8 +5142,10 @@ class Gate(Instruction):
|
|
5164
5142
|
id_map[id(self)] = len(id_map)
|
5165
5143
|
if self._attr_name is not None:
|
5166
5144
|
self._attr_name.find_reachable(id_map)
|
5167
|
-
if self.
|
5168
|
-
self.
|
5145
|
+
if self._attr_gate is not None:
|
5146
|
+
self._attr_gate.find_reachable(id_map)
|
5147
|
+
if self._attr_parameter is not None:
|
5148
|
+
self._attr_parameter.find_reachable(id_map)
|
5169
5149
|
for el in self._attr_annotations:
|
5170
5150
|
el.find_reachable(id_map)
|
5171
5151
|
return id_map
|
@@ -5181,10 +5161,10 @@ class Gate(Instruction):
|
|
5181
5161
|
raise NotWellFormed('name is required but not set')
|
5182
5162
|
if self._attr_name is not None:
|
5183
5163
|
self._attr_name.check_complete(id_map)
|
5184
|
-
if self.
|
5185
|
-
|
5186
|
-
if self.
|
5187
|
-
self.
|
5164
|
+
if self._attr_gate is not None:
|
5165
|
+
self._attr_gate.check_complete(id_map)
|
5166
|
+
if self._attr_parameter is not None:
|
5167
|
+
self._attr_parameter.check_complete(id_map)
|
5188
5168
|
for child in self._attr_annotations:
|
5189
5169
|
child.check_complete(id_map)
|
5190
5170
|
|
@@ -5192,7 +5172,8 @@ class Gate(Instruction):
|
|
5192
5172
|
"""Returns a shallow copy of this node."""
|
5193
5173
|
return Gate(
|
5194
5174
|
name=self._attr_name,
|
5195
|
-
|
5175
|
+
gate=self._attr_gate,
|
5176
|
+
parameter=self._attr_parameter,
|
5196
5177
|
annotations=self._attr_annotations.copy()
|
5197
5178
|
)
|
5198
5179
|
|
@@ -5204,7 +5185,8 @@ class Gate(Instruction):
|
|
5204
5185
|
the stdlib instead, which should copy links correctly."""
|
5205
5186
|
return Gate(
|
5206
5187
|
name=_cloned(self._attr_name),
|
5207
|
-
|
5188
|
+
gate=_cloned(self._attr_gate),
|
5189
|
+
parameter=_cloned(self._attr_parameter),
|
5208
5190
|
annotations=_cloned(self._attr_annotations)
|
5209
5191
|
)
|
5210
5192
|
|
@@ -5235,16 +5217,27 @@ class Gate(Instruction):
|
|
5235
5217
|
else:
|
5236
5218
|
f_name = Identifier._deserialize(field, seq_to_ob, links)
|
5237
5219
|
|
5238
|
-
# Deserialize the
|
5239
|
-
field = cbor.get('
|
5220
|
+
# Deserialize the gate field.
|
5221
|
+
field = cbor.get('gate', None)
|
5240
5222
|
if not isinstance(field, dict):
|
5241
|
-
raise ValueError('missing or invalid serialization of field
|
5242
|
-
if field.get('@T') != '
|
5243
|
-
raise ValueError('unexpected edge type for field
|
5223
|
+
raise ValueError('missing or invalid serialization of field gate')
|
5224
|
+
if field.get('@T') != '?':
|
5225
|
+
raise ValueError('unexpected edge type for field gate')
|
5244
5226
|
if field.get('@t', None) is None:
|
5245
|
-
|
5227
|
+
f_gate = None
|
5246
5228
|
else:
|
5247
|
-
|
5229
|
+
f_gate = Gate._deserialize(field, seq_to_ob, links)
|
5230
|
+
|
5231
|
+
# Deserialize the parameter field.
|
5232
|
+
field = cbor.get('parameter', None)
|
5233
|
+
if not isinstance(field, dict):
|
5234
|
+
raise ValueError('missing or invalid serialization of field parameter')
|
5235
|
+
if field.get('@T') != '?':
|
5236
|
+
raise ValueError('unexpected edge type for field parameter')
|
5237
|
+
if field.get('@t', None) is None:
|
5238
|
+
f_parameter = None
|
5239
|
+
else:
|
5240
|
+
f_parameter = Expression._deserialize(field, seq_to_ob, links)
|
5248
5241
|
|
5249
5242
|
# Deserialize the annotations field.
|
5250
5243
|
field = cbor.get('annotations', None)
|
@@ -5262,7 +5255,7 @@ class Gate(Instruction):
|
|
5262
5255
|
f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
|
5263
5256
|
|
5264
5257
|
# Construct the Gate node.
|
5265
|
-
node = Gate(f_name,
|
5258
|
+
node = Gate(f_name, f_gate, f_parameter, f_annotations)
|
5266
5259
|
|
5267
5260
|
# Deserialize annotations.
|
5268
5261
|
for key, val in cbor.items():
|
@@ -5296,13 +5289,21 @@ class Gate(Instruction):
|
|
5296
5289
|
field.update(self._attr_name._serialize(id_map))
|
5297
5290
|
cbor['name'] = field
|
5298
5291
|
|
5299
|
-
# Serialize the
|
5300
|
-
field = {'@T': '
|
5301
|
-
if self.
|
5292
|
+
# Serialize the gate field.
|
5293
|
+
field = {'@T': '?'}
|
5294
|
+
if self._attr_gate is None:
|
5302
5295
|
field['@t'] = None
|
5303
5296
|
else:
|
5304
|
-
field.update(self.
|
5305
|
-
cbor['
|
5297
|
+
field.update(self._attr_gate._serialize(id_map))
|
5298
|
+
cbor['gate'] = field
|
5299
|
+
|
5300
|
+
# Serialize the parameter field.
|
5301
|
+
field = {'@T': '?'}
|
5302
|
+
if self._attr_parameter is None:
|
5303
|
+
field['@t'] = None
|
5304
|
+
else:
|
5305
|
+
field.update(self._attr_parameter._serialize(id_map))
|
5306
|
+
cbor['parameter'] = field
|
5306
5307
|
|
5307
5308
|
# Serialize the annotations field.
|
5308
5309
|
field = {'@T': '*'}
|
@@ -5329,43 +5330,131 @@ class MultiGate(_Multiple):
|
|
5329
5330
|
|
5330
5331
|
_typemap['Gate'] = Gate
|
5331
5332
|
|
5332
|
-
class
|
5333
|
+
class Instruction(BlockStatement):
|
5334
|
+
__slots__ = []
|
5335
|
+
|
5336
|
+
def __init__(
|
5337
|
+
self,
|
5338
|
+
annotations=None,
|
5339
|
+
):
|
5340
|
+
super().__init__(annotations=annotations)
|
5341
|
+
|
5342
|
+
@staticmethod
|
5343
|
+
def _deserialize(cbor, seq_to_ob, links):
|
5344
|
+
"""Attempts to deserialize the given cbor object (in Python primitive
|
5345
|
+
representation) into a node of this type. All (sub)nodes are added to
|
5346
|
+
the seq_to_ob dict, indexed by their cbor sequence number. All links are
|
5347
|
+
registered in the links list by means of a two-tuple of the setter
|
5348
|
+
function for the link field and the sequence number of the target node.
|
5349
|
+
"""
|
5350
|
+
if not isinstance(cbor, dict):
|
5351
|
+
raise TypeError('node description object must be a dict')
|
5352
|
+
typ = cbor.get('@t', None)
|
5353
|
+
if typ is None:
|
5354
|
+
raise ValueError('type (@t) field is missing from node serialization')
|
5355
|
+
if typ == 'GateInstruction':
|
5356
|
+
return GateInstruction._deserialize(cbor, seq_to_ob, links)
|
5357
|
+
if typ == 'NonGateInstruction':
|
5358
|
+
return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
|
5359
|
+
raise ValueError('unknown or unexpected type (@t) found in node serialization')
|
5360
|
+
|
5361
|
+
def _serialize(self, id_map):
|
5362
|
+
"""Serializes this node to the Python primitive representation of its
|
5363
|
+
CBOR serialization. The tree that the node belongs to must be
|
5364
|
+
well-formed. id_map must match Python id() calls for all nodes to unique
|
5365
|
+
integers, to use for the sequence number representation of links."""
|
5366
|
+
cbor = {'@i': id_map[id(self)], '@t': 'Instruction'}
|
5367
|
+
|
5368
|
+
# Serialize the annotations field.
|
5369
|
+
field = {'@T': '*'}
|
5370
|
+
lst = []
|
5371
|
+
for el in self._attr_annotations:
|
5372
|
+
el = el._serialize(id_map)
|
5373
|
+
el['@T'] = '1'
|
5374
|
+
lst.append(el)
|
5375
|
+
field['@d'] = lst
|
5376
|
+
cbor['annotations'] = field
|
5377
|
+
|
5378
|
+
# Serialize annotations.
|
5379
|
+
for key, val in self._annot.items():
|
5380
|
+
cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
|
5381
|
+
|
5382
|
+
return cbor
|
5383
|
+
|
5384
|
+
|
5385
|
+
class MultiInstruction(_Multiple):
|
5386
|
+
"""Wrapper for an edge with multiple Instruction objects."""
|
5387
|
+
|
5388
|
+
_T = Instruction
|
5389
|
+
|
5390
|
+
|
5391
|
+
_typemap['Instruction'] = Instruction
|
5392
|
+
|
5393
|
+
class GateInstruction(Instruction):
|
5333
5394
|
__slots__ = [
|
5334
|
-
'
|
5395
|
+
'_attr_gate',
|
5396
|
+
'_attr_operands',
|
5335
5397
|
]
|
5336
5398
|
|
5337
5399
|
def __init__(
|
5338
5400
|
self,
|
5339
|
-
|
5401
|
+
gate=None,
|
5402
|
+
operands=None,
|
5403
|
+
annotations=None,
|
5340
5404
|
):
|
5341
|
-
super().__init__()
|
5342
|
-
self.
|
5405
|
+
super().__init__(annotations=annotations)
|
5406
|
+
self.gate = gate
|
5407
|
+
self.operands = operands
|
5343
5408
|
|
5344
5409
|
@property
|
5345
|
-
def
|
5346
|
-
return self.
|
5410
|
+
def gate(self):
|
5411
|
+
return self._attr_gate
|
5347
5412
|
|
5348
|
-
@
|
5349
|
-
def
|
5413
|
+
@gate.setter
|
5414
|
+
def gate(self, val):
|
5350
5415
|
if val is None:
|
5351
|
-
del self.
|
5416
|
+
del self.gate
|
5352
5417
|
return
|
5353
|
-
if not isinstance(val,
|
5418
|
+
if not isinstance(val, Gate):
|
5354
5419
|
# Try to "typecast" if this isn't an obvious mistake.
|
5355
5420
|
if isinstance(val, Node):
|
5356
|
-
raise TypeError('
|
5357
|
-
val =
|
5358
|
-
self.
|
5421
|
+
raise TypeError('gate must be of type Gate')
|
5422
|
+
val = Gate(val)
|
5423
|
+
self._attr_gate = val
|
5359
5424
|
|
5360
|
-
@
|
5361
|
-
def
|
5362
|
-
self.
|
5425
|
+
@gate.deleter
|
5426
|
+
def gate(self):
|
5427
|
+
self._attr_gate = None
|
5428
|
+
|
5429
|
+
@property
|
5430
|
+
def operands(self):
|
5431
|
+
return self._attr_operands
|
5432
|
+
|
5433
|
+
@operands.setter
|
5434
|
+
def operands(self, val):
|
5435
|
+
if val is None:
|
5436
|
+
del self.operands
|
5437
|
+
return
|
5438
|
+
if not isinstance(val, ExpressionList):
|
5439
|
+
# Try to "typecast" if this isn't an obvious mistake.
|
5440
|
+
if isinstance(val, Node):
|
5441
|
+
raise TypeError('operands must be of type ExpressionList')
|
5442
|
+
val = ExpressionList(val)
|
5443
|
+
self._attr_operands = val
|
5444
|
+
|
5445
|
+
@operands.deleter
|
5446
|
+
def operands(self):
|
5447
|
+
self._attr_operands = None
|
5363
5448
|
|
5364
5449
|
def __eq__(self, other):
|
5365
5450
|
"""Equality operator. Ignores annotations!"""
|
5366
|
-
if not isinstance(other,
|
5451
|
+
if not isinstance(other, GateInstruction):
|
5367
5452
|
return False
|
5368
|
-
if self.
|
5453
|
+
if self.gate != other.gate:
|
5454
|
+
return False
|
5455
|
+
if self.operands != other.operands:
|
5456
|
+
return False
|
5457
|
+
if self.annotations != other.annotations:
|
5369
5458
|
return False
|
5370
5459
|
return True
|
5371
5460
|
|
@@ -5376,7 +5465,7 @@ class GlobalBlock(Block):
|
|
5376
5465
|
strings of the annotations that are to be printed. links specifies the
|
5377
5466
|
maximum link recursion depth."""
|
5378
5467
|
s = [' '*indent]
|
5379
|
-
s.append('
|
5468
|
+
s.append('GateInstruction(')
|
5380
5469
|
if annotations is None:
|
5381
5470
|
annotations = []
|
5382
5471
|
for key in annotations:
|
@@ -5385,12 +5474,276 @@ class GlobalBlock(Block):
|
|
5385
5474
|
s.append('\n')
|
5386
5475
|
indent += 1
|
5387
5476
|
s.append(' '*indent)
|
5388
|
-
s.append('
|
5389
|
-
if
|
5477
|
+
s.append('gate: ')
|
5478
|
+
if self.gate is None:
|
5479
|
+
s.append('!MISSING\n')
|
5480
|
+
else:
|
5481
|
+
s.append('<\n')
|
5482
|
+
s.append(self.gate.dump(indent + 1, annotations, links) + '\n')
|
5483
|
+
s.append(' '*indent + '>\n')
|
5484
|
+
s.append(' '*indent)
|
5485
|
+
s.append('operands: ')
|
5486
|
+
if self.operands is None:
|
5487
|
+
s.append('!MISSING\n')
|
5488
|
+
else:
|
5489
|
+
s.append('<\n')
|
5490
|
+
s.append(self.operands.dump(indent + 1, annotations, links) + '\n')
|
5491
|
+
s.append(' '*indent + '>\n')
|
5492
|
+
s.append(' '*indent)
|
5493
|
+
s.append('annotations: ')
|
5494
|
+
if not self.annotations:
|
5390
5495
|
s.append('-\n')
|
5391
5496
|
else:
|
5392
5497
|
s.append('[\n')
|
5393
|
-
for child in self.
|
5498
|
+
for child in self.annotations:
|
5499
|
+
s.append(child.dump(indent + 1, annotations, links) + '\n')
|
5500
|
+
s.append(' '*indent + ']\n')
|
5501
|
+
indent -= 1
|
5502
|
+
s.append(' '*indent)
|
5503
|
+
s.append(')')
|
5504
|
+
return ''.join(s)
|
5505
|
+
|
5506
|
+
__str__ = dump
|
5507
|
+
__repr__ = dump
|
5508
|
+
|
5509
|
+
def find_reachable(self, id_map=None):
|
5510
|
+
"""Returns a dictionary mapping Python id() values to stable sequence
|
5511
|
+
numbers for all nodes in the tree rooted at this node. If id_map is
|
5512
|
+
specified, found nodes are appended to it."""
|
5513
|
+
if id_map is None:
|
5514
|
+
id_map = {}
|
5515
|
+
if id(self) in id_map:
|
5516
|
+
raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
|
5517
|
+
id_map[id(self)] = len(id_map)
|
5518
|
+
if self._attr_gate is not None:
|
5519
|
+
self._attr_gate.find_reachable(id_map)
|
5520
|
+
if self._attr_operands is not None:
|
5521
|
+
self._attr_operands.find_reachable(id_map)
|
5522
|
+
for el in self._attr_annotations:
|
5523
|
+
el.find_reachable(id_map)
|
5524
|
+
return id_map
|
5525
|
+
|
5526
|
+
def check_complete(self, id_map=None):
|
5527
|
+
"""Raises NotWellFormed if the tree rooted at this node is not
|
5528
|
+
well-formed. If id_map is specified, this tree is only a subtree in the
|
5529
|
+
context of a larger tree, and id_map must be a dict mapping from Python
|
5530
|
+
id() codes to tree indices for all reachable nodes."""
|
5531
|
+
if id_map is None:
|
5532
|
+
id_map = self.find_reachable()
|
5533
|
+
if self._attr_gate is None:
|
5534
|
+
raise NotWellFormed('gate is required but not set')
|
5535
|
+
if self._attr_gate is not None:
|
5536
|
+
self._attr_gate.check_complete(id_map)
|
5537
|
+
if self._attr_operands is None:
|
5538
|
+
raise NotWellFormed('operands is required but not set')
|
5539
|
+
if self._attr_operands is not None:
|
5540
|
+
self._attr_operands.check_complete(id_map)
|
5541
|
+
for child in self._attr_annotations:
|
5542
|
+
child.check_complete(id_map)
|
5543
|
+
|
5544
|
+
def copy(self):
|
5545
|
+
"""Returns a shallow copy of this node."""
|
5546
|
+
return GateInstruction(
|
5547
|
+
gate=self._attr_gate,
|
5548
|
+
operands=self._attr_operands,
|
5549
|
+
annotations=self._attr_annotations.copy()
|
5550
|
+
)
|
5551
|
+
|
5552
|
+
def clone(self):
|
5553
|
+
"""Returns a deep copy of this node. This mimics the C++ interface,
|
5554
|
+
deficiencies with links included; that is, links always point to the
|
5555
|
+
original tree. If you're not cloning a subtree in a context where this
|
5556
|
+
is the desired behavior, you may want to use the copy.deepcopy() from
|
5557
|
+
the stdlib instead, which should copy links correctly."""
|
5558
|
+
return GateInstruction(
|
5559
|
+
gate=_cloned(self._attr_gate),
|
5560
|
+
operands=_cloned(self._attr_operands),
|
5561
|
+
annotations=_cloned(self._attr_annotations)
|
5562
|
+
)
|
5563
|
+
|
5564
|
+
@staticmethod
|
5565
|
+
def _deserialize(cbor, seq_to_ob, links):
|
5566
|
+
"""Attempts to deserialize the given cbor object (in Python primitive
|
5567
|
+
representation) into a node of this type. All (sub)nodes are added to
|
5568
|
+
the seq_to_ob dict, indexed by their cbor sequence number. All links are
|
5569
|
+
registered in the links list by means of a two-tuple of the setter
|
5570
|
+
function for the link field and the sequence number of the target node.
|
5571
|
+
"""
|
5572
|
+
if not isinstance(cbor, dict):
|
5573
|
+
raise TypeError('node description object must be a dict')
|
5574
|
+
typ = cbor.get('@t', None)
|
5575
|
+
if typ is None:
|
5576
|
+
raise ValueError('type (@t) field is missing from node serialization')
|
5577
|
+
if typ != 'GateInstruction':
|
5578
|
+
raise ValueError('found node serialization for ' + typ + ', but expected GateInstruction')
|
5579
|
+
|
5580
|
+
# Deserialize the gate field.
|
5581
|
+
field = cbor.get('gate', None)
|
5582
|
+
if not isinstance(field, dict):
|
5583
|
+
raise ValueError('missing or invalid serialization of field gate')
|
5584
|
+
if field.get('@T') != '1':
|
5585
|
+
raise ValueError('unexpected edge type for field gate')
|
5586
|
+
if field.get('@t', None) is None:
|
5587
|
+
f_gate = None
|
5588
|
+
else:
|
5589
|
+
f_gate = Gate._deserialize(field, seq_to_ob, links)
|
5590
|
+
|
5591
|
+
# Deserialize the operands field.
|
5592
|
+
field = cbor.get('operands', None)
|
5593
|
+
if not isinstance(field, dict):
|
5594
|
+
raise ValueError('missing or invalid serialization of field operands')
|
5595
|
+
if field.get('@T') != '1':
|
5596
|
+
raise ValueError('unexpected edge type for field operands')
|
5597
|
+
if field.get('@t', None) is None:
|
5598
|
+
f_operands = None
|
5599
|
+
else:
|
5600
|
+
f_operands = ExpressionList._deserialize(field, seq_to_ob, links)
|
5601
|
+
|
5602
|
+
# Deserialize the annotations field.
|
5603
|
+
field = cbor.get('annotations', None)
|
5604
|
+
if not isinstance(field, dict):
|
5605
|
+
raise ValueError('missing or invalid serialization of field annotations')
|
5606
|
+
if field.get('@T') != '*':
|
5607
|
+
raise ValueError('unexpected edge type for field annotations')
|
5608
|
+
data = field.get('@d', None)
|
5609
|
+
if not isinstance(data, list):
|
5610
|
+
raise ValueError('missing serialization of Any/Many contents')
|
5611
|
+
f_annotations = MultiAnnotationData()
|
5612
|
+
for element in data:
|
5613
|
+
if element.get('@T') != '1':
|
5614
|
+
raise ValueError('unexpected edge type for Any/Many element')
|
5615
|
+
f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
|
5616
|
+
|
5617
|
+
# Construct the GateInstruction node.
|
5618
|
+
node = GateInstruction(f_gate, f_operands, f_annotations)
|
5619
|
+
|
5620
|
+
# Deserialize annotations.
|
5621
|
+
for key, val in cbor.items():
|
5622
|
+
if not (key.startswith('{') and key.endswith('}')):
|
5623
|
+
continue
|
5624
|
+
key = key[1:-1]
|
5625
|
+
node[key] = cqasm.v3x.primitives.deserialize(key, val)
|
5626
|
+
|
5627
|
+
# Register node in sequence number lookup.
|
5628
|
+
seq = cbor.get('@i', None)
|
5629
|
+
if not isinstance(seq, int):
|
5630
|
+
raise ValueError('sequence number field (@i) is not an integer or missing from node serialization')
|
5631
|
+
if seq in seq_to_ob:
|
5632
|
+
raise ValueError('duplicate sequence number %d' % seq)
|
5633
|
+
seq_to_ob[seq] = node
|
5634
|
+
|
5635
|
+
return node
|
5636
|
+
|
5637
|
+
def _serialize(self, id_map):
|
5638
|
+
"""Serializes this node to the Python primitive representation of its
|
5639
|
+
CBOR serialization. The tree that the node belongs to must be
|
5640
|
+
well-formed. id_map must match Python id() calls for all nodes to unique
|
5641
|
+
integers, to use for the sequence number representation of links."""
|
5642
|
+
cbor = {'@i': id_map[id(self)], '@t': 'GateInstruction'}
|
5643
|
+
|
5644
|
+
# Serialize the gate field.
|
5645
|
+
field = {'@T': '1'}
|
5646
|
+
if self._attr_gate is None:
|
5647
|
+
field['@t'] = None
|
5648
|
+
else:
|
5649
|
+
field.update(self._attr_gate._serialize(id_map))
|
5650
|
+
cbor['gate'] = field
|
5651
|
+
|
5652
|
+
# Serialize the operands field.
|
5653
|
+
field = {'@T': '1'}
|
5654
|
+
if self._attr_operands is None:
|
5655
|
+
field['@t'] = None
|
5656
|
+
else:
|
5657
|
+
field.update(self._attr_operands._serialize(id_map))
|
5658
|
+
cbor['operands'] = field
|
5659
|
+
|
5660
|
+
# Serialize the annotations field.
|
5661
|
+
field = {'@T': '*'}
|
5662
|
+
lst = []
|
5663
|
+
for el in self._attr_annotations:
|
5664
|
+
el = el._serialize(id_map)
|
5665
|
+
el['@T'] = '1'
|
5666
|
+
lst.append(el)
|
5667
|
+
field['@d'] = lst
|
5668
|
+
cbor['annotations'] = field
|
5669
|
+
|
5670
|
+
# Serialize annotations.
|
5671
|
+
for key, val in self._annot.items():
|
5672
|
+
cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
|
5673
|
+
|
5674
|
+
return cbor
|
5675
|
+
|
5676
|
+
|
5677
|
+
class MultiGateInstruction(_Multiple):
|
5678
|
+
"""Wrapper for an edge with multiple GateInstruction objects."""
|
5679
|
+
|
5680
|
+
_T = GateInstruction
|
5681
|
+
|
5682
|
+
|
5683
|
+
_typemap['GateInstruction'] = GateInstruction
|
5684
|
+
|
5685
|
+
class GlobalBlock(Block):
|
5686
|
+
__slots__ = [
|
5687
|
+
'_attr_statements',
|
5688
|
+
]
|
5689
|
+
|
5690
|
+
def __init__(
|
5691
|
+
self,
|
5692
|
+
statements=None,
|
5693
|
+
):
|
5694
|
+
super().__init__()
|
5695
|
+
self.statements = statements
|
5696
|
+
|
5697
|
+
@property
|
5698
|
+
def statements(self):
|
5699
|
+
return self._attr_statements
|
5700
|
+
|
5701
|
+
@statements.setter
|
5702
|
+
def statements(self, val):
|
5703
|
+
if val is None:
|
5704
|
+
del self.statements
|
5705
|
+
return
|
5706
|
+
if not isinstance(val, MultiStatement):
|
5707
|
+
# Try to "typecast" if this isn't an obvious mistake.
|
5708
|
+
if isinstance(val, Node):
|
5709
|
+
raise TypeError('statements must be of type MultiStatement')
|
5710
|
+
val = MultiStatement(val)
|
5711
|
+
self._attr_statements = val
|
5712
|
+
|
5713
|
+
@statements.deleter
|
5714
|
+
def statements(self):
|
5715
|
+
self._attr_statements = MultiStatement()
|
5716
|
+
|
5717
|
+
def __eq__(self, other):
|
5718
|
+
"""Equality operator. Ignores annotations!"""
|
5719
|
+
if not isinstance(other, GlobalBlock):
|
5720
|
+
return False
|
5721
|
+
if self.statements != other.statements:
|
5722
|
+
return False
|
5723
|
+
return True
|
5724
|
+
|
5725
|
+
def dump(self, indent=0, annotations=None, links=1):
|
5726
|
+
"""Returns a debug representation of this tree as a multiline string.
|
5727
|
+
indent is the number of double spaces prefixed before every line.
|
5728
|
+
annotations, if specified, must be a set-like object containing the key
|
5729
|
+
strings of the annotations that are to be printed. links specifies the
|
5730
|
+
maximum link recursion depth."""
|
5731
|
+
s = [' '*indent]
|
5732
|
+
s.append('GlobalBlock(')
|
5733
|
+
if annotations is None:
|
5734
|
+
annotations = []
|
5735
|
+
for key in annotations:
|
5736
|
+
if key in self:
|
5737
|
+
s.append(' # {}: {}'.format(key, self[key]))
|
5738
|
+
s.append('\n')
|
5739
|
+
indent += 1
|
5740
|
+
s.append(' '*indent)
|
5741
|
+
s.append('statements: ')
|
5742
|
+
if not self.statements:
|
5743
|
+
s.append('-\n')
|
5744
|
+
else:
|
5745
|
+
s.append('[\n')
|
5746
|
+
for child in self.statements:
|
5394
5747
|
s.append(child.dump(indent + 1, annotations, links) + '\n')
|
5395
5748
|
s.append(' '*indent + ']\n')
|
5396
5749
|
indent -= 1
|
@@ -7826,103 +8179,25 @@ class MultiLogicalXorExpression(_Multiple):
|
|
7826
8179
|
|
7827
8180
|
_typemap['LogicalXorExpression'] = LogicalXorExpression
|
7828
8181
|
|
7829
|
-
class
|
7830
|
-
|
7831
|
-
differentiate between left hand side and right and side operands. For a
|
7832
|
-
measure instruction, the right hand side operand should be a qubit, and the
|
7833
|
-
left hand side operand a bit."""
|
7834
|
-
|
7835
|
-
__slots__ = [
|
7836
|
-
'_attr_name',
|
7837
|
-
'_attr_lhs',
|
7838
|
-
'_attr_rhs',
|
7839
|
-
]
|
8182
|
+
class ModuloExpression(ArithmeticExpression):
|
8183
|
+
__slots__ = []
|
7840
8184
|
|
7841
8185
|
def __init__(
|
7842
8186
|
self,
|
7843
|
-
name=None,
|
7844
8187
|
lhs=None,
|
7845
8188
|
rhs=None,
|
7846
|
-
annotations=None,
|
7847
8189
|
):
|
7848
|
-
super().__init__(
|
7849
|
-
self.name = name
|
7850
|
-
self.lhs = lhs
|
7851
|
-
self.rhs = rhs
|
8190
|
+
super().__init__(lhs=lhs, rhs=rhs)
|
7852
8191
|
|
7853
|
-
|
7854
|
-
|
7855
|
-
|
7856
|
-
|
7857
|
-
|
7858
|
-
|
7859
|
-
if
|
7860
|
-
|
7861
|
-
|
7862
|
-
if not isinstance(val, Identifier):
|
7863
|
-
# Try to "typecast" if this isn't an obvious mistake.
|
7864
|
-
if isinstance(val, Node):
|
7865
|
-
raise TypeError('name must be of type Identifier')
|
7866
|
-
val = Identifier(val)
|
7867
|
-
self._attr_name = val
|
7868
|
-
|
7869
|
-
@name.deleter
|
7870
|
-
def name(self):
|
7871
|
-
self._attr_name = None
|
7872
|
-
|
7873
|
-
@property
|
7874
|
-
def lhs(self):
|
7875
|
-
return self._attr_lhs
|
7876
|
-
|
7877
|
-
@lhs.setter
|
7878
|
-
def lhs(self, val):
|
7879
|
-
if val is None:
|
7880
|
-
del self.lhs
|
7881
|
-
return
|
7882
|
-
if not isinstance(val, Expression):
|
7883
|
-
# Try to "typecast" if this isn't an obvious mistake.
|
7884
|
-
if isinstance(val, Node):
|
7885
|
-
raise TypeError('lhs must be of type Expression')
|
7886
|
-
val = Expression(val)
|
7887
|
-
self._attr_lhs = val
|
7888
|
-
|
7889
|
-
@lhs.deleter
|
7890
|
-
def lhs(self):
|
7891
|
-
self._attr_lhs = None
|
7892
|
-
|
7893
|
-
@property
|
7894
|
-
def rhs(self):
|
7895
|
-
return self._attr_rhs
|
7896
|
-
|
7897
|
-
@rhs.setter
|
7898
|
-
def rhs(self, val):
|
7899
|
-
if val is None:
|
7900
|
-
del self.rhs
|
7901
|
-
return
|
7902
|
-
if not isinstance(val, Expression):
|
7903
|
-
# Try to "typecast" if this isn't an obvious mistake.
|
7904
|
-
if isinstance(val, Node):
|
7905
|
-
raise TypeError('rhs must be of type Expression')
|
7906
|
-
val = Expression(val)
|
7907
|
-
self._attr_rhs = val
|
7908
|
-
|
7909
|
-
@rhs.deleter
|
7910
|
-
def rhs(self):
|
7911
|
-
self._attr_rhs = None
|
7912
|
-
|
7913
|
-
def __eq__(self, other):
|
7914
|
-
"""Equality operator. Ignores annotations!"""
|
7915
|
-
if not isinstance(other, MeasureInstruction):
|
7916
|
-
return False
|
7917
|
-
if self.name != other.name:
|
7918
|
-
return False
|
7919
|
-
if self.lhs != other.lhs:
|
7920
|
-
return False
|
7921
|
-
if self.rhs != other.rhs:
|
7922
|
-
return False
|
7923
|
-
if self.annotations != other.annotations:
|
7924
|
-
return False
|
7925
|
-
return True
|
8192
|
+
def __eq__(self, other):
|
8193
|
+
"""Equality operator. Ignores annotations!"""
|
8194
|
+
if not isinstance(other, ModuloExpression):
|
8195
|
+
return False
|
8196
|
+
if self.lhs != other.lhs:
|
8197
|
+
return False
|
8198
|
+
if self.rhs != other.rhs:
|
8199
|
+
return False
|
8200
|
+
return True
|
7926
8201
|
|
7927
8202
|
def dump(self, indent=0, annotations=None, links=1):
|
7928
8203
|
"""Returns a debug representation of this tree as a multiline string.
|
@@ -7931,7 +8206,7 @@ class MeasureInstruction(Instruction):
|
|
7931
8206
|
strings of the annotations that are to be printed. links specifies the
|
7932
8207
|
maximum link recursion depth."""
|
7933
8208
|
s = [' '*indent]
|
7934
|
-
s.append('
|
8209
|
+
s.append('ModuloExpression(')
|
7935
8210
|
if annotations is None:
|
7936
8211
|
annotations = []
|
7937
8212
|
for key in annotations:
|
@@ -7940,14 +8215,6 @@ class MeasureInstruction(Instruction):
|
|
7940
8215
|
s.append('\n')
|
7941
8216
|
indent += 1
|
7942
8217
|
s.append(' '*indent)
|
7943
|
-
s.append('name: ')
|
7944
|
-
if self.name is None:
|
7945
|
-
s.append('!MISSING\n')
|
7946
|
-
else:
|
7947
|
-
s.append('<\n')
|
7948
|
-
s.append(self.name.dump(indent + 1, annotations, links) + '\n')
|
7949
|
-
s.append(' '*indent + '>\n')
|
7950
|
-
s.append(' '*indent)
|
7951
8218
|
s.append('lhs: ')
|
7952
8219
|
if self.lhs is None:
|
7953
8220
|
s.append('!MISSING\n')
|
@@ -7963,15 +8230,6 @@ class MeasureInstruction(Instruction):
|
|
7963
8230
|
s.append('<\n')
|
7964
8231
|
s.append(self.rhs.dump(indent + 1, annotations, links) + '\n')
|
7965
8232
|
s.append(' '*indent + '>\n')
|
7966
|
-
s.append(' '*indent)
|
7967
|
-
s.append('annotations: ')
|
7968
|
-
if not self.annotations:
|
7969
|
-
s.append('-\n')
|
7970
|
-
else:
|
7971
|
-
s.append('[\n')
|
7972
|
-
for child in self.annotations:
|
7973
|
-
s.append(child.dump(indent + 1, annotations, links) + '\n')
|
7974
|
-
s.append(' '*indent + ']\n')
|
7975
8233
|
indent -= 1
|
7976
8234
|
s.append(' '*indent)
|
7977
8235
|
s.append(')')
|
@@ -7989,14 +8247,10 @@ class MeasureInstruction(Instruction):
|
|
7989
8247
|
if id(self) in id_map:
|
7990
8248
|
raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
|
7991
8249
|
id_map[id(self)] = len(id_map)
|
7992
|
-
if self._attr_name is not None:
|
7993
|
-
self._attr_name.find_reachable(id_map)
|
7994
8250
|
if self._attr_lhs is not None:
|
7995
8251
|
self._attr_lhs.find_reachable(id_map)
|
7996
8252
|
if self._attr_rhs is not None:
|
7997
8253
|
self._attr_rhs.find_reachable(id_map)
|
7998
|
-
for el in self._attr_annotations:
|
7999
|
-
el.find_reachable(id_map)
|
8000
8254
|
return id_map
|
8001
8255
|
|
8002
8256
|
def check_complete(self, id_map=None):
|
@@ -8006,10 +8260,6 @@ class MeasureInstruction(Instruction):
|
|
8006
8260
|
id() codes to tree indices for all reachable nodes."""
|
8007
8261
|
if id_map is None:
|
8008
8262
|
id_map = self.find_reachable()
|
8009
|
-
if self._attr_name is None:
|
8010
|
-
raise NotWellFormed('name is required but not set')
|
8011
|
-
if self._attr_name is not None:
|
8012
|
-
self._attr_name.check_complete(id_map)
|
8013
8263
|
if self._attr_lhs is None:
|
8014
8264
|
raise NotWellFormed('lhs is required but not set')
|
8015
8265
|
if self._attr_lhs is not None:
|
@@ -8018,16 +8268,12 @@ class MeasureInstruction(Instruction):
|
|
8018
8268
|
raise NotWellFormed('rhs is required but not set')
|
8019
8269
|
if self._attr_rhs is not None:
|
8020
8270
|
self._attr_rhs.check_complete(id_map)
|
8021
|
-
for child in self._attr_annotations:
|
8022
|
-
child.check_complete(id_map)
|
8023
8271
|
|
8024
8272
|
def copy(self):
|
8025
8273
|
"""Returns a shallow copy of this node."""
|
8026
|
-
return
|
8027
|
-
name=self._attr_name,
|
8274
|
+
return ModuloExpression(
|
8028
8275
|
lhs=self._attr_lhs,
|
8029
|
-
rhs=self._attr_rhs
|
8030
|
-
annotations=self._attr_annotations.copy()
|
8276
|
+
rhs=self._attr_rhs
|
8031
8277
|
)
|
8032
8278
|
|
8033
8279
|
def clone(self):
|
@@ -8036,11 +8282,9 @@ class MeasureInstruction(Instruction):
|
|
8036
8282
|
original tree. If you're not cloning a subtree in a context where this
|
8037
8283
|
is the desired behavior, you may want to use the copy.deepcopy() from
|
8038
8284
|
the stdlib instead, which should copy links correctly."""
|
8039
|
-
return
|
8040
|
-
name=_cloned(self._attr_name),
|
8285
|
+
return ModuloExpression(
|
8041
8286
|
lhs=_cloned(self._attr_lhs),
|
8042
|
-
rhs=_cloned(self._attr_rhs)
|
8043
|
-
annotations=_cloned(self._attr_annotations)
|
8287
|
+
rhs=_cloned(self._attr_rhs)
|
8044
8288
|
)
|
8045
8289
|
|
8046
8290
|
@staticmethod
|
@@ -8056,19 +8300,8 @@ class MeasureInstruction(Instruction):
|
|
8056
8300
|
typ = cbor.get('@t', None)
|
8057
8301
|
if typ is None:
|
8058
8302
|
raise ValueError('type (@t) field is missing from node serialization')
|
8059
|
-
if typ != '
|
8060
|
-
raise ValueError('found node serialization for ' + typ + ', but expected
|
8061
|
-
|
8062
|
-
# Deserialize the name field.
|
8063
|
-
field = cbor.get('name', None)
|
8064
|
-
if not isinstance(field, dict):
|
8065
|
-
raise ValueError('missing or invalid serialization of field name')
|
8066
|
-
if field.get('@T') != '1':
|
8067
|
-
raise ValueError('unexpected edge type for field name')
|
8068
|
-
if field.get('@t', None) is None:
|
8069
|
-
f_name = None
|
8070
|
-
else:
|
8071
|
-
f_name = Identifier._deserialize(field, seq_to_ob, links)
|
8303
|
+
if typ != 'ModuloExpression':
|
8304
|
+
raise ValueError('found node serialization for ' + typ + ', but expected ModuloExpression')
|
8072
8305
|
|
8073
8306
|
# Deserialize the lhs field.
|
8074
8307
|
field = cbor.get('lhs', None)
|
@@ -8092,23 +8325,8 @@ class MeasureInstruction(Instruction):
|
|
8092
8325
|
else:
|
8093
8326
|
f_rhs = Expression._deserialize(field, seq_to_ob, links)
|
8094
8327
|
|
8095
|
-
#
|
8096
|
-
|
8097
|
-
if not isinstance(field, dict):
|
8098
|
-
raise ValueError('missing or invalid serialization of field annotations')
|
8099
|
-
if field.get('@T') != '*':
|
8100
|
-
raise ValueError('unexpected edge type for field annotations')
|
8101
|
-
data = field.get('@d', None)
|
8102
|
-
if not isinstance(data, list):
|
8103
|
-
raise ValueError('missing serialization of Any/Many contents')
|
8104
|
-
f_annotations = MultiAnnotationData()
|
8105
|
-
for element in data:
|
8106
|
-
if element.get('@T') != '1':
|
8107
|
-
raise ValueError('unexpected edge type for Any/Many element')
|
8108
|
-
f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
|
8109
|
-
|
8110
|
-
# Construct the MeasureInstruction node.
|
8111
|
-
node = MeasureInstruction(f_name, f_lhs, f_rhs, f_annotations)
|
8328
|
+
# Construct the ModuloExpression node.
|
8329
|
+
node = ModuloExpression(f_lhs, f_rhs)
|
8112
8330
|
|
8113
8331
|
# Deserialize annotations.
|
8114
8332
|
for key, val in cbor.items():
|
@@ -8132,15 +8350,7 @@ class MeasureInstruction(Instruction):
|
|
8132
8350
|
CBOR serialization. The tree that the node belongs to must be
|
8133
8351
|
well-formed. id_map must match Python id() calls for all nodes to unique
|
8134
8352
|
integers, to use for the sequence number representation of links."""
|
8135
|
-
cbor = {'@i': id_map[id(self)], '@t': '
|
8136
|
-
|
8137
|
-
# Serialize the name field.
|
8138
|
-
field = {'@T': '1'}
|
8139
|
-
if self._attr_name is None:
|
8140
|
-
field['@t'] = None
|
8141
|
-
else:
|
8142
|
-
field.update(self._attr_name._serialize(id_map))
|
8143
|
-
cbor['name'] = field
|
8353
|
+
cbor = {'@i': id_map[id(self)], '@t': 'ModuloExpression'}
|
8144
8354
|
|
8145
8355
|
# Serialize the lhs field.
|
8146
8356
|
field = {'@T': '1'}
|
@@ -8158,16 +8368,6 @@ class MeasureInstruction(Instruction):
|
|
8158
8368
|
field.update(self._attr_rhs._serialize(id_map))
|
8159
8369
|
cbor['rhs'] = field
|
8160
8370
|
|
8161
|
-
# Serialize the annotations field.
|
8162
|
-
field = {'@T': '*'}
|
8163
|
-
lst = []
|
8164
|
-
for el in self._attr_annotations:
|
8165
|
-
el = el._serialize(id_map)
|
8166
|
-
el['@T'] = '1'
|
8167
|
-
lst.append(el)
|
8168
|
-
field['@d'] = lst
|
8169
|
-
cbor['annotations'] = field
|
8170
|
-
|
8171
8371
|
# Serialize annotations.
|
8172
8372
|
for key, val in self._annot.items():
|
8173
8373
|
cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
|
@@ -8175,31 +8375,79 @@ class MeasureInstruction(Instruction):
|
|
8175
8375
|
return cbor
|
8176
8376
|
|
8177
8377
|
|
8178
|
-
class
|
8179
|
-
"""Wrapper for an edge with multiple
|
8378
|
+
class MultiModuloExpression(_Multiple):
|
8379
|
+
"""Wrapper for an edge with multiple ModuloExpression objects."""
|
8180
8380
|
|
8181
|
-
_T =
|
8381
|
+
_T = ModuloExpression
|
8182
8382
|
|
8183
8383
|
|
8184
|
-
_typemap['
|
8384
|
+
_typemap['ModuloExpression'] = ModuloExpression
|
8185
8385
|
|
8186
|
-
class
|
8187
|
-
__slots__ = [
|
8386
|
+
class NonGateInstruction(Instruction):
|
8387
|
+
__slots__ = [
|
8388
|
+
'_attr_name',
|
8389
|
+
'_attr_operands',
|
8390
|
+
]
|
8188
8391
|
|
8189
8392
|
def __init__(
|
8190
8393
|
self,
|
8191
|
-
|
8192
|
-
|
8394
|
+
name=None,
|
8395
|
+
operands=None,
|
8396
|
+
annotations=None,
|
8193
8397
|
):
|
8194
|
-
super().__init__(
|
8398
|
+
super().__init__(annotations=annotations)
|
8399
|
+
self.name = name
|
8400
|
+
self.operands = operands
|
8401
|
+
|
8402
|
+
@property
|
8403
|
+
def name(self):
|
8404
|
+
return self._attr_name
|
8405
|
+
|
8406
|
+
@name.setter
|
8407
|
+
def name(self, val):
|
8408
|
+
if val is None:
|
8409
|
+
del self.name
|
8410
|
+
return
|
8411
|
+
if not isinstance(val, Keyword):
|
8412
|
+
# Try to "typecast" if this isn't an obvious mistake.
|
8413
|
+
if isinstance(val, Node):
|
8414
|
+
raise TypeError('name must be of type Keyword')
|
8415
|
+
val = Keyword(val)
|
8416
|
+
self._attr_name = val
|
8417
|
+
|
8418
|
+
@name.deleter
|
8419
|
+
def name(self):
|
8420
|
+
self._attr_name = None
|
8421
|
+
|
8422
|
+
@property
|
8423
|
+
def operands(self):
|
8424
|
+
return self._attr_operands
|
8425
|
+
|
8426
|
+
@operands.setter
|
8427
|
+
def operands(self, val):
|
8428
|
+
if val is None:
|
8429
|
+
del self.operands
|
8430
|
+
return
|
8431
|
+
if not isinstance(val, ExpressionList):
|
8432
|
+
# Try to "typecast" if this isn't an obvious mistake.
|
8433
|
+
if isinstance(val, Node):
|
8434
|
+
raise TypeError('operands must be of type ExpressionList')
|
8435
|
+
val = ExpressionList(val)
|
8436
|
+
self._attr_operands = val
|
8437
|
+
|
8438
|
+
@operands.deleter
|
8439
|
+
def operands(self):
|
8440
|
+
self._attr_operands = None
|
8195
8441
|
|
8196
8442
|
def __eq__(self, other):
|
8197
8443
|
"""Equality operator. Ignores annotations!"""
|
8198
|
-
if not isinstance(other,
|
8444
|
+
if not isinstance(other, NonGateInstruction):
|
8199
8445
|
return False
|
8200
|
-
if self.
|
8446
|
+
if self.name != other.name:
|
8201
8447
|
return False
|
8202
|
-
if self.
|
8448
|
+
if self.operands != other.operands:
|
8449
|
+
return False
|
8450
|
+
if self.annotations != other.annotations:
|
8203
8451
|
return False
|
8204
8452
|
return True
|
8205
8453
|
|
@@ -8210,7 +8458,7 @@ class ModuloExpression(ArithmeticExpression):
|
|
8210
8458
|
strings of the annotations that are to be printed. links specifies the
|
8211
8459
|
maximum link recursion depth."""
|
8212
8460
|
s = [' '*indent]
|
8213
|
-
s.append('
|
8461
|
+
s.append('NonGateInstruction(')
|
8214
8462
|
if annotations is None:
|
8215
8463
|
annotations = []
|
8216
8464
|
for key in annotations:
|
@@ -8219,21 +8467,30 @@ class ModuloExpression(ArithmeticExpression):
|
|
8219
8467
|
s.append('\n')
|
8220
8468
|
indent += 1
|
8221
8469
|
s.append(' '*indent)
|
8222
|
-
s.append('
|
8223
|
-
if self.
|
8470
|
+
s.append('name: ')
|
8471
|
+
if self.name is None:
|
8224
8472
|
s.append('!MISSING\n')
|
8225
8473
|
else:
|
8226
8474
|
s.append('<\n')
|
8227
|
-
s.append(self.
|
8475
|
+
s.append(self.name.dump(indent + 1, annotations, links) + '\n')
|
8228
8476
|
s.append(' '*indent + '>\n')
|
8229
8477
|
s.append(' '*indent)
|
8230
|
-
s.append('
|
8231
|
-
if self.
|
8478
|
+
s.append('operands: ')
|
8479
|
+
if self.operands is None:
|
8232
8480
|
s.append('!MISSING\n')
|
8233
8481
|
else:
|
8234
8482
|
s.append('<\n')
|
8235
|
-
s.append(self.
|
8483
|
+
s.append(self.operands.dump(indent + 1, annotations, links) + '\n')
|
8236
8484
|
s.append(' '*indent + '>\n')
|
8485
|
+
s.append(' '*indent)
|
8486
|
+
s.append('annotations: ')
|
8487
|
+
if not self.annotations:
|
8488
|
+
s.append('-\n')
|
8489
|
+
else:
|
8490
|
+
s.append('[\n')
|
8491
|
+
for child in self.annotations:
|
8492
|
+
s.append(child.dump(indent + 1, annotations, links) + '\n')
|
8493
|
+
s.append(' '*indent + ']\n')
|
8237
8494
|
indent -= 1
|
8238
8495
|
s.append(' '*indent)
|
8239
8496
|
s.append(')')
|
@@ -8251,10 +8508,12 @@ class ModuloExpression(ArithmeticExpression):
|
|
8251
8508
|
if id(self) in id_map:
|
8252
8509
|
raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
|
8253
8510
|
id_map[id(self)] = len(id_map)
|
8254
|
-
if self.
|
8255
|
-
self.
|
8256
|
-
if self.
|
8257
|
-
self.
|
8511
|
+
if self._attr_name is not None:
|
8512
|
+
self._attr_name.find_reachable(id_map)
|
8513
|
+
if self._attr_operands is not None:
|
8514
|
+
self._attr_operands.find_reachable(id_map)
|
8515
|
+
for el in self._attr_annotations:
|
8516
|
+
el.find_reachable(id_map)
|
8258
8517
|
return id_map
|
8259
8518
|
|
8260
8519
|
def check_complete(self, id_map=None):
|
@@ -8264,20 +8523,23 @@ class ModuloExpression(ArithmeticExpression):
|
|
8264
8523
|
id() codes to tree indices for all reachable nodes."""
|
8265
8524
|
if id_map is None:
|
8266
8525
|
id_map = self.find_reachable()
|
8267
|
-
if self.
|
8268
|
-
raise NotWellFormed('
|
8269
|
-
if self.
|
8270
|
-
self.
|
8271
|
-
if self.
|
8272
|
-
raise NotWellFormed('
|
8273
|
-
if self.
|
8274
|
-
self.
|
8526
|
+
if self._attr_name is None:
|
8527
|
+
raise NotWellFormed('name is required but not set')
|
8528
|
+
if self._attr_name is not None:
|
8529
|
+
self._attr_name.check_complete(id_map)
|
8530
|
+
if self._attr_operands is None:
|
8531
|
+
raise NotWellFormed('operands is required but not set')
|
8532
|
+
if self._attr_operands is not None:
|
8533
|
+
self._attr_operands.check_complete(id_map)
|
8534
|
+
for child in self._attr_annotations:
|
8535
|
+
child.check_complete(id_map)
|
8275
8536
|
|
8276
8537
|
def copy(self):
|
8277
8538
|
"""Returns a shallow copy of this node."""
|
8278
|
-
return
|
8279
|
-
|
8280
|
-
|
8539
|
+
return NonGateInstruction(
|
8540
|
+
name=self._attr_name,
|
8541
|
+
operands=self._attr_operands,
|
8542
|
+
annotations=self._attr_annotations.copy()
|
8281
8543
|
)
|
8282
8544
|
|
8283
8545
|
def clone(self):
|
@@ -8286,9 +8548,10 @@ class ModuloExpression(ArithmeticExpression):
|
|
8286
8548
|
original tree. If you're not cloning a subtree in a context where this
|
8287
8549
|
is the desired behavior, you may want to use the copy.deepcopy() from
|
8288
8550
|
the stdlib instead, which should copy links correctly."""
|
8289
|
-
return
|
8290
|
-
|
8291
|
-
|
8551
|
+
return NonGateInstruction(
|
8552
|
+
name=_cloned(self._attr_name),
|
8553
|
+
operands=_cloned(self._attr_operands),
|
8554
|
+
annotations=_cloned(self._attr_annotations)
|
8292
8555
|
)
|
8293
8556
|
|
8294
8557
|
@staticmethod
|
@@ -8304,33 +8567,48 @@ class ModuloExpression(ArithmeticExpression):
|
|
8304
8567
|
typ = cbor.get('@t', None)
|
8305
8568
|
if typ is None:
|
8306
8569
|
raise ValueError('type (@t) field is missing from node serialization')
|
8307
|
-
if typ != '
|
8308
|
-
raise ValueError('found node serialization for ' + typ + ', but expected
|
8570
|
+
if typ != 'NonGateInstruction':
|
8571
|
+
raise ValueError('found node serialization for ' + typ + ', but expected NonGateInstruction')
|
8309
8572
|
|
8310
|
-
# Deserialize the
|
8311
|
-
field = cbor.get('
|
8573
|
+
# Deserialize the name field.
|
8574
|
+
field = cbor.get('name', None)
|
8312
8575
|
if not isinstance(field, dict):
|
8313
|
-
raise ValueError('missing or invalid serialization of field
|
8576
|
+
raise ValueError('missing or invalid serialization of field name')
|
8314
8577
|
if field.get('@T') != '1':
|
8315
|
-
raise ValueError('unexpected edge type for field
|
8578
|
+
raise ValueError('unexpected edge type for field name')
|
8316
8579
|
if field.get('@t', None) is None:
|
8317
|
-
|
8580
|
+
f_name = None
|
8318
8581
|
else:
|
8319
|
-
|
8582
|
+
f_name = Keyword._deserialize(field, seq_to_ob, links)
|
8320
8583
|
|
8321
|
-
# Deserialize the
|
8322
|
-
field = cbor.get('
|
8584
|
+
# Deserialize the operands field.
|
8585
|
+
field = cbor.get('operands', None)
|
8323
8586
|
if not isinstance(field, dict):
|
8324
|
-
raise ValueError('missing or invalid serialization of field
|
8587
|
+
raise ValueError('missing or invalid serialization of field operands')
|
8325
8588
|
if field.get('@T') != '1':
|
8326
|
-
raise ValueError('unexpected edge type for field
|
8589
|
+
raise ValueError('unexpected edge type for field operands')
|
8327
8590
|
if field.get('@t', None) is None:
|
8328
|
-
|
8591
|
+
f_operands = None
|
8329
8592
|
else:
|
8330
|
-
|
8593
|
+
f_operands = ExpressionList._deserialize(field, seq_to_ob, links)
|
8331
8594
|
|
8332
|
-
#
|
8333
|
-
|
8595
|
+
# Deserialize the annotations field.
|
8596
|
+
field = cbor.get('annotations', None)
|
8597
|
+
if not isinstance(field, dict):
|
8598
|
+
raise ValueError('missing or invalid serialization of field annotations')
|
8599
|
+
if field.get('@T') != '*':
|
8600
|
+
raise ValueError('unexpected edge type for field annotations')
|
8601
|
+
data = field.get('@d', None)
|
8602
|
+
if not isinstance(data, list):
|
8603
|
+
raise ValueError('missing serialization of Any/Many contents')
|
8604
|
+
f_annotations = MultiAnnotationData()
|
8605
|
+
for element in data:
|
8606
|
+
if element.get('@T') != '1':
|
8607
|
+
raise ValueError('unexpected edge type for Any/Many element')
|
8608
|
+
f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
|
8609
|
+
|
8610
|
+
# Construct the NonGateInstruction node.
|
8611
|
+
node = NonGateInstruction(f_name, f_operands, f_annotations)
|
8334
8612
|
|
8335
8613
|
# Deserialize annotations.
|
8336
8614
|
for key, val in cbor.items():
|
@@ -8354,23 +8632,33 @@ class ModuloExpression(ArithmeticExpression):
|
|
8354
8632
|
CBOR serialization. The tree that the node belongs to must be
|
8355
8633
|
well-formed. id_map must match Python id() calls for all nodes to unique
|
8356
8634
|
integers, to use for the sequence number representation of links."""
|
8357
|
-
cbor = {'@i': id_map[id(self)], '@t': '
|
8635
|
+
cbor = {'@i': id_map[id(self)], '@t': 'NonGateInstruction'}
|
8358
8636
|
|
8359
|
-
# Serialize the
|
8637
|
+
# Serialize the name field.
|
8360
8638
|
field = {'@T': '1'}
|
8361
|
-
if self.
|
8639
|
+
if self._attr_name is None:
|
8362
8640
|
field['@t'] = None
|
8363
8641
|
else:
|
8364
|
-
field.update(self.
|
8365
|
-
cbor['
|
8642
|
+
field.update(self._attr_name._serialize(id_map))
|
8643
|
+
cbor['name'] = field
|
8366
8644
|
|
8367
|
-
# Serialize the
|
8645
|
+
# Serialize the operands field.
|
8368
8646
|
field = {'@T': '1'}
|
8369
|
-
if self.
|
8647
|
+
if self._attr_operands is None:
|
8370
8648
|
field['@t'] = None
|
8371
8649
|
else:
|
8372
|
-
field.update(self.
|
8373
|
-
cbor['
|
8650
|
+
field.update(self._attr_operands._serialize(id_map))
|
8651
|
+
cbor['operands'] = field
|
8652
|
+
|
8653
|
+
# Serialize the annotations field.
|
8654
|
+
field = {'@T': '*'}
|
8655
|
+
lst = []
|
8656
|
+
for el in self._attr_annotations:
|
8657
|
+
el = el._serialize(id_map)
|
8658
|
+
el['@T'] = '1'
|
8659
|
+
lst.append(el)
|
8660
|
+
field['@d'] = lst
|
8661
|
+
cbor['annotations'] = field
|
8374
8662
|
|
8375
8663
|
# Serialize annotations.
|
8376
8664
|
for key, val in self._annot.items():
|
@@ -8379,13 +8667,13 @@ class ModuloExpression(ArithmeticExpression):
|
|
8379
8667
|
return cbor
|
8380
8668
|
|
8381
8669
|
|
8382
|
-
class
|
8383
|
-
"""Wrapper for an edge with multiple
|
8670
|
+
class MultiNonGateInstruction(_Multiple):
|
8671
|
+
"""Wrapper for an edge with multiple NonGateInstruction objects."""
|
8384
8672
|
|
8385
|
-
_T =
|
8673
|
+
_T = NonGateInstruction
|
8386
8674
|
|
8387
8675
|
|
8388
|
-
_typemap['
|
8676
|
+
_typemap['NonGateInstruction'] = NonGateInstruction
|
8389
8677
|
|
8390
8678
|
class PowerExpression(ArithmeticExpression):
|
8391
8679
|
__slots__ = []
|