/
test_mu.py
130 lines (118 loc) · 4.01 KB
/
test_mu.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
"""Tests for krotov.mu"""
import pytest
from qutip import ket, sigmam, sigmap, sigmax, sigmaz
import krotov
@pytest.fixture
def tls_control_system():
"""Non-trivial control system defined on a TLS"""
eps1 = lambda t, args: 0.5
eps2 = lambda t, args: 1
H1 = [0.5 * sigmaz(), [sigmap(), eps1], [sigmam(), eps1]]
H2 = [0.5 * sigmaz(), [sigmaz(), eps2]]
c_ops = [0.1 * sigmap()]
objectives = [
krotov.Objective(
initial_state=ket('0'), target=ket('1'), H=H1, c_ops=c_ops
),
krotov.Objective(
initial_state=ket('0'), target=ket('1'), H=H2, c_ops=c_ops
),
]
controls = [eps1, eps2]
controls_mapping = krotov.conversions.extract_controls_mapping(
objectives, controls
)
return objectives, controls, controls_mapping
@pytest.fixture
def tls_control_system_tdcops(tls_control_system):
"""Control system with time-dependent collapse operators"""
objectives, controls, _ = tls_control_system
c_op = [[0.1 * sigmap(), controls[0]]]
c_ops = [c_op]
H1 = objectives[0].H
H2 = objectives[1].H
objectives = [
krotov.Objective(
initial_state=ket('0'), target=ket('1'), H=H1, c_ops=c_ops
),
krotov.Objective(
initial_state=ket('0'), target=ket('1'), H=H2, c_ops=c_ops
),
]
controls_mapping = krotov.conversions.extract_controls_mapping(
objectives, controls
)
return objectives, controls, controls_mapping
def test_derivative_wrt_pulse_multiple_terms(tls_control_system):
"""Test the calculation of μ if the same control appears more than once"""
objectives, pulses, pulses_mapping = tls_control_system
# distinction between controls and pulses doesn't matter here, we're only
# considering linear controls and don't plug in any time_index
i_objective = 0
mu = krotov.mu.derivative_wrt_pulse(
objectives,
i_objective,
pulses,
pulses_mapping,
i_pulse=0,
time_index=0,
)
# 0.5 * (σ₊ + σ₋) = σₓ
for state in (ket('0'), ket('1')):
assert (mu(state) - (sigmax())(state)).norm('max') == 0
assert (mu(state)).dims == state.dims
def test_derivative_wrt_pulse_zero(tls_control_system):
"""Test that μ=0 if taking derivative wrt pulse not in objective"""
objectives, pulses, pulses_mapping = tls_control_system
# distinction between controls and pulses doesn't matter here, we're only
# considering linear controls and don't plug in any time_index
i_objective = 0
mu = krotov.mu.derivative_wrt_pulse(
objectives,
i_objective,
pulses,
pulses_mapping,
i_pulse=1,
time_index=0,
)
for state in (ket('0'), ket('1')):
assert mu(state).norm('max') == 0
assert (mu(state)).dims == state.dims
i_objective = 1
mu = krotov.mu.derivative_wrt_pulse(
objectives,
i_objective,
pulses,
pulses_mapping,
i_pulse=0,
time_index=0,
)
for state in (ket('0'), ket('1')):
assert mu(state).norm('max') == 0
assert (mu(state)).dims == state.dims
def test_derivative_wrt_pulse_no_timedependent_cops(tls_control_system_tdcops):
"""Test that time-dependent collapse operators are no allowed"""
objectives, pulses, pulses_mapping = tls_control_system_tdcops
i_objective = 0
with pytest.raises(NotImplementedError):
krotov.mu.derivative_wrt_pulse(
objectives,
i_objective,
pulses,
pulses_mapping,
i_pulse=0,
time_index=0,
)
# however, we do allow the c_ops to be time-dependent with controls we're
# not taking the derivative with respect to
mu = krotov.mu.derivative_wrt_pulse(
objectives,
i_objective,
pulses,
pulses_mapping,
i_pulse=1,
time_index=0,
)
for state in (ket('0'), ket('1')):
assert mu(state).norm('max') == 0
assert (mu(state)).dims == state.dims