opensquirrel 0.3.0__tar.gz → 0.4.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.
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/PKG-INFO +2 -2
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/__init__.py +21 -19
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/circuit.py +3 -3
- opensquirrel-0.4.0/opensquirrel/circuit_builder.py +101 -0
- opensquirrel-0.4.0/opensquirrel/circuit_matrix_calculator.py +85 -0
- opensquirrel-0.4.0/opensquirrel/default_gate_modifiers.py +46 -0
- opensquirrel-0.4.0/opensquirrel/default_instructions.py +121 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/exceptions.py +12 -0
- opensquirrel-0.4.0/opensquirrel/ir.py +1059 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/parser/libqasm/parser.py +83 -74
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/__init__.py +4 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/aba_decomposer.py +2 -1
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/cnot2cz_decomposer.py +1 -2
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/cnot_decomposer.py +0 -1
- opensquirrel-0.4.0/opensquirrel/passes/decomposer/cz_decomposer.py +75 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/general_decomposer.py +13 -10
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/swap2cnot_decomposer.py +1 -1
- opensquirrel-0.4.0/opensquirrel/passes/decomposer/swap2cz_decomposer.py +36 -0
- opensquirrel-0.4.0/opensquirrel/passes/exporter/cqasmv1_exporter.py +170 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/exporter/quantify_scheduler_exporter.py +73 -41
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/mapper/__init__.py +2 -2
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/mapper/general_mapper.py +3 -1
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/mapper/qubit_remapper.py +38 -6
- opensquirrel-0.4.0/opensquirrel/passes/mapper/simple_mappers.py +44 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/merger/general_merger.py +8 -47
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/merger/single_qubit_gates_merger.py +2 -2
- opensquirrel-0.4.0/opensquirrel/passes/router/__init__.py +5 -0
- opensquirrel-0.4.0/opensquirrel/passes/router/astar_router.py +58 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/router/general_router.py +4 -1
- opensquirrel-0.4.0/opensquirrel/passes/router/heuristics.py +33 -0
- opensquirrel-0.4.0/opensquirrel/passes/router/shortest_path_router.py +41 -0
- opensquirrel-0.4.0/opensquirrel/passes/validator/__init__.py +7 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/validator/general_validator.py +3 -0
- opensquirrel-0.4.0/opensquirrel/passes/validator/primitive_gate_validator.py +30 -0
- opensquirrel-0.3.0/opensquirrel/passes/router/routing_checker.py → opensquirrel-0.4.0/opensquirrel/passes/validator/routing_validator.py +15 -5
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/reindexer/qubit_reindexer.py +44 -11
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/utils/check_mapper.py +1 -2
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/utils/matrix_expander.py +39 -6
- opensquirrel-0.4.0/opensquirrel/writer/writer.py +166 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/pyproject.toml +5 -4
- opensquirrel-0.3.0/opensquirrel/circuit_builder.py +0 -135
- opensquirrel-0.3.0/opensquirrel/circuit_matrix_calculator.py +0 -38
- opensquirrel-0.3.0/opensquirrel/default_gate_modifiers.py +0 -42
- opensquirrel-0.3.0/opensquirrel/default_instructions.py +0 -243
- opensquirrel-0.3.0/opensquirrel/instruction_library.py +0 -27
- opensquirrel-0.3.0/opensquirrel/ir.py +0 -851
- opensquirrel-0.3.0/opensquirrel/passes/exporter/cqasmv1_exporter.py +0 -107
- opensquirrel-0.3.0/opensquirrel/passes/mapper/simple_mappers.py +0 -20
- opensquirrel-0.3.0/opensquirrel/passes/router/__init__.py +0 -7
- opensquirrel-0.3.0/opensquirrel/passes/validator/__init__.py +0 -6
- opensquirrel-0.3.0/opensquirrel/passes/validator/native_gate_validator.py +0 -26
- opensquirrel-0.3.0/opensquirrel/writer/writer.py +0 -97
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/LICENSE.md +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/README.md +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/common.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/parser/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/parser/libqasm/README.md +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/parser/libqasm/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/decomposer/mckay_decomposer.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/exporter/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/exporter/export_format.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/mapper/mapping.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/mapper/utils.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/passes/merger/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/register_manager.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/reindexer/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/utils/__init__.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/utils/identity_filter.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/utils/list.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/utils/math.py +0 -0
- {opensquirrel-0.3.0 → opensquirrel-0.4.0}/opensquirrel/writer/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: opensquirrel
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.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,7 +15,7 @@ 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 (==
|
|
18
|
+
Requires-Dist: libqasm (==1.2.0)
|
|
19
19
|
Requires-Dist: networkx (>=3.0.0,<4.0.0)
|
|
20
20
|
Requires-Dist: numpy (>=1.26)
|
|
21
21
|
Requires-Dist: tox (>=4.24.1,<5.0.0)
|
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
from opensquirrel.circuit import Circuit
|
|
2
2
|
from opensquirrel.circuit_builder import CircuitBuilder
|
|
3
|
-
from opensquirrel.
|
|
3
|
+
from opensquirrel.ir import (
|
|
4
4
|
CNOT,
|
|
5
5
|
CR,
|
|
6
6
|
CZ,
|
|
7
7
|
SWAP,
|
|
8
8
|
X90,
|
|
9
9
|
Y90,
|
|
10
|
+
Barrier,
|
|
10
11
|
CRk,
|
|
11
12
|
H,
|
|
12
13
|
I,
|
|
14
|
+
Init,
|
|
15
|
+
Measure,
|
|
16
|
+
MinusX90,
|
|
17
|
+
MinusY90,
|
|
18
|
+
Reset,
|
|
19
|
+
Rn,
|
|
13
20
|
Rx,
|
|
14
21
|
Ry,
|
|
15
22
|
Rz,
|
|
16
23
|
S,
|
|
17
|
-
|
|
24
|
+
SDagger,
|
|
18
25
|
T,
|
|
19
|
-
|
|
26
|
+
TDagger,
|
|
27
|
+
Wait,
|
|
20
28
|
X,
|
|
21
29
|
Y,
|
|
22
30
|
Z,
|
|
23
|
-
barrier,
|
|
24
|
-
init,
|
|
25
|
-
measure,
|
|
26
|
-
mX90,
|
|
27
|
-
mY90,
|
|
28
|
-
reset,
|
|
29
|
-
wait,
|
|
30
31
|
)
|
|
31
32
|
from opensquirrel.writer import writer
|
|
32
33
|
|
|
@@ -37,27 +38,28 @@ __all__ = [
|
|
|
37
38
|
"SWAP",
|
|
38
39
|
"X90",
|
|
39
40
|
"Y90",
|
|
41
|
+
"Barrier",
|
|
40
42
|
"CRk",
|
|
41
43
|
"Circuit",
|
|
42
44
|
"CircuitBuilder",
|
|
43
45
|
"H",
|
|
44
46
|
"I",
|
|
47
|
+
"Init",
|
|
48
|
+
"Measure",
|
|
49
|
+
"MinusX90",
|
|
50
|
+
"MinusY90",
|
|
51
|
+
"Reset",
|
|
52
|
+
"Rn",
|
|
45
53
|
"Rx",
|
|
46
54
|
"Ry",
|
|
47
55
|
"Rz",
|
|
48
56
|
"S",
|
|
49
|
-
"
|
|
57
|
+
"SDagger",
|
|
50
58
|
"T",
|
|
51
|
-
"
|
|
59
|
+
"TDagger",
|
|
60
|
+
"Wait",
|
|
52
61
|
"X",
|
|
53
62
|
"Y",
|
|
54
63
|
"Z",
|
|
55
|
-
"barrier",
|
|
56
|
-
"init",
|
|
57
|
-
"mX90",
|
|
58
|
-
"mY90",
|
|
59
|
-
"measure",
|
|
60
|
-
"reset",
|
|
61
|
-
"wait",
|
|
62
64
|
"writer",
|
|
63
65
|
]
|
|
@@ -116,14 +116,14 @@ class Circuit:
|
|
|
116
116
|
|
|
117
117
|
remap_ir(self, mapper.get_mapping())
|
|
118
118
|
|
|
119
|
-
def replace(self,
|
|
119
|
+
def replace(self, gate: type[Gate], replacement_gates_function: Callable[..., list[Gate]]) -> None:
|
|
120
120
|
"""Manually replace occurrences of a given gate with a list of gates.
|
|
121
|
-
`
|
|
121
|
+
`replacement_gates_function` is a callable that takes the arguments of the gate that is to be replaced and
|
|
122
122
|
returns the decomposition as a list of gates.
|
|
123
123
|
"""
|
|
124
124
|
from opensquirrel.passes.decomposer import general_decomposer
|
|
125
125
|
|
|
126
|
-
general_decomposer.replace(self.ir,
|
|
126
|
+
general_decomposer.replace(self.ir, gate, replacement_gates_function)
|
|
127
127
|
|
|
128
128
|
def export(self, fmt: ExportFormat | None = None) -> Any:
|
|
129
129
|
if fmt == ExportFormat.QUANTIFY_SCHEDULER:
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
from functools import partial
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from typing_extensions import Self
|
|
9
|
+
|
|
10
|
+
from opensquirrel.circuit import Circuit
|
|
11
|
+
from opensquirrel.default_instructions import default_instruction_set
|
|
12
|
+
from opensquirrel.ir import IR, AsmDeclaration, Bit, BitLike, Instruction, Qubit, QubitLike
|
|
13
|
+
from opensquirrel.register_manager import BitRegister, QubitRegister, RegisterManager
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CircuitBuilder:
|
|
17
|
+
"""
|
|
18
|
+
A class using the builder pattern to make construction of circuits easy from Python.
|
|
19
|
+
Adds corresponding instruction when a method is called. Checks that instructions are known and called with the right
|
|
20
|
+
arguments.
|
|
21
|
+
Mainly here to allow for Qiskit-style circuit construction:
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
qubit_register_size (int): Size of the qubit register
|
|
25
|
+
bit_register_size (int): Size of the bit register
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
>>> CircuitBuilder(qubit_register_size=3, bit_register_size=3).\
|
|
29
|
+
H(0).CNOT(0, 1).CNOT(0, 2).\
|
|
30
|
+
to_circuit()
|
|
31
|
+
version 3.0
|
|
32
|
+
<BLANKLINE>
|
|
33
|
+
qubit[3] q
|
|
34
|
+
<BLANKLINE>
|
|
35
|
+
h q[0]
|
|
36
|
+
cnot q[0], q[1]
|
|
37
|
+
cnot q[0], q[2]
|
|
38
|
+
<BLANKLINE>
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self, qubit_register_size: int, bit_register_size: int = 0) -> None:
|
|
42
|
+
self.register_manager = RegisterManager(QubitRegister(qubit_register_size), BitRegister(bit_register_size))
|
|
43
|
+
self.ir = IR()
|
|
44
|
+
|
|
45
|
+
def __getattr__(self, attr: Any) -> Callable[..., Self]:
|
|
46
|
+
return partial(self._add_statement, attr)
|
|
47
|
+
|
|
48
|
+
def _check_qubit_out_of_bounds_access(self, qubit: QubitLike) -> None:
|
|
49
|
+
"""Throw error if qubit index is outside the qubit register range.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
qubit: qubit to check.
|
|
53
|
+
"""
|
|
54
|
+
index = Qubit(qubit).index
|
|
55
|
+
if index >= self.register_manager.get_qubit_register_size():
|
|
56
|
+
msg = "qubit index is out of bounds"
|
|
57
|
+
raise IndexError(msg)
|
|
58
|
+
|
|
59
|
+
def _check_bit_out_of_bounds_access(self, bit: BitLike) -> None:
|
|
60
|
+
"""Throw error if bit index is outside the bit register range.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
bit: bit to check.
|
|
64
|
+
"""
|
|
65
|
+
index = Bit(bit).index
|
|
66
|
+
if index >= self.register_manager.get_bit_register_size():
|
|
67
|
+
msg = "bit index is out of bounds"
|
|
68
|
+
raise IndexError(msg)
|
|
69
|
+
|
|
70
|
+
def _check_out_of_bounds_access(self, instruction: Instruction) -> None:
|
|
71
|
+
for qubit in instruction.get_qubit_operands():
|
|
72
|
+
self._check_qubit_out_of_bounds_access(qubit)
|
|
73
|
+
for bit in instruction.get_bit_operands():
|
|
74
|
+
self._check_bit_out_of_bounds_access(bit)
|
|
75
|
+
|
|
76
|
+
def _add_statement(self, attr: str, *args: Any) -> Self:
|
|
77
|
+
if attr == "asm":
|
|
78
|
+
try:
|
|
79
|
+
asm_declaration = AsmDeclaration(*args)
|
|
80
|
+
self.ir.add_asm_declaration(asm_declaration)
|
|
81
|
+
except TypeError:
|
|
82
|
+
msg = f"trying to build '{attr}' with the wrong number or type of arguments: '{args}'"
|
|
83
|
+
raise TypeError(msg) from None
|
|
84
|
+
return self
|
|
85
|
+
|
|
86
|
+
if attr not in default_instruction_set:
|
|
87
|
+
msg = f"unknown instruction '{attr}'"
|
|
88
|
+
raise ValueError(msg)
|
|
89
|
+
try:
|
|
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
|
|
94
|
+
|
|
95
|
+
self._check_out_of_bounds_access(instruction)
|
|
96
|
+
|
|
97
|
+
self.ir.add_statement(instruction)
|
|
98
|
+
return self
|
|
99
|
+
|
|
100
|
+
def to_circuit(self) -> Circuit:
|
|
101
|
+
return Circuit(deepcopy(self.register_manager), deepcopy(self.ir))
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from numpy.typing import NDArray
|
|
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
|
+
)
|
|
23
|
+
from opensquirrel.utils import get_matrix
|
|
24
|
+
|
|
25
|
+
if TYPE_CHECKING:
|
|
26
|
+
from opensquirrel.circuit import Circuit
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _CircuitMatrixCalculator(IRVisitor):
|
|
30
|
+
def __init__(self, qubit_register_size: int) -> None:
|
|
31
|
+
self.qubit_register_size = qubit_register_size
|
|
32
|
+
self.matrix = np.eye(1 << self.qubit_register_size, dtype=np.complex128)
|
|
33
|
+
|
|
34
|
+
def visit_gate(self, gate: Gate) -> None:
|
|
35
|
+
big_matrix = get_matrix(gate, qubit_register_size=self.qubit_register_size)
|
|
36
|
+
self.matrix = np.asarray(big_matrix @ self.matrix, dtype=np.complex128)
|
|
37
|
+
|
|
38
|
+
def visit_bloch_sphere_rotation(self, gate: BlochSphereRotation) -> None:
|
|
39
|
+
self.visit_gate(gate)
|
|
40
|
+
|
|
41
|
+
def visit_bsr_no_params(self, gate: BsrNoParams) -> None:
|
|
42
|
+
self.visit_gate(gate)
|
|
43
|
+
|
|
44
|
+
def visit_bsr_full_params(self, gate: BsrFullParams) -> None:
|
|
45
|
+
self.visit_gate(gate)
|
|
46
|
+
|
|
47
|
+
def visit_bsr_angle_param(self, gate: BsrAngleParam) -> None:
|
|
48
|
+
self.visit_gate(gate)
|
|
49
|
+
|
|
50
|
+
def visit_matrix_gate(self, gate: MatrixGate) -> None:
|
|
51
|
+
self.visit_gate(gate)
|
|
52
|
+
|
|
53
|
+
def visit_swap(self, gate: SWAP) -> None:
|
|
54
|
+
self.visit_gate(gate)
|
|
55
|
+
|
|
56
|
+
def visit_controlled_gate(self, gate: ControlledGate) -> None:
|
|
57
|
+
self.visit_gate(gate)
|
|
58
|
+
|
|
59
|
+
def visit_cnot(self, gate: CNOT) -> None:
|
|
60
|
+
self.visit_gate(gate)
|
|
61
|
+
|
|
62
|
+
def visit_cz(self, gate: CZ) -> None:
|
|
63
|
+
self.visit_gate(gate)
|
|
64
|
+
|
|
65
|
+
def visit_cr(self, gate: CR) -> None:
|
|
66
|
+
self.visit_gate(gate)
|
|
67
|
+
|
|
68
|
+
def visit_crk(self, gate: CRk) -> None:
|
|
69
|
+
self.visit_gate(gate)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def get_circuit_matrix(circuit: Circuit) -> NDArray[np.complex128]:
|
|
73
|
+
"""Compute the (large) unitary matrix corresponding to the circuit.
|
|
74
|
+
|
|
75
|
+
This matrix has 4**n elements, where n is the number of qubits. Result is stored as a numpy array of complex
|
|
76
|
+
numbers.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
Matrix representation of the circuit.
|
|
80
|
+
"""
|
|
81
|
+
impl = _CircuitMatrixCalculator(circuit.qubit_register_size)
|
|
82
|
+
|
|
83
|
+
circuit.ir.accept(impl)
|
|
84
|
+
|
|
85
|
+
return impl.matrix
|
|
@@ -0,0 +1,46 @@
|
|
|
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, gate_generator: Callable[..., BlochSphereRotation]) -> None:
|
|
15
|
+
self.gate_generator = gate_generator
|
|
16
|
+
|
|
17
|
+
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
|
|
21
|
+
return BlochSphereRotation.try_match_replace_with_default(
|
|
22
|
+
BlochSphereRotation(qubit=gate.qubit, axis=gate.axis, angle=modified_angle, phase=modified_phase)
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class PowerGateModifier(GateModifier):
|
|
27
|
+
def __init__(self, exponent: SupportsFloat, gate_generator: Callable[..., BlochSphereRotation]) -> None:
|
|
28
|
+
self.exponent = exponent
|
|
29
|
+
self.gate_generator = gate_generator
|
|
30
|
+
|
|
31
|
+
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)
|
|
35
|
+
return BlochSphereRotation.try_match_replace_with_default(
|
|
36
|
+
BlochSphereRotation(qubit=gate.qubit, axis=gate.axis, angle=modified_angle, phase=modified_phase)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ControlGateModifier(GateModifier):
|
|
41
|
+
def __init__(self, gate_generator: Callable[..., BlochSphereRotation]) -> None:
|
|
42
|
+
self.gate_generator = gate_generator
|
|
43
|
+
|
|
44
|
+
def __call__(self, control: QubitLike, *args: Any) -> ControlledGate:
|
|
45
|
+
gate: BlochSphereRotation = self.gate_generator(*args)
|
|
46
|
+
return ControlledGate(control, gate)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
|
|
5
|
+
from opensquirrel.ir import (
|
|
6
|
+
CNOT,
|
|
7
|
+
CR,
|
|
8
|
+
CZ,
|
|
9
|
+
SWAP,
|
|
10
|
+
X90,
|
|
11
|
+
Y90,
|
|
12
|
+
Barrier,
|
|
13
|
+
BlochSphereRotation,
|
|
14
|
+
BsrAngleParam,
|
|
15
|
+
BsrFullParams,
|
|
16
|
+
BsrNoParams,
|
|
17
|
+
ControlledGate,
|
|
18
|
+
CRk,
|
|
19
|
+
Gate,
|
|
20
|
+
H,
|
|
21
|
+
I,
|
|
22
|
+
Init,
|
|
23
|
+
Instruction,
|
|
24
|
+
MatrixGate,
|
|
25
|
+
Measure,
|
|
26
|
+
MinusX90,
|
|
27
|
+
MinusY90,
|
|
28
|
+
NonUnitary,
|
|
29
|
+
Reset,
|
|
30
|
+
Rn,
|
|
31
|
+
Rx,
|
|
32
|
+
Ry,
|
|
33
|
+
Rz,
|
|
34
|
+
S,
|
|
35
|
+
SDagger,
|
|
36
|
+
T,
|
|
37
|
+
TDagger,
|
|
38
|
+
Unitary,
|
|
39
|
+
Wait,
|
|
40
|
+
X,
|
|
41
|
+
Y,
|
|
42
|
+
Z,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
default_bsr_without_params_set: Mapping[str, type[BsrNoParams]]
|
|
46
|
+
default_bsr_without_params_set = {
|
|
47
|
+
"H": H,
|
|
48
|
+
"I": I,
|
|
49
|
+
"S": S,
|
|
50
|
+
"Sdag": SDagger,
|
|
51
|
+
"T": T,
|
|
52
|
+
"Tdag": TDagger,
|
|
53
|
+
"X": X,
|
|
54
|
+
"X90": X90,
|
|
55
|
+
"Y": Y,
|
|
56
|
+
"Y90": Y90,
|
|
57
|
+
"Z": Z,
|
|
58
|
+
"mX90": MinusX90,
|
|
59
|
+
"mY90": MinusY90,
|
|
60
|
+
}
|
|
61
|
+
default_bsr_full_params_set: Mapping[str, type[BsrFullParams]]
|
|
62
|
+
default_bsr_full_params_set = {
|
|
63
|
+
"Rn": Rn,
|
|
64
|
+
}
|
|
65
|
+
default_bsr_with_angle_param_set: Mapping[str, type[BsrAngleParam]]
|
|
66
|
+
default_bsr_with_angle_param_set = {
|
|
67
|
+
"Rx": Rx,
|
|
68
|
+
"Ry": Ry,
|
|
69
|
+
"Rz": Rz,
|
|
70
|
+
}
|
|
71
|
+
default_bloch_sphere_rotation_set: Mapping[str, type[BlochSphereRotation]]
|
|
72
|
+
default_bloch_sphere_rotation_set = {
|
|
73
|
+
**default_bsr_full_params_set,
|
|
74
|
+
**default_bsr_without_params_set,
|
|
75
|
+
**default_bsr_with_angle_param_set,
|
|
76
|
+
}
|
|
77
|
+
default_controlled_gate_set: Mapping[str, type[ControlledGate]]
|
|
78
|
+
default_controlled_gate_set = {
|
|
79
|
+
"CNOT": CNOT,
|
|
80
|
+
"CR": CR,
|
|
81
|
+
"CRk": CRk,
|
|
82
|
+
"CZ": CZ,
|
|
83
|
+
}
|
|
84
|
+
default_matrix_gate_set: Mapping[str, type[MatrixGate]]
|
|
85
|
+
default_matrix_gate_set = {
|
|
86
|
+
"SWAP": SWAP,
|
|
87
|
+
}
|
|
88
|
+
default_gate_alias_set = {
|
|
89
|
+
"Hadamard": H,
|
|
90
|
+
"Identity": I,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
default_gate_set: Mapping[str, type[Gate]]
|
|
94
|
+
default_gate_set = {
|
|
95
|
+
**default_bloch_sphere_rotation_set,
|
|
96
|
+
**default_controlled_gate_set,
|
|
97
|
+
**default_matrix_gate_set,
|
|
98
|
+
**default_gate_alias_set,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
default_unitary_set: Mapping[str, type[Unitary]]
|
|
102
|
+
default_unitary_set = {**default_gate_set}
|
|
103
|
+
|
|
104
|
+
default_non_unitary_set: Mapping[str, type[NonUnitary]]
|
|
105
|
+
default_non_unitary_set = {
|
|
106
|
+
"barrier": Barrier,
|
|
107
|
+
"init": Init,
|
|
108
|
+
"measure": Measure,
|
|
109
|
+
"reset": Reset,
|
|
110
|
+
"wait": Wait,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
default_instruction_set: Mapping[str, type[Instruction]]
|
|
114
|
+
default_instruction_set = {
|
|
115
|
+
**default_unitary_set,
|
|
116
|
+
**default_non_unitary_set,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def is_anonymous_gate(name: str) -> bool:
|
|
121
|
+
return name not in default_gate_set
|
|
@@ -17,3 +17,15 @@ class UnsupportedGateError(Exception):
|
|
|
17
17
|
|
|
18
18
|
class ExporterError(Exception):
|
|
19
19
|
"""Should be raised when a circuit cannot be exported to the desired output format."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class NoRoutingPathError(Exception):
|
|
23
|
+
"""Should be raised when no routing path is available between qubits."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, message: str, *args: Any) -> None:
|
|
26
|
+
"""Init of the ``NoRoutingPathError``.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
message: Error message.
|
|
30
|
+
"""
|
|
31
|
+
super().__init__(message, *args)
|