Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Iterator Types I
using QuBase
abstract QuantumPropagator
type Euler <: QuantumPropagator
hamiltonian::QuMatrix
init_state::QuVector
tlist::Array
Euler(hamiltonian, init_state, tlist) = new(hamiltonian, init_state, tlist)
end
# Euler(hamiltonian, init_state, tlist) = solver_euler(hamiltonian, init_state, tlist)
type EulerState
psi::QuVector
tint::Float64
end
function Base.start(prob::Euler)
return EulerState(prob.init_state, prob.tlist[1])
end
function Base.next(prob::Euler, state::EulerState)
op = prob.hamiltonian
t_list = prob.tlist
current_state = state.psi
current_t = state.tint
if current_t != t_list[end]
next_t = t_list[findfirst(t_list, current_t)+1] - current_t
next_state = (eye(op)-im*op*(t_list[findfirst(t_list, current_t)+1]-current_t))*current_state
return (next_state, next_t), EulerState(next_state, next_t)
else
return EulerState(current_state, current_t)
end
end
function Base.done(prob::Euler, state::EulerState)
if state.tint == prob.tlist[end]
return EulerState(state.psi, state.tint)
else
return "Futher iterations can be done"
end
end
#function propagate(hamiltonian, init_state, tlist)
# return Euler(hamiltonian, init_state, tlist)
#end
function initialize(hamiltonian::QuMatrix, init_state::QuVector, tlist::Array)
final_state = Array(typeof(init_state),0)
sizehint(final_state, length(tlist))
push!(final_state, init_state)
return final_state
end
function solver_euler(hamiltonian::QuMatrix, init_state::QuVector, tlist::Array)
final_state = initialize(hamiltonian, init_state, tlist)
id = eye(hamiltonian)
next_step = start(tlist)
while !done(tlist, next_step)
step, next_step = next(tlist, next_step)
if next_step == length(tlist)+1
break
else
uni = id-im*hamiltonian*(tlist[next_step]-step)
push!(final_state, uni*final_state[next_step-1])
end
end
return final_state
end
t = [0.:.1:2*pi]
v = [0.+0.9im, 0.+0.5im]
qv = QuArray(v)
# println(solver_euler(sigmax, qv, t))
x = Euler(sigmax, qv, t)
star = start(x)
println(star)
y = next(x, star)
println(y)
println(next(x, y[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment