forked from qutip/qutip
/
test_subsystem_apply.py
138 lines (120 loc) · 6.06 KB
/
test_subsystem_apply.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
# This file is part of QuTiP: Quantum Toolbox in Python.
#
# Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
from numpy.linalg import norm
from numpy.testing import assert_, run_module_suite
from qutip.random_objects import rand_dm, rand_unitary, rand_kraus_map
from qutip.subsystem_apply import subsystem_apply
from qutip.superop_reps import kraus_to_super
from qutip.superoperator import mat2vec, vec2mat
from qutip.tensor import tensor
from qutip.qobj import Qobj
class TestSubsystemApply(object):
"""
A test class for the QuTiP function for applying superoperators to
subsystems.
The four tests below determine whether efficient numerics, naive numerics
and semi-analytic results are identical.
"""
def test_SimpleSingleApply(self):
"""
Non-composite system, operator on Hilbert space.
"""
rho_3 = rand_dm(3)
single_op = rand_unitary(3)
analytic_result = single_op * rho_3 * single_op.dag()
naive_result = subsystem_apply(rho_3, single_op, [True],
reference=True)
efficient_result = subsystem_apply(rho_3, single_op, [True])
naive_diff = (analytic_result - naive_result).data.todense()
efficient_diff = (efficient_result - analytic_result).data.todense()
assert_(norm(naive_diff) < 1e-12 and norm(efficient_diff) < 1e-12)
def test_SimpleSuperApply(self):
"""
Non-composite system, operator on Liouville space.
"""
rho_3 = rand_dm(3)
superop = kraus_to_super(rand_kraus_map(3))
analytic_result = vec2mat(superop.data.todense() *
mat2vec(rho_3.data.todense()))
naive_result = subsystem_apply(rho_3, superop, [True],
reference=True)
naive_diff = (analytic_result - naive_result).data.todense()
assert_(norm(naive_diff) < 1e-12)
efficient_result = subsystem_apply(rho_3, superop, [True])
efficient_diff = (efficient_result - analytic_result).data.todense()
assert_(norm(efficient_diff) < 1e-12)
def test_ComplexSingleApply(self):
"""
Composite system, operator on Hilbert space.
"""
rho_list = list(map(rand_dm, [2, 3, 2, 3, 2]))
rho_input = tensor(rho_list)
single_op = rand_unitary(3)
analytic_result = rho_list
analytic_result[1] = single_op * analytic_result[1] * single_op.dag()
analytic_result[3] = single_op * analytic_result[3] * single_op.dag()
analytic_result = tensor(analytic_result)
naive_result = subsystem_apply(rho_input, single_op,
[False, True, False, True, False],
reference=True)
naive_diff = (analytic_result - naive_result).data.todense()
assert_(norm(naive_diff) < 1e-12)
efficient_result = subsystem_apply(rho_input, single_op,
[False, True, False, True, False])
efficient_diff = (efficient_result - analytic_result).data.todense()
assert_(norm(efficient_diff) < 1e-12)
def test_ComplexSuperApply(self):
"""
Superoperator: Efficient numerics and reference return same result,
acting on non-composite system
"""
rho_list = list(map(rand_dm, [2, 3, 2, 3, 2]))
rho_input = tensor(rho_list)
superop = kraus_to_super(rand_kraus_map(3))
analytic_result = rho_list
analytic_result[1] = Qobj(vec2mat(superop.data.todense() *
mat2vec(analytic_result[1].data.todense())))
analytic_result[3] = Qobj(vec2mat(superop.data.todense() *
mat2vec(analytic_result[3].data.todense())))
analytic_result = tensor(analytic_result)
naive_result = subsystem_apply(rho_input, superop,
[False, True, False, True, False],
reference=True)
naive_diff = (analytic_result - naive_result).data.todense()
assert_(norm(naive_diff) < 1e-12)
efficient_result = subsystem_apply(rho_input, superop,
[False, True, False, True, False])
efficient_diff = (efficient_result - analytic_result).data.todense()
assert_(norm(efficient_diff) < 1e-12)
if __name__ == "__main__":
run_module_suite()