libqasm 0.6.7__cp312-cp312-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ import cqasm.v3x.types
2
+
3
+
4
+ class Instruction(object):
5
+ def __init__(self, name, types=None):
6
+ super().__init__()
7
+ self._name = str(name)
8
+ if types is None:
9
+ self._types = ()
10
+ else:
11
+ for typ in types:
12
+ if not isinstance(typ, cqasm.v3x.types.Node):
13
+ raise TypeError('types must be an iterable of cqasm.v3x.types.Node if specified')
14
+ self._types = tuple(types)
15
+
16
+ @property
17
+ def name(self):
18
+ return self._name
19
+
20
+ @property
21
+ def types(self):
22
+ return self._types
23
+
24
+ def __str__(self):
25
+ return '{}({})'.format(self._name, ', '.join(map(str, self._types)))
26
+
27
+
28
+ class InstructionRef(object):
29
+ def __init__(self, *args, **kwargs):
30
+ if not args and not kwargs:
31
+ self._data = None
32
+ elif len(args) == 1 and not kwargs and (isinstance(args[0], Instruction) or args[0] is None):
33
+ self._data = args[0]
34
+ else:
35
+ self._data = Instruction(*args, **kwargs)
36
+
37
+ @property
38
+ def data(self):
39
+ return self._data
40
+
41
+ def __str__(self):
42
+ return 'unresolved' if self._data is None else str(self._data)
@@ -0,0 +1,72 @@
1
+ import cqasm.v3x.instruction
2
+ import cqasm.v3x.types
3
+
4
+ Str = str
5
+ Bool = bool
6
+ Int = int
7
+ Float = float
8
+ Complex = complex
9
+
10
+
11
+ class Version(tuple):
12
+ """cQASM file version number."""
13
+
14
+ def __new__(cls, elements):
15
+ if isinstance(elements, str):
16
+ elements = elements.split('.')
17
+ return super(Version, cls).__new__(cls, tuple(map(int, elements)))
18
+
19
+
20
+ def serialize(typ, val):
21
+ if isinstance(typ, str):
22
+ return None
23
+ elif typ is Str:
24
+ return {'x': val}
25
+ elif typ is Bool:
26
+ return {'x': val}
27
+ elif typ is Int:
28
+ return {'x': val}
29
+ elif typ is Float:
30
+ return {'x': val}
31
+ elif typ is Version:
32
+ return {'x': list(val)}
33
+ elif typ is cqasm.v3x.instruction.InstructionRef:
34
+ if val.data is None:
35
+ return {}
36
+ else:
37
+ return {
38
+ 'n': val.data.name,
39
+ 't': [x.serialize() for x in val.data.types]
40
+ }
41
+ else:
42
+ assert False
43
+
44
+
45
+ def deserialize(typ, val):
46
+ if isinstance(typ, str):
47
+ return None
48
+ elif typ is Str and isinstance(val['x'], bytes):
49
+ # CBOR strings are bytes objects. The correct conversion to Str would be through a decoding.
50
+ # Str(b'qubit', 'utf-8') would generate the string "qubit". This is the same as b'qubit'.decode('utf-8')
51
+ # Whereas Str(b'qubit') would generate the string "b'qubit'"
52
+ return Str(val['x'], 'utf-8')
53
+ elif typ is Str:
54
+ return Str(val['x'])
55
+ elif typ is Bool:
56
+ return Bool(val['x'])
57
+ elif typ is Int:
58
+ return Int(val['x'])
59
+ elif typ is Float:
60
+ return Float(val['x'])
61
+ elif typ is Version:
62
+ return Version(val['x'])
63
+ elif typ is cqasm.v3x.instruction.InstructionRef:
64
+ if 'n' in val:
65
+ return cqasm.v3x.instruction.InstructionRef(
66
+ val['n'],
67
+ [cqasm.v3x.types.Node.deserialize(x) for x in val['t']]
68
+ )
69
+ else:
70
+ return cqasm.v3x.instruction.InstructionRef()
71
+ else:
72
+ assert False