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

Misc #273

Closed
PhilipVinc opened this issue Jun 26, 2020 · 4 comments
Closed

Misc #273

PhilipVinc opened this issue Jun 26, 2020 · 4 comments

Comments

@PhilipVinc
Copy link
Contributor

PhilipVinc commented Jun 26, 2020

expect(op::AbstractOperator{B,B}, state::Ket{B}) where B<:Basis = state.data' * (op * state).data
expect(op::AbstractOperator{B1,B2}, state::AbstractOperator{B2,B2}) where {B1<:Basis,B2<:Basis} = tr(op*state)

we could add a specialization for data operators
which would make expect non allocating.

expect(op::DataOperator{B,B}, state::Ket{B}) where B<:Basis = dot(state.data', op.data, state.data)
expect(op:: DataOperator{B1,B2}, state::AbstractOperator{B2,B2}) where {B1<:Basis,B2<:Basis} = dot(op.data', state.data)
@david-pl
Copy link
Member

david-pl commented Jul 1, 2020

There already are specialized methods implemented: https://github.com/qojulia/QuantumOpticsBase.jl/blob/46b7693e7742095ad60a092ae23e17ffe7a56b9d/src/operators_dense.jl#L148

Or do you mean adding methods for when state::AbstractOperator ?

@PhilipVinc
Copy link
Contributor Author

yeah, I mean for a mixed state (state::AbstractOperator), currently (if I am not mistaken), the definition is
expect(op::AbstractOperator{B1,B2}, state::AbstractOperator{B2,B2}) where {B1<:Basis,B2<:Basis} = tr(op*state)

which will allocate because of the matmul.

but by being smart with linear algebra, it is in general true that tr(op*state) = dot(op.data', state.data).

@david-pl
Copy link
Member

Getting back to this now and I'm still a bit confused.

The methods for ::AbstractOperator types use the matmul definition tr(op*state) because they don't necessarily have a .data field (they can be lazy). So you can't just use dot (you'd need to implement a custom version).

For operators that do have .data fields (<:DataOperator), there are already specialized methods that don't allocate e.g. here https://github.com/qojulia/QuantumOpticsBase.jl/blob/1a432191a43490814e946803296d602c59fda37c/src/operators_dense.jl#L158 (and another one for sparse data).

Funny enough, the method for state::Ket actually wasn't using dot 🤷 Here's the fix: qojulia/QuantumOpticsBase.jl#34

Can we close this issue?

@PhilipVinc
Copy link
Contributor Author

i think so, yes. probably i had missed the specialized method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants