-
Notifications
You must be signed in to change notification settings - Fork 340
/
test_memory.py
85 lines (64 loc) · 2.59 KB
/
test_memory.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
import numpy as np
from pyquil.experiment._memory import (
merge_memory_map_lists,
pauli_term_to_preparation_memory_map,
pauli_term_to_measurement_memory_map,
)
from pyquil.paulis import sX, sY
from pyquil.quilatom import (
MemoryReference,
quil_cis,
quil_cos,
quil_exp,
quil_sin,
quil_sqrt,
substitute,
substitute_array,
)
def test_merge_memory_map_lists():
mml = [
{"a": [1.0], "b": [3.0], "c": [4.0]},
{"a": [1.0], "b": [5.0], "c": [6.0]},
{"a": [2.0], "b": [3.0], "c": [4.0]},
{"a": [2.0], "b": [5.0], "c": [6.0]},
]
assert mml == merge_memory_map_lists(
[{"a": [1.0]}, {"a": [2.0]}], [{"b": [3.0], "c": [4.0]}, {"b": [5.0], "c": [6.0]}]
)
def test_pauli_term_to_preparation_memory_map():
assert pauli_term_to_preparation_memory_map(sX(0)) == {
"preparation_alpha": [0.0],
"preparation_beta": [np.pi / 2],
"preparation_gamma": [0.0],
}
def test_pauli_term_to_measurement_memory_map():
assert pauli_term_to_measurement_memory_map(sY(1)) == {
"measurement_alpha": [0.0, np.pi / 2],
"measurement_beta": [0.0, np.pi / 2],
"measurement_gamma": [0.0, -np.pi / 2],
}
def test_substitute_memory_reference():
x = MemoryReference("x", 0, declared_size=2)
x_0 = MemoryReference("x", 0, declared_size=2)
x_1 = MemoryReference("x", 1, declared_size=2)
# complete substitutions
assert substitute(x_0, {x: [+5, -5]}) == +5
assert substitute(x_1, {x: [+5, -5]}) == -5
assert substitute(x_0 + x_1, {x: [+5, -5]}) == 0
assert substitute(x_0 - x_1, {x: [+5, -5]}) == 10
assert substitute(x_0 * x_1, {x: [+5, -5]}) == -25
assert substitute(x_0 / x_1, {x: [+5, -5]}) == -1
assert substitute(x_0 * x_0 ** 2 / x_1, {x: [5, 10]}) == 12.5
assert np.isclose(substitute(quil_exp(x_0), {x: [5, 10]}), np.exp(5))
assert np.isclose(substitute(quil_sin(x_0), {x: [5, 10]}), np.sin(5))
assert np.isclose(substitute(quil_cos(x_0), {x: [5, 10]}), np.cos(5))
assert np.isclose(substitute(quil_sqrt(x), {x: [5, 10]}), np.sqrt(5))
assert np.isclose(substitute(quil_cis(x), {x: [5, 10]}), np.exp(1j * 5.0))
# incomplete substitutions
y = MemoryReference("y", 0, declared_size=1)
z = MemoryReference("z", 0, declared_size=1)
assert substitute(y + z, {y: [5]}) == 5 + z
assert substitute(quil_cis(z), {y: [5]}) == quil_cis(z)
# array substitution pass-through
a = MemoryReference("a", 0, declared_size=1)
assert np.allclose(substitute_array([quil_sin(a), quil_cos(a)], {a: [5]}), [np.sin(5), np.cos(5)])