Sie sind auf Seite 1von 5

Spin Chain: a quantum fantasy for two pianos

Oscar Riveros
November 06, 2013

Part I

Tutorial 03
Algorithmic Composition with Python and Music21 http://twitter.com/maxtuno http://soundcloud.com/maxtuno http://www.reverbnation.com/maxtuno http://mx-clojure.blogspot.com/ http://googledrive.com/host/0B436L4jac0r_VWZfbXBkUHVpV3M/ http://soundcloud.com/maxtuno/spin-chain-by-oscar-riveros A quantum fantasy for two pianos (Includes credits and references QuTip, Im just the music author)

1 QuTiP example: dynamics of a spin chain


J.R. Johansson and P.D. Nation For more information about QuTiP see http://qutip.org
In [29]: %pylab --no-import-all inline

Populating the interactive namespace from numpy and matplotlib


In [30]: from music21 import * from qutip import * from pylab import * import time

Hamiltonian: H= 1 2
N

hn z (n)
n

1 2

N 1 (n) ( n) (n) [Jx x (n)x (n + 1) + Jy y (n)y (n + 1) + Jz z (n)z (n + 1)] n

In [31]: def integrate(N, h, Jx, Jy, Jz, psi0, tlist, gamma, solver): si sx sy sz = = = = qeye(2) sigmax() sigmay() sigmaz()

sx_list = [] sy_list = [] sz_list = [] for n in range(N): op_list = [] for m in range(N): op_list.append(si) op_list[n] = sx sx_list.append(tensor(op_list)) op_list[n] = sy sy_list.append(tensor(op_list)) op_list[n] = sz sz_list.append(tensor(op_list)) # construct the hamiltonian H = 0 # energy splitting terms for n in range(N): H += - 0.5 * h[n] * sz_list[n] # interaction terms for n in range(N-1): H += - 0.5 * Jx[n] * sx_list[n] * sx_list[n+1] H += - 0.5 * Jy[n] * sy_list[n] * sy_list[n+1] H += - 0.5 * Jz[n] * sz_list[n] * sz_list[n+1] # collapse operators c_op_list = [] # spin dephasing for n in range(N): if gamma[n] > 0.0: c_op_list.append(sqrt(gamma[n]) * sz_list[n]) # evolve and calculate expectation values if solver == "me": result = mesolve(H, psi0, tlist, c_op_list, sz_list) elif solver == "mc": ntraj = 250 result = mcsolve(H, psi0, tlist, c_op_list, sz_list, ntraj) return result.expect

In [32]: # # set up the calculation # #solver = "me" # use the ode solver solver = "mc" # use the monte-carlo solver

N = 4

# number of spins

# array of spin energy splittings and coupling strengths. here we use # uniform parameters, but in general we dont have too h = 1.0 * 2 * pi * ones(N) Jz = 0.1 * 2 * pi * ones(N) Jx = 0.1 * 2 * pi * ones(N) Jy = 0.1 * 2 * pi * ones(N) # dephasing rate gamma = 0.01 * ones(N) # intial state, first spin in state |1>, the rest in state |0> psi_list = [] psi_list.append(basis(2,1)) for n in range(N-1): psi_list.append(basis(2,0)) psi0 = tensor(psi_list) tlist = linspace(0, 25, 100) start_time = time.time() sz_expt = integrate(N, h, Jx, Jy, Jz, psi0, tlist, gamma, solver) print(time elapsed = + str(time.time() - start_time))

10.0% (25/250) Est. time remaining: 00:00:01:21 20.0% (50/250) Est. time remaining: 00:00:01:20 30.0% (76/250) Est. time remaining: 00:00:01:11 40.0% (100/250) Est. time remaining: 00:00:01:05 50.0% (125/250) Est. time remaining: 00:00:00:57 60.0% (150/250) Est. time remaining: 00:00:00:45 70.0% (175/250) Est. time remaining: 00:00:00:34 80.0% (200/250) Est. time remaining: 00:00:00:22 90.0% (225/250) Est. time remaining: 00:00:00:11 100.0% (250/250) Est. time remaining: 00:00:00:00 time elapsed = 107.066267967
In [33]: # # plot # rc(text, usetex=True) rc(font, family=serif) figure(figsize=(10,6)) for n in range(N): plot(tlist, real(sz_expt[n]), label=r$\langle\sigma_z^{(%d)}\rangle$%n) legend(loc=0) xlabel(rTime [ns]) ylabel(r\langle\sigma_z\rangle) title(rDynamics of a Heisenberg spin chain);

2 Versions
In [34]: from qutip.ipynbtools import version_table version_table() Out [34]: <IPython.core.display.HTML at 0x1068cc510>

3 Let there be Music. And there was music . . .


In [35]: stream_1 stream_2 stream_3 stream_4 = = = = stream.Stream(); stream.Stream(); stream.Stream(); stream.Stream();

In [36]: def discrete_oarr(theta, low_note, high_note): return [low_note + (high_note - low_note) * abs(theta), 127 * abs(theta), In [37]: pitchesList_1 pitchesList_2 pitchesList_3 pitchesList_4 = = = = map(lambda map(lambda map(lambda map(lambda x: x: x: x: discrete_oarr(x discrete_oarr(x discrete_oarr(x discrete_oarr(x , , , , 55, 55, 48, 36, 103), real(sz_expt[0])) 103), real(sz_expt[1])) 91), real(sz_expt[2])) 76), real(sz_expt[3]))

round(ab

In [38]: [stream_1.append(note.Note(midi=pitch[0], [stream_2.append(note.Note(midi=pitch[0], [stream_3.append(note.Note(midi=pitch[0], [stream_4.append(note.Note(midi=pitch[0],

velocity=pitch[1], velocity=pitch[1], velocity=pitch[1], velocity=pitch[1],

quarterLength=pitch[2])) quarterLength=pitch[2])) quarterLength=pitch[2])) quarterLength=pitch[2]))

f f f f

In [39]: score = stream.Stream() score.insert(0, stream_1) score.insert(0, stream_2) score.insert(0, stream_3) score.insert(0, stream_4)

In [40]: score.write(midi) Out [40]: /var/folders/4t/54tv_bvd6kz28x8zwhy3mcp80000gn/T/music21/tmpEQwJzg.mi

d
In [253]:

Das könnte Ihnen auch gefallen