libqasm 0.6.7__cp39-cp39-macosx_10_9_x86_64.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/__init__.py ADDED
@@ -0,0 +1 @@
1
+ #
cqasm/v3x/__init__.py ADDED
@@ -0,0 +1,114 @@
1
+ import cqasm.v3x.ast as ast
2
+ import cqasm.v3x.semantic as semantic
3
+ import libqasm
4
+
5
+
6
+ class Analyzer(libqasm.V3xAnalyzer):
7
+ """!
8
+ Main class for parsing and analyzing cQASM v3.0 files.
9
+
10
+ This class works as a binding for accessing C++ code from Python.
11
+
12
+ The parsing methods are static because they do not change the status of the analyzer.
13
+ Instead, they just invoke free functions that create a temporary instance of a parser.
14
+
15
+ None of the parsing or the analyzing methods perform any version check.
16
+
17
+ `parse_file`, `parse_string`, `analyze_file`, and `analyze_string`:
18
+
19
+ - return a vector of strings.
20
+ If the length of the vector is 1, the string is a serialization
21
+ of the syntactic/semantic (in the case of parsing/analyzing) Abstract Syntax Tree (AST)
22
+ of the input program.
23
+ Otherwise, it is a list of errors.
24
+
25
+ `parse_file_to_json`, `parse_string_to_json`, `analyze_file_to_json`, and `analyze_string_to_json`:
26
+
27
+ - return a string in JSON format.
28
+ If the parsing was successful, the string contains a JSON representation
29
+ of the AST of the input program.
30
+ Otherwise, it will contain a list of errors.
31
+ The JSON representation of each error follows the Language Server Protocol (LSP) specification.
32
+ Every error is mapped to an LSP Diagnostic structure:
33
+ `severity` is hardcoded to 1 at the moment (value corresponding to an Error level).
34
+
35
+ `parse_string`, `parse_string_to_json`, `analyze_string, and `analyze_string_to_json`:
36
+
37
+ - have an optional second argument: `file_name`. It is only used when reporting errors.
38
+
39
+ **Example**:
40
+
41
+ result = libqasm.analyze_file("grover.cq");
42
+
43
+ **Example**:
44
+
45
+ program = r'''
46
+ version 3.0
47
+ qubit[2] q
48
+ bit[2] b
49
+ H q[0]
50
+ CNOT q[0], q[1]
51
+ b = measure q
52
+ '''
53
+ result = libqasm.parse_string_to_json(program, "bell.cq");
54
+ """
55
+
56
+ @staticmethod
57
+ def parse_file(*args) -> list[str]:
58
+ """! Parses a file containing a cQASM v3.0 program."""
59
+ ret = libqasm.V3xAnalyzer.parse_file(*args)
60
+ if len(ret) == 1:
61
+ serialized_ast_str = str(ret[0])
62
+ serialized_ast_bytes = serialized_ast_str.encode(encoding='utf-8', errors="surrogateescape")
63
+ deserialized_ast = ast.Root.deserialize(serialized_ast_bytes)
64
+ return deserialized_ast
65
+ return [str(error) for error in ret[1:]]
66
+
67
+ @staticmethod
68
+ def parse_file_to_json(*args) -> str:
69
+ """! Parses a file containing a cQASM v3.0 program."""
70
+ return libqasm.V3xAnalyzer.parse_file_to_json(*args)
71
+
72
+ @staticmethod
73
+ def parse_string(*args) -> list[str]:
74
+ """! Parses a string containing a cQASM v3.0 program."""
75
+ ret = libqasm.V3xAnalyzer.parse_string(*args)
76
+ if len(ret) == 1:
77
+ serialized_ast_str = str(ret[0])
78
+ serialized_ast_bytes = serialized_ast_str.encode(encoding='utf-8', errors="surrogateescape")
79
+ deserialized_ast = ast.Root.deserialize(serialized_ast_bytes)
80
+ return deserialized_ast
81
+ return [str(error) for error in ret[1:]]
82
+
83
+ @staticmethod
84
+ def parse_string_to_json(*args) -> str:
85
+ """! Parses a string containing a cQASM v3.0 program."""
86
+ return libqasm.V3xAnalyzer.parse_string_to_json(*args)
87
+
88
+ def analyze_file(self, *args) -> list[str]:
89
+ """! Parses and analyzes a file containing a cQASM v3.0 program."""
90
+ ret = super().analyze_file(*args)
91
+ if len(ret) == 1:
92
+ serialized_ast_str = str(ret[0])
93
+ serialized_ast_bytes = serialized_ast_str.encode(encoding='utf-8', errors="surrogateescape")
94
+ deserialized_ast = semantic.Program.deserialize(serialized_ast_bytes)
95
+ return deserialized_ast
96
+ return [str(error) for error in ret[1:]]
97
+
98
+ def analyze_file_to_json(self, *args) -> str:
99
+ """! Parses and analyzes a file containing a cQASM v3.0 program."""
100
+ return super().analyze_file_to_json(*args)
101
+
102
+ def analyze_string(self, *args) -> list[str]:
103
+ """! Parses and analyzes a string containing a cQASM v3.0 program."""
104
+ ret = super().analyze_string(*args)
105
+ if len(ret) == 1:
106
+ serialized_ast_str = str(ret[0])
107
+ serialized_ast_bytes = serialized_ast_str.encode(encoding='utf-8', errors="surrogateescape")
108
+ deserialized_ast = semantic.Program.deserialize(serialized_ast_bytes)
109
+ return deserialized_ast
110
+ return [str(error) for error in ret[1:]]
111
+
112
+ def analyze_string_to_json(self, *args) -> str:
113
+ """! Parses and analyzes a string containing a cQASM v3.0 program."""
114
+ return super().analyze_string_to_json(*args)