forked from qutip/qutip
/
test_wigner.py
179 lines (124 loc) · 5.02 KB
/
test_wigner.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
179
# This file is part of QuTiP.
#
# QuTiP is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# QuTiP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with QuTiP. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011 and later, Paul D. Nation & Robert J. Johansson
#
###########################################################################
from numpy import linspace, sum, abs
from numpy.testing import assert_, run_module_suite, assert_equal
from qutip import *
from scipy.special import laguerre
from numpy.random import rand
def test_wigner_coherent():
"wigner: test wigner function calculation for coherent states"
xvec = linspace(-5.0, 5.0, 100)
yvec = xvec
X, Y = meshgrid(xvec, yvec)
a = X + 1j * Y # consistent with g=2 option to wigner function
dx = xvec[1] - xvec[0]
dy = yvec[1] - yvec[0]
N = 20
beta = rand() + rand() * 1.0j
psi = coherent(N, beta)
# calculate the wigner function using qutip and analytic formula
W_qutip = wigner(psi, xvec, yvec, g=2)
W_analytic = 2 / pi * exp(-2 * abs(a - beta) ** 2)
# check difference
assert_(sum(abs(W_qutip - W_analytic) ** 2) < 1e-4)
# check normalization
assert_(sum(W_qutip) * dx * dy - 1.0 < 1e-8)
assert_(sum(W_analytic) * dx * dy - 1.0 < 1e-8)
def test_wigner_fock():
"wigner: test wigner function calculation for Fock states"
xvec = linspace(-5.0, 5.0, 100)
yvec = xvec
X, Y = meshgrid(xvec, yvec)
a = X + 1j * Y # consistent with g=2 option to wigner function
dx = xvec[1] - xvec[0]
dy = yvec[1] - yvec[0]
N = 15
for n in [2, 3, 4, 5, 6]:
psi = fock(N, n)
# calculate the wigner function using qutip and analytic formula
W_qutip = wigner(psi, xvec, yvec, g=2)
W_analytic = 2 / pi * (-1) ** n * exp(-2 * abs(a) ** 2) * polyval(
laguerre(n), 4 * abs(a) ** 2)
# check difference
assert_(sum(abs(W_qutip - W_analytic)) < 1e-4)
# check normalization
assert_(sum(W_qutip) * dx * dy - 1.0 < 1e-8)
assert_(sum(W_analytic) * dx * dy - 1.0 < 1e-8)
def test_wigner_compare_methods_dm():
"wigner: compare wigner methods for random density matrices"
xvec = linspace(-5.0, 5.0, 100)
yvec = xvec
X, Y = meshgrid(xvec, yvec)
a = X + 1j * Y # consistent with g=2 option to wigner function
dx = xvec[1] - xvec[0]
dy = yvec[1] - yvec[0]
N = 15
for n in range(10):
# try ten different random density matrices
rho = rand_dm(N, 0.5 + rand() / 2)
# calculate the wigner function using qutip and analytic formula
W_qutip1 = wigner(rho, xvec, yvec, g=2)
W_qutip2 = wigner(rho, xvec, yvec, g=2, method='laguerre')
# check difference
assert_(sum(abs(W_qutip1 - W_qutip1)) < 1e-4)
# check normalization
assert_(sum(W_qutip1) * dx * dy - 1.0 < 1e-8)
assert_(sum(W_qutip2) * dx * dy - 1.0 < 1e-8)
def test_wigner_compare_methods_ket():
"wigner: compare wigner methods for random state vectors"
xvec = linspace(-5.0, 5.0, 100)
yvec = xvec
X, Y = meshgrid(xvec, yvec)
a = X + 1j * Y # consistent with g=2 option to wigner function
dx = xvec[1] - xvec[0]
dy = yvec[1] - yvec[0]
N = 15
for n in range(10):
# try ten different random density matrices
psi = rand_ket(N, 0.5 + rand() / 2)
# calculate the wigner function using qutip and analytic formula
W_qutip1 = wigner(psi, xvec, yvec, g=2)
W_qutip2 = wigner(psi, xvec, yvec, g=2, method='laguerre')
# check difference
assert_(sum(abs(W_qutip1 - W_qutip1)) < 1e-4)
# check normalization
assert_(sum(W_qutip1) * dx * dy - 1.0 < 1e-8)
assert_(sum(W_qutip2) * dx * dy - 1.0 < 1e-8)
def test_wigner_fft_comparse_ket():
"Wigner: Compare Wigner fft and iterative for rand. ket"
N = 20
xvec = linspace(-10, 10, 128)
for i in range(3):
rho = rand_ket(N)
Wfft, yvec = wigner(rho, xvec, xvec, method='fft')
W = wigner(rho, xvec, yvec, method='iterative')
Wdiff = abs(W - Wfft)
assert_equal(sum(abs(Wdiff)) < 1e-7, True)
def test_wigner_fft_comparse_dm():
"Wigner: Compare Wigner fft and iterative for rand. dm"
N = 20
xvec = linspace(-10, 10, 128)
for i in range(3):
rho = rand_dm(N)
Wfft, yvec = wigner(rho, xvec, xvec, method='fft')
W = wigner(rho, xvec, yvec, method='iterative')
Wdiff = abs(W - Wfft)
assert_equal(sum(abs(Wdiff)) < 1e-7, True)
if __name__ == "__main__":
run_module_suite()