Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some usability improvements for project_traceout!() #35

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the problem is that the symbolic basis states like Z1, X2 etc aren't direct subtypes of Ket, but SpecialKet here.
Should I just replace it with

function project_traceout!(state::Union{Ket,Operator},stateindex,psis::Base.AbstractVecOrTuple{<:Union{Ket, QuantumSymbolics.SpecialKet}})

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))