quanta-lang 0.1.0__tar.gz
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.
- quanta_lang-0.1.0/LICENSE +21 -0
- quanta_lang-0.1.0/PKG-INFO +195 -0
- quanta_lang-0.1.0/README.md +141 -0
- quanta_lang-0.1.0/pyproject.toml +60 -0
- quanta_lang-0.1.0/setup.cfg +4 -0
- quanta_lang-0.1.0/src/quanta/__init__.py +8 -0
- quanta_lang-0.1.0/src/quanta/__main__.py +8 -0
- quanta_lang-0.1.0/src/quanta/api.py +40 -0
- quanta_lang-0.1.0/src/quanta/ast/__init__.py +29 -0
- quanta_lang-0.1.0/src/quanta/ast/nodes.py +192 -0
- quanta_lang-0.1.0/src/quanta/ast/visitor.py +83 -0
- quanta_lang-0.1.0/src/quanta/cli/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/cli/main.py +98 -0
- quanta_lang-0.1.0/src/quanta/compiler.py +49 -0
- quanta_lang-0.1.0/src/quanta/errors.py +42 -0
- quanta_lang-0.1.0/src/quanta/ir/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/ir/ir_nodes.py +24 -0
- quanta_lang-0.1.0/src/quanta/lexer/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/lexer/lexer.py +314 -0
- quanta_lang-0.1.0/src/quanta/lower/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/lower/qasm3.py +327 -0
- quanta_lang-0.1.0/src/quanta/parser/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/parser/parser.py +461 -0
- quanta_lang-0.1.0/src/quanta/runtime/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/runtime/qiskit.py +62 -0
- quanta_lang-0.1.0/src/quanta/sema/__init__.py +5 -0
- quanta_lang-0.1.0/src/quanta/sema/typecheck.py +6 -0
- quanta_lang-0.1.0/src/quanta/sema/validation.py +179 -0
- quanta_lang-0.1.0/src/quanta/stdlib/__init__.py +1 -0
- quanta_lang-0.1.0/src/quanta_lang.egg-info/PKG-INFO +195 -0
- quanta_lang-0.1.0/src/quanta_lang.egg-info/SOURCES.txt +36 -0
- quanta_lang-0.1.0/src/quanta_lang.egg-info/dependency_links.txt +1 -0
- quanta_lang-0.1.0/src/quanta_lang.egg-info/entry_points.txt +2 -0
- quanta_lang-0.1.0/src/quanta_lang.egg-info/requires.txt +9 -0
- quanta_lang-0.1.0/src/quanta_lang.egg-info/top_level.txt +1 -0
- quanta_lang-0.1.0/tests/test_examples.py +30 -0
- quanta_lang-0.1.0/tests/test_lowering.py +41 -0
- quanta_lang-0.1.0/tests/test_parser.py +62 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Quanta Language Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: quanta-lang
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Quanta: A high-level language that compiles to OpenQASM 3
|
|
5
|
+
Author-email: Quanta Language Contributors <quanta@example.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 Quanta Language Contributors
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/quanta-lang/quanta
|
|
29
|
+
Project-URL: Documentation, https://github.com/quanta-lang/quanta/docs
|
|
30
|
+
Project-URL: Repository, https://github.com/quanta-lang/quanta
|
|
31
|
+
Project-URL: Issues, https://github.com/quanta-lang/quanta/issues
|
|
32
|
+
Classifier: Development Status :: 3 - Alpha
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: Intended Audience :: Science/Research
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Programming Language :: Python :: 3
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Topic :: Scientific/Engineering
|
|
41
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
42
|
+
Requires-Python: >=3.10
|
|
43
|
+
Description-Content-Type: text/markdown
|
|
44
|
+
License-File: LICENSE
|
|
45
|
+
Requires-Dist: qiskit>=1.0
|
|
46
|
+
Requires-Dist: qiskit-qasm3-import>=0.6.0
|
|
47
|
+
Requires-Dist: lark>=1.1.9
|
|
48
|
+
Provides-Extra: dev
|
|
49
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
50
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
51
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
52
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
53
|
+
Dynamic: license-file
|
|
54
|
+
|
|
55
|
+
# Quanta Language
|
|
56
|
+
|
|
57
|
+
**Quanta** is a high-level, Python-like language that compiles to OpenQASM 3. It provides a clean, readable syntax for quantum circuit development while maintaining full compatibility with OpenQASM 3 and Qiskit.
|
|
58
|
+
|
|
59
|
+
## Features
|
|
60
|
+
|
|
61
|
+
- 🐍 **Python-like syntax** - Familiar and readable
|
|
62
|
+
- ⚛️ **Function-style gates** - Gates as function calls: `H(q[0])`, `CNot(q[0], q[1])`
|
|
63
|
+
- 🔒 **Static analysis** - Compile-time safety checks
|
|
64
|
+
- 🎯 **OpenQASM 3 output** - Direct compilation to standard QASM
|
|
65
|
+
- 🚀 **Qiskit integration** - Seamless execution with Qiskit backends
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install quanta-lang
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Quick Start
|
|
74
|
+
|
|
75
|
+
### As a Library
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from quanta import compile, run
|
|
79
|
+
|
|
80
|
+
# Compile Quanta source to OpenQASM 3
|
|
81
|
+
source = """
|
|
82
|
+
qubit[2] q
|
|
83
|
+
bit[2] c
|
|
84
|
+
|
|
85
|
+
gate Bell(a, b) {
|
|
86
|
+
H(a)
|
|
87
|
+
CNot(a, b)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
Bell(q[0], q[1])
|
|
91
|
+
measure_all(q, c)
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
qasm = compile(source)
|
|
95
|
+
print(qasm)
|
|
96
|
+
|
|
97
|
+
# Run and get results
|
|
98
|
+
result = run(source, shots=1024)
|
|
99
|
+
print(result)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### CLI Usage
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# Compile to QASM
|
|
106
|
+
quanta compile example.qta -o output.qasm
|
|
107
|
+
|
|
108
|
+
# Run circuit
|
|
109
|
+
quanta run example.qta --shots 1024
|
|
110
|
+
|
|
111
|
+
# Check syntax
|
|
112
|
+
quanta check example.qta
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Example
|
|
116
|
+
|
|
117
|
+
### Quanta Source (`bell.qta`)
|
|
118
|
+
|
|
119
|
+
```quanta
|
|
120
|
+
// Bell state example
|
|
121
|
+
qubit[2] q
|
|
122
|
+
bit[2] c
|
|
123
|
+
|
|
124
|
+
gate Bell(a, b) {
|
|
125
|
+
H(a)
|
|
126
|
+
CNot(a, b)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
Bell(q[0], q[1])
|
|
130
|
+
|
|
131
|
+
measure_all(q, c)
|
|
132
|
+
print(c)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Generated OpenQASM 3
|
|
136
|
+
|
|
137
|
+
```qasm
|
|
138
|
+
OPENQASM 3;
|
|
139
|
+
include "stdgates.inc";
|
|
140
|
+
|
|
141
|
+
qubit[2] q;
|
|
142
|
+
bit[2] c;
|
|
143
|
+
|
|
144
|
+
h q[0];
|
|
145
|
+
cx q[0], q[1];
|
|
146
|
+
|
|
147
|
+
measure q[0] -> c[0];
|
|
148
|
+
measure q[1] -> c[1];
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Language Features
|
|
152
|
+
|
|
153
|
+
- **Types**: `int`, `float`, `bool`, `str`, `list`, `dict`, `qubit`, `bit`
|
|
154
|
+
- **Gate Macros**: `gate` keyword for compile-time circuit composition
|
|
155
|
+
- **Modifiers**: `ctrl` and `inv` (dagger) modifiers for gates
|
|
156
|
+
- **Functions**: Compile-time inlined for quantum operations
|
|
157
|
+
- **Control Flow**: `for` loops (unrolled), `if/else` (classical only)
|
|
158
|
+
- **Gate Set**: `H`, `X`, `CNot`, `CZ`, `Swap`, `RZ`, `Measure`, and more
|
|
159
|
+
- **Standard Library**: `print()`, `len()`, `measure_all()`, `reset()`, `assert()`, `range()`
|
|
160
|
+
|
|
161
|
+
## Language Features
|
|
162
|
+
|
|
163
|
+
- **Types**: `int`, `float`, `bool`, `str`, `list`, `dict`, `qubit`, `bit`
|
|
164
|
+
- **Functions**: Compile-time inlined for quantum operations
|
|
165
|
+
- **Control Flow**: `for` loops (unrolled), `if/else` (classical only)
|
|
166
|
+
- **Gate Set**: `H`, `X`, `CNot`, `CZ`, `Swap`, `RZ`, `Measure`, and more
|
|
167
|
+
- **Standard Library**: `print()`, `len()`, `measure_all()`, `reset()`, `assert()`
|
|
168
|
+
|
|
169
|
+
## Documentation
|
|
170
|
+
|
|
171
|
+
- [Language Specification](docs/language.md)
|
|
172
|
+
- [Compiler Pipeline](docs/compiler.md)
|
|
173
|
+
- [Roadmap](docs/roadmap.md)
|
|
174
|
+
|
|
175
|
+
## Development
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
# Install in development mode
|
|
179
|
+
pip install -e .[dev]
|
|
180
|
+
|
|
181
|
+
# Run tests
|
|
182
|
+
pytest
|
|
183
|
+
|
|
184
|
+
# Format code
|
|
185
|
+
black src tests
|
|
186
|
+
ruff check src tests
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
192
|
+
|
|
193
|
+
## Contributing
|
|
194
|
+
|
|
195
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Quanta Language
|
|
2
|
+
|
|
3
|
+
**Quanta** is a high-level, Python-like language that compiles to OpenQASM 3. It provides a clean, readable syntax for quantum circuit development while maintaining full compatibility with OpenQASM 3 and Qiskit.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🐍 **Python-like syntax** - Familiar and readable
|
|
8
|
+
- ⚛️ **Function-style gates** - Gates as function calls: `H(q[0])`, `CNot(q[0], q[1])`
|
|
9
|
+
- 🔒 **Static analysis** - Compile-time safety checks
|
|
10
|
+
- 🎯 **OpenQASM 3 output** - Direct compilation to standard QASM
|
|
11
|
+
- 🚀 **Qiskit integration** - Seamless execution with Qiskit backends
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install quanta-lang
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### As a Library
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from quanta import compile, run
|
|
25
|
+
|
|
26
|
+
# Compile Quanta source to OpenQASM 3
|
|
27
|
+
source = """
|
|
28
|
+
qubit[2] q
|
|
29
|
+
bit[2] c
|
|
30
|
+
|
|
31
|
+
gate Bell(a, b) {
|
|
32
|
+
H(a)
|
|
33
|
+
CNot(a, b)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
Bell(q[0], q[1])
|
|
37
|
+
measure_all(q, c)
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
qasm = compile(source)
|
|
41
|
+
print(qasm)
|
|
42
|
+
|
|
43
|
+
# Run and get results
|
|
44
|
+
result = run(source, shots=1024)
|
|
45
|
+
print(result)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### CLI Usage
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
# Compile to QASM
|
|
52
|
+
quanta compile example.qta -o output.qasm
|
|
53
|
+
|
|
54
|
+
# Run circuit
|
|
55
|
+
quanta run example.qta --shots 1024
|
|
56
|
+
|
|
57
|
+
# Check syntax
|
|
58
|
+
quanta check example.qta
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Example
|
|
62
|
+
|
|
63
|
+
### Quanta Source (`bell.qta`)
|
|
64
|
+
|
|
65
|
+
```quanta
|
|
66
|
+
// Bell state example
|
|
67
|
+
qubit[2] q
|
|
68
|
+
bit[2] c
|
|
69
|
+
|
|
70
|
+
gate Bell(a, b) {
|
|
71
|
+
H(a)
|
|
72
|
+
CNot(a, b)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
Bell(q[0], q[1])
|
|
76
|
+
|
|
77
|
+
measure_all(q, c)
|
|
78
|
+
print(c)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Generated OpenQASM 3
|
|
82
|
+
|
|
83
|
+
```qasm
|
|
84
|
+
OPENQASM 3;
|
|
85
|
+
include "stdgates.inc";
|
|
86
|
+
|
|
87
|
+
qubit[2] q;
|
|
88
|
+
bit[2] c;
|
|
89
|
+
|
|
90
|
+
h q[0];
|
|
91
|
+
cx q[0], q[1];
|
|
92
|
+
|
|
93
|
+
measure q[0] -> c[0];
|
|
94
|
+
measure q[1] -> c[1];
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Language Features
|
|
98
|
+
|
|
99
|
+
- **Types**: `int`, `float`, `bool`, `str`, `list`, `dict`, `qubit`, `bit`
|
|
100
|
+
- **Gate Macros**: `gate` keyword for compile-time circuit composition
|
|
101
|
+
- **Modifiers**: `ctrl` and `inv` (dagger) modifiers for gates
|
|
102
|
+
- **Functions**: Compile-time inlined for quantum operations
|
|
103
|
+
- **Control Flow**: `for` loops (unrolled), `if/else` (classical only)
|
|
104
|
+
- **Gate Set**: `H`, `X`, `CNot`, `CZ`, `Swap`, `RZ`, `Measure`, and more
|
|
105
|
+
- **Standard Library**: `print()`, `len()`, `measure_all()`, `reset()`, `assert()`, `range()`
|
|
106
|
+
|
|
107
|
+
## Language Features
|
|
108
|
+
|
|
109
|
+
- **Types**: `int`, `float`, `bool`, `str`, `list`, `dict`, `qubit`, `bit`
|
|
110
|
+
- **Functions**: Compile-time inlined for quantum operations
|
|
111
|
+
- **Control Flow**: `for` loops (unrolled), `if/else` (classical only)
|
|
112
|
+
- **Gate Set**: `H`, `X`, `CNot`, `CZ`, `Swap`, `RZ`, `Measure`, and more
|
|
113
|
+
- **Standard Library**: `print()`, `len()`, `measure_all()`, `reset()`, `assert()`
|
|
114
|
+
|
|
115
|
+
## Documentation
|
|
116
|
+
|
|
117
|
+
- [Language Specification](docs/language.md)
|
|
118
|
+
- [Compiler Pipeline](docs/compiler.md)
|
|
119
|
+
- [Roadmap](docs/roadmap.md)
|
|
120
|
+
|
|
121
|
+
## Development
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# Install in development mode
|
|
125
|
+
pip install -e .[dev]
|
|
126
|
+
|
|
127
|
+
# Run tests
|
|
128
|
+
pytest
|
|
129
|
+
|
|
130
|
+
# Format code
|
|
131
|
+
black src tests
|
|
132
|
+
ruff check src tests
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
138
|
+
|
|
139
|
+
## Contributing
|
|
140
|
+
|
|
141
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "quanta-lang"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Quanta: A high-level language that compiles to OpenQASM 3"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Quanta Language Contributors", email = "quanta@example.com" }
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.10"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Topic :: Scientific/Engineering",
|
|
25
|
+
"Topic :: Software Development :: Compilers",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
dependencies = [
|
|
29
|
+
"qiskit>=1.0",
|
|
30
|
+
"qiskit-qasm3-import>=0.6.0",
|
|
31
|
+
"lark>=1.1.9",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=7.0",
|
|
37
|
+
"pytest-cov>=4.0",
|
|
38
|
+
"black>=23.0",
|
|
39
|
+
"ruff>=0.1.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.scripts]
|
|
43
|
+
quanta = "quanta.cli.main:main"
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://github.com/quanta-lang/quanta"
|
|
47
|
+
Documentation = "https://github.com/quanta-lang/quanta/docs"
|
|
48
|
+
Repository = "https://github.com/quanta-lang/quanta"
|
|
49
|
+
Issues = "https://github.com/quanta-lang/quanta/issues"
|
|
50
|
+
|
|
51
|
+
[tool.setuptools.packages.find]
|
|
52
|
+
where = ["src"]
|
|
53
|
+
|
|
54
|
+
[tool.black]
|
|
55
|
+
line-length = 100
|
|
56
|
+
target-version = ['py310']
|
|
57
|
+
|
|
58
|
+
[tool.ruff]
|
|
59
|
+
line-length = 100
|
|
60
|
+
target-version = "py310"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Public API for Quanta compiler
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Dict, Any, Optional
|
|
6
|
+
from .compiler import Compiler
|
|
7
|
+
from .runtime.qiskit import run_circuit
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def compile(source: str) -> str:
|
|
11
|
+
"""
|
|
12
|
+
Compile Quanta source code to OpenQASM 3.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
source: Quanta source code as a string
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
OpenQASM 3 code as a string
|
|
19
|
+
|
|
20
|
+
Raises:
|
|
21
|
+
QuantaError: If compilation fails
|
|
22
|
+
"""
|
|
23
|
+
compiler = Compiler()
|
|
24
|
+
return compiler.compile(source)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def run(source: str, shots: int = 1024, backend: Optional[str] = None) -> Dict[str, Any]:
|
|
28
|
+
"""
|
|
29
|
+
Compile and run Quanta source code.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
source: Quanta source code as a string
|
|
33
|
+
shots: Number of measurement shots
|
|
34
|
+
backend: Qiskit backend name (default: 'qasm_simulator')
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Dictionary with measurement results
|
|
38
|
+
"""
|
|
39
|
+
qasm = compile(source)
|
|
40
|
+
return run_circuit(qasm, shots=shots, backend=backend)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""AST (Abstract Syntax Tree) module"""
|
|
2
|
+
|
|
3
|
+
from .nodes import *
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"Program",
|
|
7
|
+
"Stmt",
|
|
8
|
+
"Expr",
|
|
9
|
+
"VarDecl",
|
|
10
|
+
"ConstDecl",
|
|
11
|
+
"LetDecl",
|
|
12
|
+
"QuantumDecl",
|
|
13
|
+
"FuncDecl",
|
|
14
|
+
"GateDecl",
|
|
15
|
+
"ClassDecl",
|
|
16
|
+
"ForStmt",
|
|
17
|
+
"IfStmt",
|
|
18
|
+
"ReturnStmt",
|
|
19
|
+
"ExprStmt",
|
|
20
|
+
"CallExpr",
|
|
21
|
+
"IndexExpr",
|
|
22
|
+
"BinaryExpr",
|
|
23
|
+
"UnaryExpr",
|
|
24
|
+
"VarExpr",
|
|
25
|
+
"LiteralExpr",
|
|
26
|
+
"ListExpr",
|
|
27
|
+
"GroupExpr",
|
|
28
|
+
"AssignExpr",
|
|
29
|
+
]
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AST node definitions for Quanta language
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from typing import List, Optional, Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Node(ABC):
|
|
10
|
+
"""Base class for all AST nodes"""
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Stmt(Node):
|
|
15
|
+
"""Base class for statements"""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Expr(Node):
|
|
20
|
+
"""Base class for expressions"""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Program(Node):
|
|
25
|
+
"""Root node representing a complete program"""
|
|
26
|
+
|
|
27
|
+
def __init__(self, statements: List[Stmt]):
|
|
28
|
+
self.statements = statements
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class VarDecl(Stmt):
|
|
32
|
+
"""Variable declaration"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, name: str, type_hint: Optional[str], value: Optional[Expr]):
|
|
35
|
+
self.name = name
|
|
36
|
+
self.type_hint = type_hint
|
|
37
|
+
self.value = value
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ConstDecl(Stmt):
|
|
41
|
+
"""Constant declaration (compile-time literal)"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, name: str, value: Expr):
|
|
44
|
+
self.name = name
|
|
45
|
+
self.value = value
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class LetDecl(Stmt):
|
|
49
|
+
"""Let declaration (immutable value)"""
|
|
50
|
+
|
|
51
|
+
def __init__(self, name: str, value: Expr):
|
|
52
|
+
self.name = name
|
|
53
|
+
self.value = value
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class QuantumDecl(Stmt):
|
|
57
|
+
"""Quantum register declaration (qubit/bit)"""
|
|
58
|
+
|
|
59
|
+
def __init__(self, kind: str, size: Optional[int], name: str):
|
|
60
|
+
self.kind = kind # "qubit" or "bit"
|
|
61
|
+
self.size = size
|
|
62
|
+
self.name = name
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class FuncDecl(Stmt):
|
|
66
|
+
"""Function declaration"""
|
|
67
|
+
|
|
68
|
+
def __init__(self, name: str, params: List[str], return_type: Optional[str], body: List[Stmt]):
|
|
69
|
+
self.name = name
|
|
70
|
+
self.params = params
|
|
71
|
+
self.return_type = return_type
|
|
72
|
+
self.body = body
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class GateDecl(Stmt):
|
|
76
|
+
"""Gate macro declaration (compile-time circuit composition)"""
|
|
77
|
+
|
|
78
|
+
def __init__(self, name: str, params: List[str], body: List[Stmt]):
|
|
79
|
+
self.name = name
|
|
80
|
+
self.params = params
|
|
81
|
+
self.body = body
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class ClassDecl(Stmt):
|
|
85
|
+
"""Class declaration"""
|
|
86
|
+
|
|
87
|
+
def __init__(self, name: str, members: List[Stmt]):
|
|
88
|
+
self.name = name
|
|
89
|
+
self.members = members
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class ForStmt(Stmt):
|
|
93
|
+
"""For loop statement"""
|
|
94
|
+
|
|
95
|
+
def __init__(self, iterator: str, iterable: Expr, body: List[Stmt]):
|
|
96
|
+
self.iterator = iterator
|
|
97
|
+
self.iterable = iterable
|
|
98
|
+
self.body = body
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class IfStmt(Stmt):
|
|
102
|
+
"""If statement"""
|
|
103
|
+
|
|
104
|
+
def __init__(self, condition: Expr, then_body: List[Stmt], else_body: List[Stmt]):
|
|
105
|
+
self.condition = condition
|
|
106
|
+
self.then_body = then_body
|
|
107
|
+
self.else_body = else_body
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class ReturnStmt(Stmt):
|
|
111
|
+
"""Return statement"""
|
|
112
|
+
|
|
113
|
+
def __init__(self, value: Optional[Expr]):
|
|
114
|
+
self.value = value
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class ExprStmt(Stmt):
|
|
118
|
+
"""Expression statement"""
|
|
119
|
+
|
|
120
|
+
def __init__(self, expr: Expr):
|
|
121
|
+
self.expr = expr
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class CallExpr(Expr):
|
|
125
|
+
"""Function/gate call expression"""
|
|
126
|
+
|
|
127
|
+
def __init__(self, callee: Expr, args: List[Expr], modifiers: Optional[List[str]] = None, ctrl_count: Optional[int] = None):
|
|
128
|
+
self.callee = callee
|
|
129
|
+
self.args = args
|
|
130
|
+
self.modifiers = modifiers or [] # List of "ctrl" and/or "inv"
|
|
131
|
+
self.ctrl_count = ctrl_count # Number of control qubits for ctrl[n]
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class IndexExpr(Expr):
|
|
135
|
+
"""Index expression (array/register access)"""
|
|
136
|
+
|
|
137
|
+
def __init__(self, base: Expr, index: Expr):
|
|
138
|
+
self.base = base
|
|
139
|
+
self.index = index
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class BinaryExpr(Expr):
|
|
143
|
+
"""Binary expression"""
|
|
144
|
+
|
|
145
|
+
def __init__(self, left: Expr, op: str, right: Expr):
|
|
146
|
+
self.left = left
|
|
147
|
+
self.op = op
|
|
148
|
+
self.right = right
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class UnaryExpr(Expr):
|
|
152
|
+
"""Unary expression"""
|
|
153
|
+
|
|
154
|
+
def __init__(self, op: str, right: Expr):
|
|
155
|
+
self.op = op
|
|
156
|
+
self.right = right
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class VarExpr(Expr):
|
|
160
|
+
"""Variable reference"""
|
|
161
|
+
|
|
162
|
+
def __init__(self, name: str):
|
|
163
|
+
self.name = name
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class LiteralExpr(Expr):
|
|
167
|
+
"""Literal expression (number, string, boolean)"""
|
|
168
|
+
|
|
169
|
+
def __init__(self, value: Any):
|
|
170
|
+
self.value = value
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class ListExpr(Expr):
|
|
174
|
+
"""List literal expression"""
|
|
175
|
+
|
|
176
|
+
def __init__(self, elements: List[Expr]):
|
|
177
|
+
self.elements = elements
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
class GroupExpr(Expr):
|
|
181
|
+
"""Grouped expression (parentheses)"""
|
|
182
|
+
|
|
183
|
+
def __init__(self, expr: Expr):
|
|
184
|
+
self.expr = expr
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class AssignExpr(Expr):
|
|
188
|
+
"""Assignment expression"""
|
|
189
|
+
|
|
190
|
+
def __init__(self, name: str, value: Expr):
|
|
191
|
+
self.name = name
|
|
192
|
+
self.value = value
|