libqasm 0.6.5__cp39-cp39-win_amd64.whl → 0.6.7__cp39-cp39-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/ast.py CHANGED
@@ -1172,6 +1172,8 @@ class Annotated(Node):
1172
1172
  return Gate._deserialize(cbor, seq_to_ob, links)
1173
1173
  if typ == 'MeasureInstruction':
1174
1174
  return MeasureInstruction._deserialize(cbor, seq_to_ob, links)
1175
+ if typ == 'ResetInstruction':
1176
+ return ResetInstruction._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):
@@ -2526,6 +2528,8 @@ class Statement(Annotated):
2526
2528
  return Gate._deserialize(cbor, seq_to_ob, links)
2527
2529
  if typ == 'MeasureInstruction':
2528
2530
  return MeasureInstruction._deserialize(cbor, seq_to_ob, links)
2531
+ if typ == 'ResetInstruction':
2532
+ return ResetInstruction._deserialize(cbor, seq_to_ob, links)
2529
2533
  raise ValueError('unknown or unexpected type (@t) found in node serialization')
2530
2534
 
2531
2535
  def _serialize(self, id_map):
@@ -2588,6 +2592,8 @@ class BlockStatement(Statement):
2588
2592
  return Gate._deserialize(cbor, seq_to_ob, links)
2589
2593
  if typ == 'MeasureInstruction':
2590
2594
  return MeasureInstruction._deserialize(cbor, seq_to_ob, links)
2595
+ if typ == 'ResetInstruction':
2596
+ return ResetInstruction._deserialize(cbor, seq_to_ob, links)
2591
2597
  raise ValueError('unknown or unexpected type (@t) found in node serialization')
2592
2598
 
2593
2599
  def _serialize(self, id_map):
@@ -5003,6 +5009,8 @@ class Instruction(BlockStatement):
5003
5009
  return Gate._deserialize(cbor, seq_to_ob, links)
5004
5010
  if typ == 'MeasureInstruction':
5005
5011
  return MeasureInstruction._deserialize(cbor, seq_to_ob, links)
5012
+ if typ == 'ResetInstruction':
5013
+ return ResetInstruction._deserialize(cbor, seq_to_ob, links)
5006
5014
  raise ValueError('unknown or unexpected type (@t) found in node serialization')
5007
5015
 
5008
5016
  def _serialize(self, id_map):
@@ -5331,91 +5339,41 @@ _typemap['Gate'] = Gate
5331
5339
 
5332
5340
  class GlobalBlock(Block):
5333
5341
  __slots__ = [
5334
- '_attr_qubit_variable_declaration',
5335
- '_attr_gates',
5336
- '_attr_measure_instructions',
5342
+ '_attr_statements',
5337
5343
  ]
5338
5344
 
5339
5345
  def __init__(
5340
5346
  self,
5341
- qubit_variable_declaration=None,
5342
- gates=None,
5343
- measure_instructions=None,
5347
+ statements=None,
5344
5348
  ):
5345
5349
  super().__init__()
5346
- self.qubit_variable_declaration = qubit_variable_declaration
5347
- self.gates = gates
5348
- self.measure_instructions = measure_instructions
5350
+ self.statements = statements
5349
5351
 
5350
5352
  @property
5351
- def qubit_variable_declaration(self):
5352
- return self._attr_qubit_variable_declaration
5353
+ def statements(self):
5354
+ return self._attr_statements
5353
5355
 
5354
- @qubit_variable_declaration.setter
5355
- def qubit_variable_declaration(self, val):
5356
+ @statements.setter
5357
+ def statements(self, val):
5356
5358
  if val is None:
5357
- del self.qubit_variable_declaration
5359
+ del self.statements
5358
5360
  return
5359
- if not isinstance(val, Variable):
5361
+ if not isinstance(val, MultiStatement):
5360
5362
  # Try to "typecast" if this isn't an obvious mistake.
5361
5363
  if isinstance(val, Node):
5362
- raise TypeError('qubit_variable_declaration must be of type Variable')
5363
- val = Variable(val)
5364
- self._attr_qubit_variable_declaration = val
5364
+ raise TypeError('statements must be of type MultiStatement')
5365
+ val = MultiStatement(val)
5366
+ self._attr_statements = val
5365
5367
 
5366
- @qubit_variable_declaration.deleter
5367
- def qubit_variable_declaration(self):
5368
- self._attr_qubit_variable_declaration = None
5369
-
5370
- @property
5371
- def gates(self):
5372
- return self._attr_gates
5373
-
5374
- @gates.setter
5375
- def gates(self, val):
5376
- if val is None:
5377
- del self.gates
5378
- return
5379
- if not isinstance(val, MultiGate):
5380
- # Try to "typecast" if this isn't an obvious mistake.
5381
- if isinstance(val, Node):
5382
- raise TypeError('gates must be of type MultiGate')
5383
- val = MultiGate(val)
5384
- self._attr_gates = val
5385
-
5386
- @gates.deleter
5387
- def gates(self):
5388
- self._attr_gates = MultiGate()
5389
-
5390
- @property
5391
- def measure_instructions(self):
5392
- return self._attr_measure_instructions
5393
-
5394
- @measure_instructions.setter
5395
- def measure_instructions(self, val):
5396
- if val is None:
5397
- del self.measure_instructions
5398
- return
5399
- if not isinstance(val, MultiMeasureInstruction):
5400
- # Try to "typecast" if this isn't an obvious mistake.
5401
- if isinstance(val, Node):
5402
- raise TypeError('measure_instructions must be of type MultiMeasureInstruction')
5403
- val = MultiMeasureInstruction(val)
5404
- self._attr_measure_instructions = val
5405
-
5406
- @measure_instructions.deleter
5407
- def measure_instructions(self):
5408
- self._attr_measure_instructions = MultiMeasureInstruction()
5368
+ @statements.deleter
5369
+ def statements(self):
5370
+ self._attr_statements = MultiStatement()
5409
5371
 
5410
5372
  def __eq__(self, other):
5411
5373
  """Equality operator. Ignores annotations!"""
5412
5374
  if not isinstance(other, GlobalBlock):
5413
5375
  return False
5414
- if self.qubit_variable_declaration != other.qubit_variable_declaration:
5415
- return False
5416
- if self.gates != other.gates:
5417
- return False
5418
- if self.measure_instructions != other.measure_instructions:
5376
+ if self.statements != other.statements:
5419
5377
  return False
5420
5378
  return True
5421
5379
 
@@ -5435,29 +5393,12 @@ class GlobalBlock(Block):
5435
5393
  s.append('\n')
5436
5394
  indent += 1
5437
5395
  s.append(' '*indent)
5438
- s.append('qubit_variable_declaration: ')
5439
- if self.qubit_variable_declaration is None:
5440
- s.append('!MISSING\n')
5441
- else:
5442
- s.append('<\n')
5443
- s.append(self.qubit_variable_declaration.dump(indent + 1, annotations, links) + '\n')
5444
- s.append(' '*indent + '>\n')
5445
- s.append(' '*indent)
5446
- s.append('gates: ')
5447
- if not self.gates:
5396
+ s.append('statements: ')
5397
+ if not self.statements:
5448
5398
  s.append('-\n')
5449
5399
  else:
5450
5400
  s.append('[\n')
5451
- for child in self.gates:
5452
- s.append(child.dump(indent + 1, annotations, links) + '\n')
5453
- s.append(' '*indent + ']\n')
5454
- s.append(' '*indent)
5455
- s.append('measure_instructions: ')
5456
- if not self.measure_instructions:
5457
- s.append('-\n')
5458
- else:
5459
- s.append('[\n')
5460
- for child in self.measure_instructions:
5401
+ for child in self.statements:
5461
5402
  s.append(child.dump(indent + 1, annotations, links) + '\n')
5462
5403
  s.append(' '*indent + ']\n')
5463
5404
  indent -= 1
@@ -5477,11 +5418,7 @@ class GlobalBlock(Block):
5477
5418
  if id(self) in id_map:
5478
5419
  raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
5479
5420
  id_map[id(self)] = len(id_map)
5480
- if self._attr_qubit_variable_declaration is not None:
5481
- self._attr_qubit_variable_declaration.find_reachable(id_map)
5482
- for el in self._attr_gates:
5483
- el.find_reachable(id_map)
5484
- for el in self._attr_measure_instructions:
5421
+ for el in self._attr_statements:
5485
5422
  el.find_reachable(id_map)
5486
5423
  return id_map
5487
5424
 
@@ -5492,21 +5429,13 @@ class GlobalBlock(Block):
5492
5429
  id() codes to tree indices for all reachable nodes."""
5493
5430
  if id_map is None:
5494
5431
  id_map = self.find_reachable()
5495
- if self._attr_qubit_variable_declaration is None:
5496
- raise NotWellFormed('qubit_variable_declaration is required but not set')
5497
- if self._attr_qubit_variable_declaration is not None:
5498
- self._attr_qubit_variable_declaration.check_complete(id_map)
5499
- for child in self._attr_gates:
5500
- child.check_complete(id_map)
5501
- for child in self._attr_measure_instructions:
5432
+ for child in self._attr_statements:
5502
5433
  child.check_complete(id_map)
5503
5434
 
5504
5435
  def copy(self):
5505
5436
  """Returns a shallow copy of this node."""
5506
5437
  return GlobalBlock(
5507
- qubit_variable_declaration=self._attr_qubit_variable_declaration,
5508
- gates=self._attr_gates.copy(),
5509
- measure_instructions=self._attr_measure_instructions.copy()
5438
+ statements=self._attr_statements.copy()
5510
5439
  )
5511
5440
 
5512
5441
  def clone(self):
@@ -5516,9 +5445,7 @@ class GlobalBlock(Block):
5516
5445
  is the desired behavior, you may want to use the copy.deepcopy() from
5517
5446
  the stdlib instead, which should copy links correctly."""
5518
5447
  return GlobalBlock(
5519
- qubit_variable_declaration=_cloned(self._attr_qubit_variable_declaration),
5520
- gates=_cloned(self._attr_gates),
5521
- measure_instructions=_cloned(self._attr_measure_instructions)
5448
+ statements=_cloned(self._attr_statements)
5522
5449
  )
5523
5450
 
5524
5451
  @staticmethod
@@ -5537,49 +5464,23 @@ class GlobalBlock(Block):
5537
5464
  if typ != 'GlobalBlock':
5538
5465
  raise ValueError('found node serialization for ' + typ + ', but expected GlobalBlock')
5539
5466
 
5540
- # Deserialize the qubit_variable_declaration field.
5541
- field = cbor.get('qubit_variable_declaration', None)
5542
- if not isinstance(field, dict):
5543
- raise ValueError('missing or invalid serialization of field qubit_variable_declaration')
5544
- if field.get('@T') != '1':
5545
- raise ValueError('unexpected edge type for field qubit_variable_declaration')
5546
- if field.get('@t', None) is None:
5547
- f_qubit_variable_declaration = None
5548
- else:
5549
- f_qubit_variable_declaration = Variable._deserialize(field, seq_to_ob, links)
5550
-
5551
- # Deserialize the gates field.
5552
- field = cbor.get('gates', None)
5553
- if not isinstance(field, dict):
5554
- raise ValueError('missing or invalid serialization of field gates')
5555
- if field.get('@T') != '*':
5556
- raise ValueError('unexpected edge type for field gates')
5557
- data = field.get('@d', None)
5558
- if not isinstance(data, list):
5559
- raise ValueError('missing serialization of Any/Many contents')
5560
- f_gates = MultiGate()
5561
- for element in data:
5562
- if element.get('@T') != '1':
5563
- raise ValueError('unexpected edge type for Any/Many element')
5564
- f_gates.append(Gate._deserialize(element, seq_to_ob, links))
5565
-
5566
- # Deserialize the measure_instructions field.
5567
- field = cbor.get('measure_instructions', None)
5467
+ # Deserialize the statements field.
5468
+ field = cbor.get('statements', None)
5568
5469
  if not isinstance(field, dict):
5569
- raise ValueError('missing or invalid serialization of field measure_instructions')
5470
+ raise ValueError('missing or invalid serialization of field statements')
5570
5471
  if field.get('@T') != '*':
5571
- raise ValueError('unexpected edge type for field measure_instructions')
5472
+ raise ValueError('unexpected edge type for field statements')
5572
5473
  data = field.get('@d', None)
5573
5474
  if not isinstance(data, list):
5574
5475
  raise ValueError('missing serialization of Any/Many contents')
5575
- f_measure_instructions = MultiMeasureInstruction()
5476
+ f_statements = MultiStatement()
5576
5477
  for element in data:
5577
5478
  if element.get('@T') != '1':
5578
5479
  raise ValueError('unexpected edge type for Any/Many element')
5579
- f_measure_instructions.append(MeasureInstruction._deserialize(element, seq_to_ob, links))
5480
+ f_statements.append(Statement._deserialize(element, seq_to_ob, links))
5580
5481
 
5581
5482
  # Construct the GlobalBlock node.
5582
- node = GlobalBlock(f_qubit_variable_declaration, f_gates, f_measure_instructions)
5483
+ node = GlobalBlock(f_statements)
5583
5484
 
5584
5485
  # Deserialize annotations.
5585
5486
  for key, val in cbor.items():
@@ -5605,33 +5506,15 @@ class GlobalBlock(Block):
5605
5506
  integers, to use for the sequence number representation of links."""
5606
5507
  cbor = {'@i': id_map[id(self)], '@t': 'GlobalBlock'}
5607
5508
 
5608
- # Serialize the qubit_variable_declaration field.
5609
- field = {'@T': '1'}
5610
- if self._attr_qubit_variable_declaration is None:
5611
- field['@t'] = None
5612
- else:
5613
- field.update(self._attr_qubit_variable_declaration._serialize(id_map))
5614
- cbor['qubit_variable_declaration'] = field
5615
-
5616
- # Serialize the gates field.
5617
- field = {'@T': '*'}
5618
- lst = []
5619
- for el in self._attr_gates:
5620
- el = el._serialize(id_map)
5621
- el['@T'] = '1'
5622
- lst.append(el)
5623
- field['@d'] = lst
5624
- cbor['gates'] = field
5625
-
5626
- # Serialize the measure_instructions field.
5509
+ # Serialize the statements field.
5627
5510
  field = {'@T': '*'}
5628
5511
  lst = []
5629
- for el in self._attr_measure_instructions:
5512
+ for el in self._attr_statements:
5630
5513
  el = el._serialize(id_map)
5631
5514
  el['@T'] = '1'
5632
5515
  lst.append(el)
5633
5516
  field['@d'] = lst
5634
- cbor['measure_instructions'] = field
5517
+ cbor['statements'] = field
5635
5518
 
5636
5519
  # Serialize annotations.
5637
5520
  for key, val in self._annot.items():
@@ -7952,20 +7835,28 @@ class MultiLogicalXorExpression(_Multiple):
7952
7835
  _typemap['LogicalXorExpression'] = LogicalXorExpression
7953
7836
 
7954
7837
  class MeasureInstruction(Instruction):
7838
+ """For some instructions, like the measure instruction, we want to
7839
+ differentiate between left hand side and right and side operands. For a
7840
+ measure instruction, the right hand side operand should be a qubit, and the
7841
+ left hand side operand a bit."""
7842
+
7955
7843
  __slots__ = [
7956
7844
  '_attr_name',
7957
- '_attr_operand',
7845
+ '_attr_lhs',
7846
+ '_attr_rhs',
7958
7847
  ]
7959
7848
 
7960
7849
  def __init__(
7961
7850
  self,
7962
7851
  name=None,
7963
- operand=None,
7852
+ lhs=None,
7853
+ rhs=None,
7964
7854
  annotations=None,
7965
7855
  ):
7966
7856
  super().__init__(annotations=annotations)
7967
7857
  self.name = name
7968
- self.operand = operand
7858
+ self.lhs = lhs
7859
+ self.rhs = rhs
7969
7860
 
7970
7861
  @property
7971
7862
  def name(self):
@@ -7988,24 +7879,44 @@ class MeasureInstruction(Instruction):
7988
7879
  self._attr_name = None
7989
7880
 
7990
7881
  @property
7991
- def operand(self):
7992
- return self._attr_operand
7882
+ def lhs(self):
7883
+ return self._attr_lhs
7993
7884
 
7994
- @operand.setter
7995
- def operand(self, val):
7885
+ @lhs.setter
7886
+ def lhs(self, val):
7996
7887
  if val is None:
7997
- del self.operand
7888
+ del self.lhs
7998
7889
  return
7999
7890
  if not isinstance(val, Expression):
8000
7891
  # Try to "typecast" if this isn't an obvious mistake.
8001
7892
  if isinstance(val, Node):
8002
- raise TypeError('operand must be of type Expression')
7893
+ raise TypeError('lhs must be of type Expression')
8003
7894
  val = Expression(val)
8004
- self._attr_operand = val
7895
+ self._attr_lhs = val
8005
7896
 
8006
- @operand.deleter
8007
- def operand(self):
8008
- self._attr_operand = None
7897
+ @lhs.deleter
7898
+ def lhs(self):
7899
+ self._attr_lhs = None
7900
+
7901
+ @property
7902
+ def rhs(self):
7903
+ return self._attr_rhs
7904
+
7905
+ @rhs.setter
7906
+ def rhs(self, val):
7907
+ if val is None:
7908
+ del self.rhs
7909
+ return
7910
+ if not isinstance(val, Expression):
7911
+ # Try to "typecast" if this isn't an obvious mistake.
7912
+ if isinstance(val, Node):
7913
+ raise TypeError('rhs must be of type Expression')
7914
+ val = Expression(val)
7915
+ self._attr_rhs = val
7916
+
7917
+ @rhs.deleter
7918
+ def rhs(self):
7919
+ self._attr_rhs = None
8009
7920
 
8010
7921
  def __eq__(self, other):
8011
7922
  """Equality operator. Ignores annotations!"""
@@ -8013,7 +7924,9 @@ class MeasureInstruction(Instruction):
8013
7924
  return False
8014
7925
  if self.name != other.name:
8015
7926
  return False
8016
- if self.operand != other.operand:
7927
+ if self.lhs != other.lhs:
7928
+ return False
7929
+ if self.rhs != other.rhs:
8017
7930
  return False
8018
7931
  if self.annotations != other.annotations:
8019
7932
  return False
@@ -8043,12 +7956,20 @@ class MeasureInstruction(Instruction):
8043
7956
  s.append(self.name.dump(indent + 1, annotations, links) + '\n')
8044
7957
  s.append(' '*indent + '>\n')
8045
7958
  s.append(' '*indent)
8046
- s.append('operand: ')
8047
- if self.operand is None:
7959
+ s.append('lhs: ')
7960
+ if self.lhs is None:
8048
7961
  s.append('!MISSING\n')
8049
7962
  else:
8050
7963
  s.append('<\n')
8051
- s.append(self.operand.dump(indent + 1, annotations, links) + '\n')
7964
+ s.append(self.lhs.dump(indent + 1, annotations, links) + '\n')
7965
+ s.append(' '*indent + '>\n')
7966
+ s.append(' '*indent)
7967
+ s.append('rhs: ')
7968
+ if self.rhs is None:
7969
+ s.append('!MISSING\n')
7970
+ else:
7971
+ s.append('<\n')
7972
+ s.append(self.rhs.dump(indent + 1, annotations, links) + '\n')
8052
7973
  s.append(' '*indent + '>\n')
8053
7974
  s.append(' '*indent)
8054
7975
  s.append('annotations: ')
@@ -8078,8 +7999,10 @@ class MeasureInstruction(Instruction):
8078
7999
  id_map[id(self)] = len(id_map)
8079
8000
  if self._attr_name is not None:
8080
8001
  self._attr_name.find_reachable(id_map)
8081
- if self._attr_operand is not None:
8082
- self._attr_operand.find_reachable(id_map)
8002
+ if self._attr_lhs is not None:
8003
+ self._attr_lhs.find_reachable(id_map)
8004
+ if self._attr_rhs is not None:
8005
+ self._attr_rhs.find_reachable(id_map)
8083
8006
  for el in self._attr_annotations:
8084
8007
  el.find_reachable(id_map)
8085
8008
  return id_map
@@ -8095,10 +8018,14 @@ class MeasureInstruction(Instruction):
8095
8018
  raise NotWellFormed('name is required but not set')
8096
8019
  if self._attr_name is not None:
8097
8020
  self._attr_name.check_complete(id_map)
8098
- if self._attr_operand is None:
8099
- raise NotWellFormed('operand is required but not set')
8100
- if self._attr_operand is not None:
8101
- self._attr_operand.check_complete(id_map)
8021
+ if self._attr_lhs is None:
8022
+ raise NotWellFormed('lhs is required but not set')
8023
+ if self._attr_lhs is not None:
8024
+ self._attr_lhs.check_complete(id_map)
8025
+ if self._attr_rhs is None:
8026
+ raise NotWellFormed('rhs is required but not set')
8027
+ if self._attr_rhs is not None:
8028
+ self._attr_rhs.check_complete(id_map)
8102
8029
  for child in self._attr_annotations:
8103
8030
  child.check_complete(id_map)
8104
8031
 
@@ -8106,7 +8033,8 @@ class MeasureInstruction(Instruction):
8106
8033
  """Returns a shallow copy of this node."""
8107
8034
  return MeasureInstruction(
8108
8035
  name=self._attr_name,
8109
- operand=self._attr_operand,
8036
+ lhs=self._attr_lhs,
8037
+ rhs=self._attr_rhs,
8110
8038
  annotations=self._attr_annotations.copy()
8111
8039
  )
8112
8040
 
@@ -8118,7 +8046,8 @@ class MeasureInstruction(Instruction):
8118
8046
  the stdlib instead, which should copy links correctly."""
8119
8047
  return MeasureInstruction(
8120
8048
  name=_cloned(self._attr_name),
8121
- operand=_cloned(self._attr_operand),
8049
+ lhs=_cloned(self._attr_lhs),
8050
+ rhs=_cloned(self._attr_rhs),
8122
8051
  annotations=_cloned(self._attr_annotations)
8123
8052
  )
8124
8053
 
@@ -8149,16 +8078,27 @@ class MeasureInstruction(Instruction):
8149
8078
  else:
8150
8079
  f_name = Identifier._deserialize(field, seq_to_ob, links)
8151
8080
 
8152
- # Deserialize the operand field.
8153
- field = cbor.get('operand', None)
8081
+ # Deserialize the lhs field.
8082
+ field = cbor.get('lhs', None)
8154
8083
  if not isinstance(field, dict):
8155
- raise ValueError('missing or invalid serialization of field operand')
8084
+ raise ValueError('missing or invalid serialization of field lhs')
8156
8085
  if field.get('@T') != '1':
8157
- raise ValueError('unexpected edge type for field operand')
8086
+ raise ValueError('unexpected edge type for field lhs')
8158
8087
  if field.get('@t', None) is None:
8159
- f_operand = None
8088
+ f_lhs = None
8160
8089
  else:
8161
- f_operand = Expression._deserialize(field, seq_to_ob, links)
8090
+ f_lhs = Expression._deserialize(field, seq_to_ob, links)
8091
+
8092
+ # Deserialize the rhs field.
8093
+ field = cbor.get('rhs', None)
8094
+ if not isinstance(field, dict):
8095
+ raise ValueError('missing or invalid serialization of field rhs')
8096
+ if field.get('@T') != '1':
8097
+ raise ValueError('unexpected edge type for field rhs')
8098
+ if field.get('@t', None) is None:
8099
+ f_rhs = None
8100
+ else:
8101
+ f_rhs = Expression._deserialize(field, seq_to_ob, links)
8162
8102
 
8163
8103
  # Deserialize the annotations field.
8164
8104
  field = cbor.get('annotations', None)
@@ -8176,7 +8116,7 @@ class MeasureInstruction(Instruction):
8176
8116
  f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
8177
8117
 
8178
8118
  # Construct the MeasureInstruction node.
8179
- node = MeasureInstruction(f_name, f_operand, f_annotations)
8119
+ node = MeasureInstruction(f_name, f_lhs, f_rhs, f_annotations)
8180
8120
 
8181
8121
  # Deserialize annotations.
8182
8122
  for key, val in cbor.items():
@@ -8210,13 +8150,21 @@ class MeasureInstruction(Instruction):
8210
8150
  field.update(self._attr_name._serialize(id_map))
8211
8151
  cbor['name'] = field
8212
8152
 
8213
- # Serialize the operand field.
8153
+ # Serialize the lhs field.
8214
8154
  field = {'@T': '1'}
8215
- if self._attr_operand is None:
8155
+ if self._attr_lhs is None:
8216
8156
  field['@t'] = None
8217
8157
  else:
8218
- field.update(self._attr_operand._serialize(id_map))
8219
- cbor['operand'] = field
8158
+ field.update(self._attr_lhs._serialize(id_map))
8159
+ cbor['lhs'] = field
8160
+
8161
+ # Serialize the rhs field.
8162
+ field = {'@T': '1'}
8163
+ if self._attr_rhs is None:
8164
+ field['@t'] = None
8165
+ else:
8166
+ field.update(self._attr_rhs._serialize(id_map))
8167
+ cbor['rhs'] = field
8220
8168
 
8221
8169
  # Serialize the annotations field.
8222
8170
  field = {'@T': '*'}
@@ -8991,7 +8939,7 @@ class Program(Root):
8991
8939
  s.append(' '*indent)
8992
8940
  s.append('block: ')
8993
8941
  if self.block is None:
8994
- s.append('-\n')
8942
+ s.append('!MISSING\n')
8995
8943
  else:
8996
8944
  s.append('<\n')
8997
8945
  s.append(self.block.dump(indent + 1, annotations, links) + '\n')
@@ -9030,6 +8978,8 @@ class Program(Root):
9030
8978
  raise NotWellFormed('version is required but not set')
9031
8979
  if self._attr_version is not None:
9032
8980
  self._attr_version.check_complete(id_map)
8981
+ if self._attr_block is None:
8982
+ raise NotWellFormed('block is required but not set')
9033
8983
  if self._attr_block is not None:
9034
8984
  self._attr_block.check_complete(id_map)
9035
8985
 
@@ -9082,7 +9032,7 @@ class Program(Root):
9082
9032
  field = cbor.get('block', None)
9083
9033
  if not isinstance(field, dict):
9084
9034
  raise ValueError('missing or invalid serialization of field block')
9085
- if field.get('@T') != '?':
9035
+ if field.get('@T') != '1':
9086
9036
  raise ValueError('unexpected edge type for field block')
9087
9037
  if field.get('@t', None) is None:
9088
9038
  f_block = None
@@ -9125,7 +9075,7 @@ class Program(Root):
9125
9075
  cbor['version'] = field
9126
9076
 
9127
9077
  # Serialize the block field.
9128
- field = {'@T': '?'}
9078
+ field = {'@T': '1'}
9129
9079
  if self._attr_block is None:
9130
9080
  field['@t'] = None
9131
9081
  else:
@@ -9147,6 +9097,296 @@ class MultiProgram(_Multiple):
9147
9097
 
9148
9098
  _typemap['Program'] = Program
9149
9099
 
9100
+ class ResetInstruction(Instruction):
9101
+ __slots__ = [
9102
+ '_attr_name',
9103
+ '_attr_operand',
9104
+ ]
9105
+
9106
+ def __init__(
9107
+ self,
9108
+ name=None,
9109
+ operand=None,
9110
+ annotations=None,
9111
+ ):
9112
+ super().__init__(annotations=annotations)
9113
+ self.name = name
9114
+ self.operand = operand
9115
+
9116
+ @property
9117
+ def name(self):
9118
+ return self._attr_name
9119
+
9120
+ @name.setter
9121
+ def name(self, val):
9122
+ if val is None:
9123
+ del self.name
9124
+ return
9125
+ if not isinstance(val, Identifier):
9126
+ # Try to "typecast" if this isn't an obvious mistake.
9127
+ if isinstance(val, Node):
9128
+ raise TypeError('name must be of type Identifier')
9129
+ val = Identifier(val)
9130
+ self._attr_name = val
9131
+
9132
+ @name.deleter
9133
+ def name(self):
9134
+ self._attr_name = None
9135
+
9136
+ @property
9137
+ def operand(self):
9138
+ return self._attr_operand
9139
+
9140
+ @operand.setter
9141
+ def operand(self, val):
9142
+ if val is None:
9143
+ del self.operand
9144
+ return
9145
+ if not isinstance(val, Expression):
9146
+ # Try to "typecast" if this isn't an obvious mistake.
9147
+ if isinstance(val, Node):
9148
+ raise TypeError('operand must be of type Expression')
9149
+ val = Expression(val)
9150
+ self._attr_operand = val
9151
+
9152
+ @operand.deleter
9153
+ def operand(self):
9154
+ self._attr_operand = None
9155
+
9156
+ def __eq__(self, other):
9157
+ """Equality operator. Ignores annotations!"""
9158
+ if not isinstance(other, ResetInstruction):
9159
+ return False
9160
+ if self.name != other.name:
9161
+ return False
9162
+ if self.operand != other.operand:
9163
+ return False
9164
+ if self.annotations != other.annotations:
9165
+ return False
9166
+ return True
9167
+
9168
+ def dump(self, indent=0, annotations=None, links=1):
9169
+ """Returns a debug representation of this tree as a multiline string.
9170
+ indent is the number of double spaces prefixed before every line.
9171
+ annotations, if specified, must be a set-like object containing the key
9172
+ strings of the annotations that are to be printed. links specifies the
9173
+ maximum link recursion depth."""
9174
+ s = [' '*indent]
9175
+ s.append('ResetInstruction(')
9176
+ if annotations is None:
9177
+ annotations = []
9178
+ for key in annotations:
9179
+ if key in self:
9180
+ s.append(' # {}: {}'.format(key, self[key]))
9181
+ s.append('\n')
9182
+ indent += 1
9183
+ s.append(' '*indent)
9184
+ s.append('name: ')
9185
+ if self.name is None:
9186
+ s.append('!MISSING\n')
9187
+ else:
9188
+ s.append('<\n')
9189
+ s.append(self.name.dump(indent + 1, annotations, links) + '\n')
9190
+ s.append(' '*indent + '>\n')
9191
+ s.append(' '*indent)
9192
+ s.append('operand: ')
9193
+ if self.operand is None:
9194
+ s.append('-\n')
9195
+ else:
9196
+ s.append('<\n')
9197
+ s.append(self.operand.dump(indent + 1, annotations, links) + '\n')
9198
+ s.append(' '*indent + '>\n')
9199
+ s.append(' '*indent)
9200
+ s.append('annotations: ')
9201
+ if not self.annotations:
9202
+ s.append('-\n')
9203
+ else:
9204
+ s.append('[\n')
9205
+ for child in self.annotations:
9206
+ s.append(child.dump(indent + 1, annotations, links) + '\n')
9207
+ s.append(' '*indent + ']\n')
9208
+ indent -= 1
9209
+ s.append(' '*indent)
9210
+ s.append(')')
9211
+ return ''.join(s)
9212
+
9213
+ __str__ = dump
9214
+ __repr__ = dump
9215
+
9216
+ def find_reachable(self, id_map=None):
9217
+ """Returns a dictionary mapping Python id() values to stable sequence
9218
+ numbers for all nodes in the tree rooted at this node. If id_map is
9219
+ specified, found nodes are appended to it."""
9220
+ if id_map is None:
9221
+ id_map = {}
9222
+ if id(self) in id_map:
9223
+ raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
9224
+ id_map[id(self)] = len(id_map)
9225
+ if self._attr_name is not None:
9226
+ self._attr_name.find_reachable(id_map)
9227
+ if self._attr_operand is not None:
9228
+ self._attr_operand.find_reachable(id_map)
9229
+ for el in self._attr_annotations:
9230
+ el.find_reachable(id_map)
9231
+ return id_map
9232
+
9233
+ def check_complete(self, id_map=None):
9234
+ """Raises NotWellFormed if the tree rooted at this node is not
9235
+ well-formed. If id_map is specified, this tree is only a subtree in the
9236
+ context of a larger tree, and id_map must be a dict mapping from Python
9237
+ id() codes to tree indices for all reachable nodes."""
9238
+ if id_map is None:
9239
+ id_map = self.find_reachable()
9240
+ if self._attr_name is None:
9241
+ raise NotWellFormed('name is required but not set')
9242
+ if self._attr_name is not None:
9243
+ self._attr_name.check_complete(id_map)
9244
+ if self._attr_operand is not None:
9245
+ self._attr_operand.check_complete(id_map)
9246
+ for child in self._attr_annotations:
9247
+ child.check_complete(id_map)
9248
+
9249
+ def copy(self):
9250
+ """Returns a shallow copy of this node."""
9251
+ return ResetInstruction(
9252
+ name=self._attr_name,
9253
+ operand=self._attr_operand,
9254
+ annotations=self._attr_annotations.copy()
9255
+ )
9256
+
9257
+ def clone(self):
9258
+ """Returns a deep copy of this node. This mimics the C++ interface,
9259
+ deficiencies with links included; that is, links always point to the
9260
+ original tree. If you're not cloning a subtree in a context where this
9261
+ is the desired behavior, you may want to use the copy.deepcopy() from
9262
+ the stdlib instead, which should copy links correctly."""
9263
+ return ResetInstruction(
9264
+ name=_cloned(self._attr_name),
9265
+ operand=_cloned(self._attr_operand),
9266
+ annotations=_cloned(self._attr_annotations)
9267
+ )
9268
+
9269
+ @staticmethod
9270
+ def _deserialize(cbor, seq_to_ob, links):
9271
+ """Attempts to deserialize the given cbor object (in Python primitive
9272
+ representation) into a node of this type. All (sub)nodes are added to
9273
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
9274
+ registered in the links list by means of a two-tuple of the setter
9275
+ function for the link field and the sequence number of the target node.
9276
+ """
9277
+ if not isinstance(cbor, dict):
9278
+ raise TypeError('node description object must be a dict')
9279
+ typ = cbor.get('@t', None)
9280
+ if typ is None:
9281
+ raise ValueError('type (@t) field is missing from node serialization')
9282
+ if typ != 'ResetInstruction':
9283
+ raise ValueError('found node serialization for ' + typ + ', but expected ResetInstruction')
9284
+
9285
+ # Deserialize the name field.
9286
+ field = cbor.get('name', None)
9287
+ if not isinstance(field, dict):
9288
+ raise ValueError('missing or invalid serialization of field name')
9289
+ if field.get('@T') != '1':
9290
+ raise ValueError('unexpected edge type for field name')
9291
+ if field.get('@t', None) is None:
9292
+ f_name = None
9293
+ else:
9294
+ f_name = Identifier._deserialize(field, seq_to_ob, links)
9295
+
9296
+ # Deserialize the operand field.
9297
+ field = cbor.get('operand', None)
9298
+ if not isinstance(field, dict):
9299
+ raise ValueError('missing or invalid serialization of field operand')
9300
+ if field.get('@T') != '?':
9301
+ raise ValueError('unexpected edge type for field operand')
9302
+ if field.get('@t', None) is None:
9303
+ f_operand = None
9304
+ else:
9305
+ f_operand = Expression._deserialize(field, seq_to_ob, links)
9306
+
9307
+ # Deserialize the annotations field.
9308
+ field = cbor.get('annotations', None)
9309
+ if not isinstance(field, dict):
9310
+ raise ValueError('missing or invalid serialization of field annotations')
9311
+ if field.get('@T') != '*':
9312
+ raise ValueError('unexpected edge type for field annotations')
9313
+ data = field.get('@d', None)
9314
+ if not isinstance(data, list):
9315
+ raise ValueError('missing serialization of Any/Many contents')
9316
+ f_annotations = MultiAnnotationData()
9317
+ for element in data:
9318
+ if element.get('@T') != '1':
9319
+ raise ValueError('unexpected edge type for Any/Many element')
9320
+ f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
9321
+
9322
+ # Construct the ResetInstruction node.
9323
+ node = ResetInstruction(f_name, f_operand, f_annotations)
9324
+
9325
+ # Deserialize annotations.
9326
+ for key, val in cbor.items():
9327
+ if not (key.startswith('{') and key.endswith('}')):
9328
+ continue
9329
+ key = key[1:-1]
9330
+ node[key] = cqasm.v3x.primitives.deserialize(key, val)
9331
+
9332
+ # Register node in sequence number lookup.
9333
+ seq = cbor.get('@i', None)
9334
+ if not isinstance(seq, int):
9335
+ raise ValueError('sequence number field (@i) is not an integer or missing from node serialization')
9336
+ if seq in seq_to_ob:
9337
+ raise ValueError('duplicate sequence number %d' % seq)
9338
+ seq_to_ob[seq] = node
9339
+
9340
+ return node
9341
+
9342
+ def _serialize(self, id_map):
9343
+ """Serializes this node to the Python primitive representation of its
9344
+ CBOR serialization. The tree that the node belongs to must be
9345
+ well-formed. id_map must match Python id() calls for all nodes to unique
9346
+ integers, to use for the sequence number representation of links."""
9347
+ cbor = {'@i': id_map[id(self)], '@t': 'ResetInstruction'}
9348
+
9349
+ # Serialize the name field.
9350
+ field = {'@T': '1'}
9351
+ if self._attr_name is None:
9352
+ field['@t'] = None
9353
+ else:
9354
+ field.update(self._attr_name._serialize(id_map))
9355
+ cbor['name'] = field
9356
+
9357
+ # Serialize the operand field.
9358
+ field = {'@T': '?'}
9359
+ if self._attr_operand is None:
9360
+ field['@t'] = None
9361
+ else:
9362
+ field.update(self._attr_operand._serialize(id_map))
9363
+ cbor['operand'] = field
9364
+
9365
+ # Serialize the annotations field.
9366
+ field = {'@T': '*'}
9367
+ lst = []
9368
+ for el in self._attr_annotations:
9369
+ el = el._serialize(id_map)
9370
+ el['@T'] = '1'
9371
+ lst.append(el)
9372
+ field['@d'] = lst
9373
+ cbor['annotations'] = field
9374
+
9375
+ # Serialize annotations.
9376
+ for key, val in self._annot.items():
9377
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
9378
+
9379
+ return cbor
9380
+
9381
+
9382
+ class MultiResetInstruction(_Multiple):
9383
+ """Wrapper for an edge with multiple ResetInstruction objects."""
9384
+
9385
+ _T = ResetInstruction
9386
+
9387
+
9388
+ _typemap['ResetInstruction'] = ResetInstruction
9389
+
9150
9390
  class ShiftExpression(BinaryExpression):
9151
9391
  __slots__ = []
9152
9392