Skip to content

Commit

Permalink
Fix expect method with LazyTensor and LazyKets
Browse files Browse the repository at this point in the history
  • Loading branch information
david-pl committed Feb 21, 2024
1 parent 376274b commit 9aacf70
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/states_lazyket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ function expect(op::LazyTensor{B, B}, state::LazyKet{B}) where B <: Basis

T = promote_type(eltype(op), eltype(state))
exp_val = convert(T, op.factor)

# loop over all operators and match with corresponding kets
for (i, op_) in enumerate(op.operators)
exp_val *= expect(op_, kets[inds[i]])
end

# loop over remaining kets and just add the norm (should be one for normalized ones, but hey, who knows..)
for i in 1:length(kets)
if i inds
exp_val *= expect(ops[i], kets[i])
else
exp_val *= dot(kets[i], kets[i])
if i inds
exp_val *= dot(kets[i].data, kets[i].data)
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/test_states.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,8 @@ op_prod = rand(ComplexF64) * LazyProduct(op, op)
op_nested = rand(ComplexF64) * LazySum(op_prod, op)
@test expect(op_nested, ψ) expect(dense(op_prod), Ket(ψ)) * op_nested.factors[1] + expect(dense(op), Ket(ψ)) * op_nested.factors[2]

# test lazytensor with missing indices
op = rand(ComplexF64) * LazyTensor(b, b, (1, 3), (op1, op3))
@test expect(op, ψ) expect(sparse(op), Ket(ψ))

end

0 comments on commit 9aacf70

Please sign in to comment.