graphix-qasm-parser 0.1.0__tar.gz → 0.1.1__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.
Files changed (28) hide show
  1. graphix_qasm_parser-0.1.1/.github/workflows/ci.yml +37 -0
  2. graphix_qasm_parser-0.1.1/.github/workflows/release.yml +29 -0
  3. graphix_qasm_parser-0.1.1/.github/workflows/ruff.yml +51 -0
  4. graphix_qasm_parser-0.1.1/.github/workflows/typecheck.yml +32 -0
  5. graphix_qasm_parser-0.1.1/.gitignore +3 -0
  6. graphix_qasm_parser-0.1.1/CHANGELOG.md +15 -0
  7. graphix_qasm_parser-0.1.1/CODE_OF_CONDUCT.md +128 -0
  8. graphix_qasm_parser-0.1.1/CONTRIBUTING.md +5 -0
  9. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/PKG-INFO +2 -2
  10. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/README.md +2 -1
  11. graphix_qasm_parser-0.1.1/graphix_qasm_parser/_version.py +34 -0
  12. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser/parser.py +40 -5
  13. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser.egg-info/PKG-INFO +2 -2
  14. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser.egg-info/SOURCES.txt +10 -0
  15. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser.egg-info/requires.txt +1 -1
  16. graphix_qasm_parser-0.1.1/noxfile.py +19 -0
  17. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/pyproject.toml +8 -2
  18. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/requirements.txt +1 -1
  19. graphix_qasm_parser-0.1.1/tests/__init__.py +1 -0
  20. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/tests/test_parser.py +74 -17
  21. graphix_qasm_parser-0.1.0/graphix_qasm_parser/_version.py +0 -0
  22. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/LICENSE +0 -0
  23. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser/__init__.py +0 -0
  24. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser/py.typed +0 -0
  25. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser.egg-info/dependency_links.txt +0 -0
  26. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/graphix_qasm_parser.egg-info/top_level.txt +0 -0
  27. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/requirements-dev.txt +0 -0
  28. {graphix_qasm_parser-0.1.0 → graphix_qasm_parser-0.1.1}/setup.cfg +0 -0
@@ -0,0 +1,37 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ workflow_dispatch:
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ concurrency:
11
+ group: ${{ github.workflow }}-${{ github.ref }}
12
+ cancel-in-progress: true
13
+
14
+ jobs:
15
+ test:
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ os: ["ubuntu-latest", "windows-latest", "macos-latest"]
20
+ python: ["3.10", "3.11", "3.12", "3.13"]
21
+
22
+ name: "Python ${{ matrix.python }} / ${{ matrix.os }}"
23
+ runs-on: ${{ matrix.os }}
24
+
25
+ steps:
26
+ - uses: actions/checkout@v4
27
+
28
+ - uses: actions/setup-python@v5
29
+ with:
30
+ python-version: ${{ matrix.python }}
31
+
32
+ - run: python -m pip install --upgrade pip
33
+
34
+ - name: Setup nox
35
+ run: pip install -c requirements-dev.txt nox
36
+
37
+ - run: nox --python ${{ matrix.python }}
@@ -0,0 +1,29 @@
1
+ name: publish
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ env:
8
+ python-version: "3.13"
9
+
10
+ jobs:
11
+ publish:
12
+ name: Build and publish Python distributions to PyPI
13
+ runs-on: ubuntu-latest
14
+ environment: release
15
+ permissions:
16
+ id-token: write
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - uses: actions/setup-python@v5
20
+ with:
21
+ python-version: ${{ env.python-version }}
22
+ - name: Install pypa/build
23
+ run: |
24
+ python -m pip install -U pip
25
+ pip install build
26
+ - name: Build a binary wheel and a source tarball
27
+ run: python -m build --sdist --wheel --outdir dist/ .
28
+ - name: Publish distribution 📦 to PyPI
29
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,51 @@
1
+ name: ruff
2
+
3
+ on:
4
+ pull_request:
5
+ workflow_dispatch:
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ concurrency:
11
+ group: ${{ github.workflow }}-${{ github.ref }}
12
+ cancel-in-progress: true
13
+
14
+ jobs:
15
+ lint:
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+
20
+ - name: Cache pip dependencies
21
+ uses: actions/cache@v4
22
+ with:
23
+ path: ~/.cache/pip
24
+ key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }}
25
+ restore-keys: |
26
+ ${{ runner.os }}-pip-
27
+
28
+ - name: Install dependencies
29
+ run: python -m pip install --upgrade pip && python -m pip install ruff -c requirements-dev.txt
30
+
31
+ - name: Run Ruff Linter
32
+ run: ruff check --output-format=github
33
+
34
+ format:
35
+ runs-on: ubuntu-latest
36
+ steps:
37
+ - uses: actions/checkout@v4
38
+
39
+ - name: Cache pip dependencies
40
+ uses: actions/cache@v4
41
+ with:
42
+ path: ~/.cache/pip
43
+ key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }}
44
+ restore-keys: |
45
+ ${{ runner.os }}-pip-
46
+
47
+ - name: Install dependencies
48
+ run: python -m pip install --upgrade pip && python -m pip install ruff -c requirements-dev.txt
49
+
50
+ - name: Run Ruff Formatter Check
51
+ run: ruff format --check
@@ -0,0 +1,32 @@
1
+ name: typecheck
2
+
3
+ on:
4
+ pull_request:
5
+ workflow_dispatch:
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ concurrency:
11
+ group: ${{ github.workflow }}-${{ github.ref }}
12
+ cancel-in-progress: true
13
+
14
+ env:
15
+ python-version: "3.13"
16
+
17
+ jobs:
18
+ mypy-pyright:
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v4
23
+
24
+ - uses: actions/setup-python@v5
25
+ with:
26
+ python-version: ${{ env.python-version }}
27
+
28
+ - run: |
29
+ python -m pip install --upgrade pip
30
+ pip install .[dev]
31
+
32
+ - run: mypy
@@ -0,0 +1,3 @@
1
+ __pycache__/
2
+ /graphix_qasm_parser.egg-info/
3
+ /graphix_qasm_parser/_version.py
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.1] - 2026-02-05
9
+
10
+ - #7: Parsing of `CZ` gate
11
+
12
+ - #8, #9: Compatibility with the new angle convention in Graphix (https://github.com/TeamGraphix/graphix/pull/399)
13
+
14
+
15
+ ## [0.1.0] - 2025-11-24
@@ -0,0 +1,128 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ - Demonstrating empathy and kindness toward other people
21
+ - Being respectful of differing opinions, viewpoints, and experiences
22
+ - Giving and gracefully accepting constructive feedback
23
+ - Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ - Focusing on what is best not just for us as individuals, but for the
26
+ overall community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ - The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ - Trolling, insulting or derogatory comments, and personal or political attacks
33
+ - Public or private harassment
34
+ - Publishing others' private information, such as a physical or email
35
+ address, without their explicit permission
36
+ - Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ <shinichi.sunami@gmail.com>.
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.0, available at
119
+ <https://www.contributor-covenant.org/version/2/0/code_of_conduct.html>.
120
+
121
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
+ enforcement ladder](https://github.com/mozilla/diversity).
123
+
124
+ [homepage]: https://www.contributor-covenant.org
125
+
126
+ For answers to common questions about this code of conduct, see the FAQ at
127
+ <https://www.contributor-covenant.org/faq>. Translations are available at
128
+ <https://www.contributor-covenant.org/translations>.
@@ -0,0 +1,5 @@
1
+ # Contributing
2
+
3
+ Thank you for your interest in `Graphix`!
4
+ Contribution guidelines are the same as for the main `Graphix` repository:
5
+ https://github.com/TeamGraphix/graphix/blob/master/CONTRIBUTING.md
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: graphix-qasm-parser
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Author-email: Thierry Martinez <Thierry.Martinez@inria.fr>
5
5
  Maintainer-email: Thierry Martinez <Thierry.Martinez@inria.fr>
6
6
  License-File: LICENSE
7
- Requires-Dist: graphix>=0.3.1
7
+ Requires-Dist: graphix>=0.3.3
8
8
  Requires-Dist: openqasm-parser>=3.1.0
9
9
  Provides-Extra: dev
10
10
  Requires-Dist: mypy==1.17.0; extra == "dev"
@@ -53,8 +53,9 @@ circuit = parser.parse_file("my_circuit.qasm")
53
53
  |------------------------------------------------------------------|---------------------|
54
54
  | [ccx](https://openqasm.com/language/standard_library.html#ccx) | CCX |
55
55
  | [crz](https://openqasm.com/language/standard_library.html#crz) | RZZ |
56
- | [cx](https://openqasm.com/language/standard_library.html#cx) | CX |
56
+ | [cx](https://openqasm.com/language/standard_library.html#cx) | CNOT |
57
57
  | [swap](https://openqasm.com/language/standard_library.html#swap) | SWAP |
58
+ | [cz](https://openqasm.com/language/standard_library.html#cz) | CZ |
58
59
  | [h](https://openqasm.com/language/standard_library.html#h) | H |
59
60
  | [s](https://openqasm.com/language/standard_library.html#s) | S |
60
61
  | [x](https://openqasm.com/language/standard_library.html#x) | X |
@@ -0,0 +1,34 @@
1
+ # file generated by setuptools-scm
2
+ # don't change, don't track in version control
3
+
4
+ __all__ = [
5
+ "__version__",
6
+ "__version_tuple__",
7
+ "version",
8
+ "version_tuple",
9
+ "__commit_id__",
10
+ "commit_id",
11
+ ]
12
+
13
+ TYPE_CHECKING = False
14
+ if TYPE_CHECKING:
15
+ from typing import Tuple
16
+ from typing import Union
17
+
18
+ VERSION_TUPLE = Tuple[Union[int, str], ...]
19
+ COMMIT_ID = Union[str, None]
20
+ else:
21
+ VERSION_TUPLE = object
22
+ COMMIT_ID = object
23
+
24
+ version: str
25
+ __version__: str
26
+ __version_tuple__: VERSION_TUPLE
27
+ version_tuple: VERSION_TUPLE
28
+ commit_id: COMMIT_ID
29
+ __commit_id__: COMMIT_ID
30
+
31
+ __version__ = version = '0.1.1'
32
+ __version_tuple__ = version_tuple = (0, 1, 1)
33
+
34
+ __commit_id__ = commit_id = 'gd19a6333f'
@@ -24,6 +24,37 @@ if TYPE_CHECKING:
24
24
 
25
25
  from graphix.instruction import Instruction
26
26
 
27
+ # Compatibility with graphix <= 0.3.3
28
+ # See https://github.com/TeamGraphix/graphix/pull/379
29
+
30
+ ANGLE_PI: float
31
+
32
+ def rad_to_angle(angle: float) -> float:
33
+ """Prototype for rad_to_angle."""
34
+ ...
35
+
36
+ CZ = SWAP
37
+ else:
38
+ try:
39
+ from graphix.instruction import CZ
40
+ except ImportError:
41
+
42
+ def CZ(_q0: int, _q1: int) -> None: # noqa: N802
43
+ """In older versions of graphix (<= 0.3.3), CZ instructions were not supported."""
44
+ msg = "CZ instructions are not supported by graphix <= 0.3.3"
45
+ raise NotImplementedError(msg)
46
+
47
+ try:
48
+ from graphix.fundamentals import ANGLE_PI, rad_to_angle
49
+ except ImportError:
50
+ # Compatibility with graphix <= 0.3.3
51
+ # See https://github.com/TeamGraphix/graphix/pull/399
52
+ ANGLE_PI = math.pi
53
+
54
+ def rad_to_angle(angle: float) -> float:
55
+ """In older versions of graphix (<= 0.3.3), instruction angles were expressed in radians."""
56
+ return angle
57
+
27
58
 
28
59
  class OpenQASMParser:
29
60
  """Graphix OpenQASM parser."""
@@ -295,13 +326,16 @@ class _CircuitVisitor(qasm3ParserVisitor):
295
326
  instruction = CCX(target=operands[2], controls=(operands[0], operands[1]))
296
327
  elif gate == "crz":
297
328
  # https://openqasm.com/language/standard_library.html#crz
298
- instruction = RZZ(target=operands[1], control=operands[0], angle=exprs[0])
329
+ instruction = RZZ(target=operands[1], control=operands[0], angle=rad_to_angle(exprs[0]))
299
330
  elif gate == "cx":
300
331
  # https://openqasm.com/language/standard_library.html#cx
301
332
  instruction = CNOT(target=operands[1], control=operands[0])
302
333
  elif gate == "swap":
303
334
  # https://openqasm.com/language/standard_library.html#swap
304
335
  instruction = SWAP(targets=(operands[0], operands[1]))
336
+ elif gate == "cz":
337
+ # https://openqasm.com/language/standard_library.html#cz
338
+ instruction = CZ(targets=(operands[0], operands[1]))
305
339
  elif gate == "h":
306
340
  # https://openqasm.com/language/standard_library.html#h
307
341
  instruction = H(target=operands[0])
@@ -322,13 +356,13 @@ class _CircuitVisitor(qasm3ParserVisitor):
322
356
  instruction = I(target=operands[0])
323
357
  elif gate == "rx":
324
358
  # https://openqasm.com/language/standard_library.html#rx
325
- instruction = RX(target=operands[0], angle=exprs[0])
359
+ instruction = RX(target=operands[0], angle=rad_to_angle(exprs[0]))
326
360
  elif gate == "ry":
327
361
  # https://openqasm.com/language/standard_library.html#ry
328
- instruction = RY(target=operands[0], angle=exprs[0])
362
+ instruction = RY(target=operands[0], angle=rad_to_angle(exprs[0]))
329
363
  elif gate == "rz":
330
364
  # https://openqasm.com/language/standard_library.html#rz
331
- instruction = RZ(target=operands[0], angle=exprs[0])
365
+ instruction = RZ(target=operands[0], angle=rad_to_angle(exprs[0]))
332
366
  else:
333
367
  msg = f"Unknown gate: {gate}"
334
368
  raise NotImplementedError(msg)
@@ -440,7 +474,8 @@ class _ExpressionVisitor(qasm3ParserVisitor):
440
474
  raise NotImplementedError(msg)
441
475
 
442
476
  def parse_binary_operator(
443
- self, ctx: qasm3Parser.AdditiveExpressionContext | qasm3Parser.MultiplicativeExpressionContext
477
+ self,
478
+ ctx: qasm3Parser.AdditiveExpressionContext | qasm3Parser.MultiplicativeExpressionContext,
444
479
  ) -> _Value:
445
480
  lhs_expr: qasm3Parser.ExpressionContext = ctx.getChild(0)
446
481
  rhs_expr: qasm3Parser.ExpressionContext = ctx.getChild(2)
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: graphix-qasm-parser
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Author-email: Thierry Martinez <Thierry.Martinez@inria.fr>
5
5
  Maintainer-email: Thierry Martinez <Thierry.Martinez@inria.fr>
6
6
  License-File: LICENSE
7
- Requires-Dist: graphix>=0.3.1
7
+ Requires-Dist: graphix>=0.3.3
8
8
  Requires-Dist: openqasm-parser>=3.1.0
9
9
  Provides-Extra: dev
10
10
  Requires-Dist: mypy==1.17.0; extra == "dev"
@@ -1,8 +1,17 @@
1
+ .gitignore
2
+ CHANGELOG.md
3
+ CODE_OF_CONDUCT.md
4
+ CONTRIBUTING.md
1
5
  LICENSE
2
6
  README.md
7
+ noxfile.py
3
8
  pyproject.toml
4
9
  requirements-dev.txt
5
10
  requirements.txt
11
+ .github/workflows/ci.yml
12
+ .github/workflows/release.yml
13
+ .github/workflows/ruff.yml
14
+ .github/workflows/typecheck.yml
6
15
  graphix_qasm_parser/__init__.py
7
16
  graphix_qasm_parser/_version.py
8
17
  graphix_qasm_parser/parser.py
@@ -12,4 +21,5 @@ graphix_qasm_parser.egg-info/SOURCES.txt
12
21
  graphix_qasm_parser.egg-info/dependency_links.txt
13
22
  graphix_qasm_parser.egg-info/requires.txt
14
23
  graphix_qasm_parser.egg-info/top_level.txt
24
+ tests/__init__.py
15
25
  tests/test_parser.py
@@ -1,4 +1,4 @@
1
- graphix>=0.3.1
1
+ graphix>=0.3.3
2
2
  openqasm-parser>=3.1.0
3
3
 
4
4
  [dev]
@@ -0,0 +1,19 @@
1
+ """Run tests with nox."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import nox
6
+ from nox import Session
7
+
8
+
9
+ def install_pytest(session: Session) -> None:
10
+ """Install pytest when requirements-dev.txt is not installed."""
11
+ session.install("pytest")
12
+
13
+
14
+ @nox.session(python=["3.9", "3.10", "3.11", "3.12", "3.13"])
15
+ def tests(session: Session) -> None:
16
+ """Run the test suite with minimal dependencies."""
17
+ session.install(".")
18
+ install_pytest(session)
19
+ session.run("pytest")
@@ -1,3 +1,7 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77", "wheel", "setuptools_scm"]
3
+ build-backend = "setuptools.build_meta"
4
+
1
5
  [project]
2
6
  name = "graphix-qasm-parser"
3
7
  authors = [
@@ -7,8 +11,10 @@ maintainers = [
7
11
  { name = "Thierry Martinez", email = "Thierry.Martinez@inria.fr" },
8
12
  ]
9
13
  license-files = ["LICENSE"]
10
- version = "0.1.0"
11
- dynamic = ["dependencies", "optional-dependencies"]
14
+ dynamic = ["version", "dependencies", "optional-dependencies"]
15
+
16
+ [tool.setuptools_scm]
17
+ version_file = "graphix_qasm_parser/_version.py"
12
18
 
13
19
  [tool.setuptools.dynamic]
14
20
  dependencies = { file = ["requirements.txt"] }
@@ -1,2 +1,2 @@
1
- graphix>=0.3.1
1
+ graphix>=0.3.3
2
2
  openqasm-parser>=3.1.0
@@ -0,0 +1 @@
1
+ """Tests for Graphix QASM parser."""
@@ -1,12 +1,49 @@
1
1
  """Tests for Graphix QASM parser."""
2
2
 
3
3
  import math
4
+ from typing import TYPE_CHECKING
4
5
 
5
6
  import pytest
6
7
  from graphix.instruction import CCX, CNOT, RX, RY, RZ, RZZ, SWAP, H, S, X, Y, Z
7
8
 
8
9
  from graphix_qasm_parser import OpenQASMParser
9
10
 
11
+ if TYPE_CHECKING:
12
+ # Compatibility with graphix <= 0.3.3
13
+ # See https://github.com/TeamGraphix/graphix/pull/379
14
+
15
+ ANGLE_PI: float
16
+
17
+ def rad_to_angle(angle: float) -> float:
18
+ """Prototype for rad_to_angle."""
19
+ ...
20
+
21
+ CZ = SWAP
22
+ HAS_CZ = True
23
+ else:
24
+ try:
25
+ from graphix.instruction import CZ
26
+
27
+ HAS_CZ = True
28
+ except ImportError:
29
+ HAS_CZ = False
30
+
31
+ def CZ(_q0: int, _q1: int) -> None: # noqa: N802
32
+ """In older versions of graphix (<= 0.3.3), CZ instructions were not supported."""
33
+ msg = "CZ instructions are not supported by graphix <= 0.3.3"
34
+ raise NotImplementedError(msg)
35
+
36
+ try:
37
+ from graphix.fundamentals import ANGLE_PI, rad_to_angle
38
+ except ImportError:
39
+ from math import pi as ANGLE_PI # noqa: N812
40
+
41
+ # Compatibility with graphix <= 0.3.3
42
+ # See https://github.com/TeamGraphix/graphix/pull/399
43
+ def rad_to_angle(angle: float) -> float:
44
+ """In older versions of graphix (<= 0.3.3), instruction angles were expressed in radians."""
45
+ return angle
46
+
10
47
 
11
48
  def test_parse_simple_circuit() -> None:
12
49
  """Test parse simple circuit."""
@@ -22,7 +59,7 @@ rz(5*pi/4) q;
22
59
  instruction = circuit.instruction[0]
23
60
  assert isinstance(instruction, RZ)
24
61
  assert isinstance(instruction.angle, float)
25
- assert math.isclose(instruction.angle, 5 * math.pi / 4)
62
+ assert math.isclose(instruction.angle, 5 * ANGLE_PI / 4)
26
63
 
27
64
 
28
65
  def test_parse_simple_circuit_old_syntax() -> None:
@@ -39,7 +76,7 @@ rz(5*pi/4) q;
39
76
  instruction = circuit.instruction[0]
40
77
  assert isinstance(instruction, RZ)
41
78
  assert isinstance(instruction.angle, float)
42
- assert math.isclose(instruction.angle, 5 * math.pi / 4)
79
+ assert math.isclose(instruction.angle, 5 * ANGLE_PI / 4)
43
80
 
44
81
 
45
82
  def test_parse_all_instructions() -> None: # noqa: PLR0915
@@ -51,6 +88,7 @@ ccx q[0], q[1], q[2];
51
88
  crz(pi/3) q[0], q[1];
52
89
  cx q[0], q[1];
53
90
  swap q[0], q[1];
91
+ // cz q[0], q[1];
54
92
  h q[0];
55
93
  s q[0];
56
94
  x q[0];
@@ -73,7 +111,7 @@ rz(pi/4) q[0];
73
111
  assert instruction.target == 1
74
112
  assert instruction.control == 0
75
113
  assert isinstance(instruction.angle, float)
76
- assert math.isclose(instruction.angle, math.pi / 3)
114
+ assert math.isclose(instruction.angle, ANGLE_PI / 3)
77
115
  instruction = next(iterator)
78
116
  assert isinstance(instruction, CNOT)
79
117
  assert instruction.target == 1
@@ -100,17 +138,36 @@ rz(pi/4) q[0];
100
138
  assert isinstance(instruction, RX)
101
139
  assert instruction.target == 0
102
140
  assert isinstance(instruction.angle, float)
103
- assert math.isclose(instruction.angle, math.pi / 4)
141
+ assert math.isclose(instruction.angle, ANGLE_PI / 4)
104
142
  instruction = next(iterator)
105
143
  assert isinstance(instruction, RY)
106
144
  assert instruction.target == 0
107
145
  assert isinstance(instruction.angle, float)
108
- assert math.isclose(instruction.angle, math.pi / 4)
146
+ assert math.isclose(instruction.angle, ANGLE_PI / 4)
109
147
  instruction = next(iterator)
110
148
  assert isinstance(instruction, RZ)
111
149
  assert instruction.target == 0
112
150
  assert isinstance(instruction.angle, float)
113
- assert math.isclose(instruction.angle, math.pi / 4)
151
+ assert math.isclose(instruction.angle, ANGLE_PI / 4)
152
+ with pytest.raises(StopIteration):
153
+ next(iterator)
154
+
155
+
156
+ @pytest.mark.skipif(not HAS_CZ, reason="CZ instructions are not supported by graphix <= 0.3.3")
157
+ def test_parse_cz() -> None:
158
+ """Test parse CZ instructions."""
159
+ s = """
160
+ include "qelib1.inc";
161
+ qubit[2] q;
162
+ cz q[0], q[1];
163
+ """
164
+ parser = OpenQASMParser()
165
+ circuit = parser.parse_str(s)
166
+ assert circuit.width == 2
167
+ iterator = iter(circuit.instruction)
168
+ instruction = next(iterator)
169
+ assert isinstance(instruction, CZ)
170
+ assert instruction.targets == (0, 1)
114
171
  with pytest.raises(StopIteration):
115
172
  next(iterator)
116
173
 
@@ -138,43 +195,43 @@ rz(π) q;
138
195
  instruction = next(iterator)
139
196
  assert isinstance(instruction, RZ)
140
197
  assert isinstance(instruction.angle, float)
141
- assert math.isclose(instruction.angle, 1)
198
+ assert math.isclose(instruction.angle, rad_to_angle(1))
142
199
  instruction = next(iterator)
143
200
  assert isinstance(instruction, RZ)
144
201
  assert isinstance(instruction.angle, float)
145
- assert math.isclose(instruction.angle, 1.5)
202
+ assert math.isclose(instruction.angle, rad_to_angle(1.5))
146
203
  instruction = next(iterator)
147
204
  assert isinstance(instruction, RZ)
148
205
  assert isinstance(instruction.angle, float)
149
- assert math.isclose(instruction.angle, -1)
206
+ assert math.isclose(instruction.angle, rad_to_angle(-1))
150
207
  instruction = next(iterator)
151
208
  assert isinstance(instruction, RZ)
152
209
  assert isinstance(instruction.angle, float)
153
- assert math.isclose(instruction.angle, 1 + 2)
210
+ assert math.isclose(instruction.angle, rad_to_angle(1 + 2))
154
211
  instruction = next(iterator)
155
212
  assert isinstance(instruction, RZ)
156
213
  assert isinstance(instruction.angle, float)
157
- assert math.isclose(instruction.angle, 1 - 2)
214
+ assert math.isclose(instruction.angle, rad_to_angle(1 - 2))
158
215
  instruction = next(iterator)
159
216
  assert isinstance(instruction, RZ)
160
217
  assert isinstance(instruction.angle, float)
161
- assert math.isclose(instruction.angle, 1 * 2)
218
+ assert math.isclose(instruction.angle, rad_to_angle(1 * 2))
162
219
  instruction = next(iterator)
163
220
  assert isinstance(instruction, RZ)
164
221
  assert isinstance(instruction.angle, float)
165
- assert math.isclose(instruction.angle, 1 / 2)
222
+ assert math.isclose(instruction.angle, rad_to_angle(1 / 2))
166
223
  instruction = next(iterator)
167
224
  assert isinstance(instruction, RZ)
168
225
  assert isinstance(instruction.angle, float)
169
- assert math.isclose(instruction.angle, 1 - (2 + 3))
226
+ assert math.isclose(instruction.angle, rad_to_angle(1 - (2 + 3)))
170
227
  instruction = next(iterator)
171
228
  assert isinstance(instruction, RZ)
172
229
  assert isinstance(instruction.angle, float)
173
- assert math.isclose(instruction.angle, math.pi)
230
+ assert math.isclose(instruction.angle, ANGLE_PI)
174
231
  instruction = next(iterator)
175
232
  assert isinstance(instruction, RZ)
176
233
  assert isinstance(instruction.angle, float)
177
- assert math.isclose(instruction.angle, math.pi)
234
+ assert math.isclose(instruction.angle, ANGLE_PI)
178
235
  with pytest.raises(StopIteration):
179
236
  next(iterator)
180
237
 
@@ -195,6 +252,6 @@ rz(alpha) q[0];
195
252
  instruction = next(iterator)
196
253
  assert isinstance(instruction, RZ)
197
254
  assert isinstance(instruction.angle, float)
198
- assert math.isclose(instruction.angle, math.pi / 4)
255
+ assert math.isclose(instruction.angle, ANGLE_PI / 4)
199
256
  with pytest.raises(StopIteration):
200
257
  next(iterator)
File without changes