Quantar 0.1.0__py3-none-any.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.
- lang_systems/__init__.py +38 -0
- lang_systems/ast_builder.py +272 -0
- lang_systems/lsp/__init__.py +1 -0
- lang_systems/lsp/__main__.py +3 -0
- lang_systems/lsp/server.py +247 -0
- lang_systems/operators.py +367 -0
- lang_systems/pegen_gram_gen.py +159 -0
- lang_systems/quantar.gram +27 -0
- lang_systems/quantar.py +63 -0
- lang_systems/quantar_parser.py +11729 -0
- lang_systems/quantar_tokens.py +87 -0
- lang_systems/runner.py +55 -0
- lang_systems/runtime.py +63 -0
- lang_systems/visitors.py +30 -0
- quantar-0.1.0.dist-info/METADATA +399 -0
- quantar-0.1.0.dist-info/RECORD +39 -0
- quantar-0.1.0.dist-info/WHEEL +4 -0
- quantar-0.1.0.dist-info/entry_points.txt +3 -0
- quantar-0.1.0.dist-info/licenses/LICENSE +201 -0
- quantum/__init__.py +60 -0
- quantum/basic_funcs.py +262 -0
- quantum/composites.py +33 -0
- quantum/gate_abs.py +127 -0
- quantum/gate_matrix.py +171 -0
- quantum/kernels.py +234 -0
- quantum/parse_qasm.py +393 -0
- quantum/qasm.py +89 -0
- quantum/qubit_abs.py +682 -0
- stl/__init__.py +1 -0
- stl/physics_algs/__init__.py +57 -0
- stl/physics_algs/duals_and_coords.py +482 -0
- stl/physics_algs/units.py +923 -0
- stl/physics_algs/vector_fields.py +270 -0
- stl/quantum_algs/__init__.py +70 -0
- stl/quantum_algs/fast_factor.py +195 -0
- stl/quantum_algs/grovers_alg.py +125 -0
- stl/quantum_algs/quantum_optim.py +154 -0
- stl/quantum_algs/quantum_optim_hl.py +362 -0
- stl/quantum_algs/shors_alg.py +371 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Tokenizer glue for Quantar's symbolic operators.
|
|
2
|
+
|
|
3
|
+
Python's tokenizer splits a symbol such as ``<->`` into ``<``, ``-``, ``>``, so
|
|
4
|
+
the generated parser could never match it. This module post-processes the token
|
|
5
|
+
stream and merges maximal runs of adjacent ``OP``/``NAME`` tokens that together
|
|
6
|
+
spell an operator known to the language into a single ``OP`` token.
|
|
7
|
+
|
|
8
|
+
Identifier operators (``CNOT``) need no help -- they are ordinary ``NAME`` tokens.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import io
|
|
12
|
+
import token
|
|
13
|
+
import tokenize
|
|
14
|
+
from collections.abc import Iterator
|
|
15
|
+
|
|
16
|
+
from lang_systems.operators import BUILTIN_OPERATOR_KINDS, list_operators
|
|
17
|
+
|
|
18
|
+
_MERGEABLE = (token.OP, token.NAME)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def symbolic_operators() -> list[str]:
|
|
22
|
+
"""Known operator names that are not plain identifiers, longest first."""
|
|
23
|
+
names = set(list_operators()) | set(BUILTIN_OPERATOR_KINDS)
|
|
24
|
+
symbols = [name for name in names if not name.isidentifier()]
|
|
25
|
+
return sorted(symbols, key=len, reverse=True)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _match(tokens: list[tokenize.TokenInfo], start: int, symbol: str):
|
|
29
|
+
"""Try to match `symbol` against adjacent tokens from `start`.
|
|
30
|
+
|
|
31
|
+
Returns (merged_token, index_after) or None.
|
|
32
|
+
"""
|
|
33
|
+
position = 0
|
|
34
|
+
index = start
|
|
35
|
+
while position < len(symbol):
|
|
36
|
+
if index >= len(tokens):
|
|
37
|
+
return None
|
|
38
|
+
current = tokens[index]
|
|
39
|
+
if current.type not in _MERGEABLE:
|
|
40
|
+
return None
|
|
41
|
+
piece = current.string
|
|
42
|
+
if not symbol.startswith(piece, position):
|
|
43
|
+
return None
|
|
44
|
+
if index > start and tokens[index - 1].end != current.start:
|
|
45
|
+
return None
|
|
46
|
+
position += len(piece)
|
|
47
|
+
index += 1
|
|
48
|
+
|
|
49
|
+
if position != len(symbol):
|
|
50
|
+
return None
|
|
51
|
+
|
|
52
|
+
first, last = tokens[start], tokens[index - 1]
|
|
53
|
+
merged = tokenize.TokenInfo(
|
|
54
|
+
type=token.OP,
|
|
55
|
+
string=symbol,
|
|
56
|
+
start=first.start,
|
|
57
|
+
end=last.end,
|
|
58
|
+
line=first.line,
|
|
59
|
+
)
|
|
60
|
+
return merged, index
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def merge_operators(tokens: list[tokenize.TokenInfo], symbols: list[str]):
|
|
64
|
+
"""Yield `tokens`, replacing adjacent runs that spell a symbol with one OP token."""
|
|
65
|
+
index = 0
|
|
66
|
+
total = len(tokens)
|
|
67
|
+
while index < total:
|
|
68
|
+
current = tokens[index]
|
|
69
|
+
if current.type in _MERGEABLE:
|
|
70
|
+
for symbol in symbols:
|
|
71
|
+
matched = _match(tokens, index, symbol)
|
|
72
|
+
if matched is not None:
|
|
73
|
+
merged, index = matched
|
|
74
|
+
yield merged
|
|
75
|
+
break
|
|
76
|
+
else:
|
|
77
|
+
yield current
|
|
78
|
+
index += 1
|
|
79
|
+
else:
|
|
80
|
+
yield current
|
|
81
|
+
index += 1
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def tokenize_source(source: str) -> Iterator[tokenize.TokenInfo]:
|
|
85
|
+
"""Tokenize Quantar source, merging registered symbolic operators."""
|
|
86
|
+
tokens = list(tokenize.generate_tokens(io.StringIO(source).readline))
|
|
87
|
+
return merge_operators(tokens, symbolic_operators())
|
lang_systems/runner.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from . import ast_builder
|
|
2
|
+
from .runtime import runtime_globals
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import argparse
|
|
5
|
+
from ast import unparse
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
def qtr_runner(filename: str | Path, output_path: str | Path | None = None):
|
|
9
|
+
path = Path(filename)
|
|
10
|
+
|
|
11
|
+
if not path.exists():
|
|
12
|
+
raise FileNotFoundError(f"Quantar source file {path} not found, either expand the scope or change the file name")
|
|
13
|
+
|
|
14
|
+
if path.suffix not in (".qtr", ".py"):
|
|
15
|
+
raise ValueError(f"Invalid file extension {path.suffix}. Expected .qtr, .py")
|
|
16
|
+
|
|
17
|
+
source_code = path.read_text(encoding="utf-8")
|
|
18
|
+
|
|
19
|
+
tree = ast_builder.parse_ast(source_code, str(filename))
|
|
20
|
+
|
|
21
|
+
if output_path:
|
|
22
|
+
python_source = unparse(ast_builder.lower_ast(tree))
|
|
23
|
+
runtime_imports = (
|
|
24
|
+
"from lang_systems.runtime import apply_quantar_operator as __quantar_apply__\n\n"
|
|
25
|
+
)
|
|
26
|
+
final_code = runtime_imports + python_source
|
|
27
|
+
|
|
28
|
+
out_path = Path(output_path) if output_path else path.with_suffix(".py")
|
|
29
|
+
out_path.write_text(final_code, "utf-8")
|
|
30
|
+
print(f"[COMPILE] Compiled to {out_path}")
|
|
31
|
+
|
|
32
|
+
else:
|
|
33
|
+
python_ast = ast_builder.compile_ast(tree, str(filename))
|
|
34
|
+
exec(python_ast, {"__name__": "__main__", **runtime_globals()})
|
|
35
|
+
|
|
36
|
+
def main():
|
|
37
|
+
parser = argparse.ArgumentParser("qtr", description = "A CLI tool to run .qtr or .py files (Quantar files)")
|
|
38
|
+
parser.add_argument("filename", help="The name of the file to process")
|
|
39
|
+
parser.add_argument("-c", "--compile", help = "Compile the given file into a .py file at this location", metavar = "OUTPUT_PATH")
|
|
40
|
+
|
|
41
|
+
args = parser.parse_args()
|
|
42
|
+
|
|
43
|
+
try:
|
|
44
|
+
qtr_runner(args.filename, args.compile)
|
|
45
|
+
except Exception as e:
|
|
46
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
47
|
+
sys.exit(1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
if len(sys.argv) > 1:
|
|
53
|
+
main()
|
|
54
|
+
else:
|
|
55
|
+
qtr_runner("C:\\Users\\kaart\\programming_stuff\\Quantar\\test\\test1.qtr")
|
lang_systems/runtime.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Runtime bridge for executing lowered Quantar ASTs.
|
|
2
|
+
|
|
3
|
+
The language core stays independent of the quantum package. Lowered
|
|
4
|
+
``QuantumOp`` nodes call ``__quantar_apply__(name, *operands)``; this module
|
|
5
|
+
provides that function and a ready-made globals dictionary for ``exec``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from lang_systems.operators import (
|
|
13
|
+
apply_operator,
|
|
14
|
+
get_global_operator,
|
|
15
|
+
get_global_operator_matrix,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _load_quantum_operators() -> None:
|
|
20
|
+
"""Import the quantum gate registrations on first use."""
|
|
21
|
+
try:
|
|
22
|
+
import quantum.gate_matrix # noqa: F401
|
|
23
|
+
except ImportError:
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _common_computer(operands: tuple[Any, ...]):
|
|
28
|
+
computer = getattr(operands[0], "computer", None)
|
|
29
|
+
if computer is None:
|
|
30
|
+
raise TypeError("gate operators require qubit operands")
|
|
31
|
+
if any(getattr(operand, "computer", None) is not computer for operand in operands):
|
|
32
|
+
raise ValueError("all qubits in a gate operation must belong to the same computer")
|
|
33
|
+
return computer
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def apply_quantar_operator(name: str, *operands: Any) -> Any:
|
|
37
|
+
"""Apply a registered operator or gate to its operands."""
|
|
38
|
+
operator, kind = get_global_operator(name)
|
|
39
|
+
if operator is None:
|
|
40
|
+
_load_quantum_operators()
|
|
41
|
+
operator, kind = get_global_operator(name)
|
|
42
|
+
|
|
43
|
+
if operator is None:
|
|
44
|
+
raise ValueError(f"Unknown operator: {name!r}")
|
|
45
|
+
|
|
46
|
+
if kind == "matrix":
|
|
47
|
+
matrix = get_global_operator_matrix(name)
|
|
48
|
+
if matrix is None:
|
|
49
|
+
raise ValueError(f"Operator {name!r} is not a usable matrix")
|
|
50
|
+
if not operands:
|
|
51
|
+
return matrix
|
|
52
|
+
computer = _common_computer(operands)
|
|
53
|
+
return computer.apply_gate(matrix, target_qubits=list(operands))
|
|
54
|
+
|
|
55
|
+
return apply_operator(name, *operands)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def runtime_globals() -> dict[str, Any]:
|
|
59
|
+
"""Return globals that make a lowered Quantar AST executable."""
|
|
60
|
+
return {"__quantar_apply__": apply_quantar_operator}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
__all__ = ["apply_quantar_operator", "runtime_globals"]
|
lang_systems/visitors.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Visitors for Quantar's extended AST.
|
|
2
|
+
|
|
3
|
+
These are ordinary :class:`ast.NodeVisitor` subclasses. Subclass
|
|
4
|
+
:class:`QuantarNodeVisitor` when you want custom operators to be traversed by
|
|
5
|
+
default, then override :meth:`visit_QuantumOp` to do your own work.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import ast
|
|
9
|
+
|
|
10
|
+
from lang_systems.ast_builder import QuantumOp
|
|
11
|
+
|
|
12
|
+
__all__ = ["QuantarNodeVisitor", "OperatorCollector"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class QuantarNodeVisitor(ast.NodeVisitor):
|
|
16
|
+
"""Base visitor that descends into :class:`QuantumOp` operands."""
|
|
17
|
+
|
|
18
|
+
def visit_QuantumOp(self, node: QuantumOp):
|
|
19
|
+
self.generic_visit(node)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class OperatorCollector(QuantarNodeVisitor):
|
|
23
|
+
"""Collect ``(operator, kind)`` pairs from a Quantar AST."""
|
|
24
|
+
|
|
25
|
+
def __init__(self):
|
|
26
|
+
self.operators: list[tuple[str, str]] = []
|
|
27
|
+
|
|
28
|
+
def visit_QuantumOp(self, node: QuantumOp):
|
|
29
|
+
self.operators.append((node.operator, node.kind))
|
|
30
|
+
self.generic_visit(node)
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: Quantar
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python superset for easy quantum computing, operator creation and physics simulations.
|
|
5
|
+
License: Apache License
|
|
6
|
+
Version 2.0, January 2004
|
|
7
|
+
http://www.apache.org/licenses/
|
|
8
|
+
|
|
9
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
10
|
+
|
|
11
|
+
1. Definitions.
|
|
12
|
+
|
|
13
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
14
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
15
|
+
|
|
16
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
17
|
+
the copyright owner that is granting the License.
|
|
18
|
+
|
|
19
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
20
|
+
other entities that control, are controlled by, or are under common
|
|
21
|
+
control with that entity. For the purposes of this definition,
|
|
22
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
23
|
+
direction or management of such entity, whether by contract or
|
|
24
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
25
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
26
|
+
|
|
27
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
28
|
+
exercising permissions granted by this License.
|
|
29
|
+
|
|
30
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
31
|
+
including but not limited to software source code, documentation
|
|
32
|
+
source, and configuration files.
|
|
33
|
+
|
|
34
|
+
"Object" form shall mean any form resulting from mechanical
|
|
35
|
+
transformation or translation of a Source form, including but
|
|
36
|
+
not limited to compiled object code, generated documentation,
|
|
37
|
+
and conversions to other media types.
|
|
38
|
+
|
|
39
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
40
|
+
Object form, made available under the License, as indicated by a
|
|
41
|
+
copyright notice that is included in or attached to the work
|
|
42
|
+
(an example is provided in the Appendix below).
|
|
43
|
+
|
|
44
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
45
|
+
form, that is based on (or derived from) the Work and for which the
|
|
46
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
47
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
48
|
+
of this License, Derivative Works shall not include works that remain
|
|
49
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
50
|
+
the Work and Derivative Works thereof.
|
|
51
|
+
|
|
52
|
+
"Contribution" shall mean any work of authorship, including
|
|
53
|
+
the original version of the Work and any modifications or additions
|
|
54
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
55
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
56
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
57
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
58
|
+
means any form of electronic, verbal, or written communication sent
|
|
59
|
+
to the Licensor or its representatives, including but not limited to
|
|
60
|
+
communication on electronic mailing lists, source code control systems,
|
|
61
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
62
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
63
|
+
excluding communication that is conspicuously marked or otherwise
|
|
64
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
65
|
+
|
|
66
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
67
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
68
|
+
subsequently incorporated within the Work.
|
|
69
|
+
|
|
70
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
74
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
75
|
+
Work and such Derivative Works in Source or Object form.
|
|
76
|
+
|
|
77
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
78
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
79
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
80
|
+
(except as stated in this section) patent license to make, have made,
|
|
81
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
82
|
+
where such license applies only to those patent claims licensable
|
|
83
|
+
by such Contributor that are necessarily infringed by their
|
|
84
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
85
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
86
|
+
institute patent litigation against any entity (including a
|
|
87
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
88
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
89
|
+
or contributory patent infringement, then any patent licenses
|
|
90
|
+
granted to You under this License for that Work shall terminate
|
|
91
|
+
as of the date such litigation is filed.
|
|
92
|
+
|
|
93
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
94
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
95
|
+
modifications, and in Source or Object form, provided that You
|
|
96
|
+
meet the following conditions:
|
|
97
|
+
|
|
98
|
+
(a) You must give any other recipients of the Work or
|
|
99
|
+
Derivative Works a copy of this License; and
|
|
100
|
+
|
|
101
|
+
(b) You must cause any modified files to carry prominent notices
|
|
102
|
+
stating that You changed the files; and
|
|
103
|
+
|
|
104
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
105
|
+
that You distribute, all copyright, patent, trademark, and
|
|
106
|
+
attribution notices from the Source form of the Work,
|
|
107
|
+
excluding those notices that do not pertain to any part of
|
|
108
|
+
the Derivative Works; and
|
|
109
|
+
|
|
110
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
111
|
+
distribution, then any Derivative Works that You distribute must
|
|
112
|
+
include a readable copy of the attribution notices contained
|
|
113
|
+
within such NOTICE file, excluding those notices that do not
|
|
114
|
+
pertain to any part of the Derivative Works, in at least one
|
|
115
|
+
of the following places: within a NOTICE text file distributed
|
|
116
|
+
as part of the Derivative Works; within the Source form or
|
|
117
|
+
documentation, if provided along with the Derivative Works; or,
|
|
118
|
+
within a display generated by the Derivative Works, if and
|
|
119
|
+
wherever such third-party notices normally appear. The contents
|
|
120
|
+
of the NOTICE file are for informational purposes only and
|
|
121
|
+
do not modify the License. You may add Your own attribution
|
|
122
|
+
notices within Derivative Works that You distribute, alongside
|
|
123
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
124
|
+
that such additional attribution notices cannot be construed
|
|
125
|
+
as modifying the License.
|
|
126
|
+
|
|
127
|
+
You may add Your own copyright statement to Your modifications and
|
|
128
|
+
may provide additional or different license terms and conditions
|
|
129
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
130
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
131
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
132
|
+
the conditions stated in this License.
|
|
133
|
+
|
|
134
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
135
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
136
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
137
|
+
this License, without any additional terms or conditions.
|
|
138
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
139
|
+
the terms of any separate license agreement you may have executed
|
|
140
|
+
with Licensor regarding such Contributions.
|
|
141
|
+
|
|
142
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
143
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
144
|
+
except as required for reasonable and customary use in describing the
|
|
145
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
146
|
+
|
|
147
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
148
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
149
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
150
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
151
|
+
implied, including, without limitation, any warranties or conditions
|
|
152
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
153
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
154
|
+
appropriateness of using or redistributing the Work and assume any
|
|
155
|
+
risks associated with Your exercise of permissions under this License.
|
|
156
|
+
|
|
157
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
158
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
159
|
+
unless required by applicable law (such as deliberate and grossly
|
|
160
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
161
|
+
liable to You for damages, including any direct, indirect, special,
|
|
162
|
+
incidental, or consequential damages of any character arising as a
|
|
163
|
+
result of this License or out of the use or inability to use the
|
|
164
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
165
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
166
|
+
other commercial damages or losses), even if such Contributor
|
|
167
|
+
has been advised of the possibility of such damages.
|
|
168
|
+
|
|
169
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
170
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
171
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
172
|
+
or other liability obligations and/or rights consistent with this
|
|
173
|
+
License. However, in accepting such obligations, You may act only
|
|
174
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
175
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
176
|
+
defend, and hold each Contributor harmless for any liability
|
|
177
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
178
|
+
of your accepting any such warranty or additional liability.
|
|
179
|
+
|
|
180
|
+
END OF TERMS AND CONDITIONS
|
|
181
|
+
|
|
182
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
183
|
+
|
|
184
|
+
To apply the Apache License to your work, attach the following
|
|
185
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
186
|
+
replaced with your own identifying information. (Don't include
|
|
187
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
188
|
+
comment syntax for the file format. We also recommend that a
|
|
189
|
+
file or class name and description of purpose be included on the
|
|
190
|
+
same "printed page" as the copyright notice for easier
|
|
191
|
+
identification within third-party archives.
|
|
192
|
+
|
|
193
|
+
Copyright [yyyy] [name of copyright owner]
|
|
194
|
+
|
|
195
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
196
|
+
you may not use this file except in compliance with the License.
|
|
197
|
+
You may obtain a copy of the License at
|
|
198
|
+
|
|
199
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
200
|
+
|
|
201
|
+
Unless required by applicable law or agreed to in writing, software
|
|
202
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
203
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
204
|
+
See the License for the specific language governing permissions and
|
|
205
|
+
limitations under the License.
|
|
206
|
+
License-File: LICENSE
|
|
207
|
+
Requires-Python: >=3.12
|
|
208
|
+
Requires-Dist: numba
|
|
209
|
+
Requires-Dist: numpy
|
|
210
|
+
Requires-Dist: openqasm3[parser]
|
|
211
|
+
Requires-Dist: pegen>=0.3.0
|
|
212
|
+
Provides-Extra: backends
|
|
213
|
+
Requires-Dist: pennylane; extra == 'backends'
|
|
214
|
+
Requires-Dist: qbraid; extra == 'backends'
|
|
215
|
+
Requires-Dist: qiskit; extra == 'backends'
|
|
216
|
+
Requires-Dist: qiskit-aer; extra == 'backends'
|
|
217
|
+
Provides-Extra: lsp
|
|
218
|
+
Requires-Dist: lsprotocol; extra == 'lsp'
|
|
219
|
+
Requires-Dist: pygls<3,>=2.1; extra == 'lsp'
|
|
220
|
+
Provides-Extra: optimization
|
|
221
|
+
Requires-Dist: scipy; extra == 'optimization'
|
|
222
|
+
Description-Content-Type: text/markdown
|
|
223
|
+
|
|
224
|
+
# Quantar
|
|
225
|
+
|
|
226
|
+
Quantar is a strict superset of Python for quantum computing and physics
|
|
227
|
+
simulations. It keeps Python syntax and adds custom infix operators such as
|
|
228
|
+
`a <-> b`, plus a library of quantum gates, circuits, algorithms, and physics
|
|
229
|
+
helpers.
|
|
230
|
+
|
|
231
|
+
## Requirements
|
|
232
|
+
|
|
233
|
+
- Python 3.12 or newer
|
|
234
|
+
- NumPy, Numba, and `openqasm3[parser]` (installed automatically)
|
|
235
|
+
|
|
236
|
+
## Installation
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
pip install quantar
|
|
240
|
+
|
|
241
|
+
# or with uv
|
|
242
|
+
uv pip install quantar
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Optional extras:
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
# alternative quantum backends
|
|
249
|
+
pip install quantar[backends]
|
|
250
|
+
|
|
251
|
+
# SciPy-powered COBYLA optimization
|
|
252
|
+
pip install quantar[optimization]
|
|
253
|
+
|
|
254
|
+
# LSP support
|
|
255
|
+
pip install quantar[lsp]
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Running Quantar
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
qtr path/to/program.qtr
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Compile a file to ordinary Python instead of executing it:
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
qtr path/to/program.qtr --compile path/to/program.py
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
During development, the same runner is available as a module:
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
python -m lang_systems.runner path/to/program.qtr
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Small registers use vectorized NumPy so short programs skip Numba's startup and
|
|
277
|
+
compilation cost. Registers with at least 1,048,576 amplitudes use the JIT. Set
|
|
278
|
+
`QUANTAR_NUMBA_MIN_AMPLITUDES` to change that cutoff, or `QUANTAR_NUMBA=0` to
|
|
279
|
+
disable Numba entirely.
|
|
280
|
+
|
|
281
|
+
## VS Code extension
|
|
282
|
+
|
|
283
|
+
The extension lives in `editors/vscode`. It provides `.qtr` syntax highlighting,
|
|
284
|
+
parser diagnostics, and registry-driven operator highlighting through a
|
|
285
|
+
`pygls` language server.
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
pip install -e ".[lsp]"
|
|
289
|
+
cd editors/vscode
|
|
290
|
+
npm install
|
|
291
|
+
npm run compile
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Open `editors/vscode` in VS Code and run the `Run Quantar Extension` launch
|
|
295
|
+
configuration. The extension also adds `Quantar: Run File` and
|
|
296
|
+
`Quantar: Compile File to Python` commands.
|
|
297
|
+
|
|
298
|
+
## Parsing Quantar
|
|
299
|
+
|
|
300
|
+
`lang_systems.parse()` returns pegen's raw parse tree. This is the low-level
|
|
301
|
+
form used by tooling and tests:
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
from lang_systems import parse
|
|
305
|
+
|
|
306
|
+
tree = parse("a <-> b\nx = 1 + 2\n")
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
`lang_systems.parse_ast()` returns a normal Python AST with custom `QuantumOp`
|
|
310
|
+
nodes:
|
|
311
|
+
|
|
312
|
+
```python
|
|
313
|
+
import ast
|
|
314
|
+
|
|
315
|
+
from lang_systems import OperatorCollector, parse_ast
|
|
316
|
+
|
|
317
|
+
module = parse_ast("x = a <-> b\n")
|
|
318
|
+
print(ast.dump(module, indent=2))
|
|
319
|
+
|
|
320
|
+
collector = OperatorCollector()
|
|
321
|
+
collector.visit(module)
|
|
322
|
+
print(collector.operators) # [("<->", "binary")]
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Custom operators are registered with `lang_systems.operators.operator`:
|
|
326
|
+
|
|
327
|
+
```python
|
|
328
|
+
from lang_systems.operators import operator
|
|
329
|
+
|
|
330
|
+
@operator.binary("><")
|
|
331
|
+
def reverse(left, right):
|
|
332
|
+
return right, left
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
If the operators live in another module, import that module before parsing:
|
|
336
|
+
|
|
337
|
+
```python
|
|
338
|
+
from lang_systems import load_operators, parse
|
|
339
|
+
|
|
340
|
+
load_operators("my_package.quantar_ops")
|
|
341
|
+
tree = parse("a >< b\n")
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Quantar as a library
|
|
345
|
+
|
|
346
|
+
The quantum and physics packages can be imported directly from Python:
|
|
347
|
+
|
|
348
|
+
```python
|
|
349
|
+
import quantum
|
|
350
|
+
from stl.quantum_algs.shors_alg import shors_alg
|
|
351
|
+
from stl.physics_algs.units import Quantity, Unit
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
See the modules under `src/quantum/` and `src/stl/` for the available gates,
|
|
355
|
+
circuits, algorithms, and physics helpers.
|
|
356
|
+
|
|
357
|
+
## Development
|
|
358
|
+
|
|
359
|
+
The Python packages live under `src/`. Install the project in editable mode
|
|
360
|
+
before running the CLI or LSP from a checkout:
|
|
361
|
+
|
|
362
|
+
```bash
|
|
363
|
+
pip install -e ".[lsp]"
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
Regenerate the parser after changing `src/lang_systems/quantar.gram`:
|
|
367
|
+
|
|
368
|
+
```bash
|
|
369
|
+
python -m lang_systems.pegen_gram_gen
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
Build the wheel and source distribution:
|
|
373
|
+
|
|
374
|
+
```bash
|
|
375
|
+
uv build
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
The generated parser is `src/lang_systems/quantar_parser.py`; the build hook in
|
|
379
|
+
`hatch_build.py` regenerates it automatically. The parser is generated from
|
|
380
|
+
CPython's `Grammar/python.gram` plus the operator overlay in
|
|
381
|
+
`src/lang_systems/quantar.gram`.
|
|
382
|
+
|
|
383
|
+
## Status
|
|
384
|
+
|
|
385
|
+
Quantar currently provides the parser, AST, quantum library, physics library,
|
|
386
|
+
a CLI runner/compiler, and a VS Code extension scaffold. A bytecode interpreter
|
|
387
|
+
and debugger are not implemented yet.
|
|
388
|
+
|
|
389
|
+
## Feedback
|
|
390
|
+
|
|
391
|
+
Feedback and bug reports are welcome through GitHub issues or by contacting the
|
|
392
|
+
maintainer. The main contributor is
|
|
393
|
+
[Gautham_rgb](https://github.com/Gautham-rgb/).
|
|
394
|
+
|
|
395
|
+
## Notes on AI
|
|
396
|
+
|
|
397
|
+
Most of the code was written by people. Some AI assistance was used for
|
|
398
|
+
boilerplate and for explaining the mathematics behind topics such as Kronecker
|
|
399
|
+
products and gate matrices.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
lang_systems/__init__.py,sha256=Uk_Ye5kAqHEzmzKowezc-uEMUE-FiBtX3P3-8_Xra_M,1472
|
|
2
|
+
lang_systems/ast_builder.py,sha256=K27JHuaEDfwn9E-RKKrrl3zTSSssEdGaqcqZqF72KBM,8467
|
|
3
|
+
lang_systems/operators.py,sha256=Dp7ojkOW2xaBtQyFMYTt52gW3sTAqc1sytUstD46RRA,11410
|
|
4
|
+
lang_systems/pegen_gram_gen.py,sha256=xilypYOUqBQyO1WwNVSn_uvgEPhIjG6IzuvVqvNOQqs,5931
|
|
5
|
+
lang_systems/quantar.gram,sha256=_2z-KAzAUuZ8prpdP0ggJbHLB9JiN9a0B90Tf3jG8YM,1211
|
|
6
|
+
lang_systems/quantar.py,sha256=NZ8YSHlPyuMTKV6z2MbeOMTDhfK9pGtGMuJ3SrtOxW0,2195
|
|
7
|
+
lang_systems/quantar_parser.py,sha256=UCZ2rZ8cvONSk-ES6WEGreaofFVHizyASimqQaZ2V5U,341672
|
|
8
|
+
lang_systems/quantar_tokens.py,sha256=hnAivrO6-uzc06PUoUyN1gZjvFk-npcDlYW3k_2Y4wc,2843
|
|
9
|
+
lang_systems/runner.py,sha256=rMoCUB1Gdj4YlqjHNxHy1O8_ktSCVe_hxS_RJa7Rw6k,1972
|
|
10
|
+
lang_systems/runtime.py,sha256=8tIP-_u82HKUbgtoTxUMN7XpBkKTSkDiSTadPePvGNY,2034
|
|
11
|
+
lang_systems/visitors.py,sha256=rEXr_SdVh0760dUry7TddC4cKOvWge_AHZFNWMrRUsk,898
|
|
12
|
+
lang_systems/lsp/__init__.py,sha256=AqHsUzUez-UV1YLL-TQddyTwRc97JsFUOMXW6WsOqlo,43
|
|
13
|
+
lang_systems/lsp/__main__.py,sha256=U7x7FmdVJL7KA1wzRNA6x0uxHhfzNFLxTKluq_bmxhk,49
|
|
14
|
+
lang_systems/lsp/server.py,sha256=WNZzzprQU_f13otFNNFBBrGqZwB23Aiyt5rAcL7vGy8,7568
|
|
15
|
+
quantum/__init__.py,sha256=FmxPXMSONmAvAZ3NbBF3d8-E77B5iYU_tuswYLu7Ylg,2096
|
|
16
|
+
quantum/basic_funcs.py,sha256=DMd9NY_g4cRFwy8a10iXzhy39EA-cm_bwJ4rfMf9F48,10784
|
|
17
|
+
quantum/composites.py,sha256=_Yp8yXWgu-U6MTDCbg1xRrguez70QQtL4ove7zuicxk,1086
|
|
18
|
+
quantum/gate_abs.py,sha256=HEYR21ojq1f_ODA4zwTT35MQjN8fuD1m8M8pJ43-mRc,5295
|
|
19
|
+
quantum/gate_matrix.py,sha256=XzK4Q014nzGBgBmTgCHIdtbPaf-EnsqHF8QrkClVeCA,5273
|
|
20
|
+
quantum/kernels.py,sha256=sXWm21vdzDep_1m-uHblv34Z_idLQY0yLw95Ns6SebU,9619
|
|
21
|
+
quantum/parse_qasm.py,sha256=TLWNk8Ju52YeO7VDuNWzSqO4Hiki9saJOw6VIJbMo7s,14473
|
|
22
|
+
quantum/qasm.py,sha256=-ZgLAbwpPkJqv68TdbSobrOn34S-UJGh1T4RmKNjfN0,3212
|
|
23
|
+
quantum/qubit_abs.py,sha256=0rWCvaMGDwMR6LKsbh8VO6D0ICP39oHquzDBm9JXV2M,28667
|
|
24
|
+
stl/__init__.py,sha256=1Y4StCuEgfzv0HDlhTaXkMkGGZDEmej_4RLMasYl5nA,48
|
|
25
|
+
stl/physics_algs/__init__.py,sha256=hEdAieZ__De5btHaXt3xfm9WCRF0ZU6XA472HOG8jlU,984
|
|
26
|
+
stl/physics_algs/duals_and_coords.py,sha256=bPc4KgBga2DxxHP5H1uQvzrDOBxQs1PGLRgiahpc6Nc,18206
|
|
27
|
+
stl/physics_algs/units.py,sha256=F9YGMAlz8x3LtEbEtO3VF8gI-6bK-_bBvW0q-YAxSz8,30330
|
|
28
|
+
stl/physics_algs/vector_fields.py,sha256=8XH59cpUPBayZRnEuKulcFbBdVYedM7XfVsCmhn_68E,10011
|
|
29
|
+
stl/quantum_algs/__init__.py,sha256=XEo-8UXUV83k6mZ_BYejGtR0FmW0PHjxvsLvJLFumpI,1958
|
|
30
|
+
stl/quantum_algs/fast_factor.py,sha256=gwQoq2qKj_QjL318dldMdAE0nt7IEGi9ZPtbDfKy7iI,5237
|
|
31
|
+
stl/quantum_algs/grovers_alg.py,sha256=wp8CSIWmu_-vAxZXo4otmLe3yPZbplIkunqsY2PQ6Ok,4241
|
|
32
|
+
stl/quantum_algs/quantum_optim.py,sha256=CPhrJgkFzopXwiw6exXVZW36-dyEJqmsy0-un8IjnvU,6017
|
|
33
|
+
stl/quantum_algs/quantum_optim_hl.py,sha256=_5SY9v5o5-0g9v4lkQxDMAvrKLXw_Si9co5ApJ2RaFc,12582
|
|
34
|
+
stl/quantum_algs/shors_alg.py,sha256=Bok4CTQf7My9P9YsURGoa1_IU-zyWlDVqqmD503xzm8,12048
|
|
35
|
+
quantar-0.1.0.dist-info/METADATA,sha256=b5-kFkF_ElhaBHEFTCZK23jb2Gt2wfF9IxgbmbFq7zs,17889
|
|
36
|
+
quantar-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
37
|
+
quantar-0.1.0.dist-info/entry_points.txt,sha256=_bpkTQuyCSNjWjK0NeveV_FuSqQjCqTxb8cwiox5Sd8,92
|
|
38
|
+
quantar-0.1.0.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
|
|
39
|
+
quantar-0.1.0.dist-info/RECORD,,
|