Skip to content

Commit a49249f

Browse files
Fixed PEP8 formatting
- replaced %time with qutip internal time measurement - shortened many lines - fixed typos
1 parent c377071 commit a49249f

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

tutorials-v4/miscellaneous/excitation-number-restricted-states-jc-chain.md

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ jupyter:
55
extension: .md
66
format_name: markdown
77
format_version: '1.3'
8-
jupytext_version: 1.16.1
8+
jupytext_version: 1.13.8
99
kernelspec:
10-
display_name: Python 3 (ipykernel)
10+
display_name: qutip-tutorials
1111
language: python
1212
name: python3
1313
---
@@ -58,21 +58,9 @@ To find all available functions to work with ENR states see [Energy Restricted O
5858

5959
```python
6060
import numpy as np
61-
from qutip import (
62-
basis,
63-
tensor,
64-
identity,
65-
destroy,
66-
mesolve,
67-
liouvillian_ref,
68-
plot_expectation_values,
69-
enr_destroy,
70-
enr_fock,
71-
enr_state_dictionaries,
72-
Qobj,
73-
Options,
74-
about,
75-
)
61+
from qutip import (Options, Qobj, about, basis, destroy, enr_destroy, enr_fock,
62+
enr_state_dictionaries, identity, liouvillian_ref, mesolve,
63+
plot_expectation_values, tensor)
7664

7765
%matplotlib inline
7866
```
@@ -116,7 +104,7 @@ def solve(d, psi0):
116104
# atomic annihilation operators
117105
sm = d[1::2]
118106

119-
# when constructing the Hamiltonian notice the ordering of annihilation and creation operators (see introduction)
107+
# notice the ordering of annihilation and creation operators
120108
H0 = sum([aa.dag() * aa for aa in a]) + sum([s.dag() * s for s in sm])
121109

122110
# atom-cavity couplings
@@ -136,9 +124,8 @@ def solve(d, psi0):
136124

137125
times = np.linspace(0, 250, 1000)
138126
L = liouvillian_ref(H, c_ops)
139-
result = mesolve(
140-
H, psi0, times, c_ops, e_ops, options=Options(nsteps=5000, store_states=True)
141-
)
127+
opt = Options(nsteps=5000, store_states=True)
128+
result = mesolve(H, psi0, times, c_ops, e_ops, options=opt)
142129
return result, H, L
143130
```
144131

@@ -172,27 +159,28 @@ d[0].dag() * d[1] == d[1] * d[0].dag()
172159
Solving the time evolution:
173160

174161
```python
175-
%time res1, H1, L1 = solve(d, psi0)
162+
res1, H1, L1 = solve(d, psi0)
176163
```
177164

178165
### Using ENR States and Operators
179166

180167
```python
181-
d = enr_destroy(dims, excite)
182-
psi0 = enr_fock(dims, excite, [init_excite if n == 1 else 0 for n in range(2 * N)])
168+
d_enr = enr_destroy(dims, excite)
169+
init_enr = [init_excite if n == 1 else 0 for n in range(2 * N)]
170+
psi0_enr = enr_fock(dims, excite, init_enr)
183171
```
184172

185173
Using ENR states forces us to give up on the standard tensor structure of multiple Hilbert spaces.
186174
Operators for different systems therefore generally no longer commute:
187175

188176
```python
189-
d[0].dag() * d[1] == d[1] * d[0].dag()
177+
d_enr[0].dag() * d_enr[1] == d_enr[1] * d_enr[0].dag()
190178
```
191179

192180
Solving the time evolution:
193181

194182
```python
195-
%time res2, H2, L2 = solve(d, psi0)
183+
res2, H2, L2 = solve(d_enr, psi0_enr)
196184
```
197185

198186
### Comparison of Expectation Values
@@ -237,8 +225,10 @@ def ENR_ptrace(rho, sel, excitations):
237225
rest = np.setdiff1d(np.arange(len(drho)), sel)
238226
for state in idx2state:
239227
for state2 in idx2state:
240-
# if the parts of the states of the system(s) being traced out are diagonal, add this to the new DM
241-
if np.all(np.asarray(state).take(rest) == np.asarray(state2).take(rest)):
228+
# add diagonal elements to the new density matrix
229+
state_red = np.asarray(state).take(rest)
230+
state2_red = np.asarray(state2).take(rest)
231+
if np.all(state_red == state2_red):
242232
rhout[
243233
state2idx2[tuple(np.asarray(state).take(sel))],
244234
state2idx2[tuple(np.asarray(state2).take(sel))],

tutorials-v5/miscellaneous/excitation-number-restricted-states-jc-chain.md

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ jupyter:
55
extension: .md
66
format_name: markdown
77
format_version: '1.3'
8-
jupytext_version: 1.16.1
8+
jupytext_version: 1.13.8
99
kernelspec:
10-
display_name: Python 3 (ipykernel)
10+
display_name: qutip-tutorials-v5
1111
language: python
1212
name: python3
1313
---
@@ -43,7 +43,7 @@ a1, a2, a3, a4 = enr_destroy([5, 5, 5, 5], excitations=2)
4343
```
4444

4545
creates destruction operators for each mode.
46-
From this point onwards, the annihiltion operators a1, ..., a4 can be used to setup a Hamiltonian, collapse operators and expectation-value operators, etc., following the usual patterne.
46+
From this point onwards, the annihiltion operators a1, ..., a4 can be used to setup a Hamiltonian, collapse operators and expectation-value operators, etc., following the usual pattern.
4747

4848
In this example we outline the advantage of ENR states by comparing them with the regular qutip implementation.
4949
For this we calculate the time evolution and the partial trace for each and see consistent results with notable performance improvements.
@@ -58,20 +58,9 @@ To find all available functions to work with ENR states see [Energy Restricted O
5858

5959
```python
6060
import numpy as np
61-
from qutip import (
62-
basis,
63-
tensor,
64-
identity,
65-
destroy,
66-
mesolve,
67-
liouvillian,
68-
plot_expectation_values,
69-
enr_destroy,
70-
enr_fock,
71-
enr_state_dictionaries,
72-
Qobj,
73-
about,
74-
)
61+
from qutip import (Qobj, about, basis, destroy, enr_destroy, enr_fock,
62+
enr_state_dictionaries, identity, liouvillian, mesolve,
63+
plot_expectation_values, tensor)
7564
from qutip.core.energy_restricted import EnrSpace
7665

7766
%matplotlib inline
@@ -116,7 +105,7 @@ def solve(d, psi0):
116105
# atomic annihilation operators
117106
sm = d[1::2]
118107

119-
# when constructing the Hamiltonian notice the ordering of annihilation and creation operators (see introduction)
108+
# notice the ordering of annihilation and creation operators
120109
H0 = sum([aa.dag() * aa for aa in a]) + sum([s.dag() * s for s in sm])
121110

122111
# atom-cavity couplings
@@ -136,9 +125,8 @@ def solve(d, psi0):
136125

137126
times = np.linspace(0, 250, 1000)
138127
L = liouvillian(H, c_ops)
139-
result = mesolve(
140-
H, psi0, times, c_ops, e_ops, options={"nsteps": 5000, "store_states": True}
141-
)
128+
opt = {"nsteps": 5000, "store_states": True}
129+
result = mesolve(H, psi0, times, c_ops, e_ops, options=opt)
142130
return result, H, L
143131
```
144132

@@ -172,27 +160,30 @@ d[0].dag() * d[1] == d[1] * d[0].dag()
172160
Solving the time evolution:
173161

174162
```python
175-
%time res1, H1, L1 = solve(d, psi0)
163+
res1, H1, L1 = solve(d, psi0)
164+
print(f"Run time: {res1.stats['run time']}s")
176165
```
177166

178167
### Using ENR States and Operators
179168

180169
```python
181-
d = enr_destroy(dims, excite)
182-
psi0 = enr_fock(dims, excite, [init_excite if n == 1 else 0 for n in range(2 * N)])
170+
d_enr = enr_destroy(dims, excite)
171+
init_enr = [init_excite if n == 1 else 0 for n in range(2 * N)]
172+
psi0_enr = enr_fock(dims, excite, init_enr)
183173
```
184174

185175
Using ENR states forces us to give up on the standard tensor structure of multiple Hilbert spaces.
186176
Operators for different systems therefore generally no longer commute:
187177

188178
```python
189-
d[0].dag() * d[1] == d[1] * d[0].dag()
179+
d_enr[0].dag() * d_enr[1] == d_enr[1] * d_enr[0].dag()
190180
```
191181

192182
Solving the time evolution:
193183

194184
```python
195-
%time res2, H2, L2 = solve(d, psi0)
185+
res2, H2, L2 = solve(d_enr, psi0_enr)
186+
print(f"Run time: {res2.stats['run time']}s")
196187
```
197188

198189
### Comparison of Expectation Values
@@ -239,8 +230,10 @@ def ENR_ptrace(rho, sel, excitations):
239230
rest = np.setdiff1d(np.arange(len(drho)), sel)
240231
for state in state2idx:
241232
for state2 in state2idx:
242-
# if the parts of the states of the systems(s) being traced out are diagonal, add this to the new DM
243-
if np.all(np.asarray(state).take(rest) == np.asarray(state2).take(rest)):
233+
# add diagonal elements to the new density matrix
234+
state_red = np.asarray(state).take(rest)
235+
state2_red = np.asarray(state2).take(rest)
236+
if np.all(state_red == state2_red):
244237
rhout[
245238
state2idx2[tuple(np.asarray(state).take(sel))],
246239
state2idx2[tuple(np.asarray(state2).take(sel))],

0 commit comments

Comments
 (0)