opensquirrel 0.2.0__tar.gz → 0.3.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 (56) hide show
  1. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/PKG-INFO +3 -3
  2. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/circuit.py +8 -3
  3. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/ir.py +9 -5
  4. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/parser/libqasm/parser.py +1 -1
  5. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/aba_decomposer.py +2 -2
  6. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/cnot_decomposer.py +1 -1
  7. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/exporter/cqasmv1_exporter.py +1 -1
  8. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/merger/general_merger.py +13 -5
  9. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/merger/single_qubit_gates_merger.py +2 -2
  10. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/router/routing_checker.py +2 -2
  11. opensquirrel-0.3.1/opensquirrel/passes/validator/__init__.py +6 -0
  12. opensquirrel-0.3.1/opensquirrel/passes/validator/general_validator.py +10 -0
  13. opensquirrel-0.3.1/opensquirrel/passes/validator/native_gate_validator.py +26 -0
  14. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/writer/writer.py +1 -1
  15. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/pyproject.toml +4 -8
  16. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/LICENSE.md +0 -0
  17. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/README.md +0 -0
  18. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/__init__.py +0 -0
  19. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/circuit_builder.py +0 -0
  20. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/circuit_matrix_calculator.py +0 -0
  21. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/common.py +0 -0
  22. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/default_gate_modifiers.py +0 -0
  23. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/default_instructions.py +0 -0
  24. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/exceptions.py +0 -0
  25. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/instruction_library.py +0 -0
  26. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/parser/__init__.py +0 -0
  27. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/parser/libqasm/README.md +0 -0
  28. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/parser/libqasm/__init__.py +0 -0
  29. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/__init__.py +0 -0
  30. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/__init__.py +0 -0
  31. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/cnot2cz_decomposer.py +0 -0
  32. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/general_decomposer.py +0 -0
  33. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/mckay_decomposer.py +0 -0
  34. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/decomposer/swap2cnot_decomposer.py +0 -0
  35. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/exporter/__init__.py +0 -0
  36. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/exporter/export_format.py +0 -0
  37. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/exporter/quantify_scheduler_exporter.py +0 -0
  38. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/mapper/__init__.py +0 -0
  39. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/mapper/general_mapper.py +0 -0
  40. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/mapper/mapping.py +0 -0
  41. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/mapper/qubit_remapper.py +0 -0
  42. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/mapper/simple_mappers.py +0 -0
  43. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/mapper/utils.py +0 -0
  44. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/merger/__init__.py +0 -0
  45. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/router/__init__.py +0 -0
  46. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/passes/router/general_router.py +0 -0
  47. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/register_manager.py +0 -0
  48. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/reindexer/__init__.py +0 -0
  49. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/reindexer/qubit_reindexer.py +0 -0
  50. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/utils/__init__.py +0 -0
  51. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/utils/check_mapper.py +0 -0
  52. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/utils/identity_filter.py +0 -0
  53. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/utils/list.py +0 -0
  54. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/utils/math.py +0 -0
  55. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/utils/matrix_expander.py +0 -0
  56. {opensquirrel-0.2.0 → opensquirrel-0.3.1}/opensquirrel/writer/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: opensquirrel
3
- Version: 0.2.0
3
+ Version: 0.3.1
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
@@ -17,8 +17,8 @@ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
18
  Requires-Dist: libqasm (==0.6.9)
19
19
  Requires-Dist: networkx (>=3.0.0,<4.0.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"
20
+ Requires-Dist: numpy (>=1.26)
21
+ Requires-Dist: tox (>=4.24.1,<5.0.0)
22
22
  Project-URL: Repository, https://github.com/QuTech-Delft/OpenSquirrel
23
23
  Description-Content-Type: text/markdown
24
24
 
@@ -9,8 +9,9 @@ if TYPE_CHECKING:
9
9
  from opensquirrel.ir import IR, Gate
10
10
  from opensquirrel.passes.decomposer import Decomposer
11
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
12
+ from opensquirrel.passes.merger import Merger
13
+ from opensquirrel.passes.router import Router
14
+ from opensquirrel.passes.validator import Validator
14
15
  from opensquirrel.register_manager import RegisterManager
15
16
 
16
17
 
@@ -87,8 +88,12 @@ class Circuit:
87
88
  def bit_register_name(self) -> str:
88
89
  return self.register_manager.get_bit_register_name()
89
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
+
90
95
  def route(self, router: Router) -> None:
91
- """Generic router pass. It applies the given Router to the circuit."""
96
+ """Generic router pass. It applies the given router to the circuit."""
92
97
  router.route(self.ir)
93
98
 
94
99
  def merge(self, merger: Merger) -> None:
@@ -213,7 +213,7 @@ class Axis(Sequence[np.float64], Expression):
213
213
 
214
214
  axis: An ``AxisLike`` to create the axis from.
215
215
  """
216
- axis_to_parse = axis[0] if len(axis) == 1 else cast(AxisLike, axis)
216
+ axis_to_parse = axis[0] if len(axis) == 1 else cast("AxisLike", axis)
217
217
  self._value = self.normalize(self.parse(axis_to_parse))
218
218
 
219
219
  @property
@@ -281,7 +281,7 @@ class Axis(Sequence[np.float64], Expression):
281
281
 
282
282
  def __getitem__(self, index: int | slice, /) -> np.float64 | list[np.float64]:
283
283
  """Get the item at `index`."""
284
- return cast(np.float64, self.value[index])
284
+ return cast("np.float64", self.value[index])
285
285
 
286
286
  def __len__(self) -> int:
287
287
  """Length of the axis, which is always 3."""
@@ -519,9 +519,13 @@ class Gate(Unitary, ABC):
519
519
  return self.generator.__name__
520
520
  return "Anonymous gate: " + self.__repr__()
521
521
 
522
+ @property
523
+ def is_named_gate(self) -> bool:
524
+ return not (self.generator is None or self.generator.__name__ is None)
525
+
522
526
  @property
523
527
  def is_anonymous(self) -> bool:
524
- return self.generator is None
528
+ return not self.is_named_gate
525
529
 
526
530
  @staticmethod
527
531
  def _check_repeated_qubit_operands(qubits: Sequence[Qubit]) -> bool:
@@ -706,7 +710,7 @@ def named_gate(gate_generator: Callable[..., ControlledGate]) -> Callable[..., C
706
710
 
707
711
 
708
712
  def named_gate(gate_generator: Callable[..., Gate]) -> Callable[..., Gate]:
709
- return cast(Callable[..., Gate], instruction_decorator(gate_generator))
713
+ return cast("Callable[..., Gate]", instruction_decorator(gate_generator))
710
714
 
711
715
 
712
716
  @overload
@@ -730,7 +734,7 @@ def non_unitary(non_unitary_generator: Callable[..., Wait]) -> Callable[..., Wai
730
734
 
731
735
 
732
736
  def non_unitary(non_unitary_generator: Callable[..., NonUnitary]) -> Callable[..., NonUnitary]:
733
- return cast(Callable[..., NonUnitary], instruction_decorator(non_unitary_generator))
737
+ return cast("Callable[..., NonUnitary]", instruction_decorator(non_unitary_generator))
734
738
 
735
739
 
736
740
  def compare_gates(g1: Gate, g2: Gate) -> bool:
@@ -174,7 +174,7 @@ class Parser:
174
174
  def _get_gate_f(instruction: cqasm.semantic.GateInstruction) -> Callable[..., Gate]:
175
175
  gate_name = instruction.gate.name
176
176
  if gate_name in ["inv", "pow", "ctrl"]:
177
- modified_gate_f = cast(Callable[..., BlochSphereRotation], Parser._get_gate_f(instruction.gate))
177
+ modified_gate_f = cast("Callable[..., BlochSphereRotation]", Parser._get_gate_f(instruction.gate))
178
178
  if gate_name == "inv":
179
179
  return InverseGateModifier(modified_gate_f)
180
180
  if gate_name == "pow":
@@ -52,8 +52,8 @@ class ABADecomposer(Decomposer, ABC):
52
52
  Returns:
53
53
  A triplet (a, b, c) where a, b, and c are the values of x, y, and z reordered.
54
54
  """
55
- _axis = Axis(axis)
56
- return _axis[self.index_a], _axis[self.index_b], _axis[self._find_unused_index()]
55
+ axis_ = Axis(axis)
56
+ return axis_[self.index_a], axis_[self.index_b], axis_[self._find_unused_index()]
57
57
 
58
58
  @staticmethod
59
59
  def _are_b_and_c_axes_in_negative_octant(b_axis_value: float, c_axis_value: float) -> bool:
@@ -38,7 +38,7 @@ class CNOTDecomposer(Decomposer):
38
38
  # See https://threeplusone.com/pubs/on_gates.pdf
39
39
 
40
40
  # Try special case first, see https://arxiv.org/pdf/quant-ph/9503016.pdf lemma 5.5
41
- controlled_rotation_times_x = general_merger.compose_bloch_sphere_rotations(X(target_qubit), g.target_gate)
41
+ controlled_rotation_times_x = general_merger.compose_bloch_sphere_rotations(g.target_gate, X(target_qubit))
42
42
  theta0_with_x, theta1_with_x, theta2_with_x = ZYZDecomposer().get_decomposition_angles(
43
43
  controlled_rotation_times_x.axis,
44
44
  controlled_rotation_times_x.angle,
@@ -91,7 +91,7 @@ def _dump_barrier_group(indices: list[int]) -> str:
91
91
 
92
92
 
93
93
  def _get_barrier_index(line: str) -> int:
94
- barrier_index_match = re.search("\d+", line)
94
+ barrier_index_match = re.search(r"\d+", line)
95
95
  if not barrier_index_match:
96
96
  msg = "expecting a barrier index but found none"
97
97
  raise CqasmV1ExporterParseError(msg)
@@ -15,8 +15,16 @@ from opensquirrel.utils import acos, flatten_list
15
15
 
16
16
  def compose_bloch_sphere_rotations(bsr_a: BlochSphereRotation, bsr_b: BlochSphereRotation) -> BlochSphereRotation:
17
17
  """Computes the Bloch sphere rotation resulting from the composition of two Bloch sphere rotations.
18
- The first rotation is applied and then the second.
19
- If the final Bloch sphere rotation is anonymous, we will try to match it to a default gate.
18
+ The first rotation (A) is applied and then the second (B):
19
+
20
+ As separate gates:
21
+ A q
22
+ B q
23
+
24
+ A linear operations:
25
+ (B * A) q
26
+
27
+ If the final Bloch sphere rotation is anonymous, we try to match it to a default gate.
20
28
 
21
29
  Uses Rodrigues' rotation formula (see https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula).
22
30
  """
@@ -40,7 +48,7 @@ def compose_bloch_sphere_rotations(bsr_a: BlochSphereRotation, bsr_b: BlochSpher
40
48
  * (
41
49
  sin(bsr_a.angle / 2) * cos(bsr_b.angle / 2) * bsr_a.axis.value
42
50
  + cos(bsr_a.angle / 2) * sin(bsr_b.angle / 2) * bsr_b.axis.value
43
- + sin(bsr_a.angle / 2) * sin(bsr_b.angle / 2) * np.cross(bsr_a.axis, bsr_b.axis)
51
+ + sin(bsr_a.angle / 2) * sin(bsr_b.angle / 2) * np.cross(bsr_b.axis, bsr_a.axis)
44
52
  )
45
53
  ),
46
54
  order_of_magnitude,
@@ -119,8 +127,8 @@ def can_move_before(statement: Statement, statement_group: list[Statement]) -> b
119
127
  first_statement_from_group = statement_group[0]
120
128
  if not isinstance(first_statement_from_group, Barrier):
121
129
  return False
122
- instruction = cast(Instruction, statement)
123
- return can_move_statement_before_barrier(instruction, cast(list[Instruction], statement_group))
130
+ instruction = cast("Instruction", statement)
131
+ return can_move_statement_before_barrier(instruction, cast("list[Instruction]", statement_group))
124
132
 
125
133
 
126
134
  def group_linked_barriers(statements: list[Statement]) -> list[list[Statement]]:
@@ -22,10 +22,10 @@ class SingleQubitGatesMerger(Merger):
22
22
  statement = ir.statements[statement_index]
23
23
 
24
24
  # Accumulate consecutive Bloch sphere rotations
25
- instruction: Instruction = cast(Instruction, statement)
25
+ instruction: Instruction = cast("Instruction", statement)
26
26
  if isinstance(instruction, BlochSphereRotation):
27
27
  already_accumulated = accumulators_per_qubit[instruction.qubit]
28
- composed = compose_bloch_sphere_rotations(instruction, already_accumulated)
28
+ composed = compose_bloch_sphere_rotations(already_accumulated, instruction)
29
29
  accumulators_per_qubit[instruction.qubit] = composed
30
30
  del ir.statements[statement_index]
31
31
  continue
@@ -11,7 +11,7 @@ class RoutingChecker(Router):
11
11
  def route(self, ir: IR) -> None:
12
12
  non_executable_interactions = []
13
13
  for statement in ir.statements:
14
- instruction: Instruction = cast(Instruction, statement)
14
+ instruction: Instruction = cast("Instruction", statement)
15
15
  args = instruction.arguments
16
16
  if args and len(args) > 1 and all(isinstance(arg, Qubit) for arg in args):
17
17
  qubit_args = [arg for arg in args if isinstance(arg, Qubit)]
@@ -22,7 +22,7 @@ class RoutingChecker(Router):
22
22
 
23
23
  if non_executable_interactions:
24
24
  error_message = (
25
- f"The following qubit interactions in the circuit prevent a 1-to-1 mapping:"
25
+ f"the following qubit interactions in the circuit prevent a 1-to-1 mapping:"
26
26
  f"{set(non_executable_interactions)}"
27
27
  )
28
28
  raise ValueError(error_message)
@@ -0,0 +1,6 @@
1
+ """Init file for the validator passes."""
2
+
3
+ from opensquirrel.passes.validator.general_validator import Validator
4
+ from opensquirrel.passes.validator.native_gate_validator import NativeGateValidator
5
+
6
+ __all__ = ["NativeGateValidator", "Validator"]
@@ -0,0 +1,10 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from opensquirrel.ir import IR
4
+
5
+
6
+ class Validator(ABC):
7
+ @abstractmethod
8
+ def validate(self, ir: IR) -> None:
9
+ """Base validate method to be implemented by inheriting validator classes."""
10
+ raise NotImplementedError
@@ -0,0 +1,26 @@
1
+ from opensquirrel.ir import IR, Unitary
2
+ from opensquirrel.passes.validator import Validator
3
+
4
+
5
+ class NativeGateValidator(Validator):
6
+ def __init__(self, native_gate_set: list[str]) -> None:
7
+ self.native_gate_set = native_gate_set
8
+
9
+ def validate(self, ir: IR) -> None:
10
+ """
11
+ Check if all unitary gates in the circuit are part of the native gate set.
12
+
13
+ Args:
14
+ ir (IR): The intermediate representation of the circuit to be checked.
15
+
16
+ Raises:
17
+ ValueError: If any unitary gate in the circuit is not part of the native gate set.
18
+ """
19
+ gates_not_in_native_gate_set = [
20
+ statement.name
21
+ for statement in ir.statements
22
+ if isinstance(statement, Unitary) and statement.name not in self.native_gate_set
23
+ ]
24
+ if gates_not_in_native_gate_set:
25
+ error_message = f"the following gates are not in the native gate set: {set(gates_not_in_native_gate_set)}"
26
+ raise ValueError(error_message)
@@ -66,7 +66,7 @@ class _WriterImpl(IRVisitor):
66
66
  gate_generator = []
67
67
  if gate.generator is not None:
68
68
  gate_generator = list(inspect.signature(gate.generator).parameters.keys())
69
- qubit_function_keys = ["target", "control", "qubit"]
69
+ qubit_function_keys = ["target", "control", "qubit", "qubit0", "qubit1"]
70
70
  if gate.is_anonymous:
71
71
  if "MatrixGate" in gate_name:
72
72
  # In the case of a MatrixGate the newlines should be removed from the array
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "opensquirrel"
3
- version = "0.2.0"
3
+ version = "0.3.1"
4
4
  description = "A quantum circuit transformation and manipulation tool"
5
5
  authors = [
6
6
  "Quantum Inspire <support@quantum-inspire.com>"
@@ -29,19 +29,17 @@ packages = [
29
29
 
30
30
  [tool.poetry.dependencies]
31
31
  python = "^3.9"
32
- numpy = [
33
- { version = "^1.26", python = "<3.10" },
34
- { version = "^2.0", python = "^3.10" },
35
- ]
32
+ numpy = ">=1.26"
36
33
  libqasm = "0.6.9"
37
34
  networkx = "^3.0.0"
35
+ tox = "^4.24.1"
38
36
 
39
37
  [tool.poetry.group.dev.dependencies]
40
38
  coverage = {extras = ["toml"], version = "^7.3.2"}
41
39
  pytest = {extras = ["toml"], version = ">=7.4.3,<9.0.0"}
42
40
  pytest-cov = ">=4.1,<7.0"
43
41
  mypy = "^1.7.0"
44
- ruff = ">=0.5,<0.7"
42
+ ruff = ">=0.5,<0.10"
45
43
  tox = "^4.16.0"
46
44
  sympy = "^1.13.1"
47
45
  IPython = ">=7.12,<9.0"
@@ -156,8 +154,6 @@ select = [
156
154
  ]
157
155
  ignore = [
158
156
  "A005", # module shadowing a Python builtin module
159
- "ANN101", # self should not be typed
160
- "ANN102", # cls should not be typed
161
157
  "ANN401", # typing.Any can be useful (for example in __eq__)
162
158
  "COM812", # Conflicts with ruff format
163
159
  "ISC001", # Possible conflicts with ruff format
File without changes
File without changes