opensquirrel 0.4.0__tar.gz → 0.6.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.
Files changed (81) hide show
  1. opensquirrel-0.6.0/.gitignore +163 -0
  2. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/PKG-INFO +20 -21
  3. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/README.md +6 -7
  4. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/__init__.py +11 -7
  5. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/circuit.py +41 -32
  6. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/circuit_builder.py +3 -3
  7. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/circuit_matrix_calculator.py +10 -15
  8. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/common.py +25 -7
  9. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/default_gate_modifiers.py +14 -8
  10. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/default_instructions.py +27 -15
  11. opensquirrel-0.6.0/opensquirrel/ir/__init__.py +33 -0
  12. opensquirrel-0.6.0/opensquirrel/ir/default_gates/__init__.py +45 -0
  13. opensquirrel-0.6.0/opensquirrel/ir/default_gates/single_qubit_gates.py +101 -0
  14. opensquirrel-0.6.0/opensquirrel/ir/default_gates/two_qubit_gates.py +97 -0
  15. opensquirrel-0.6.0/opensquirrel/ir/expression.py +292 -0
  16. opensquirrel-0.6.0/opensquirrel/ir/ir.py +155 -0
  17. opensquirrel-0.6.0/opensquirrel/ir/non_unitary.py +134 -0
  18. opensquirrel-0.6.0/opensquirrel/ir/semantics/__init__.py +12 -0
  19. opensquirrel-0.6.0/opensquirrel/ir/semantics/bsr.py +150 -0
  20. opensquirrel-0.6.0/opensquirrel/ir/semantics/controlled_gate.py +38 -0
  21. opensquirrel-0.6.0/opensquirrel/ir/semantics/matrix_gate.py +61 -0
  22. opensquirrel-0.6.0/opensquirrel/ir/statement.py +49 -0
  23. opensquirrel-0.6.0/opensquirrel/ir/unitary.py +54 -0
  24. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/__init__.py +0 -2
  25. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/aba_decomposer.py +2 -1
  26. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/cnot2cz_decomposer.py +9 -4
  27. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/cnot_decomposer.py +8 -4
  28. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/cz_decomposer.py +8 -4
  29. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/mckay_decomposer.py +3 -2
  30. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/swap2cnot_decomposer.py +1 -1
  31. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/swap2cz_decomposer.py +8 -8
  32. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/exporter/__init__.py +3 -1
  33. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/exporter/cqasmv1_exporter.py +15 -11
  34. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/exporter/quantify_scheduler_exporter.py +22 -17
  35. opensquirrel-0.6.0/opensquirrel/passes/mapper/__init__.py +9 -0
  36. opensquirrel-0.6.0/opensquirrel/passes/mapper/general_mapper.py +20 -0
  37. opensquirrel-0.6.0/opensquirrel/passes/mapper/mip_mapper.py +157 -0
  38. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/mapper/qubit_remapper.py +26 -22
  39. opensquirrel-0.6.0/opensquirrel/passes/mapper/simple_mappers.py +67 -0
  40. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/merger/__init__.py +0 -2
  41. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/merger/general_merger.py +2 -1
  42. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/merger/single_qubit_gates_merger.py +5 -2
  43. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/router/__init__.py +4 -2
  44. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/router/astar_router.py +24 -9
  45. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/router/shortest_path_router.py +22 -6
  46. opensquirrel-0.6.0/opensquirrel/passes/validator/__init__.py +7 -0
  47. opensquirrel-0.4.0/opensquirrel/passes/validator/routing_validator.py → opensquirrel-0.6.0/opensquirrel/passes/validator/interaction_validator.py +6 -5
  48. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/validator/primitive_gate_validator.py +1 -1
  49. opensquirrel-0.6.0/opensquirrel/reader/__init__.py +5 -0
  50. opensquirrel-0.4.0/opensquirrel/parser/libqasm/parser.py → opensquirrel-0.6.0/opensquirrel/reader/libqasm_parser.py +12 -10
  51. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/reindexer/__init__.py +3 -1
  52. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/reindexer/qubit_reindexer.py +20 -15
  53. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/utils/check_mapper.py +3 -1
  54. opensquirrel-0.6.0/opensquirrel/utils/context.py +22 -0
  55. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/utils/matrix_expander.py +18 -12
  56. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/writer/writer.py +13 -9
  57. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/pyproject.toml +65 -69
  58. opensquirrel-0.4.0/opensquirrel/ir.py +0 -1059
  59. opensquirrel-0.4.0/opensquirrel/parser/libqasm/README.md +0 -3
  60. opensquirrel-0.4.0/opensquirrel/passes/__init__.py +0 -0
  61. opensquirrel-0.4.0/opensquirrel/passes/mapper/__init__.py +0 -4
  62. opensquirrel-0.4.0/opensquirrel/passes/mapper/general_mapper.py +0 -24
  63. opensquirrel-0.4.0/opensquirrel/passes/mapper/simple_mappers.py +0 -44
  64. opensquirrel-0.4.0/opensquirrel/passes/validator/__init__.py +0 -7
  65. opensquirrel-0.4.0/opensquirrel/writer/__init__.py +0 -0
  66. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/LICENSE.md +0 -0
  67. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/exceptions.py +0 -0
  68. {opensquirrel-0.4.0/opensquirrel/parser → opensquirrel-0.6.0/opensquirrel/passes}/__init__.py +0 -0
  69. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/decomposer/general_decomposer.py +0 -0
  70. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/exporter/export_format.py +0 -0
  71. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/mapper/mapping.py +0 -0
  72. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/mapper/utils.py +0 -0
  73. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/router/general_router.py +0 -0
  74. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/router/heuristics.py +0 -0
  75. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/passes/validator/general_validator.py +0 -0
  76. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/register_manager.py +0 -0
  77. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/utils/__init__.py +0 -0
  78. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/utils/identity_filter.py +0 -0
  79. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/utils/list.py +0 -0
  80. {opensquirrel-0.4.0 → opensquirrel-0.6.0}/opensquirrel/utils/math.py +0 -0
  81. {opensquirrel-0.4.0/opensquirrel/parser/libqasm → opensquirrel-0.6.0/opensquirrel/writer}/__init__.py +0 -0
@@ -0,0 +1,163 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # MkDocs documentation
72
+ site/
73
+ public/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+ example/tutorials/.ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ # For a library or package, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # .python-version
91
+
92
+ # pipenv
93
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
95
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
96
+ # install all needed dependencies.
97
+ #Pipfile.lock
98
+
99
+ # poetry
100
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
102
+ # commonly ignored for libraries.
103
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104
+ #poetry.lock
105
+
106
+ # pdm
107
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108
+ #pdm.lock
109
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110
+ # in version control.
111
+ # https://pdm.fming.dev/#use-with-ide
112
+ .pdm.toml
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+ venv38/
133
+
134
+ # Spyder project settings
135
+ .spyderproject
136
+ .spyproject
137
+
138
+ # Rope project settings
139
+ .ropeproject
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ .idea/
157
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
160
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
161
+ #.idea/
162
+
163
+ %LOCALAPPDATA%
@@ -1,13 +1,13 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: opensquirrel
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: A quantum circuit transformation and manipulation tool
5
- Home-page: https://github.com/QuTech-Delft/OpenSquirrel
6
- License: Apache-2.0
7
- Keywords: quantum,circuits,compilation
8
- Author: Quantum Inspire
9
- Author-email: support@quantum-inspire.com
10
- Requires-Python: >=3.9,<4.0
5
+ Project-URL: Homepage, https://qutech-delft.github.io/OpenSquirrel/
6
+ Project-URL: Repository, https://github.com/QuTech-Delft/OpenSquirrel
7
+ Author-email: Quantum Inspire <support@quantum-inspire.com>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE.md
10
+ Keywords: circuits,compilation,quantum
11
11
  Classifier: Development Status :: 3 - Alpha
12
12
  Classifier: License :: OSI Approved :: Apache Software License
13
13
  Classifier: Programming Language :: Python :: 3
@@ -15,11 +15,12 @@ Classifier: Programming Language :: Python :: 3.9
15
15
  Classifier: Programming Language :: Python :: 3.10
16
16
  Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
- Requires-Dist: libqasm (==1.2.0)
19
- Requires-Dist: networkx (>=3.0.0,<4.0.0)
20
- Requires-Dist: numpy (>=1.26)
21
- Requires-Dist: tox (>=4.24.1,<5.0.0)
22
- Project-URL: Repository, https://github.com/QuTech-Delft/OpenSquirrel
18
+ Requires-Python: >=3.9.0
19
+ Requires-Dist: libqasm==1.2.1
20
+ Requires-Dist: networkx<4,>=3.0.0
21
+ Requires-Dist: numpy>=1.26
22
+ Requires-Dist: scipy<2.0,>=1.14; python_version >= '3.10'
23
+ Requires-Dist: scipy>=1.11.4; python_version < '3.10'
23
24
  Description-Content-Type: text/markdown
24
25
 
25
26
  # OpenSquirrel: a flexible quantum program compiler.
@@ -85,16 +86,15 @@ each traversing and modifying it (_e.g._, decomposition of the gates).
85
86
  Here is an example of building a circuit using the `CircuitBuilder`:
86
87
 
87
88
  ```python
88
- import math
89
+ from math import pi
89
90
  from opensquirrel.circuit_builder import CircuitBuilder
90
- from opensquirrel.ir import Qubit
91
91
 
92
92
  # Initialize the builder and build your circuit
93
93
  builder = CircuitBuilder(qubit_register_size=1)
94
- builder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), math.pi / 3)
94
+ builder.H(0).Z(0).Y(0).Rx(0, pi / 3)
95
95
 
96
96
  # Get the circuit from the circuit builder
97
- qc = builder.to_circuit()
97
+ circuit = builder.to_circuit()
98
98
  ```
99
99
 
100
100
  Alternatively, one can define the same circuit as a cQASM string:
@@ -112,7 +112,7 @@ cqasm_string = ("""
112
112
  """)
113
113
 
114
114
  from opensquirrel.circuit import Circuit
115
- qc = Circuit.from_string(cqasm_string)
115
+ circuit = Circuit.from_string(cqasm_string)
116
116
  ```
117
117
 
118
118
  The circuit can then be decomposed using a decomposition strategy.
@@ -123,7 +123,7 @@ In the example below, the circuit is decomposed using the Z-Y-Z decomposer.
123
123
  ```python
124
124
  from opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer
125
125
 
126
- qc.decompose(decomposer=ZYZDecomposer())
126
+ circuit.decompose(decomposer=ZYZDecomposer())
127
127
  ```
128
128
 
129
129
  Once the circuit is decomposed, it can be written back to cQASM.
@@ -132,7 +132,7 @@ This is done by invoking the `writer` class, as can be seen below.
132
132
  ```python
133
133
  from opensquirrel.writer import writer
134
134
 
135
- writer.circuit_to_string(qc)
135
+ writer.circuit_to_string(circuit)
136
136
  ```
137
137
 
138
138
  The output is then given by the following cQASM string:
@@ -172,4 +172,3 @@ OpenSquirrel is licensed under the Apache License, Version 2.0. See
172
172
  ## Authors
173
173
 
174
174
  Quantum Inspire: [support@quantum-inspire.com](mailto:"support@quantum-inspire.com")
175
-
@@ -61,16 +61,15 @@ each traversing and modifying it (_e.g._, decomposition of the gates).
61
61
  Here is an example of building a circuit using the `CircuitBuilder`:
62
62
 
63
63
  ```python
64
- import math
64
+ from math import pi
65
65
  from opensquirrel.circuit_builder import CircuitBuilder
66
- from opensquirrel.ir import Qubit
67
66
 
68
67
  # Initialize the builder and build your circuit
69
68
  builder = CircuitBuilder(qubit_register_size=1)
70
- builder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), math.pi / 3)
69
+ builder.H(0).Z(0).Y(0).Rx(0, pi / 3)
71
70
 
72
71
  # Get the circuit from the circuit builder
73
- qc = builder.to_circuit()
72
+ circuit = builder.to_circuit()
74
73
  ```
75
74
 
76
75
  Alternatively, one can define the same circuit as a cQASM string:
@@ -88,7 +87,7 @@ cqasm_string = ("""
88
87
  """)
89
88
 
90
89
  from opensquirrel.circuit import Circuit
91
- qc = Circuit.from_string(cqasm_string)
90
+ circuit = Circuit.from_string(cqasm_string)
92
91
  ```
93
92
 
94
93
  The circuit can then be decomposed using a decomposition strategy.
@@ -99,7 +98,7 @@ In the example below, the circuit is decomposed using the Z-Y-Z decomposer.
99
98
  ```python
100
99
  from opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer
101
100
 
102
- qc.decompose(decomposer=ZYZDecomposer())
101
+ circuit.decompose(decomposer=ZYZDecomposer())
103
102
  ```
104
103
 
105
104
  Once the circuit is decomposed, it can be written back to cQASM.
@@ -108,7 +107,7 @@ This is done by invoking the `writer` class, as can be seen below.
108
107
  ```python
109
108
  from opensquirrel.writer import writer
110
109
 
111
- writer.circuit_to_string(qc)
110
+ writer.circuit_to_string(circuit)
112
111
  ```
113
112
 
114
113
  The output is then given by the following cQASM string:
@@ -1,21 +1,24 @@
1
1
  from opensquirrel.circuit import Circuit
2
2
  from opensquirrel.circuit_builder import CircuitBuilder
3
3
  from opensquirrel.ir import (
4
+ Barrier,
5
+ Init,
6
+ Measure,
7
+ Reset,
8
+ Wait,
9
+ )
10
+ from opensquirrel.ir.default_gates import (
4
11
  CNOT,
5
12
  CR,
6
13
  CZ,
7
14
  SWAP,
8
15
  X90,
9
16
  Y90,
10
- Barrier,
11
17
  CRk,
12
18
  H,
13
19
  I,
14
- Init,
15
- Measure,
16
20
  MinusX90,
17
21
  MinusY90,
18
- Reset,
19
22
  Rn,
20
23
  Rx,
21
24
  Ry,
@@ -24,12 +27,10 @@ from opensquirrel.ir import (
24
27
  SDagger,
25
28
  T,
26
29
  TDagger,
27
- Wait,
28
30
  X,
29
31
  Y,
30
32
  Z,
31
33
  )
32
- from opensquirrel.writer import writer
33
34
 
34
35
  __all__ = [
35
36
  "CNOT",
@@ -61,5 +62,8 @@ __all__ = [
61
62
  "X",
62
63
  "Y",
63
64
  "Z",
64
- "writer",
65
65
  ]
66
+
67
+ from importlib.metadata import version
68
+
69
+ __version__ = version("opensquirrel")
@@ -3,15 +3,15 @@ from __future__ import annotations
3
3
  from collections.abc import Callable
4
4
  from typing import TYPE_CHECKING, Any
5
5
 
6
+ from opensquirrel.ir import IR, AsmDeclaration, Gate
6
7
  from opensquirrel.passes.exporter import ExportFormat
7
8
 
8
9
  if TYPE_CHECKING:
9
- from opensquirrel.ir import IR, Gate
10
- from opensquirrel.passes.decomposer import Decomposer
11
- from opensquirrel.passes.mapper import Mapper
12
- from opensquirrel.passes.merger import Merger
13
- from opensquirrel.passes.router import Router
14
- from opensquirrel.passes.validator import Validator
10
+ from opensquirrel.passes.decomposer.general_decomposer import Decomposer
11
+ from opensquirrel.passes.mapper.general_mapper import Mapper
12
+ from opensquirrel.passes.merger.general_merger import Merger
13
+ from opensquirrel.passes.router.general_router import Router
14
+ from opensquirrel.passes.validator.general_validator import Validator
15
15
  from opensquirrel.register_manager import RegisterManager
16
16
 
17
17
 
@@ -46,7 +46,7 @@ class Circuit:
46
46
 
47
47
  def __repr__(self) -> str:
48
48
  """Write the circuit to a cQASM 3 string."""
49
- from opensquirrel import writer
49
+ from opensquirrel.writer import writer
50
50
 
51
51
  return writer.circuit_to_string(self)
52
52
 
@@ -68,9 +68,9 @@ class Circuit:
68
68
  Args:
69
69
  cqasm3_string: a cQASM 3 string
70
70
  """
71
- from opensquirrel.parser.libqasm.parser import Parser
71
+ from opensquirrel.reader import LibQasmParser
72
72
 
73
- return Parser().circuit_from_string(cqasm3_string)
73
+ return LibQasmParser().circuit_from_string(cqasm3_string)
74
74
 
75
75
  @property
76
76
  def qubit_register_size(self) -> int:
@@ -88,17 +88,13 @@ class Circuit:
88
88
  def bit_register_name(self) -> str:
89
89
  return self.register_manager.get_bit_register_name()
90
90
 
91
- def validate(self, validator: Validator) -> None:
92
- """Generic validator pass. It applies the given validator to the circuit."""
93
- validator.validate(self.ir)
94
-
95
- def route(self, router: Router) -> None:
96
- """Generic router pass. It applies the given router to the circuit."""
97
- router.route(self.ir)
98
-
99
- def merge(self, merger: Merger) -> None:
100
- """Generic merge pass. It applies the given merger to the circuit."""
101
- merger.merge(self.ir, self.qubit_register_size)
91
+ def asm_filter(self, backend_name: str) -> None:
92
+ self.ir.statements = [
93
+ statement
94
+ for statement in self.ir.statements
95
+ if not isinstance(statement, AsmDeclaration)
96
+ or (isinstance(statement, AsmDeclaration) and backend_name in str(statement.backend_name))
97
+ ]
102
98
 
103
99
  def decompose(self, decomposer: Decomposer) -> None:
104
100
  """Generic decomposition pass.
@@ -108,13 +104,34 @@ class Circuit:
108
104
 
109
105
  general_decomposer.decompose(self.ir, decomposer)
110
106
 
107
+ def export(self, fmt: ExportFormat | None = None) -> Any:
108
+ if fmt == ExportFormat.QUANTIFY_SCHEDULER:
109
+ from opensquirrel.passes.exporter import quantify_scheduler_exporter
110
+
111
+ return quantify_scheduler_exporter.export(self)
112
+ if fmt == ExportFormat.CQASM_V1:
113
+ from opensquirrel.passes.exporter import cqasmv1_exporter
114
+
115
+ return cqasmv1_exporter.export(self)
116
+ msg = "unknown exporter format"
117
+ raise ValueError(msg)
118
+
111
119
  def map(self, mapper: Mapper) -> None:
112
120
  """Generic qubit mapper pass.
113
121
  Map the (virtual) qubits of the circuit to the physical qubits of the target hardware.
114
122
  """
115
123
  from opensquirrel.passes.mapper.qubit_remapper import remap_ir
116
124
 
117
- remap_ir(self, mapper.get_mapping())
125
+ mapping = mapper.map(self.ir, self.qubit_register_size)
126
+ remap_ir(self, mapping)
127
+
128
+ def merge(self, merger: Merger) -> None:
129
+ """Generic merge pass. It applies the given merger to the circuit."""
130
+ merger.merge(self.ir, self.qubit_register_size)
131
+
132
+ def route(self, router: Router) -> None:
133
+ """Generic router pass. It applies the given router to the circuit."""
134
+ router.route(self.ir)
118
135
 
119
136
  def replace(self, gate: type[Gate], replacement_gates_function: Callable[..., list[Gate]]) -> None:
120
137
  """Manually replace occurrences of a given gate with a list of gates.
@@ -125,14 +142,6 @@ class Circuit:
125
142
 
126
143
  general_decomposer.replace(self.ir, gate, replacement_gates_function)
127
144
 
128
- def export(self, fmt: ExportFormat | None = None) -> Any:
129
- if fmt == ExportFormat.QUANTIFY_SCHEDULER:
130
- from opensquirrel.passes.exporter import quantify_scheduler_exporter
131
-
132
- return quantify_scheduler_exporter.export(self)
133
- if fmt == ExportFormat.CQASM_V1:
134
- from opensquirrel.passes.exporter import cqasmv1_exporter
135
-
136
- return cqasmv1_exporter.export(self)
137
- msg = "unknown exporter format"
138
- raise ValueError(msg)
145
+ def validate(self, validator: Validator) -> None:
146
+ """Generic validator pass. It applies the given validator to the circuit."""
147
+ validator.validate(self.ir)
@@ -88,9 +88,9 @@ class CircuitBuilder:
88
88
  raise ValueError(msg)
89
89
  try:
90
90
  instruction = default_instruction_set[attr](*args)
91
- except TypeError:
92
- msg = f"trying to build '{attr}' with the wrong number or type of arguments: '{args}'"
93
- raise TypeError(msg) from None
91
+ except TypeError as e:
92
+ msg = f"trying to build {attr!r} with the wrong number or type of arguments: {args!r}: {e}"
93
+ raise TypeError(msg) from e
94
94
 
95
95
  self._check_out_of_bounds_access(instruction)
96
96
 
@@ -5,25 +5,20 @@ from typing import TYPE_CHECKING
5
5
  import numpy as np
6
6
  from numpy.typing import NDArray
7
7
 
8
- from opensquirrel.ir import (
9
- CNOT,
10
- CR,
11
- CZ,
12
- SWAP,
13
- BlochSphereRotation,
14
- BsrAngleParam,
15
- BsrFullParams,
16
- BsrNoParams,
17
- ControlledGate,
18
- CRk,
19
- Gate,
20
- IRVisitor,
21
- MatrixGate,
22
- )
8
+ from opensquirrel.ir import Gate, IRVisitor
23
9
  from opensquirrel.utils import get_matrix
24
10
 
25
11
  if TYPE_CHECKING:
12
+ from opensquirrel import CNOT, CR, CZ, SWAP, CRk
26
13
  from opensquirrel.circuit import Circuit
14
+ from opensquirrel.ir.semantics import (
15
+ BlochSphereRotation,
16
+ BsrAngleParam,
17
+ BsrFullParams,
18
+ BsrNoParams,
19
+ ControlledGate,
20
+ MatrixGate,
21
+ )
27
22
 
28
23
 
29
24
  class _CircuitMatrixCalculator(IRVisitor):
@@ -1,12 +1,17 @@
1
1
  from __future__ import annotations
2
2
 
3
- import math
4
- from typing import SupportsFloat
3
+ from math import tau
4
+ from typing import TYPE_CHECKING, SupportsFloat
5
5
 
6
6
  import numpy as np
7
7
  from numpy.typing import NDArray
8
8
 
9
+ if TYPE_CHECKING:
10
+ from opensquirrel.ir.expression import Axis
11
+
12
+
9
13
  ATOL = 0.000_000_1
14
+ REPR_DECIMALS = 5
10
15
 
11
16
 
12
17
  def normalize_angle(x: SupportsFloat) -> float:
@@ -19,11 +24,11 @@ def normalize_angle(x: SupportsFloat) -> float:
19
24
  The normalized angle.
20
25
  """
21
26
  x = float(x)
22
- t = x - 2 * math.pi * (x // (2 * math.pi) + 1)
23
- if t < -math.pi + ATOL:
24
- t += 2 * math.pi
25
- elif t > math.pi:
26
- t -= 2 * math.pi
27
+ t = x - tau * (x // tau + 1)
28
+ if t < -tau / 2 + ATOL:
29
+ t += tau
30
+ elif t > tau / 2:
31
+ t -= tau
27
32
  return t
28
33
 
29
34
 
@@ -60,3 +65,16 @@ def is_identity_matrix_up_to_a_global_phase(matrix: NDArray[np.complex128]) -> b
60
65
  Whether matrix is an identity matrix up to a global phase.
61
66
  """
62
67
  return are_matrices_equivalent_up_to_global_phase(matrix, np.eye(matrix.shape[0], dtype=np.complex128))
68
+
69
+
70
+ def repr_round(value: float | Axis | NDArray[np.complex128], decimals: int = REPR_DECIMALS) -> str:
71
+ """
72
+ Given a numerical value (of type `float`, `Axis`, or `NDArray[np.complex128]`):
73
+ - rounds it to `REPR_DECIMALS`,
74
+ - converts it to string, and
75
+ - removes the newlines.
76
+
77
+ Returns:
78
+ A single-line string representation of a numerical value.
79
+ """
80
+ return f"{np.round(value, decimals)}".replace("\n", "")
@@ -1,9 +1,13 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from collections.abc import Callable
4
- from typing import Any, SupportsFloat
4
+ from typing import TYPE_CHECKING, Any, SupportsFloat
5
5
 
6
- from opensquirrel.ir import BlochSphereRotation, ControlledGate, QubitLike
6
+ from opensquirrel.ir.semantics import BlochSphereRotation, ControlledGate
7
+ from opensquirrel.utils.context import temporary_class_attr
8
+
9
+ if TYPE_CHECKING:
10
+ from opensquirrel.ir import QubitLike
7
11
 
8
12
 
9
13
  class GateModifier:
@@ -15,9 +19,10 @@ class InverseGateModifier(GateModifier):
15
19
  self.gate_generator = gate_generator
16
20
 
17
21
  def __call__(self, *args: Any) -> BlochSphereRotation:
18
- gate: BlochSphereRotation = self.gate_generator(*args)
19
- modified_angle = gate.angle * -1
20
- modified_phase = gate.phase * -1
22
+ with temporary_class_attr(BlochSphereRotation, attr="normalize_angle_params", value=False):
23
+ gate: BlochSphereRotation = self.gate_generator(*args)
24
+ modified_angle = gate.angle * -1
25
+ modified_phase = gate.phase * -1
21
26
  return BlochSphereRotation.try_match_replace_with_default(
22
27
  BlochSphereRotation(qubit=gate.qubit, axis=gate.axis, angle=modified_angle, phase=modified_phase)
23
28
  )
@@ -29,9 +34,10 @@ class PowerGateModifier(GateModifier):
29
34
  self.gate_generator = gate_generator
30
35
 
31
36
  def __call__(self, *args: Any) -> BlochSphereRotation:
32
- gate: BlochSphereRotation = self.gate_generator(*args)
33
- modified_angle = gate.angle * float(self.exponent)
34
- modified_phase = gate.phase * float(self.exponent)
37
+ with temporary_class_attr(BlochSphereRotation, attr="normalize_angle_params", value=False):
38
+ gate: BlochSphereRotation = self.gate_generator(*args)
39
+ modified_angle = gate.angle * float(self.exponent)
40
+ modified_phase = gate.phase * float(self.exponent)
35
41
  return BlochSphereRotation.try_match_replace_with_default(
36
42
  BlochSphereRotation(qubit=gate.qubit, axis=gate.axis, angle=modified_angle, phase=modified_phase)
37
43
  )