-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest_backends.py
178 lines (151 loc) · 5.13 KB
/
test_backends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import importlib
import platform
import shutil
import sys
from pathlib import Path
import numpy as np
import pytest
import qibojit
from qibo import construct_backend, gates, list_available_backends, set_backend
from qibo.backends import GlobalBackend, MetaBackend, get_backend
####################### Test `matrix` #######################
GATES = [
("H", (0,), np.array([[1, 1], [1, -1]]) / np.sqrt(2)),
("X", (0,), np.array([[0, 1], [1, 0]])),
("Y", (0,), np.array([[0, -1j], [1j, 0]])),
("Z", (1,), np.array([[1, 0], [0, -1]])),
("S", (2,), np.array([[1, 0], [0, 1j]])),
("T", (2,), np.array([[1, 0], [0, np.exp(1j * np.pi / 4.0)]])),
(
"CNOT",
(0, 1),
np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]),
),
("CZ", (1, 3), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])),
(
"SWAP",
(2, 4),
np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]),
),
(
"FSWAP",
(2, 4),
np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, -1]]),
),
(
"TOFFOLI",
(1, 2, 3),
np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0],
]
),
),
]
@pytest.mark.parametrize("gate,qubits,target_matrix", GATES)
def test_matrix(backend, gate, qubits, target_matrix):
gate = getattr(gates, gate)(*qubits)
backend.assert_allclose(gate.matrix(backend), target_matrix)
GATES = [
(
"RX",
lambda x: np.array(
[
[np.cos(x / 2.0), -1j * np.sin(x / 2.0)],
[-1j * np.sin(x / 2.0), np.cos(x / 2.0)],
]
),
),
(
"RY",
lambda x: np.array(
[[np.cos(x / 2.0), -np.sin(x / 2.0)], [np.sin(x / 2.0), np.cos(x / 2.0)]]
),
),
("RZ", lambda x: np.diag([np.exp(-1j * x / 2.0), np.exp(1j * x / 2.0)])),
("U1", lambda x: np.diag([1, np.exp(1j * x)])),
("CU1", lambda x: np.diag([1, 1, 1, np.exp(1j * x)])),
]
@pytest.mark.parametrize("gate,target_matrix", GATES)
def test_matrix_rotations(backend, gate, target_matrix):
"""Check that `_construct_unitary` method constructs the proper matrix."""
theta = 0.1234
if gate == "CU1":
gate = getattr(gates, gate)(0, 1, theta)
else:
gate = getattr(gates, gate)(0, theta)
backend.assert_allclose(gate.matrix(backend), target_matrix(theta))
backend.assert_allclose(gate.matrix(backend), target_matrix(theta))
def test_plus_density_matrix(backend):
matrix = backend.plus_density_matrix(4)
target_matrix = np.ones((16, 16)) / 16
backend.assert_allclose(matrix, target_matrix)
def test_set_backend_error():
with pytest.raises(ValueError):
set_backend("non-existing-backend")
def test_metabackend_load_error():
with pytest.raises(ValueError):
MetaBackend.load("non-existing-backend")
def test_construct_backend(backend):
assert isinstance(
construct_backend(backend.name, platform=backend.platform), backend.__class__
)
@pytest.fixture
def uninstall_qibojit():
p = Path(qibojit.__file__).parent
modules = list(sys.modules.keys())
for mod in modules:
if mod.startswith("qibojit"):
del sys.modules[mod]
# avoid using directly cwd since it would still in the PYTHONPATH
tdir = Path.cwd() / "tmp"
tdir.mkdir()
shutil.move(p, tdir)
yield
shutil.move(tdir / p.name, p)
tdir.rmdir()
importlib.import_module("qibojit")
def test_default_backend(uninstall_qibojit):
"""Reproducing https://github.com/qiboteam/qibo/issues/1424."""
# reset global backend
GlobalBackend._instance = None
# attempt loading
default_backend = get_backend()
def test_list_available_backends():
tensorflow = False if platform.system() == "Windows" else True
qulacs = (
False if platform.system() == "Darwin" and sys.version_info[1] == 9 else True
)
available_backends = {
"numpy": True,
"tensorflow": tensorflow,
"pytorch": True,
"qulacs": qulacs,
"qibojit": {"numba": True, "cupy": False, "cuquantum": False},
"qibolab": False,
"qibo-cloud-backends": False,
"qibotn": {"cutensornet": False, "qutensornet": True},
}
assert available_backends == list_available_backends(
"qibojit", "qibolab", "qibo-cloud-backends", "qibotn"
)
def test_gradients_pytorch():
from qibo.backends import PyTorchBackend # pylint: disable=import-outside-toplevel
backend = PyTorchBackend()
gate = gates.RX(0, 0.1)
matrix = gate.matrix(backend)
assert matrix.requires_grad
assert backend.gradients
backend.requires_grad(False)
gate = gates.RX(0, 0.1)
matrix = gate.matrix(backend)
assert not matrix.requires_grad
assert not backend.gradients
assert not backend.matrices.requires_grad