opensquirrel 0.0.6__py3-none-any.whl
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/__init__.py +3 -0
- opensquirrel/circuit.py +120 -0
- opensquirrel/circuit_builder.py +60 -0
- opensquirrel/circuit_matrix_calculator.py +30 -0
- opensquirrel/common.py +56 -0
- opensquirrel/default_gates.py +115 -0
- opensquirrel/gate_library.py +13 -0
- opensquirrel/mckay_decomposer.py +56 -0
- opensquirrel/merger.py +84 -0
- opensquirrel/parsing/__init__.py +0 -0
- opensquirrel/parsing/antlr/README.md +4 -0
- opensquirrel/parsing/antlr/__init__.py +0 -0
- opensquirrel/parsing/antlr/generated/CQasm3.interp +50 -0
- opensquirrel/parsing/antlr/generated/CQasm3.tokens +28 -0
- opensquirrel/parsing/antlr/generated/CQasm3Lexer.interp +68 -0
- opensquirrel/parsing/antlr/generated/CQasm3Lexer.py +1247 -0
- opensquirrel/parsing/antlr/generated/CQasm3Lexer.tokens +28 -0
- opensquirrel/parsing/antlr/generated/CQasm3Listener.py +101 -0
- opensquirrel/parsing/antlr/generated/CQasm3Parser.py +1350 -0
- opensquirrel/parsing/antlr/generated/CQasm3Visitor.py +58 -0
- opensquirrel/parsing/antlr/generated/CQasmLexer.interp +68 -0
- opensquirrel/parsing/antlr/generated/CQasmLexer.py +1247 -0
- opensquirrel/parsing/antlr/generated/CQasmLexer.tokens +28 -0
- opensquirrel/parsing/antlr/generated/README.md +4 -0
- opensquirrel/parsing/antlr/generated/__init__.py +0 -0
- opensquirrel/parsing/antlr/grammar/CQasm3.g4 +26 -0
- opensquirrel/parsing/antlr/grammar/__init__.py +0 -0
- opensquirrel/parsing/antlr/qubit_range_checker.py +66 -0
- opensquirrel/parsing/antlr/squirrel_error_handler.py +12 -0
- opensquirrel/parsing/antlr/squirrel_ir_creator.py +73 -0
- opensquirrel/parsing/antlr/squirrel_ir_from_string.py +56 -0
- opensquirrel/parsing/antlr/type_checker.py +75 -0
- opensquirrel/parsing/libqasm/README.md +3 -0
- opensquirrel/parsing/libqasm/__init__.py +0 -0
- opensquirrel/parsing/libqasm/libqasm_ir_creator.py +125 -0
- opensquirrel/replacer.py +92 -0
- opensquirrel/squirrel_ir.py +268 -0
- opensquirrel/utils/__init__.py +0 -0
- opensquirrel/utils/matrix_expander.py +185 -0
- opensquirrel/writer.py +37 -0
- opensquirrel-0.0.6.dist-info/LICENSE.md +201 -0
- opensquirrel-0.0.6.dist-info/METADATA +115 -0
- opensquirrel-0.0.6.dist-info/RECORD +44 -0
- opensquirrel-0.0.6.dist-info/WHEEL +4 -0
opensquirrel/__init__.py
ADDED
opensquirrel/circuit.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
from typing import Callable, Dict, List
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
import opensquirrel.parsing.antlr.squirrel_ir_from_string
|
|
6
|
+
from opensquirrel import circuit_matrix_calculator, mckay_decomposer, merger, replacer, writer
|
|
7
|
+
from opensquirrel.default_gates import default_gate_aliases, default_gate_set
|
|
8
|
+
from opensquirrel.parsing.libqasm.libqasm_ir_creator import LibqasmIRCreator
|
|
9
|
+
from opensquirrel.squirrel_ir import Gate, SquirrelIR
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Circuit:
|
|
13
|
+
"""The Circuit class is the only interface to access OpenSquirrel's features.
|
|
14
|
+
|
|
15
|
+
Examples:
|
|
16
|
+
>>> c = Circuit.from_string("version 3.0; qubit[3] q; h q[0]")
|
|
17
|
+
>>> c
|
|
18
|
+
version 3.0
|
|
19
|
+
<BLANKLINE>
|
|
20
|
+
qubit[3] q
|
|
21
|
+
<BLANKLINE>
|
|
22
|
+
h q[0]
|
|
23
|
+
<BLANKLINE>
|
|
24
|
+
>>> c.decompose(decomposer=mckay_decomposer.mckay_decomposer)
|
|
25
|
+
>>> c
|
|
26
|
+
version 3.0
|
|
27
|
+
<BLANKLINE>
|
|
28
|
+
qubit[3] q
|
|
29
|
+
<BLANKLINE>
|
|
30
|
+
x90 q[0]
|
|
31
|
+
rz q[0], 1.5707963
|
|
32
|
+
x90 q[0]
|
|
33
|
+
<BLANKLINE>
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, squirrel_ir: SquirrelIR):
|
|
37
|
+
"""Create a circuit object from a SquirrelIR object."""
|
|
38
|
+
|
|
39
|
+
self.squirrel_ir = squirrel_ir
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_string(
|
|
43
|
+
cls,
|
|
44
|
+
cqasm3_string: str,
|
|
45
|
+
gate_set: [Callable[..., Gate]] = default_gate_set,
|
|
46
|
+
gate_aliases: Dict[str, Callable[..., Gate]] = default_gate_aliases,
|
|
47
|
+
use_libqasm: bool = False,
|
|
48
|
+
):
|
|
49
|
+
"""Create a circuit object from a cQasm3 string. All the gates in the circuit need to be defined in
|
|
50
|
+
the `gates` argument.
|
|
51
|
+
|
|
52
|
+
* type-checking is performed, eliminating qubit indices errors and incoherencies
|
|
53
|
+
* checks that used gates are supported and mentioned in `gates` with appropriate signatures
|
|
54
|
+
* does not support map or variables, and other things...
|
|
55
|
+
* for example of `gates` dictionary, please look at TestGates.py
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
cqasm3_string: a cqasm 3 string
|
|
60
|
+
gate_set: an array of gate semantic functions. See default_gates for examples
|
|
61
|
+
gate_aliases: a dictionary of extra aliases, mapping strings to functions in the gate set
|
|
62
|
+
use_libqasm: if True, use libqasm instead of build-in ANTLR parser.
|
|
63
|
+
Note: those two separate implementations may diverge and libqasm should be taken as reference.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
A Circuit object corresponding to the input string. Throws on parsing errors.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
if use_libqasm:
|
|
70
|
+
libqasm_ir_creator = LibqasmIRCreator(gate_set=gate_set, gate_aliases=gate_aliases)
|
|
71
|
+
return Circuit(libqasm_ir_creator.squirrel_ir_from_string(cqasm3_string))
|
|
72
|
+
|
|
73
|
+
return Circuit(
|
|
74
|
+
opensquirrel.parsing.antlr.squirrel_ir_from_string.squirrel_ir_from_string(
|
|
75
|
+
cqasm3_string, gate_set=gate_set, gate_aliases=gate_aliases
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def number_of_qubits(self) -> int:
|
|
81
|
+
return self.squirrel_ir.number_of_qubits
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def qubit_register_name(self) -> str:
|
|
85
|
+
return self.squirrel_ir.qubit_register_name
|
|
86
|
+
|
|
87
|
+
def merge_single_qubit_gates(self):
|
|
88
|
+
"""Perform gate fusion on all one-qubit gates.
|
|
89
|
+
Gates obtained from merging other gates become anonymous gates.
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
merger.merge_single_qubit_gates(self.squirrel_ir)
|
|
93
|
+
|
|
94
|
+
def decompose(self, decomposer: Callable[[Gate], List[Gate]]):
|
|
95
|
+
"""Generic decomposition pass. It applies the given decomposer function
|
|
96
|
+
to every gate in the circuit."""
|
|
97
|
+
replacer.decompose(self.squirrel_ir, decomposer)
|
|
98
|
+
|
|
99
|
+
def replace(self, gate_generator: Callable[..., Gate], f):
|
|
100
|
+
"""Manually replace occurrences of a given gate with a list of gates.
|
|
101
|
+
`f` is a callable that takes the arguments of the replaced gate
|
|
102
|
+
and returns the decomposition as a list of gates.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
replacer.replace(self.squirrel_ir, gate_generator, f)
|
|
106
|
+
|
|
107
|
+
def test_get_circuit_matrix(self) -> np.ndarray:
|
|
108
|
+
"""Get the (large) unitary matrix corresponding to the circuit.
|
|
109
|
+
|
|
110
|
+
* this matrix has 4**n elements, where n is the number of qubits
|
|
111
|
+
* therefore this function is only here for testing purposes on small number of qubits
|
|
112
|
+
* result is stored as a numpy array of complex numbers
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
return circuit_matrix_calculator.get_circuit_matrix(self.squirrel_ir)
|
|
116
|
+
|
|
117
|
+
def __repr__(self) -> str:
|
|
118
|
+
"""Write the circuit to a cQasm3 string."""
|
|
119
|
+
|
|
120
|
+
return writer.squirrel_ir_to_string(self.squirrel_ir)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from typing import Callable, Dict
|
|
3
|
+
|
|
4
|
+
from opensquirrel.circuit import Circuit
|
|
5
|
+
from opensquirrel.default_gates import default_gate_aliases, default_gate_set
|
|
6
|
+
from opensquirrel.gate_library import GateLibrary
|
|
7
|
+
from opensquirrel.squirrel_ir import Comment, Gate, Qubit, SquirrelIR
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CircuitBuilder(GateLibrary):
|
|
11
|
+
"""
|
|
12
|
+
A class using the builder pattern to make construction of circuits easy from Python.
|
|
13
|
+
Adds corresponding gate when a method is called. Checks gates are known and called with the right arguments.
|
|
14
|
+
Mainly here to allow for Qiskit-style circuit construction:
|
|
15
|
+
|
|
16
|
+
>>> CircuitBuilder(number_of_qubits=3).h(Qubit(0)).cnot(Qubit(0), Qubit(1)).cnot(Qubit(0), Qubit(2)).to_circuit()
|
|
17
|
+
version 3.0
|
|
18
|
+
<BLANKLINE>
|
|
19
|
+
qubit[3] q
|
|
20
|
+
<BLANKLINE>
|
|
21
|
+
h q[0]
|
|
22
|
+
cnot q[0], q[1]
|
|
23
|
+
cnot q[0], q[2]
|
|
24
|
+
<BLANKLINE>
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
_default_qubit_register_name = "q"
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
number_of_qubits: int,
|
|
32
|
+
gate_set: [Callable[..., Gate]] = default_gate_set,
|
|
33
|
+
gate_aliases: Dict[str, Callable[..., Gate]] = default_gate_aliases,
|
|
34
|
+
):
|
|
35
|
+
GateLibrary.__init__(self, gate_set, gate_aliases)
|
|
36
|
+
self.squirrel_ir = SquirrelIR(
|
|
37
|
+
number_of_qubits=number_of_qubits, qubit_register_name=self._default_qubit_register_name
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
def __getattr__(self, attr):
|
|
41
|
+
def add_comment(comment_string: str):
|
|
42
|
+
self.squirrel_ir.add_comment(Comment(comment_string))
|
|
43
|
+
return self
|
|
44
|
+
|
|
45
|
+
def add_this_gate(*args):
|
|
46
|
+
generator_f = GateLibrary.get_gate_f(self, attr)
|
|
47
|
+
|
|
48
|
+
for i, par in enumerate(inspect.signature(generator_f).parameters.values()):
|
|
49
|
+
if not isinstance(args[i], par.annotation):
|
|
50
|
+
raise TypeError(
|
|
51
|
+
f"Wrong argument type for gate `{attr}`, got {type(args[i])} but expected {par.annotation}"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
self.squirrel_ir.add_gate(generator_f(*args))
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
return add_comment if attr == "comment" else add_this_gate
|
|
58
|
+
|
|
59
|
+
def to_circuit(self) -> Circuit:
|
|
60
|
+
return Circuit(self.squirrel_ir)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from opensquirrel.squirrel_ir import Comment, Gate, SquirrelIR, SquirrelIRVisitor
|
|
4
|
+
from opensquirrel.utils.matrix_expander import get_matrix
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class _CircuitMatrixCalculator(SquirrelIRVisitor):
|
|
8
|
+
def __init__(self, number_of_qubits):
|
|
9
|
+
self.number_of_qubits = number_of_qubits
|
|
10
|
+
self.matrix = np.eye(1 << self.number_of_qubits, dtype=np.complex128)
|
|
11
|
+
|
|
12
|
+
def visit_gate(self, gate: Gate):
|
|
13
|
+
big_matrix = get_matrix(gate, number_of_qubits=self.number_of_qubits)
|
|
14
|
+
self.matrix = big_matrix @ self.matrix
|
|
15
|
+
|
|
16
|
+
def visit_comment(self, comment: Comment):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_circuit_matrix(squirrel_ir: SquirrelIR):
|
|
21
|
+
"""
|
|
22
|
+
Compute the Numpy unitary matrix corresponding to the circuit.
|
|
23
|
+
The size of this matrix grows exponentially with the number of qubits.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
impl = _CircuitMatrixCalculator(squirrel_ir.number_of_qubits)
|
|
27
|
+
|
|
28
|
+
squirrel_ir.accept(impl)
|
|
29
|
+
|
|
30
|
+
return impl.matrix
|
opensquirrel/common.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import cmath
|
|
2
|
+
import math
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
ATOL = 0.0000001
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def normalize_angle(x: float) -> float:
|
|
11
|
+
t = x - 2 * math.pi * (x // (2 * math.pi) + 1)
|
|
12
|
+
if t < -math.pi + ATOL:
|
|
13
|
+
t += 2 * math.pi
|
|
14
|
+
elif t > math.pi:
|
|
15
|
+
t -= 2 * math.pi
|
|
16
|
+
return t
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def normalize_axis(axis: np.ndarray):
|
|
20
|
+
norm = np.linalg.norm(axis)
|
|
21
|
+
axis /= norm
|
|
22
|
+
return axis
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
X = np.array([[0, 1], [1, 0]])
|
|
26
|
+
Y = np.array([[0, -1j], [1j, 0]])
|
|
27
|
+
Z = np.array([[1, 0], [0, -1]])
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def can1(axis: Tuple[float, float, float], angle: float, phase: float = 0) -> np.ndarray:
|
|
31
|
+
nx, ny, nz = axis
|
|
32
|
+
norm = math.sqrt(nx**2 + ny**2 + nz**2)
|
|
33
|
+
assert norm > 0.00000001
|
|
34
|
+
|
|
35
|
+
nx /= norm
|
|
36
|
+
ny /= norm
|
|
37
|
+
nz /= norm
|
|
38
|
+
|
|
39
|
+
result = cmath.rect(1, phase) * (
|
|
40
|
+
math.cos(angle / 2) * np.identity(2) - 1j * math.sin(angle / 2) * (nx * X + ny * Y + nz * Z)
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
return result
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def are_matrices_equivalent_up_to_global_phase(matrix_a, matrix_b) -> bool:
|
|
47
|
+
first_non_zero = next(
|
|
48
|
+
(i, j) for i in range(matrix_a.shape[0]) for j in range(matrix_a.shape[1]) if abs(matrix_a[i, j]) > ATOL
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if abs(matrix_b[first_non_zero]) < ATOL:
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
phase_difference = matrix_a[first_non_zero] / matrix_b[first_non_zero]
|
|
55
|
+
|
|
56
|
+
return np.allclose(matrix_a, phase_difference * matrix_b)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
from opensquirrel.squirrel_ir import *
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@named_gate
|
|
7
|
+
def h(q: Qubit) -> Gate:
|
|
8
|
+
return BlochSphereRotation(qubit=q, axis=(1, 0, 1), angle=math.pi, phase=math.pi / 2)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@named_gate
|
|
12
|
+
def x(q: Qubit) -> Gate:
|
|
13
|
+
return BlochSphereRotation(qubit=q, axis=(1, 0, 0), angle=math.pi, phase=math.pi / 2)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@named_gate
|
|
17
|
+
def x90(q: Qubit) -> Gate:
|
|
18
|
+
return BlochSphereRotation(qubit=q, axis=(1, 0, 0), angle=math.pi / 2, phase=0)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@named_gate
|
|
22
|
+
def xm90(q: Qubit) -> Gate:
|
|
23
|
+
return BlochSphereRotation(qubit=q, axis=(1, 0, 0), angle=-math.pi / 2, phase=0)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@named_gate
|
|
27
|
+
def y(q: Qubit) -> Gate:
|
|
28
|
+
return BlochSphereRotation(qubit=q, axis=(0, 1, 0), angle=math.pi, phase=math.pi / 2)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@named_gate
|
|
32
|
+
def y90(q: Qubit) -> Gate:
|
|
33
|
+
return BlochSphereRotation(qubit=q, axis=(0, 1, 0), angle=math.pi / 2, phase=0)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@named_gate
|
|
37
|
+
def z(q: Qubit) -> Gate:
|
|
38
|
+
return BlochSphereRotation(qubit=q, axis=(0, 0, 1), angle=math.pi, phase=math.pi / 2)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@named_gate
|
|
42
|
+
def z90(q: Qubit) -> Gate:
|
|
43
|
+
return BlochSphereRotation(qubit=q, axis=(0, 0, 1), angle=math.pi / 2, phase=0)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@named_gate
|
|
47
|
+
def rx(q: Qubit, theta: Float) -> Gate:
|
|
48
|
+
return BlochSphereRotation(qubit=q, axis=(1, 0, 0), angle=theta.value, phase=0)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@named_gate
|
|
52
|
+
def ry(q: Qubit, theta: Float) -> Gate:
|
|
53
|
+
return BlochSphereRotation(qubit=q, axis=(0, 1, 0), angle=theta.value, phase=0)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@named_gate
|
|
57
|
+
def rz(q: Qubit, theta: Float) -> Gate:
|
|
58
|
+
return BlochSphereRotation(qubit=q, axis=(0, 0, 1), angle=theta.value, phase=0)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@named_gate
|
|
62
|
+
def cnot(control: Qubit, target: Qubit) -> Gate:
|
|
63
|
+
return ControlledGate(control, x(target))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@named_gate
|
|
67
|
+
def cz(control: Qubit, target: Qubit) -> Gate:
|
|
68
|
+
return ControlledGate(control, z(target))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@named_gate
|
|
72
|
+
def cr(control: Qubit, target: Qubit, theta: Float) -> Gate:
|
|
73
|
+
return ControlledGate(
|
|
74
|
+
control, BlochSphereRotation(qubit=target, axis=(0, 0, 1), angle=theta.value, phase=theta.value / 2)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@named_gate
|
|
79
|
+
def crk(control: Qubit, target: Qubit, k: Int) -> Gate:
|
|
80
|
+
theta = 2 * math.pi / (2**k.value)
|
|
81
|
+
return ControlledGate(control, BlochSphereRotation(qubit=target, axis=(0, 0, 1), angle=theta, phase=theta / 2))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@named_gate
|
|
85
|
+
def swap(q1: Qubit, q2: Qubit) -> Gate:
|
|
86
|
+
return MatrixGate(
|
|
87
|
+
np.array(
|
|
88
|
+
[
|
|
89
|
+
[1, 0, 0, 0],
|
|
90
|
+
[0, 0, 1, 0],
|
|
91
|
+
[0, 1, 0, 0],
|
|
92
|
+
[0, 0, 0, 1],
|
|
93
|
+
]
|
|
94
|
+
),
|
|
95
|
+
[q1, q2],
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@named_gate
|
|
100
|
+
def sqrt_swap(q1: Qubit, q2: Qubit) -> Gate:
|
|
101
|
+
return MatrixGate(
|
|
102
|
+
np.array(
|
|
103
|
+
[
|
|
104
|
+
[1, 0, 0, 0],
|
|
105
|
+
[0, (1 + 1j) / 2, (1 - 1j) / 2, 0],
|
|
106
|
+
[0, (1 - 1j) / 2, (1 + 1j) / 2, 0],
|
|
107
|
+
[0, 0, 0, 1],
|
|
108
|
+
]
|
|
109
|
+
),
|
|
110
|
+
[q1, q2],
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
default_gate_set = [h, x, x90, xm90, y, y90, z, z90, cz, cr, crk, cnot, rx, ry, rz, x, swap, sqrt_swap]
|
|
115
|
+
default_gate_aliases = {"X": x, "RX": rx, "RY": ry, "RZ": rz, "Hadamard": h, "H": h}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class GateLibrary:
|
|
2
|
+
def __init__(self, gate_set, gate_aliases):
|
|
3
|
+
self.gate_set = gate_set
|
|
4
|
+
self.gate_aliases = gate_aliases
|
|
5
|
+
|
|
6
|
+
def get_gate_f(self, gate_name: str):
|
|
7
|
+
try:
|
|
8
|
+
generator_f = next(f for f in self.gate_set if f.__name__ == gate_name)
|
|
9
|
+
except StopIteration:
|
|
10
|
+
if gate_name not in self.gate_aliases:
|
|
11
|
+
raise Exception(f"Unknown gate `{gate_name}`")
|
|
12
|
+
generator_f = self.gate_aliases[gate_name]
|
|
13
|
+
return generator_f
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from math import acos, atan2, cos, pi, sin, sqrt
|
|
2
|
+
from typing import Tuple
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
from opensquirrel.common import ATOL, normalize_angle
|
|
7
|
+
from opensquirrel.default_gates import rz, x90
|
|
8
|
+
from opensquirrel.squirrel_ir import BlochSphereRotation, Float, Gate, Qubit, SquirrelIR
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def mckay_decomposer(g: Gate):
|
|
12
|
+
"""Return the McKay decomposition of a 1-qubit gate as a list of gates.
|
|
13
|
+
gate ----> Rz.Rx(pi/2).Rz.Rx(pi/2).Rz
|
|
14
|
+
|
|
15
|
+
_global phase is deemed irrelevant_, therefore a simulator backend might produce different output
|
|
16
|
+
for the input and output - the results should be equivalent modulo global phase.
|
|
17
|
+
"""
|
|
18
|
+
if not isinstance(g, BlochSphereRotation):
|
|
19
|
+
return [g]
|
|
20
|
+
|
|
21
|
+
if abs(g.angle) < ATOL:
|
|
22
|
+
return []
|
|
23
|
+
|
|
24
|
+
# McKay decomposition
|
|
25
|
+
|
|
26
|
+
za_mod = sqrt(cos(g.angle / 2) ** 2 + (g.axis[2] * sin(g.angle / 2)) ** 2)
|
|
27
|
+
zb_mod = abs(sin(g.angle / 2)) * sqrt(g.axis[0] ** 2 + g.axis[1] ** 2)
|
|
28
|
+
|
|
29
|
+
theta = pi - 2 * atan2(zb_mod, za_mod)
|
|
30
|
+
|
|
31
|
+
alpha = atan2(-sin(g.angle / 2) * g.axis[2], cos(g.angle / 2))
|
|
32
|
+
beta = atan2(-sin(g.angle / 2) * g.axis[0], -sin(g.angle / 2) * g.axis[1])
|
|
33
|
+
|
|
34
|
+
lam = beta - alpha
|
|
35
|
+
phi = -beta - alpha - pi
|
|
36
|
+
|
|
37
|
+
lam = normalize_angle(lam)
|
|
38
|
+
phi = normalize_angle(phi)
|
|
39
|
+
theta = normalize_angle(theta)
|
|
40
|
+
|
|
41
|
+
decomposed_g = []
|
|
42
|
+
|
|
43
|
+
if abs(lam) > ATOL:
|
|
44
|
+
decomposed_g.append(rz(g.qubit, Float(lam)))
|
|
45
|
+
|
|
46
|
+
decomposed_g.append(x90(g.qubit))
|
|
47
|
+
|
|
48
|
+
if abs(theta) > ATOL:
|
|
49
|
+
decomposed_g.append(rz(g.qubit, Float(theta)))
|
|
50
|
+
|
|
51
|
+
decomposed_g.append(x90(g.qubit))
|
|
52
|
+
|
|
53
|
+
if abs(phi) > ATOL:
|
|
54
|
+
decomposed_g.append(rz(g.qubit, Float(phi)))
|
|
55
|
+
|
|
56
|
+
return decomposed_g
|
opensquirrel/merger.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from math import acos, atan2, cos, pi, sin, sqrt
|
|
2
|
+
from typing import List, Optional
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
from opensquirrel.common import ATOL
|
|
7
|
+
from opensquirrel.squirrel_ir import BlochSphereRotation, Gate, Qubit, SquirrelIR
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def compose_bloch_sphere_rotations(a: BlochSphereRotation, b: BlochSphereRotation) -> BlochSphereRotation:
|
|
11
|
+
"""Computes the Bloch sphere rotation resulting from the composition of two Block sphere rotations.
|
|
12
|
+
The first rotation is applied and then the second.
|
|
13
|
+
The resulting gate is anonymous except if `a` is the identity and `b` is not anonymous, or vice versa.
|
|
14
|
+
|
|
15
|
+
Uses Rodrigues' rotation formula, see for instance https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula.
|
|
16
|
+
"""
|
|
17
|
+
assert a.qubit == b.qubit, "Cannot merge two BlochSphereRotation's on different qubits"
|
|
18
|
+
|
|
19
|
+
combined_angle = 2 * acos(
|
|
20
|
+
cos(a.angle / 2) * cos(b.angle / 2) - sin(a.angle / 2) * sin(b.angle / 2) * np.dot(a.axis, b.axis)
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
if abs(sin(combined_angle / 2)) < ATOL:
|
|
24
|
+
return BlochSphereRotation.identity(a.qubit)
|
|
25
|
+
|
|
26
|
+
combined_axis = (
|
|
27
|
+
1
|
|
28
|
+
/ sin(combined_angle / 2)
|
|
29
|
+
* (
|
|
30
|
+
sin(a.angle / 2) * cos(b.angle / 2) * a.axis
|
|
31
|
+
+ cos(a.angle / 2) * sin(b.angle / 2) * b.axis
|
|
32
|
+
+ sin(a.angle / 2) * sin(b.angle / 2) * np.cross(a.axis, b.axis)
|
|
33
|
+
)
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
combined_phase = a.phase + b.phase
|
|
37
|
+
|
|
38
|
+
generator = b.generator if a.is_identity() else a.generator if b.is_identity() else None
|
|
39
|
+
arguments = b.arguments if a.is_identity() else a.arguments if b.is_identity() else None
|
|
40
|
+
|
|
41
|
+
return BlochSphereRotation(
|
|
42
|
+
qubit=a.qubit,
|
|
43
|
+
axis=combined_axis,
|
|
44
|
+
angle=combined_angle,
|
|
45
|
+
phase=combined_phase,
|
|
46
|
+
generator=generator,
|
|
47
|
+
arguments=arguments,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def merge_single_qubit_gates(squirrel_ir: SquirrelIR):
|
|
52
|
+
accumulators_per_qubit: dict[Qubit, BlochSphereRotation] = {
|
|
53
|
+
Qubit(q): BlochSphereRotation.identity(Qubit(q)) for q in range(squirrel_ir.number_of_qubits)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
statement_index = 0
|
|
57
|
+
while statement_index < len(squirrel_ir.statements):
|
|
58
|
+
statement = squirrel_ir.statements[statement_index]
|
|
59
|
+
|
|
60
|
+
if not isinstance(statement, Gate):
|
|
61
|
+
# Skip
|
|
62
|
+
statement_index += 1
|
|
63
|
+
continue
|
|
64
|
+
|
|
65
|
+
if isinstance(statement, BlochSphereRotation):
|
|
66
|
+
# Accumulate
|
|
67
|
+
already_accumulated = accumulators_per_qubit.get(statement.qubit)
|
|
68
|
+
|
|
69
|
+
composed = compose_bloch_sphere_rotations(statement, already_accumulated)
|
|
70
|
+
accumulators_per_qubit[statement.qubit] = composed
|
|
71
|
+
|
|
72
|
+
del squirrel_ir.statements[statement_index]
|
|
73
|
+
continue
|
|
74
|
+
|
|
75
|
+
for qubit_operand in statement.get_qubit_operands():
|
|
76
|
+
if not accumulators_per_qubit[qubit_operand].is_identity():
|
|
77
|
+
squirrel_ir.statements.insert(statement_index, accumulators_per_qubit[qubit_operand])
|
|
78
|
+
accumulators_per_qubit[qubit_operand] = BlochSphereRotation.identity(qubit_operand)
|
|
79
|
+
statement_index += 1
|
|
80
|
+
statement_index += 1
|
|
81
|
+
|
|
82
|
+
for accumulated_bloch_sphere_rotation in accumulators_per_qubit.values():
|
|
83
|
+
if not accumulated_bloch_sphere_rotation.is_identity():
|
|
84
|
+
squirrel_ir.statements.append(accumulated_bloch_sphere_rotation)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
token literal names:
|
|
2
|
+
null
|
|
3
|
+
';'
|
|
4
|
+
'\r\n'
|
|
5
|
+
'\n'
|
|
6
|
+
'3.0'
|
|
7
|
+
'qubit['
|
|
8
|
+
']'
|
|
9
|
+
','
|
|
10
|
+
'['
|
|
11
|
+
':'
|
|
12
|
+
'-'
|
|
13
|
+
'version'
|
|
14
|
+
null
|
|
15
|
+
null
|
|
16
|
+
null
|
|
17
|
+
null
|
|
18
|
+
null
|
|
19
|
+
null
|
|
20
|
+
|
|
21
|
+
token symbolic names:
|
|
22
|
+
null
|
|
23
|
+
null
|
|
24
|
+
null
|
|
25
|
+
null
|
|
26
|
+
null
|
|
27
|
+
null
|
|
28
|
+
null
|
|
29
|
+
null
|
|
30
|
+
null
|
|
31
|
+
null
|
|
32
|
+
null
|
|
33
|
+
VERSION
|
|
34
|
+
ID
|
|
35
|
+
FLOAT
|
|
36
|
+
INT
|
|
37
|
+
WS
|
|
38
|
+
COMMENT
|
|
39
|
+
MULTILINE_COMMENT
|
|
40
|
+
|
|
41
|
+
rule names:
|
|
42
|
+
stateSep
|
|
43
|
+
prog
|
|
44
|
+
qubitRegisterDeclaration
|
|
45
|
+
gateApplication
|
|
46
|
+
expr
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
atn:
|
|
50
|
+
[4, 1, 17, 79, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 1, 0, 4, 0, 12, 8, 0, 11, 0, 12, 0, 13, 1, 1, 3, 1, 17, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 26, 8, 1, 10, 1, 12, 1, 29, 9, 1, 1, 1, 3, 1, 32, 8, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 5, 3, 45, 8, 3, 10, 3, 12, 3, 48, 9, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 5, 4, 59, 8, 4, 10, 4, 12, 4, 62, 9, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 3, 4, 77, 8, 4, 1, 4, 0, 0, 5, 0, 2, 4, 6, 8, 0, 1, 1, 0, 1, 3, 85, 0, 11, 1, 0, 0, 0, 2, 16, 1, 0, 0, 0, 4, 35, 1, 0, 0, 0, 6, 40, 1, 0, 0, 0, 8, 76, 1, 0, 0, 0, 10, 12, 7, 0, 0, 0, 11, 10, 1, 0, 0, 0, 12, 13, 1, 0, 0, 0, 13, 11, 1, 0, 0, 0, 13, 14, 1, 0, 0, 0, 14, 1, 1, 0, 0, 0, 15, 17, 3, 0, 0, 0, 16, 15, 1, 0, 0, 0, 16, 17, 1, 0, 0, 0, 17, 18, 1, 0, 0, 0, 18, 19, 5, 11, 0, 0, 19, 20, 5, 4, 0, 0, 20, 21, 3, 0, 0, 0, 21, 27, 3, 4, 2, 0, 22, 23, 3, 0, 0, 0, 23, 24, 3, 6, 3, 0, 24, 26, 1, 0, 0, 0, 25, 22, 1, 0, 0, 0, 26, 29, 1, 0, 0, 0, 27, 25, 1, 0, 0, 0, 27, 28, 1, 0, 0, 0, 28, 31, 1, 0, 0, 0, 29, 27, 1, 0, 0, 0, 30, 32, 3, 0, 0, 0, 31, 30, 1, 0, 0, 0, 31, 32, 1, 0, 0, 0, 32, 33, 1, 0, 0, 0, 33, 34, 5, 0, 0, 1, 34, 3, 1, 0, 0, 0, 35, 36, 5, 5, 0, 0, 36, 37, 5, 14, 0, 0, 37, 38, 5, 6, 0, 0, 38, 39, 5, 12, 0, 0, 39, 5, 1, 0, 0, 0, 40, 41, 5, 12, 0, 0, 41, 46, 3, 8, 4, 0, 42, 43, 5, 7, 0, 0, 43, 45, 3, 8, 4, 0, 44, 42, 1, 0, 0, 0, 45, 48, 1, 0, 0, 0, 46, 44, 1, 0, 0, 0, 46, 47, 1, 0, 0, 0, 47, 7, 1, 0, 0, 0, 48, 46, 1, 0, 0, 0, 49, 50, 5, 12, 0, 0, 50, 51, 5, 8, 0, 0, 51, 52, 5, 14, 0, 0, 52, 77, 5, 6, 0, 0, 53, 54, 5, 12, 0, 0, 54, 55, 5, 8, 0, 0, 55, 60, 5, 14, 0, 0, 56, 57, 5, 7, 0, 0, 57, 59, 5, 14, 0, 0, 58, 56, 1, 0, 0, 0, 59, 62, 1, 0, 0, 0, 60, 58, 1, 0, 0, 0, 60, 61, 1, 0, 0, 0, 61, 63, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 63, 77, 5, 6, 0, 0, 64, 65, 5, 12, 0, 0, 65, 66, 5, 8, 0, 0, 66, 67, 5, 14, 0, 0, 67, 68, 5, 9, 0, 0, 68, 69, 5, 14, 0, 0, 69, 77, 5, 6, 0, 0, 70, 77, 5, 14, 0, 0, 71, 72, 5, 10, 0, 0, 72, 77, 5, 14, 0, 0, 73, 77, 5, 13, 0, 0, 74, 75, 5, 10, 0, 0, 75, 77, 5, 13, 0, 0, 76, 49, 1, 0, 0, 0, 76, 53, 1, 0, 0, 0, 76, 64, 1, 0, 0, 0, 76, 70, 1, 0, 0, 0, 76, 71, 1, 0, 0, 0, 76, 73, 1, 0, 0, 0, 76, 74, 1, 0, 0, 0, 77, 9, 1, 0, 0, 0, 7, 13, 16, 27, 31, 46, 60, 76]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
T__0=1
|
|
2
|
+
T__1=2
|
|
3
|
+
T__2=3
|
|
4
|
+
T__3=4
|
|
5
|
+
T__4=5
|
|
6
|
+
T__5=6
|
|
7
|
+
T__6=7
|
|
8
|
+
T__7=8
|
|
9
|
+
T__8=9
|
|
10
|
+
T__9=10
|
|
11
|
+
VERSION=11
|
|
12
|
+
ID=12
|
|
13
|
+
FLOAT=13
|
|
14
|
+
INT=14
|
|
15
|
+
WS=15
|
|
16
|
+
COMMENT=16
|
|
17
|
+
MULTILINE_COMMENT=17
|
|
18
|
+
';'=1
|
|
19
|
+
'\r\n'=2
|
|
20
|
+
'\n'=3
|
|
21
|
+
'3.0'=4
|
|
22
|
+
'qubit['=5
|
|
23
|
+
']'=6
|
|
24
|
+
','=7
|
|
25
|
+
'['=8
|
|
26
|
+
':'=9
|
|
27
|
+
'-'=10
|
|
28
|
+
'version'=11
|