libqasm 1.0.0__cp310-cp310-win_amd64.whl → 1.1.0__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/ast.py CHANGED
@@ -1174,6 +1174,8 @@ class Annotated(Node):
1174
1174
  return GateInstruction._deserialize(cbor, seq_to_ob, links)
1175
1175
  if typ == 'NonGateInstruction':
1176
1176
  return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1177
+ if typ == 'AsmDeclaration':
1178
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
1177
1179
  raise ValueError('unknown or unexpected type (@t) found in node serialization')
1178
1180
 
1179
1181
  def _serialize(self, id_map):
@@ -1523,6 +1525,473 @@ class MultiAnnotationData(_Multiple):
1523
1525
 
1524
1526
  _typemap['AnnotationData'] = AnnotationData
1525
1527
 
1528
+ class Statement(Annotated):
1529
+ __slots__ = []
1530
+
1531
+ def __init__(
1532
+ self,
1533
+ annotations=None,
1534
+ ):
1535
+ super().__init__(annotations=annotations)
1536
+
1537
+ @staticmethod
1538
+ def _deserialize(cbor, seq_to_ob, links):
1539
+ """Attempts to deserialize the given cbor object (in Python primitive
1540
+ representation) into a node of this type. All (sub)nodes are added to
1541
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
1542
+ registered in the links list by means of a two-tuple of the setter
1543
+ function for the link field and the sequence number of the target node.
1544
+ """
1545
+ if not isinstance(cbor, dict):
1546
+ raise TypeError('node description object must be a dict')
1547
+ typ = cbor.get('@t', None)
1548
+ if typ is None:
1549
+ raise ValueError('type (@t) field is missing from node serialization')
1550
+ if typ == 'Variable':
1551
+ return Variable._deserialize(cbor, seq_to_ob, links)
1552
+ if typ == 'GateInstruction':
1553
+ return GateInstruction._deserialize(cbor, seq_to_ob, links)
1554
+ if typ == 'NonGateInstruction':
1555
+ return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1556
+ if typ == 'AsmDeclaration':
1557
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
1558
+ raise ValueError('unknown or unexpected type (@t) found in node serialization')
1559
+
1560
+ def _serialize(self, id_map):
1561
+ """Serializes this node to the Python primitive representation of its
1562
+ CBOR serialization. The tree that the node belongs to must be
1563
+ well-formed. id_map must match Python id() calls for all nodes to unique
1564
+ integers, to use for the sequence number representation of links."""
1565
+ cbor = {'@i': id_map[id(self)], '@t': 'Statement'}
1566
+
1567
+ # Serialize the annotations field.
1568
+ field = {'@T': '*'}
1569
+ lst = []
1570
+ for el in self._attr_annotations:
1571
+ el = el._serialize(id_map)
1572
+ el['@T'] = '1'
1573
+ lst.append(el)
1574
+ field['@d'] = lst
1575
+ cbor['annotations'] = field
1576
+
1577
+ # Serialize annotations.
1578
+ for key, val in self._annot.items():
1579
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1580
+
1581
+ return cbor
1582
+
1583
+
1584
+ class MultiStatement(_Multiple):
1585
+ """Wrapper for an edge with multiple Statement objects."""
1586
+
1587
+ _T = Statement
1588
+
1589
+
1590
+ _typemap['Statement'] = Statement
1591
+
1592
+ class BlockStatement(Statement):
1593
+ __slots__ = []
1594
+
1595
+ def __init__(
1596
+ self,
1597
+ annotations=None,
1598
+ ):
1599
+ super().__init__(annotations=annotations)
1600
+
1601
+ @staticmethod
1602
+ def _deserialize(cbor, seq_to_ob, links):
1603
+ """Attempts to deserialize the given cbor object (in Python primitive
1604
+ representation) into a node of this type. All (sub)nodes are added to
1605
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
1606
+ registered in the links list by means of a two-tuple of the setter
1607
+ function for the link field and the sequence number of the target node.
1608
+ """
1609
+ if not isinstance(cbor, dict):
1610
+ raise TypeError('node description object must be a dict')
1611
+ typ = cbor.get('@t', None)
1612
+ if typ is None:
1613
+ raise ValueError('type (@t) field is missing from node serialization')
1614
+ if typ == 'Variable':
1615
+ return Variable._deserialize(cbor, seq_to_ob, links)
1616
+ if typ == 'GateInstruction':
1617
+ return GateInstruction._deserialize(cbor, seq_to_ob, links)
1618
+ if typ == 'NonGateInstruction':
1619
+ return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1620
+ if typ == 'AsmDeclaration':
1621
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
1622
+ raise ValueError('unknown or unexpected type (@t) found in node serialization')
1623
+
1624
+ def _serialize(self, id_map):
1625
+ """Serializes this node to the Python primitive representation of its
1626
+ CBOR serialization. The tree that the node belongs to must be
1627
+ well-formed. id_map must match Python id() calls for all nodes to unique
1628
+ integers, to use for the sequence number representation of links."""
1629
+ cbor = {'@i': id_map[id(self)], '@t': 'BlockStatement'}
1630
+
1631
+ # Serialize the annotations field.
1632
+ field = {'@T': '*'}
1633
+ lst = []
1634
+ for el in self._attr_annotations:
1635
+ el = el._serialize(id_map)
1636
+ el['@T'] = '1'
1637
+ lst.append(el)
1638
+ field['@d'] = lst
1639
+ cbor['annotations'] = field
1640
+
1641
+ # Serialize annotations.
1642
+ for key, val in self._annot.items():
1643
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1644
+
1645
+ return cbor
1646
+
1647
+
1648
+ class MultiBlockStatement(_Multiple):
1649
+ """Wrapper for an edge with multiple BlockStatement objects."""
1650
+
1651
+ _T = BlockStatement
1652
+
1653
+
1654
+ _typemap['BlockStatement'] = BlockStatement
1655
+
1656
+ class Instruction(BlockStatement):
1657
+ __slots__ = []
1658
+
1659
+ def __init__(
1660
+ self,
1661
+ annotations=None,
1662
+ ):
1663
+ super().__init__(annotations=annotations)
1664
+
1665
+ @staticmethod
1666
+ def _deserialize(cbor, seq_to_ob, links):
1667
+ """Attempts to deserialize the given cbor object (in Python primitive
1668
+ representation) into a node of this type. All (sub)nodes are added to
1669
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
1670
+ registered in the links list by means of a two-tuple of the setter
1671
+ function for the link field and the sequence number of the target node.
1672
+ """
1673
+ if not isinstance(cbor, dict):
1674
+ raise TypeError('node description object must be a dict')
1675
+ typ = cbor.get('@t', None)
1676
+ if typ is None:
1677
+ raise ValueError('type (@t) field is missing from node serialization')
1678
+ if typ == 'GateInstruction':
1679
+ return GateInstruction._deserialize(cbor, seq_to_ob, links)
1680
+ if typ == 'NonGateInstruction':
1681
+ return NonGateInstruction._deserialize(cbor, seq_to_ob, links)
1682
+ if typ == 'AsmDeclaration':
1683
+ return AsmDeclaration._deserialize(cbor, seq_to_ob, links)
1684
+ raise ValueError('unknown or unexpected type (@t) found in node serialization')
1685
+
1686
+ def _serialize(self, id_map):
1687
+ """Serializes this node to the Python primitive representation of its
1688
+ CBOR serialization. The tree that the node belongs to must be
1689
+ well-formed. id_map must match Python id() calls for all nodes to unique
1690
+ integers, to use for the sequence number representation of links."""
1691
+ cbor = {'@i': id_map[id(self)], '@t': 'Instruction'}
1692
+
1693
+ # Serialize the annotations field.
1694
+ field = {'@T': '*'}
1695
+ lst = []
1696
+ for el in self._attr_annotations:
1697
+ el = el._serialize(id_map)
1698
+ el['@T'] = '1'
1699
+ lst.append(el)
1700
+ field['@d'] = lst
1701
+ cbor['annotations'] = field
1702
+
1703
+ # Serialize annotations.
1704
+ for key, val in self._annot.items():
1705
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1706
+
1707
+ return cbor
1708
+
1709
+
1710
+ class MultiInstruction(_Multiple):
1711
+ """Wrapper for an edge with multiple Instruction objects."""
1712
+
1713
+ _T = Instruction
1714
+
1715
+
1716
+ _typemap['Instruction'] = Instruction
1717
+
1718
+ class AsmDeclaration(Instruction):
1719
+ __slots__ = [
1720
+ '_attr_backend_name',
1721
+ '_attr_backend_code',
1722
+ ]
1723
+
1724
+ def __init__(
1725
+ self,
1726
+ backend_name=None,
1727
+ backend_code=None,
1728
+ annotations=None,
1729
+ ):
1730
+ super().__init__(annotations=annotations)
1731
+ self.backend_name = backend_name
1732
+ self.backend_code = backend_code
1733
+
1734
+ @property
1735
+ def backend_name(self):
1736
+ return self._attr_backend_name
1737
+
1738
+ @backend_name.setter
1739
+ def backend_name(self, val):
1740
+ if val is None:
1741
+ del self.backend_name
1742
+ return
1743
+ if not isinstance(val, Identifier):
1744
+ # Try to "typecast" if this isn't an obvious mistake.
1745
+ if isinstance(val, Node):
1746
+ raise TypeError('backend_name must be of type Identifier')
1747
+ val = Identifier(val)
1748
+ self._attr_backend_name = val
1749
+
1750
+ @backend_name.deleter
1751
+ def backend_name(self):
1752
+ self._attr_backend_name = None
1753
+
1754
+ @property
1755
+ def backend_code(self):
1756
+ return self._attr_backend_code
1757
+
1758
+ @backend_code.setter
1759
+ def backend_code(self, val):
1760
+ if val is None:
1761
+ del self.backend_code
1762
+ return
1763
+ if not isinstance(val, cqasm.v3x.primitives.Str):
1764
+ # Try to "typecast" if this isn't an obvious mistake.
1765
+ if isinstance(val, Node):
1766
+ raise TypeError('backend_code must be of type cqasm.v3x.primitives.Str')
1767
+ val = cqasm.v3x.primitives.Str(val)
1768
+ self._attr_backend_code = val
1769
+
1770
+ @backend_code.deleter
1771
+ def backend_code(self):
1772
+ self._attr_backend_code = cqasm.v3x.primitives.Str()
1773
+
1774
+ def __eq__(self, other):
1775
+ """Equality operator. Ignores annotations!"""
1776
+ if not isinstance(other, AsmDeclaration):
1777
+ return False
1778
+ if self.backend_name != other.backend_name:
1779
+ return False
1780
+ if self.backend_code != other.backend_code:
1781
+ return False
1782
+ if self.annotations != other.annotations:
1783
+ return False
1784
+ return True
1785
+
1786
+ def dump(self, indent=0, annotations=None, links=1):
1787
+ """Returns a debug representation of this tree as a multiline string.
1788
+ indent is the number of double spaces prefixed before every line.
1789
+ annotations, if specified, must be a set-like object containing the key
1790
+ strings of the annotations that are to be printed. links specifies the
1791
+ maximum link recursion depth."""
1792
+ s = [' '*indent]
1793
+ s.append('AsmDeclaration(')
1794
+ if annotations is None:
1795
+ annotations = []
1796
+ for key in annotations:
1797
+ if key in self:
1798
+ s.append(' # {}: {}'.format(key, self[key]))
1799
+ s.append('\n')
1800
+ indent += 1
1801
+ s.append(' '*indent)
1802
+ s.append('backend_name: ')
1803
+ if self.backend_name is None:
1804
+ s.append('!MISSING\n')
1805
+ else:
1806
+ s.append('<\n')
1807
+ s.append(self.backend_name.dump(indent + 1, annotations, links) + '\n')
1808
+ s.append(' '*indent + '>\n')
1809
+ s.append(' '*indent)
1810
+ s.append('backend_code: ')
1811
+ s.append(str(self.backend_code) + '\n')
1812
+ s.append(' '*indent)
1813
+ s.append('annotations: ')
1814
+ if not self.annotations:
1815
+ s.append('-\n')
1816
+ else:
1817
+ s.append('[\n')
1818
+ for child in self.annotations:
1819
+ s.append(child.dump(indent + 1, annotations, links) + '\n')
1820
+ s.append(' '*indent + ']\n')
1821
+ indent -= 1
1822
+ s.append(' '*indent)
1823
+ s.append(')')
1824
+ return ''.join(s)
1825
+
1826
+ __str__ = dump
1827
+ __repr__ = dump
1828
+
1829
+ def find_reachable(self, id_map=None):
1830
+ """Returns a dictionary mapping Python id() values to stable sequence
1831
+ numbers for all nodes in the tree rooted at this node. If id_map is
1832
+ specified, found nodes are appended to it."""
1833
+ if id_map is None:
1834
+ id_map = {}
1835
+ if id(self) in id_map:
1836
+ raise NotWellFormed('node {!r} with id {} occurs more than once'.format(self, id(self)))
1837
+ id_map[id(self)] = len(id_map)
1838
+ if self._attr_backend_name is not None:
1839
+ self._attr_backend_name.find_reachable(id_map)
1840
+ for el in self._attr_annotations:
1841
+ el.find_reachable(id_map)
1842
+ return id_map
1843
+
1844
+ def check_complete(self, id_map=None):
1845
+ """Raises NotWellFormed if the tree rooted at this node is not
1846
+ well-formed. If id_map is specified, this tree is only a subtree in the
1847
+ context of a larger tree, and id_map must be a dict mapping from Python
1848
+ id() codes to tree indices for all reachable nodes."""
1849
+ if id_map is None:
1850
+ id_map = self.find_reachable()
1851
+ if self._attr_backend_name is None:
1852
+ raise NotWellFormed('backend_name is required but not set')
1853
+ if self._attr_backend_name is not None:
1854
+ self._attr_backend_name.check_complete(id_map)
1855
+ for child in self._attr_annotations:
1856
+ child.check_complete(id_map)
1857
+
1858
+ def copy(self):
1859
+ """Returns a shallow copy of this node."""
1860
+ return AsmDeclaration(
1861
+ backend_name=self._attr_backend_name,
1862
+ backend_code=self._attr_backend_code,
1863
+ annotations=self._attr_annotations.copy()
1864
+ )
1865
+
1866
+ def clone(self):
1867
+ """Returns a deep copy of this node. This mimics the C++ interface,
1868
+ deficiencies with links included; that is, links always point to the
1869
+ original tree. If you're not cloning a subtree in a context where this
1870
+ is the desired behavior, you may want to use the copy.deepcopy() from
1871
+ the stdlib instead, which should copy links correctly."""
1872
+ return AsmDeclaration(
1873
+ backend_name=_cloned(self._attr_backend_name),
1874
+ backend_code=_cloned(self._attr_backend_code),
1875
+ annotations=_cloned(self._attr_annotations)
1876
+ )
1877
+
1878
+ @staticmethod
1879
+ def _deserialize(cbor, seq_to_ob, links):
1880
+ """Attempts to deserialize the given cbor object (in Python primitive
1881
+ representation) into a node of this type. All (sub)nodes are added to
1882
+ the seq_to_ob dict, indexed by their cbor sequence number. All links are
1883
+ registered in the links list by means of a two-tuple of the setter
1884
+ function for the link field and the sequence number of the target node.
1885
+ """
1886
+ if not isinstance(cbor, dict):
1887
+ raise TypeError('node description object must be a dict')
1888
+ typ = cbor.get('@t', None)
1889
+ if typ is None:
1890
+ raise ValueError('type (@t) field is missing from node serialization')
1891
+ if typ != 'AsmDeclaration':
1892
+ raise ValueError('found node serialization for ' + typ + ', but expected AsmDeclaration')
1893
+
1894
+ # Deserialize the backend_name field.
1895
+ field = cbor.get('backend_name', None)
1896
+ if not isinstance(field, dict):
1897
+ raise ValueError('missing or invalid serialization of field backend_name')
1898
+ if field.get('@T') != '1':
1899
+ raise ValueError('unexpected edge type for field backend_name')
1900
+ if field.get('@t', None) is None:
1901
+ f_backend_name = None
1902
+ else:
1903
+ f_backend_name = Identifier._deserialize(field, seq_to_ob, links)
1904
+
1905
+ # Deserialize the backend_code field.
1906
+ field = cbor.get('backend_code', None)
1907
+ if not isinstance(field, dict):
1908
+ raise ValueError('missing or invalid serialization of field backend_code')
1909
+ if hasattr(cqasm.v3x.primitives.Str, 'deserialize_cbor'):
1910
+ f_backend_code = cqasm.v3x.primitives.Str.deserialize_cbor(field)
1911
+ else:
1912
+ f_backend_code = cqasm.v3x.primitives.deserialize(cqasm.v3x.primitives.Str, field)
1913
+
1914
+ # Deserialize the annotations field.
1915
+ field = cbor.get('annotations', None)
1916
+ if not isinstance(field, dict):
1917
+ raise ValueError('missing or invalid serialization of field annotations')
1918
+ if field.get('@T') != '*':
1919
+ raise ValueError('unexpected edge type for field annotations')
1920
+ data = field.get('@d', None)
1921
+ if not isinstance(data, list):
1922
+ raise ValueError('missing serialization of Any/Many contents')
1923
+ f_annotations = MultiAnnotationData()
1924
+ for element in data:
1925
+ if element.get('@T') != '1':
1926
+ raise ValueError('unexpected edge type for Any/Many element')
1927
+ f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
1928
+
1929
+ # Construct the AsmDeclaration node.
1930
+ node = AsmDeclaration(f_backend_name, f_backend_code, f_annotations)
1931
+
1932
+ # Deserialize annotations.
1933
+ for key, val in cbor.items():
1934
+ if not (key.startswith('{') and key.endswith('}')):
1935
+ continue
1936
+ key = key[1:-1]
1937
+ node[key] = cqasm.v3x.primitives.deserialize(key, val)
1938
+
1939
+ # Register node in sequence number lookup.
1940
+ seq = cbor.get('@i', None)
1941
+ if not isinstance(seq, int):
1942
+ raise ValueError('sequence number field (@i) is not an integer or missing from node serialization')
1943
+ if seq in seq_to_ob:
1944
+ raise ValueError('duplicate sequence number %d' % seq)
1945
+ seq_to_ob[seq] = node
1946
+
1947
+ return node
1948
+
1949
+ def _serialize(self, id_map):
1950
+ """Serializes this node to the Python primitive representation of its
1951
+ CBOR serialization. The tree that the node belongs to must be
1952
+ well-formed. id_map must match Python id() calls for all nodes to unique
1953
+ integers, to use for the sequence number representation of links."""
1954
+ cbor = {'@i': id_map[id(self)], '@t': 'AsmDeclaration'}
1955
+
1956
+ # Serialize the backend_name field.
1957
+ field = {'@T': '1'}
1958
+ if self._attr_backend_name is None:
1959
+ field['@t'] = None
1960
+ else:
1961
+ field.update(self._attr_backend_name._serialize(id_map))
1962
+ cbor['backend_name'] = field
1963
+
1964
+ # Serialize the backend_code field.
1965
+ if hasattr(self._attr_backend_code, 'serialize_cbor'):
1966
+ cbor['backend_code'] = self._attr_backend_code.serialize_cbor()
1967
+ else:
1968
+ cbor['backend_code'] = cqasm.v3x.primitives.serialize(cqasm.v3x.primitives.Str, self._attr_backend_code)
1969
+
1970
+ # Serialize the annotations field.
1971
+ field = {'@T': '*'}
1972
+ lst = []
1973
+ for el in self._attr_annotations:
1974
+ el = el._serialize(id_map)
1975
+ el['@T'] = '1'
1976
+ lst.append(el)
1977
+ field['@d'] = lst
1978
+ cbor['annotations'] = field
1979
+
1980
+ # Serialize annotations.
1981
+ for key, val in self._annot.items():
1982
+ cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
1983
+
1984
+ return cbor
1985
+
1986
+
1987
+ class MultiAsmDeclaration(_Multiple):
1988
+ """Wrapper for an edge with multiple AsmDeclaration objects."""
1989
+
1990
+ _T = AsmDeclaration
1991
+
1992
+
1993
+ _typemap['AsmDeclaration'] = AsmDeclaration
1994
+
1526
1995
  class BitwiseExpression(BinaryExpression):
1527
1996
  __slots__ = []
1528
1997
 
@@ -2500,130 +2969,6 @@ class MultiBlock(_Multiple):
2500
2969
 
2501
2970
  _typemap['Block'] = Block
2502
2971
 
2503
- class Statement(Annotated):
2504
- __slots__ = []
2505
-
2506
- def __init__(
2507
- self,
2508
- annotations=None,
2509
- ):
2510
- super().__init__(annotations=annotations)
2511
-
2512
- @staticmethod
2513
- def _deserialize(cbor, seq_to_ob, links):
2514
- """Attempts to deserialize the given cbor object (in Python primitive
2515
- representation) into a node of this type. All (sub)nodes are added to
2516
- the seq_to_ob dict, indexed by their cbor sequence number. All links are
2517
- registered in the links list by means of a two-tuple of the setter
2518
- function for the link field and the sequence number of the target node.
2519
- """
2520
- if not isinstance(cbor, dict):
2521
- raise TypeError('node description object must be a dict')
2522
- typ = cbor.get('@t', None)
2523
- if typ is None:
2524
- raise ValueError('type (@t) field is missing from node serialization')
2525
- if typ == 'Variable':
2526
- return Variable._deserialize(cbor, seq_to_ob, links)
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)
2531
- raise ValueError('unknown or unexpected type (@t) found in node serialization')
2532
-
2533
- def _serialize(self, id_map):
2534
- """Serializes this node to the Python primitive representation of its
2535
- CBOR serialization. The tree that the node belongs to must be
2536
- well-formed. id_map must match Python id() calls for all nodes to unique
2537
- integers, to use for the sequence number representation of links."""
2538
- cbor = {'@i': id_map[id(self)], '@t': 'Statement'}
2539
-
2540
- # Serialize the annotations field.
2541
- field = {'@T': '*'}
2542
- lst = []
2543
- for el in self._attr_annotations:
2544
- el = el._serialize(id_map)
2545
- el['@T'] = '1'
2546
- lst.append(el)
2547
- field['@d'] = lst
2548
- cbor['annotations'] = field
2549
-
2550
- # Serialize annotations.
2551
- for key, val in self._annot.items():
2552
- cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
2553
-
2554
- return cbor
2555
-
2556
-
2557
- class MultiStatement(_Multiple):
2558
- """Wrapper for an edge with multiple Statement objects."""
2559
-
2560
- _T = Statement
2561
-
2562
-
2563
- _typemap['Statement'] = Statement
2564
-
2565
- class BlockStatement(Statement):
2566
- __slots__ = []
2567
-
2568
- def __init__(
2569
- self,
2570
- annotations=None,
2571
- ):
2572
- super().__init__(annotations=annotations)
2573
-
2574
- @staticmethod
2575
- def _deserialize(cbor, seq_to_ob, links):
2576
- """Attempts to deserialize the given cbor object (in Python primitive
2577
- representation) into a node of this type. All (sub)nodes are added to
2578
- the seq_to_ob dict, indexed by their cbor sequence number. All links are
2579
- registered in the links list by means of a two-tuple of the setter
2580
- function for the link field and the sequence number of the target node.
2581
- """
2582
- if not isinstance(cbor, dict):
2583
- raise TypeError('node description object must be a dict')
2584
- typ = cbor.get('@t', None)
2585
- if typ is None:
2586
- raise ValueError('type (@t) field is missing from node serialization')
2587
- if typ == 'Variable':
2588
- return Variable._deserialize(cbor, seq_to_ob, links)
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)
2593
- raise ValueError('unknown or unexpected type (@t) found in node serialization')
2594
-
2595
- def _serialize(self, id_map):
2596
- """Serializes this node to the Python primitive representation of its
2597
- CBOR serialization. The tree that the node belongs to must be
2598
- well-formed. id_map must match Python id() calls for all nodes to unique
2599
- integers, to use for the sequence number representation of links."""
2600
- cbor = {'@i': id_map[id(self)], '@t': 'BlockStatement'}
2601
-
2602
- # Serialize the annotations field.
2603
- field = {'@T': '*'}
2604
- lst = []
2605
- for el in self._attr_annotations:
2606
- el = el._serialize(id_map)
2607
- el['@T'] = '1'
2608
- lst.append(el)
2609
- field['@d'] = lst
2610
- cbor['annotations'] = field
2611
-
2612
- # Serialize annotations.
2613
- for key, val in self._annot.items():
2614
- cbor['{%s}' % key] = _py_to_cbor(cqasm.v3x.primitives.serialize(key, val))
2615
-
2616
- return cbor
2617
-
2618
-
2619
- class MultiBlockStatement(_Multiple):
2620
- """Wrapper for an edge with multiple BlockStatement objects."""
2621
-
2622
- _T = BlockStatement
2623
-
2624
-
2625
- _typemap['BlockStatement'] = BlockStatement
2626
-
2627
2972
  class BooleanLiteral(Expression):
2628
2973
  __slots__ = [
2629
2974
  '_attr_value',
@@ -4986,20 +5331,20 @@ class Gate(Annotated):
4986
5331
  __slots__ = [
4987
5332
  '_attr_name',
4988
5333
  '_attr_gate',
4989
- '_attr_parameter',
5334
+ '_attr_parameters',
4990
5335
  ]
4991
5336
 
4992
5337
  def __init__(
4993
5338
  self,
4994
5339
  name=None,
4995
5340
  gate=None,
4996
- parameter=None,
5341
+ parameters=None,
4997
5342
  annotations=None,
4998
5343
  ):
4999
5344
  super().__init__(annotations=annotations)
5000
5345
  self.name = name
5001
5346
  self.gate = gate
5002
- self.parameter = parameter
5347
+ self.parameters = parameters
5003
5348
 
5004
5349
  @property
5005
5350
  def name(self):
@@ -5042,24 +5387,24 @@ class Gate(Annotated):
5042
5387
  self._attr_gate = None
5043
5388
 
5044
5389
  @property
5045
- def parameter(self):
5046
- return self._attr_parameter
5390
+ def parameters(self):
5391
+ return self._attr_parameters
5047
5392
 
5048
- @parameter.setter
5049
- def parameter(self, val):
5393
+ @parameters.setter
5394
+ def parameters(self, val):
5050
5395
  if val is None:
5051
- del self.parameter
5396
+ del self.parameters
5052
5397
  return
5053
- if not isinstance(val, Expression):
5398
+ if not isinstance(val, ExpressionList):
5054
5399
  # Try to "typecast" if this isn't an obvious mistake.
5055
5400
  if isinstance(val, Node):
5056
- raise TypeError('parameter must be of type Expression')
5057
- val = Expression(val)
5058
- self._attr_parameter = val
5401
+ raise TypeError('parameters must be of type ExpressionList')
5402
+ val = ExpressionList(val)
5403
+ self._attr_parameters = val
5059
5404
 
5060
- @parameter.deleter
5061
- def parameter(self):
5062
- self._attr_parameter = None
5405
+ @parameters.deleter
5406
+ def parameters(self):
5407
+ self._attr_parameters = None
5063
5408
 
5064
5409
  def __eq__(self, other):
5065
5410
  """Equality operator. Ignores annotations!"""
@@ -5069,7 +5414,7 @@ class Gate(Annotated):
5069
5414
  return False
5070
5415
  if self.gate != other.gate:
5071
5416
  return False
5072
- if self.parameter != other.parameter:
5417
+ if self.parameters != other.parameters:
5073
5418
  return False
5074
5419
  if self.annotations != other.annotations:
5075
5420
  return False
@@ -5107,12 +5452,12 @@ class Gate(Annotated):
5107
5452
  s.append(self.gate.dump(indent + 1, annotations, links) + '\n')
5108
5453
  s.append(' '*indent + '>\n')
5109
5454
  s.append(' '*indent)
5110
- s.append('parameter: ')
5111
- if self.parameter is None:
5112
- s.append('-\n')
5455
+ s.append('parameters: ')
5456
+ if self.parameters is None:
5457
+ s.append('!MISSING\n')
5113
5458
  else:
5114
5459
  s.append('<\n')
5115
- s.append(self.parameter.dump(indent + 1, annotations, links) + '\n')
5460
+ s.append(self.parameters.dump(indent + 1, annotations, links) + '\n')
5116
5461
  s.append(' '*indent + '>\n')
5117
5462
  s.append(' '*indent)
5118
5463
  s.append('annotations: ')
@@ -5144,8 +5489,8 @@ class Gate(Annotated):
5144
5489
  self._attr_name.find_reachable(id_map)
5145
5490
  if self._attr_gate is not None:
5146
5491
  self._attr_gate.find_reachable(id_map)
5147
- if self._attr_parameter is not None:
5148
- self._attr_parameter.find_reachable(id_map)
5492
+ if self._attr_parameters is not None:
5493
+ self._attr_parameters.find_reachable(id_map)
5149
5494
  for el in self._attr_annotations:
5150
5495
  el.find_reachable(id_map)
5151
5496
  return id_map
@@ -5163,8 +5508,10 @@ class Gate(Annotated):
5163
5508
  self._attr_name.check_complete(id_map)
5164
5509
  if self._attr_gate is not None:
5165
5510
  self._attr_gate.check_complete(id_map)
5166
- if self._attr_parameter is not None:
5167
- self._attr_parameter.check_complete(id_map)
5511
+ if self._attr_parameters is None:
5512
+ raise NotWellFormed('parameters is required but not set')
5513
+ if self._attr_parameters is not None:
5514
+ self._attr_parameters.check_complete(id_map)
5168
5515
  for child in self._attr_annotations:
5169
5516
  child.check_complete(id_map)
5170
5517
 
@@ -5173,7 +5520,7 @@ class Gate(Annotated):
5173
5520
  return Gate(
5174
5521
  name=self._attr_name,
5175
5522
  gate=self._attr_gate,
5176
- parameter=self._attr_parameter,
5523
+ parameters=self._attr_parameters,
5177
5524
  annotations=self._attr_annotations.copy()
5178
5525
  )
5179
5526
 
@@ -5186,7 +5533,7 @@ class Gate(Annotated):
5186
5533
  return Gate(
5187
5534
  name=_cloned(self._attr_name),
5188
5535
  gate=_cloned(self._attr_gate),
5189
- parameter=_cloned(self._attr_parameter),
5536
+ parameters=_cloned(self._attr_parameters),
5190
5537
  annotations=_cloned(self._attr_annotations)
5191
5538
  )
5192
5539
 
@@ -5228,16 +5575,16 @@ class Gate(Annotated):
5228
5575
  else:
5229
5576
  f_gate = Gate._deserialize(field, seq_to_ob, links)
5230
5577
 
5231
- # Deserialize the parameter field.
5232
- field = cbor.get('parameter', None)
5578
+ # Deserialize the parameters field.
5579
+ field = cbor.get('parameters', None)
5233
5580
  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')
5581
+ raise ValueError('missing or invalid serialization of field parameters')
5582
+ if field.get('@T') != '1':
5583
+ raise ValueError('unexpected edge type for field parameters')
5237
5584
  if field.get('@t', None) is None:
5238
- f_parameter = None
5585
+ f_parameters = None
5239
5586
  else:
5240
- f_parameter = Expression._deserialize(field, seq_to_ob, links)
5587
+ f_parameters = ExpressionList._deserialize(field, seq_to_ob, links)
5241
5588
 
5242
5589
  # Deserialize the annotations field.
5243
5590
  field = cbor.get('annotations', None)
@@ -5255,7 +5602,7 @@ class Gate(Annotated):
5255
5602
  f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
5256
5603
 
5257
5604
  # Construct the Gate node.
5258
- node = Gate(f_name, f_gate, f_parameter, f_annotations)
5605
+ node = Gate(f_name, f_gate, f_parameters, f_annotations)
5259
5606
 
5260
5607
  # Deserialize annotations.
5261
5608
  for key, val in cbor.items():
@@ -5297,13 +5644,13 @@ class Gate(Annotated):
5297
5644
  field.update(self._attr_gate._serialize(id_map))
5298
5645
  cbor['gate'] = field
5299
5646
 
5300
- # Serialize the parameter field.
5301
- field = {'@T': '?'}
5302
- if self._attr_parameter is None:
5647
+ # Serialize the parameters field.
5648
+ field = {'@T': '1'}
5649
+ if self._attr_parameters is None:
5303
5650
  field['@t'] = None
5304
5651
  else:
5305
- field.update(self._attr_parameter._serialize(id_map))
5306
- cbor['parameter'] = field
5652
+ field.update(self._attr_parameters._serialize(id_map))
5653
+ cbor['parameters'] = field
5307
5654
 
5308
5655
  # Serialize the annotations field.
5309
5656
  field = {'@T': '*'}
@@ -5330,66 +5677,6 @@ class MultiGate(_Multiple):
5330
5677
 
5331
5678
  _typemap['Gate'] = Gate
5332
5679
 
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
5680
  class GateInstruction(Instruction):
5394
5681
  __slots__ = [
5395
5682
  '_attr_gate',
@@ -8387,20 +8674,20 @@ class NonGateInstruction(Instruction):
8387
8674
  __slots__ = [
8388
8675
  '_attr_name',
8389
8676
  '_attr_operands',
8390
- '_attr_parameter',
8677
+ '_attr_parameters',
8391
8678
  ]
8392
8679
 
8393
8680
  def __init__(
8394
8681
  self,
8395
8682
  name=None,
8396
8683
  operands=None,
8397
- parameter=None,
8684
+ parameters=None,
8398
8685
  annotations=None,
8399
8686
  ):
8400
8687
  super().__init__(annotations=annotations)
8401
8688
  self.name = name
8402
8689
  self.operands = operands
8403
- self.parameter = parameter
8690
+ self.parameters = parameters
8404
8691
 
8405
8692
  @property
8406
8693
  def name(self):
@@ -8443,24 +8730,24 @@ class NonGateInstruction(Instruction):
8443
8730
  self._attr_operands = None
8444
8731
 
8445
8732
  @property
8446
- def parameter(self):
8447
- return self._attr_parameter
8733
+ def parameters(self):
8734
+ return self._attr_parameters
8448
8735
 
8449
- @parameter.setter
8450
- def parameter(self, val):
8736
+ @parameters.setter
8737
+ def parameters(self, val):
8451
8738
  if val is None:
8452
- del self.parameter
8739
+ del self.parameters
8453
8740
  return
8454
- if not isinstance(val, Expression):
8741
+ if not isinstance(val, ExpressionList):
8455
8742
  # Try to "typecast" if this isn't an obvious mistake.
8456
8743
  if isinstance(val, Node):
8457
- raise TypeError('parameter must be of type Expression')
8458
- val = Expression(val)
8459
- self._attr_parameter = val
8744
+ raise TypeError('parameters must be of type ExpressionList')
8745
+ val = ExpressionList(val)
8746
+ self._attr_parameters = val
8460
8747
 
8461
- @parameter.deleter
8462
- def parameter(self):
8463
- self._attr_parameter = None
8748
+ @parameters.deleter
8749
+ def parameters(self):
8750
+ self._attr_parameters = None
8464
8751
 
8465
8752
  def __eq__(self, other):
8466
8753
  """Equality operator. Ignores annotations!"""
@@ -8470,7 +8757,7 @@ class NonGateInstruction(Instruction):
8470
8757
  return False
8471
8758
  if self.operands != other.operands:
8472
8759
  return False
8473
- if self.parameter != other.parameter:
8760
+ if self.parameters != other.parameters:
8474
8761
  return False
8475
8762
  if self.annotations != other.annotations:
8476
8763
  return False
@@ -8508,12 +8795,12 @@ class NonGateInstruction(Instruction):
8508
8795
  s.append(self.operands.dump(indent + 1, annotations, links) + '\n')
8509
8796
  s.append(' '*indent + '>\n')
8510
8797
  s.append(' '*indent)
8511
- s.append('parameter: ')
8512
- if self.parameter is None:
8513
- s.append('-\n')
8798
+ s.append('parameters: ')
8799
+ if self.parameters is None:
8800
+ s.append('!MISSING\n')
8514
8801
  else:
8515
8802
  s.append('<\n')
8516
- s.append(self.parameter.dump(indent + 1, annotations, links) + '\n')
8803
+ s.append(self.parameters.dump(indent + 1, annotations, links) + '\n')
8517
8804
  s.append(' '*indent + '>\n')
8518
8805
  s.append(' '*indent)
8519
8806
  s.append('annotations: ')
@@ -8545,8 +8832,8 @@ class NonGateInstruction(Instruction):
8545
8832
  self._attr_name.find_reachable(id_map)
8546
8833
  if self._attr_operands is not None:
8547
8834
  self._attr_operands.find_reachable(id_map)
8548
- if self._attr_parameter is not None:
8549
- self._attr_parameter.find_reachable(id_map)
8835
+ if self._attr_parameters is not None:
8836
+ self._attr_parameters.find_reachable(id_map)
8550
8837
  for el in self._attr_annotations:
8551
8838
  el.find_reachable(id_map)
8552
8839
  return id_map
@@ -8566,8 +8853,10 @@ class NonGateInstruction(Instruction):
8566
8853
  raise NotWellFormed('operands is required but not set')
8567
8854
  if self._attr_operands is not None:
8568
8855
  self._attr_operands.check_complete(id_map)
8569
- if self._attr_parameter is not None:
8570
- self._attr_parameter.check_complete(id_map)
8856
+ if self._attr_parameters is None:
8857
+ raise NotWellFormed('parameters is required but not set')
8858
+ if self._attr_parameters is not None:
8859
+ self._attr_parameters.check_complete(id_map)
8571
8860
  for child in self._attr_annotations:
8572
8861
  child.check_complete(id_map)
8573
8862
 
@@ -8576,7 +8865,7 @@ class NonGateInstruction(Instruction):
8576
8865
  return NonGateInstruction(
8577
8866
  name=self._attr_name,
8578
8867
  operands=self._attr_operands,
8579
- parameter=self._attr_parameter,
8868
+ parameters=self._attr_parameters,
8580
8869
  annotations=self._attr_annotations.copy()
8581
8870
  )
8582
8871
 
@@ -8589,7 +8878,7 @@ class NonGateInstruction(Instruction):
8589
8878
  return NonGateInstruction(
8590
8879
  name=_cloned(self._attr_name),
8591
8880
  operands=_cloned(self._attr_operands),
8592
- parameter=_cloned(self._attr_parameter),
8881
+ parameters=_cloned(self._attr_parameters),
8593
8882
  annotations=_cloned(self._attr_annotations)
8594
8883
  )
8595
8884
 
@@ -8631,16 +8920,16 @@ class NonGateInstruction(Instruction):
8631
8920
  else:
8632
8921
  f_operands = ExpressionList._deserialize(field, seq_to_ob, links)
8633
8922
 
8634
- # Deserialize the parameter field.
8635
- field = cbor.get('parameter', None)
8923
+ # Deserialize the parameters field.
8924
+ field = cbor.get('parameters', None)
8636
8925
  if not isinstance(field, dict):
8637
- raise ValueError('missing or invalid serialization of field parameter')
8638
- if field.get('@T') != '?':
8639
- raise ValueError('unexpected edge type for field parameter')
8926
+ raise ValueError('missing or invalid serialization of field parameters')
8927
+ if field.get('@T') != '1':
8928
+ raise ValueError('unexpected edge type for field parameters')
8640
8929
  if field.get('@t', None) is None:
8641
- f_parameter = None
8930
+ f_parameters = None
8642
8931
  else:
8643
- f_parameter = Expression._deserialize(field, seq_to_ob, links)
8932
+ f_parameters = ExpressionList._deserialize(field, seq_to_ob, links)
8644
8933
 
8645
8934
  # Deserialize the annotations field.
8646
8935
  field = cbor.get('annotations', None)
@@ -8658,7 +8947,7 @@ class NonGateInstruction(Instruction):
8658
8947
  f_annotations.append(AnnotationData._deserialize(element, seq_to_ob, links))
8659
8948
 
8660
8949
  # Construct the NonGateInstruction node.
8661
- node = NonGateInstruction(f_name, f_operands, f_parameter, f_annotations)
8950
+ node = NonGateInstruction(f_name, f_operands, f_parameters, f_annotations)
8662
8951
 
8663
8952
  # Deserialize annotations.
8664
8953
  for key, val in cbor.items():
@@ -8700,13 +8989,13 @@ class NonGateInstruction(Instruction):
8700
8989
  field.update(self._attr_operands._serialize(id_map))
8701
8990
  cbor['operands'] = field
8702
8991
 
8703
- # Serialize the parameter field.
8704
- field = {'@T': '?'}
8705
- if self._attr_parameter is None:
8992
+ # Serialize the parameters field.
8993
+ field = {'@T': '1'}
8994
+ if self._attr_parameters is None:
8706
8995
  field['@t'] = None
8707
8996
  else:
8708
- field.update(self._attr_parameter._serialize(id_map))
8709
- cbor['parameter'] = field
8997
+ field.update(self._attr_parameters._serialize(id_map))
8998
+ cbor['parameters'] = field
8710
8999
 
8711
9000
  # Serialize the annotations field.
8712
9001
  field = {'@T': '*'}