opensquirrel 0.1.0__tar.gz → 0.2.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 (67) hide show
  1. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/PKG-INFO +7 -7
  2. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/README.md +3 -3
  3. opensquirrel-0.2.0/opensquirrel/__init__.py +63 -0
  4. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/circuit.py +26 -36
  5. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/circuit_builder.py +38 -53
  6. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/circuit_matrix_calculator.py +3 -6
  7. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/common.py +24 -3
  8. opensquirrel-0.2.0/opensquirrel/default_gate_modifiers.py +42 -0
  9. opensquirrel-0.2.0/opensquirrel/default_instructions.py +243 -0
  10. opensquirrel-0.2.0/opensquirrel/instruction_library.py +27 -0
  11. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/ir.py +315 -180
  12. opensquirrel-0.2.0/opensquirrel/parser/libqasm/parser.py +225 -0
  13. opensquirrel-0.2.0/opensquirrel/passes/decomposer/__init__.py +27 -0
  14. opensquirrel-0.2.0/opensquirrel/passes/decomposer/aba_decomposer.py +216 -0
  15. opensquirrel-0.2.0/opensquirrel/passes/decomposer/cnot2cz_decomposer.py +37 -0
  16. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/decomposer/cnot_decomposer.py +15 -15
  17. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/decomposer/general_decomposer.py +6 -2
  18. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/decomposer/mckay_decomposer.py +20 -15
  19. opensquirrel-0.2.0/opensquirrel/passes/decomposer/swap2cnot_decomposer.py +29 -0
  20. opensquirrel-0.2.0/opensquirrel/passes/exporter/__init__.py +3 -0
  21. opensquirrel-0.2.0/opensquirrel/passes/exporter/cqasmv1_exporter.py +107 -0
  22. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/exporter/export_format.py +1 -0
  23. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/exporter/quantify_scheduler_exporter.py +31 -21
  24. opensquirrel-0.2.0/opensquirrel/passes/mapper/__init__.py +4 -0
  25. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/mapper/general_mapper.py +1 -1
  26. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/mapper/qubit_remapper.py +19 -8
  27. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/mapper/simple_mappers.py +2 -2
  28. opensquirrel-0.2.0/opensquirrel/passes/merger/__init__.py +7 -0
  29. opensquirrel-0.2.0/opensquirrel/passes/merger/general_merger.py +168 -0
  30. opensquirrel-0.2.0/opensquirrel/passes/merger/single_qubit_gates_merger.py +52 -0
  31. opensquirrel-0.2.0/opensquirrel/passes/router/__init__.py +7 -0
  32. opensquirrel-0.2.0/opensquirrel/passes/router/general_router.py +9 -0
  33. opensquirrel-0.2.0/opensquirrel/passes/router/routing_checker.py +28 -0
  34. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/register_manager.py +1 -1
  35. opensquirrel-0.2.0/opensquirrel/reindexer/__init__.py +3 -0
  36. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/reindexer/qubit_reindexer.py +24 -3
  37. opensquirrel-0.2.0/opensquirrel/utils/__init__.py +18 -0
  38. {opensquirrel-0.1.0/opensquirrel/utils/check_passes → opensquirrel-0.2.0/opensquirrel/utils}/check_mapper.py +18 -18
  39. opensquirrel-0.2.0/opensquirrel/utils/list.py +9 -0
  40. opensquirrel-0.2.0/opensquirrel/utils/math.py +12 -0
  41. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/utils/matrix_expander.py +10 -10
  42. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/writer/writer.py +34 -27
  43. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/pyproject.toml +18 -14
  44. opensquirrel-0.1.0/opensquirrel/__init__.py +0 -5
  45. opensquirrel-0.1.0/opensquirrel/decomposer/__init__.py +0 -23
  46. opensquirrel-0.1.0/opensquirrel/decomposer/aba_decomposer.py +0 -199
  47. opensquirrel-0.1.0/opensquirrel/default_gates.py +0 -191
  48. opensquirrel-0.1.0/opensquirrel/default_measures.py +0 -14
  49. opensquirrel-0.1.0/opensquirrel/default_resets.py +0 -9
  50. opensquirrel-0.1.0/opensquirrel/instruction_library.py +0 -55
  51. opensquirrel-0.1.0/opensquirrel/mapper/__init__.py +0 -4
  52. opensquirrel-0.1.0/opensquirrel/merger/general_merger.py +0 -126
  53. opensquirrel-0.1.0/opensquirrel/parser/libqasm/parser.py +0 -191
  54. opensquirrel-0.1.0/opensquirrel/reindexer/__init__.py +0 -3
  55. opensquirrel-0.1.0/opensquirrel/utils/__init__.py +0 -3
  56. opensquirrel-0.1.0/opensquirrel/utils/check_passes/__init__.py +0 -5
  57. opensquirrel-0.1.0/opensquirrel/writer/__init__.py +0 -0
  58. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/LICENSE.md +0 -0
  59. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/exceptions.py +0 -0
  60. {opensquirrel-0.1.0/opensquirrel/exporter → opensquirrel-0.2.0/opensquirrel/parser}/__init__.py +0 -0
  61. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/parser/libqasm/README.md +0 -0
  62. {opensquirrel-0.1.0/opensquirrel/merger → opensquirrel-0.2.0/opensquirrel/parser/libqasm}/__init__.py +0 -0
  63. {opensquirrel-0.1.0/opensquirrel/parser → opensquirrel-0.2.0/opensquirrel/passes}/__init__.py +0 -0
  64. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/mapper/mapping.py +0 -0
  65. {opensquirrel-0.1.0/opensquirrel → opensquirrel-0.2.0/opensquirrel/passes}/mapper/utils.py +0 -0
  66. {opensquirrel-0.1.0 → opensquirrel-0.2.0}/opensquirrel/utils/identity_filter.py +0 -0
  67. {opensquirrel-0.1.0/opensquirrel/parser/libqasm → opensquirrel-0.2.0/opensquirrel/writer}/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: opensquirrel
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: A quantum circuit transformation and manipulation tool
5
5
  Home-page: https://github.com/QuTech-Delft/OpenSquirrel
6
6
  License: Apache-2.0
@@ -15,10 +15,10 @@ 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 (==0.6.7)
18
+ Requires-Dist: libqasm (==0.6.9)
19
19
  Requires-Dist: networkx (>=3.0.0,<4.0.0)
20
- Requires-Dist: numpy (==1.24) ; python_version < "3.9"
21
- Requires-Dist: numpy (>=1.26) ; python_version >= "3.9" and python_version < "4.0"
20
+ Requires-Dist: numpy (>=1.26,<2.0) ; python_version < "3.10"
21
+ Requires-Dist: numpy (>=2.0,<3.0) ; python_version >= "3.10" and python_version < "4.0"
22
22
  Project-URL: Repository, https://github.com/QuTech-Delft/OpenSquirrel
23
23
  Description-Content-Type: text/markdown
24
24
 
@@ -87,11 +87,11 @@ Here is an example of building a circuit using the `CircuitBuilder`:
87
87
  ```python
88
88
  import math
89
89
  from opensquirrel.circuit_builder import CircuitBuilder
90
- from opensquirrel.ir import Qubit, Float
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), Float(math.pi / 3))
94
+ builder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), math.pi / 3)
95
95
 
96
96
  # Get the circuit from the circuit builder
97
97
  qc = builder.to_circuit()
@@ -121,7 +121,7 @@ The different decomposition strategies can be found in the
121
121
  In the example below, the circuit is decomposed using the Z-Y-Z decomposer.
122
122
 
123
123
  ```python
124
- from opensquirrel.decomposer.aba_decomposer import ZYZDecomposer
124
+ from opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer
125
125
 
126
126
  qc.decompose(decomposer=ZYZDecomposer())
127
127
  ```
@@ -63,11 +63,11 @@ Here is an example of building a circuit using the `CircuitBuilder`:
63
63
  ```python
64
64
  import math
65
65
  from opensquirrel.circuit_builder import CircuitBuilder
66
- from opensquirrel.ir import Qubit, Float
66
+ from opensquirrel.ir import Qubit
67
67
 
68
68
  # Initialize the builder and build your circuit
69
69
  builder = CircuitBuilder(qubit_register_size=1)
70
- builder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), Float(math.pi / 3))
70
+ builder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), math.pi / 3)
71
71
 
72
72
  # Get the circuit from the circuit builder
73
73
  qc = builder.to_circuit()
@@ -97,7 +97,7 @@ The different decomposition strategies can be found in the
97
97
  In the example below, the circuit is decomposed using the Z-Y-Z decomposer.
98
98
 
99
99
  ```python
100
- from opensquirrel.decomposer.aba_decomposer import ZYZDecomposer
100
+ from opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer
101
101
 
102
102
  qc.decompose(decomposer=ZYZDecomposer())
103
103
  ```
@@ -0,0 +1,63 @@
1
+ from opensquirrel.circuit import Circuit
2
+ from opensquirrel.circuit_builder import CircuitBuilder
3
+ from opensquirrel.default_instructions import (
4
+ CNOT,
5
+ CR,
6
+ CZ,
7
+ SWAP,
8
+ X90,
9
+ Y90,
10
+ CRk,
11
+ H,
12
+ I,
13
+ Rx,
14
+ Ry,
15
+ Rz,
16
+ S,
17
+ Sdag,
18
+ T,
19
+ Tdag,
20
+ X,
21
+ Y,
22
+ Z,
23
+ barrier,
24
+ init,
25
+ measure,
26
+ mX90,
27
+ mY90,
28
+ reset,
29
+ wait,
30
+ )
31
+ from opensquirrel.writer import writer
32
+
33
+ __all__ = [
34
+ "CNOT",
35
+ "CR",
36
+ "CZ",
37
+ "SWAP",
38
+ "X90",
39
+ "Y90",
40
+ "CRk",
41
+ "Circuit",
42
+ "CircuitBuilder",
43
+ "H",
44
+ "I",
45
+ "Rx",
46
+ "Ry",
47
+ "Rz",
48
+ "S",
49
+ "Sdag",
50
+ "T",
51
+ "Tdag",
52
+ "X",
53
+ "Y",
54
+ "Z",
55
+ "barrier",
56
+ "init",
57
+ "mX90",
58
+ "mY90",
59
+ "measure",
60
+ "reset",
61
+ "wait",
62
+ "writer",
63
+ ]
@@ -1,16 +1,16 @@
1
1
  from __future__ import annotations
2
2
 
3
- from collections.abc import Callable, Mapping
4
- from typing import TYPE_CHECKING, Any, Literal
3
+ from collections.abc import Callable
4
+ from typing import TYPE_CHECKING, Any
5
5
 
6
- from opensquirrel.default_gates import default_gate_aliases, default_gate_set
7
- from opensquirrel.default_measures import default_measure_set
8
- from opensquirrel.exporter.export_format import ExportFormat
6
+ from opensquirrel.passes.exporter import ExportFormat
9
7
 
10
8
  if TYPE_CHECKING:
11
- from opensquirrel.decomposer import Decomposer
12
- from opensquirrel.ir import IR, Gate, Measure
13
- from opensquirrel.mapper import Mapper
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.general_merger import Merger
13
+ from opensquirrel.passes.router.general_router import Router
14
14
  from opensquirrel.register_manager import RegisterManager
15
15
 
16
16
 
@@ -45,7 +45,7 @@ class Circuit:
45
45
 
46
46
  def __repr__(self) -> str:
47
47
  """Write the circuit to a cQASM 3 string."""
48
- from opensquirrel.writer import writer
48
+ from opensquirrel import writer
49
49
 
50
50
  return writer.circuit_to_string(self)
51
51
 
@@ -55,13 +55,7 @@ class Circuit:
55
55
  return self.register_manager == other.register_manager and self.ir == other.ir
56
56
 
57
57
  @classmethod
58
- def from_string(
59
- cls,
60
- cqasm3_string: str,
61
- gate_set: list[Callable[..., Gate]] = default_gate_set,
62
- gate_aliases: Mapping[str, Callable[..., Gate]] = default_gate_aliases,
63
- measure_set: list[Callable[..., Measure]] = default_measure_set,
64
- ) -> Circuit:
58
+ def from_string(cls, cqasm3_string: str) -> Circuit:
65
59
  """Create a circuit object from a cQasm3 string. All the gates in the circuit need to be defined in
66
60
  the `gates` argument.
67
61
 
@@ -72,18 +66,10 @@ class Circuit:
72
66
 
73
67
  Args:
74
68
  cqasm3_string: a cQASM 3 string
75
- gate_set: an array of gate semantic functions. See default_gates for examples
76
- gate_aliases: a dictionary of extra gate aliases, mapping strings to functions in the gate set
77
- measure_set: an array of measurement semantic functions. See default_measures for examples
78
69
  """
79
70
  from opensquirrel.parser.libqasm.parser import Parser
80
71
 
81
- parser = Parser(
82
- gate_set=gate_set,
83
- gate_aliases=gate_aliases,
84
- measure_set=measure_set,
85
- )
86
- return parser.circuit_from_string(cqasm3_string)
72
+ return Parser().circuit_from_string(cqasm3_string)
87
73
 
88
74
  @property
89
75
  def qubit_register_size(self) -> int:
@@ -101,19 +87,19 @@ class Circuit:
101
87
  def bit_register_name(self) -> str:
102
88
  return self.register_manager.get_bit_register_name()
103
89
 
104
- def merge_single_qubit_gates(self) -> None:
105
- """Merge all consecutive 1-qubit gates in the circuit.
106
- Gates obtained from merging other gates become anonymous gates.
107
- """
108
- from opensquirrel.merger import general_merger
90
+ def route(self, router: Router) -> None:
91
+ """Generic router pass. It applies the given Router to the circuit."""
92
+ router.route(self.ir)
109
93
 
110
- general_merger.merge_single_qubit_gates(self)
94
+ def merge(self, merger: Merger) -> None:
95
+ """Generic merge pass. It applies the given merger to the circuit."""
96
+ merger.merge(self.ir, self.qubit_register_size)
111
97
 
112
98
  def decompose(self, decomposer: Decomposer) -> None:
113
99
  """Generic decomposition pass.
114
100
  It applies the given decomposer function to every gate in the circuit.
115
101
  """
116
- from opensquirrel.decomposer import general_decomposer
102
+ from opensquirrel.passes.decomposer import general_decomposer
117
103
 
118
104
  general_decomposer.decompose(self.ir, decomposer)
119
105
 
@@ -121,7 +107,7 @@ class Circuit:
121
107
  """Generic qubit mapper pass.
122
108
  Map the (virtual) qubits of the circuit to the physical qubits of the target hardware.
123
109
  """
124
- from opensquirrel.mapper.qubit_remapper import remap_ir
110
+ from opensquirrel.passes.mapper.qubit_remapper import remap_ir
125
111
 
126
112
  remap_ir(self, mapper.get_mapping())
127
113
 
@@ -130,14 +116,18 @@ class Circuit:
130
116
  `f` is a callable that takes the arguments of the gate that is to be replaced and
131
117
  returns the decomposition as a list of gates.
132
118
  """
133
- from opensquirrel.decomposer import general_decomposer
119
+ from opensquirrel.passes.decomposer import general_decomposer
134
120
 
135
121
  general_decomposer.replace(self.ir, gate_generator, f)
136
122
 
137
- def export(self, fmt: Literal[ExportFormat.QUANTIFY_SCHEDULER] | None = None) -> Any:
123
+ def export(self, fmt: ExportFormat | None = None) -> Any:
138
124
  if fmt == ExportFormat.QUANTIFY_SCHEDULER:
139
- from opensquirrel.exporter import quantify_scheduler_exporter
125
+ from opensquirrel.passes.exporter import quantify_scheduler_exporter
140
126
 
141
127
  return quantify_scheduler_exporter.export(self)
128
+ if fmt == ExportFormat.CQASM_V1:
129
+ from opensquirrel.passes.exporter import cqasmv1_exporter
130
+
131
+ return cqasmv1_exporter.export(self)
142
132
  msg = "unknown exporter format"
143
133
  raise ValueError(msg)
@@ -1,23 +1,30 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import inspect
4
- from collections.abc import Callable, Mapping
4
+ from collections.abc import Callable
5
5
  from copy import deepcopy
6
6
  from functools import partial
7
7
  from typing import Any
8
8
 
9
9
  from typing_extensions import Self
10
10
 
11
+ from opensquirrel import instruction_library
11
12
  from opensquirrel.circuit import Circuit
12
- from opensquirrel.default_gates import default_gate_aliases, default_gate_set
13
- from opensquirrel.default_measures import default_measure_set
14
- from opensquirrel.default_resets import default_reset_set
15
- from opensquirrel.instruction_library import GateLibrary, MeasureLibrary, ResetLibrary
16
- from opensquirrel.ir import ANNOTATIONS_TO_TYPE_MAP, IR, Comment, Gate, Measure, Qubit, QubitLike, Reset
13
+ from opensquirrel.ir import (
14
+ ANNOTATIONS_TO_TYPE_MAP,
15
+ IR,
16
+ Bit,
17
+ BitLike,
18
+ Instruction,
19
+ Qubit,
20
+ QubitLike,
21
+ is_bit_like_annotation,
22
+ is_qubit_like_annotation,
23
+ )
17
24
  from opensquirrel.register_manager import BitRegister, QubitRegister, RegisterManager
18
25
 
19
26
 
20
- class CircuitBuilder(GateLibrary, MeasureLibrary, ResetLibrary):
27
+ class CircuitBuilder:
21
28
  """
22
29
  A class using the builder pattern to make construction of circuits easy from Python.
23
30
  Adds corresponding instruction when a method is called. Checks that instructions are known and called with the right
@@ -27,9 +34,6 @@ class CircuitBuilder(GateLibrary, MeasureLibrary, ResetLibrary):
27
34
  Args:
28
35
  qubit_register_size (int): Size of the qubit register
29
36
  bit_register_size (int): Size of the bit register
30
- gate_set (list): Supported gates
31
- gate_aliases (dict): Supported gate aliases
32
- measure_set (list): Supported measure instructions
33
37
 
34
38
  Example:
35
39
  >>> CircuitBuilder(qubit_register_size=3, bit_register_size=3).\
@@ -43,47 +47,27 @@ class CircuitBuilder(GateLibrary, MeasureLibrary, ResetLibrary):
43
47
  cnot q[0], q[1]
44
48
  cnot q[0], q[2]
45
49
  <BLANKLINE>
46
-
47
50
  """
48
51
 
49
- def __init__(
50
- self,
51
- qubit_register_size: int,
52
- bit_register_size: int = 0,
53
- gate_set: list[Callable[..., Gate]] = default_gate_set,
54
- gate_aliases: Mapping[str, Callable[..., Gate]] = default_gate_aliases,
55
- measure_set: list[Callable[..., Measure]] = default_measure_set,
56
- reset_set: list[Callable[..., Reset]] = default_reset_set,
57
- ) -> None:
58
- GateLibrary.__init__(self, gate_set, gate_aliases)
59
- MeasureLibrary.__init__(self, measure_set)
60
- ResetLibrary.__init__(self, reset_set)
52
+ def __init__(self, qubit_register_size: int, bit_register_size: int = 0) -> None:
61
53
  self.register_manager = RegisterManager(QubitRegister(qubit_register_size), BitRegister(bit_register_size))
62
54
  self.ir = IR()
63
55
 
64
56
  def __getattr__(self, attr: Any) -> Callable[..., Self]:
65
- if attr == "comment":
66
- return self._add_comment
67
-
68
57
  return partial(self._add_instruction, attr)
69
58
 
70
- def _add_comment(self, comment_string: str) -> Self:
71
- self.ir.add_comment(Comment(comment_string))
72
- return self
73
-
74
59
  def _add_instruction(self, attr: str, *args: Any) -> Self:
75
- if any(attr == measure.__name__ for measure in self.measure_set):
76
- generator_f_measure = MeasureLibrary.get_measure_f(self, attr)
77
- self._check_generator_f_args(generator_f_measure, attr, args)
78
- self.ir.add_measure(generator_f_measure(*args))
79
- elif any(attr == reset.__name__ for reset in self.reset_set):
80
- generator_f_reset = ResetLibrary.get_reset_f(self, attr)
81
- self._check_generator_f_args(generator_f_reset, attr, args)
82
- self.ir.add_reset(generator_f_reset(*args))
83
- else:
84
- generator_f_gate = GateLibrary.get_gate_f(self, attr)
60
+ if attr in instruction_library.gate_set:
61
+ generator_f_gate = instruction_library.get_gate_f(attr)
85
62
  self._check_generator_f_args(generator_f_gate, attr, args)
86
63
  self.ir.add_gate(generator_f_gate(*args))
64
+ elif attr in instruction_library.non_unitary_set:
65
+ generator_f_non_unitary = instruction_library.get_non_unitary_f(attr)
66
+ self._check_generator_f_args(generator_f_non_unitary, attr, args)
67
+ self.ir.add_non_unitary(generator_f_non_unitary(*args))
68
+ else:
69
+ msg = f"unknown instruction '{attr}'"
70
+ raise ValueError(msg)
87
71
  return self
88
72
 
89
73
  def _check_qubit_out_of_bounds_access(self, qubit: QubitLike) -> None:
@@ -97,30 +81,31 @@ class CircuitBuilder(GateLibrary, MeasureLibrary, ResetLibrary):
97
81
  msg = "qubit index is out of bounds"
98
82
  raise IndexError(msg)
99
83
 
100
- def _check_bit_out_of_bounds_access(self, index: int) -> None:
101
- """Throw error if bit index is outside the qubit register range.
84
+ def _check_bit_out_of_bounds_access(self, bit: BitLike) -> None:
85
+ """Throw error if bit index is outside the bit register range.
102
86
 
103
87
  Args:
104
- index: bit index
88
+ bit: bit to check.
105
89
  """
90
+ index = Bit(bit).index
106
91
  if index >= self.register_manager.get_bit_register_size():
107
92
  msg = "bit index is out of bounds"
108
93
  raise IndexError(msg)
109
94
 
110
95
  def _check_generator_f_args(
111
96
  self,
112
- generator_f: Callable[..., Gate | Measure | Reset],
97
+ generator_f: Callable[..., Instruction],
113
98
  attr: str,
114
99
  args: tuple[Any, ...],
115
100
  ) -> None:
116
- """General instruction validation function. The function checks if each instruction has the proper arguments
101
+ """General instruction validation function.
102
+ The function checks if each instruction has the proper arguments
117
103
  and if the qubit and bits are within the register range.
118
104
 
119
105
  Args:
120
106
  generator_f: Instruction function
121
107
  attr: Type of instruction
122
108
  args: Arguments parsed into the function
123
-
124
109
  """
125
110
  for i, par in enumerate(inspect.signature(generator_f).parameters.values()):
126
111
  try:
@@ -131,20 +116,20 @@ class CircuitBuilder(GateLibrary, MeasureLibrary, ResetLibrary):
131
116
  msg = "unknown annotation type"
132
117
  raise TypeError(msg) from e
133
118
 
134
- # fix for python39
119
+ # Fix for Python 3.9
135
120
  try:
136
- is_incorrect_type = not isinstance(args[i], expected_type)
121
+ is_incorrect_type = not isinstance(args[i], expected_type) # type: ignore
137
122
  except TypeError:
138
- # expected type is probably a Union, which works differently in python39
139
- is_incorrect_type = not isinstance(args[i], expected_type.__args__)
123
+ # Expected type is probably a Union, which works differently in Python 3.9
124
+ is_incorrect_type = not isinstance(args[i], expected_type.__args__) # type: ignore
140
125
 
141
126
  if is_incorrect_type:
142
127
  msg = f"wrong argument type for instruction `{attr}`, got {type(args[i])} but expected {expected_type}"
143
128
  raise TypeError(msg)
144
- if expected_type in (QubitLike, Qubit):
129
+ if is_qubit_like_annotation(expected_type):
145
130
  self._check_qubit_out_of_bounds_access(args[i])
146
- elif args[i].__class__.__name__ == "Bit":
147
- self._check_bit_out_of_bounds_access(args[i].index)
131
+ elif is_bit_like_annotation(expected_type):
132
+ self._check_bit_out_of_bounds_access(args[i])
148
133
 
149
134
  def to_circuit(self) -> Circuit:
150
135
  return Circuit(deepcopy(self.register_manager), deepcopy(self.ir))
@@ -5,8 +5,8 @@ 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 Comment, Gate, IRVisitor
9
- from opensquirrel.utils.matrix_expander import get_matrix
8
+ from opensquirrel.ir import Gate, IRVisitor
9
+ from opensquirrel.utils import get_matrix
10
10
 
11
11
  if TYPE_CHECKING:
12
12
  from opensquirrel.circuit import Circuit
@@ -19,10 +19,7 @@ class _CircuitMatrixCalculator(IRVisitor):
19
19
 
20
20
  def visit_gate(self, gate: Gate) -> None:
21
21
  big_matrix = get_matrix(gate, qubit_register_size=self.qubit_register_size)
22
- self.matrix = big_matrix @ self.matrix
23
-
24
- def visit_comment(self, comment: Comment) -> None:
25
- pass
22
+ self.matrix = np.asarray(big_matrix @ self.matrix, dtype=np.complex128)
26
23
 
27
24
 
28
25
  def get_circuit_matrix(circuit: Circuit) -> NDArray[np.complex128]:
@@ -1,14 +1,24 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import math
4
+ from typing import SupportsFloat
4
5
 
5
6
  import numpy as np
6
7
  from numpy.typing import NDArray
7
8
 
8
- ATOL = 0.0000001
9
+ ATOL = 0.000_000_1
9
10
 
10
11
 
11
- def normalize_angle(x: float) -> float:
12
+ def normalize_angle(x: SupportsFloat) -> float:
13
+ r"""Normalize the angle to be in between the range of $(-\pi, \pi]$.
14
+
15
+ Args:
16
+ x: value to normalize.
17
+
18
+ Returns:
19
+ The normalized angle.
20
+ """
21
+ x = float(x)
12
22
  t = x - 2 * math.pi * (x // (2 * math.pi) + 1)
13
23
  if t < -math.pi + ATOL:
14
24
  t += 2 * math.pi
@@ -38,4 +48,15 @@ def are_matrices_equivalent_up_to_global_phase(
38
48
 
39
49
  phase_difference = matrix_a[first_non_zero] / matrix_b[first_non_zero]
40
50
 
41
- return np.allclose(matrix_a, phase_difference * matrix_b)
51
+ return np.allclose(matrix_a, phase_difference * matrix_b, atol=ATOL)
52
+
53
+
54
+ def is_identity_matrix_up_to_a_global_phase(matrix: NDArray[np.complex128]) -> bool:
55
+ """Checks whether matrix is an identity matrix up to a global phase.
56
+
57
+ Args:
58
+ matrix: matrix to check.
59
+ Returns:
60
+ Whether matrix is an identity matrix up to a global phase.
61
+ """
62
+ return are_matrices_equivalent_up_to_global_phase(matrix, np.eye(matrix.shape[0], dtype=np.complex128))
@@ -0,0 +1,42 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Callable
4
+ from typing import Any, SupportsFloat
5
+
6
+ from opensquirrel.ir import BlochSphereRotation, ControlledGate, QubitLike
7
+
8
+
9
+ class GateModifier:
10
+ pass
11
+
12
+
13
+ class InverseGateModifier(GateModifier):
14
+ def __init__(self, generator_f_gate: Callable[..., BlochSphereRotation]) -> None:
15
+ self.generator_f_gate = generator_f_gate
16
+
17
+ def __call__(self, *args: Any) -> BlochSphereRotation:
18
+ gate: BlochSphereRotation = self.generator_f_gate(*args)
19
+ modified_angle = gate.angle * -1
20
+ modified_phase = gate.phase * -1
21
+ return BlochSphereRotation(qubit=gate.qubit, axis=gate.axis, angle=modified_angle, phase=modified_phase)
22
+
23
+
24
+ class PowerGateModifier(GateModifier):
25
+ def __init__(self, exponent: SupportsFloat, generator_f_gate: Callable[..., BlochSphereRotation]) -> None:
26
+ self.exponent = exponent
27
+ self.generator_f_gate = generator_f_gate
28
+
29
+ def __call__(self, *args: Any) -> BlochSphereRotation:
30
+ gate: BlochSphereRotation = self.generator_f_gate(*args)
31
+ modified_angle = gate.angle * float(self.exponent)
32
+ modified_phase = gate.phase * float(self.exponent)
33
+ return BlochSphereRotation(qubit=gate.qubit, axis=gate.axis, angle=modified_angle, phase=modified_phase)
34
+
35
+
36
+ class ControlGateModifier(GateModifier):
37
+ def __init__(self, generator_f_gate: Callable[..., BlochSphereRotation]) -> None:
38
+ self.generator_f_gate = generator_f_gate
39
+
40
+ def __call__(self, control: QubitLike, *args: Any) -> ControlledGate:
41
+ gate: BlochSphereRotation = self.generator_f_gate(*args)
42
+ return ControlledGate(control, gate)