Skip to content

Commit

Permalink
Some usability improvements for project_traceout!() (#35)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Stefan Krastanov <[email protected]>
  • Loading branch information
ba2tro and Krastanov authored Aug 29, 2023
1 parent 29d87e4 commit 09fe3aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/backends/quantumoptics/quantumoptics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ function observable(state::Union{<:Ket,<:Operator}, indices, operation)
expect(op, state)
end

function project_traceout!(state::Union{Ket,Operator},stateindex,psis::Vector{<:Ket})
function project_traceout!(state::Union{Ket,Operator},stateindex,psis::Base.AbstractVecOrTuple{Ket})
if nsubsystems(state) == 1 # TODO is there a way to do this in a single function, instead of _overlap vs _project_and_drop
_overlaps = [_overlap(psi,state) for psi in psis]
branch_probs = cumsum(_overlaps)
@assert branch_probs[end] 1.0
if !(branch_probs[end] 1.0)
throw("State not normalized. Could be due to passing wrong state to `initialize!`")
end
j = findfirst(>=(rand()), branch_probs) # TODO what if there is numerical imprecision and sum<1
j, nothing
else
Expand Down
4 changes: 4 additions & 0 deletions test/test_project_traceout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ for rep in [QuantumOpticsRepr(), CliffordRepr()]
initialize!(a[1], X1)
@test project_traceout!(a[1], σˣ) == 1
end

r = Register(1)
initialize!(r[1], Z)
@test_throws "State not normalized. Could be due to passing wrong state to `initialize!`" project_traceout!(r[1], (L0, L1))

0 comments on commit 09fe3aa

Please sign in to comment.