<jupyter_start><jupyter_text>Cutiepy*  "Cu" stands for Quantum  "tie" stands for Tools \* provisional name  Solving Linear ODEsYou need two tools:- Linear Algebra Libraries- ODE solvers  Numpy for LinAlg, Scipy for ODE solving`zvode` solver used internally (1980s-1990s)small (N&lt;20)big (N&gt;100)time dependent    **Bad** - Severe overhead from python calls    **Depends** - Оverhead from memory allocation (can be worked around)    **Depends** - Any python function works, but python functions are very slow<jupyter_code>ts = np.linspace(...)
op = -1j*np.array(...)
f_prime = lambda t, state: op*(... t).dot(state)
state0_num = np.array(...)                                             
ode = scipy.integrate.ode(f_prime) ...
res = [state0_num]                                                              
for t in ts[1:]:                                                             
    ode.integrate(t)                                                            
    res.append(ode.y)                                                           
print(res[-1])<jupyter_output><empty_output><jupyter_text>Qutip`zvode` (from Scipy) is used as well, but linear algebra goes to `cython` (most of the time)small (N&lt;20)big (N&gt;100)time dependent    **Good** - Noticeable overhead from cython/python calls    **Good** - Minor overhead from cython/python calls    **Bad** - Severely limited in scope and Inconsistent<jupyter_code>qutip.sesolve(H,init,ts,[])<jupyter_output><empty_output><jupyter_text>if timedependent, `H` has to be:- a python function for `sesolve`- a list of lists (not tuples!?) for `mesolve` and `mcsolve`- only recent versions try to bring some consistency to the interface (not yet done) To run $\hat{H} = \hat{H}_0 + \operatorname{sin}(t)\hat{H}_1$ you write<jupyter_code>H = [H0, [H1, 'sin(t)']]<jupyter_output><empty_output><jupyter_text>Theano- no built-in solver, but you can use (again) `zvode` (from Scipy)- linear algebra is done internally with various optimizations (including in-place operations)- bad sparse matrix support (getting better)small (N&lt;20)big (N&gt;100)time dependent    **Bad** - Severe overhead from theano/python calls    **Good** - Minor overhead from theano/python calls    **Great** - Symbolic optimizations (and in-place operations!)  Summary- Everybody uses the old `zvode` through `scipy` (if it has an ODE solver at all).    - bad because if the slow detour through python- Except `Theano` time dependence awkward if supported at all. small (N&lt;20)big (N&gt;100)time dependentNumpy/Scipy    **Bad** - Severe overhead from python calls    **Depends** - Оverhead from memory allocation (can be worked around)    **Depends** - Any python function works, but python functions are very slowQutip    **Good** - Noticeable overhead from cython/python calls    **Good** - Minor overhead from cython/python calls    **Bad** - Severely limited in scope and InconsistentTheano    **Bad** - Severe overhead from theano/python calls    **Good** - Minor overhead from theano/python calls    **Great** - Symbolic optimizations (and in-place operations!)Cutiepy    ???  CutiepyHaving short simple code was as important as making it fast.- code generator (a simple one, <500 lines of code)- symbolic algebra module (also a simple one, <1000 lines of code)  Symbolic Expressions<jupyter_code>from cutiepy import *
k = Ket('psi', 4)
k
H = Operator('H', 4)
H
tensor(H,H)
k2 = Ket('psi_2', [2,2])
k2
sin(2*t)*H*k<jupyter_output><empty_output><jupyter_text>Numerical Content<jupyter_code>import numpy as np
Ket('phi', 4, np.array([0,0,0,1], dtype=complex))<jupyter_output><empty_output><jupyter_text>Built-in Operators (I should work a bit more on this)<jupyter_code>sigmax()
num(3)<jupyter_output><empty_output><jupyter_text>Lazy evaluation of expressions<jupyter_code>expression = sigmax()*sigmay()
expression
evalf(expression)<jupyter_output><empty_output><jupyter_text>Getting the numerical content<jupyter_code>numerical(destroy(3))<jupyter_output><empty_output><jupyter_text>Code generation- All these lazy expressions can be compiled to C (with cython) for very fast evaluation.- Coupled to a modern ODE solver (in C, no python detour)- Hidden behind the `sesolve`, `mesolve`, etc. intefaces  Low Level ExampleNo need to worry about this in user code, but interesting if you want to help develop the library<jupyter_code>y = Ket.anon(2)
op = -1j*num(2)
y_prime = op*y
y_prime
f = generate_cython(y_prime, # Expression
                    NDArrayFunction(), # Type of compilation
                                       # (whether to couple to an ODE solver)
                    [y]) # Arguments for the function
codegen.DEBUG = True
cf = f.compiled()
codegen.DEBUG = False
arg = np.ones((2,1), dtype=complex)
arg
cf.pythoncall(arg)<jupyter_output><empty_output><jupyter_text>High Level Example<jupyter_code>initial_state = basis(2, 0)
initial_state
ω0 = 1
Δ = 0.002
Ω = 0.005
H = ω0/2 * sigmaz() + Ω * sigmax() * sin((ω0+Δ)*t)
H
ts = 2*np.pi/Ω*np.linspace(0,1,40)
res = sesolve(H, initial_state, ts)
%matplotlib inline
import matplotlib.pyplot as plt
σz_expect = expect(sigmaz(), res)
plt.plot(ts*Ω/np.pi, σz_expect, 'r.', label='numerical result')
Ωp = (Ω**2+Δ**2)**0.5
plt.plot(ts*Ω/np.pi, 1-(Ω/Ωp)**2*2*np.sin(Ωp*ts/2)**2, 'b-',
         label=r'$1-2(\Omega^\prime/\Omega)^2\sin^2(\Omega^\prime t/2)$')
plt.title(r'$\langle\sigma_z\rangle$-vs-$t\Omega/\pi$ at '
          r'$\Delta/\Omega=%.2f$, $\omega_0/\Omega=%.2f$'%(Δ/Ω, ω0/Ω))
plt.ylim(-1,1)
plt.legend(loc=3);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12907878132323777, 'fraction_numerical': 0.018929150892374257, 'mean_word_length': 3.615640599001664, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '269736', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1802, 'n_words': 534, 'path': 'examples/Intro_Cutiepy_Presentation.ipynb', 'repo_name': 'Krastanov/cutiepy', 'size': '5547'}