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

Support a list of observables for expect #376

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ytdHuang
Copy link
Member

@ytdHuang ytdHuang commented Jan 27, 2025

Checklist

Thank you for contributing to QuantumToolbox.jl! Please make sure you have finished the following tasks before opening the PR.

  • Please read Contributing to QuantumToolbox.jl.
  • Any code changes were done in a way that does not break public API.
  • Appropriate tests were added and tested locally by running: make test.
  • Any code changes should be julia formatted by running: make format.
  • All documents (in docs/ folder) related to code changes were updated and able to build locally by running: make docs.
  • (If necessary) the CHANGELOG.md should be updated (regarding to the code changes) and built by running: make changelog.

Request for a review after you have completed all the tasks. If you have not finished them all, you can also open a Draft Pull Request to let the others know this on-going work.

Description

As title

Related issues or PRs

close #374

Copy link

codecov bot commented Jan 27, 2025

Codecov Report

Attention: Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.

Project coverage is 93.32%. Comparing base (9cc0224) to head (ca79557).

Files with missing lines Patch % Lines
src/qobj/functions.jl 95.83% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #376      +/-   ##
==========================================
+ Coverage   93.18%   93.32%   +0.13%     
==========================================
  Files          43       43              
  Lines        2790     2803      +13     
==========================================
+ Hits         2600     2616      +16     
+ Misses        190      187       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

ρ::QuantumObject,
) where {DimsType<:AbstractDimensions,TF<:Number,TR<:Real} = expect.(O, Ref(ρ))
function expect(O::AbstractVector{<:AbstractQuantumObject{OperatorQuantumObject}}, ρ::QuantumObject)
result = Vector{ComplexF64}(undef, length(O))
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to preallocate a vector first? What if we directly use the broadcasting?

Copy link
Member Author

@ytdHuang ytdHuang Jan 27, 2025

Choose a reason for hiding this comment

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

Cause I want to keep the output structure similar to QuTiP (first dimension represents observable, second one represents state), and in that case, directly use the broadcasting will result in type instability

expect(
    O::AbstractVector{AbstractQuantumObject{OperatorQuantumObject}},
    ρ::AbstractVector{<:QuantumObject},
) = expect.(O, Ref(ρ))

Copy link
Member Author

@ytdHuang ytdHuang Jan 27, 2025

Choose a reason for hiding this comment

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

at least this is what I've tried and observed

In this case, if O contains Hermitian and other general Matrix, it will return AbstractVector here after broadcasting.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I think this can be fixed somehow. For the moment let's keep it as it is.

Could you just change the method definition with

expect(
    O::AbstractVector{<:AbstractQuantumObject{OperatorQuantumObject}},
    ρ::AbstractVector{<:QuantumObject},
)
...

Instead of

expect(
    O::AbstractVector{AbstractQuantumObject{OperatorQuantumObject}},
    ρ::AbstractVector{<:QuantumObject},
)
...

The first one should be more correct I think

Copy link
Member Author

@ytdHuang ytdHuang Jan 29, 2025

Choose a reason for hiding this comment

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

uhh, I think this is what I already did in the code @@ ?

src/qobj/functions.jl Show resolved Hide resolved
src/qobj/functions.jl Show resolved Hide resolved
src/qobj/functions.jl Outdated Show resolved Hide resolved
src/qobj/functions.jl Outdated Show resolved Hide resolved
src/qobj/functions.jl Show resolved Hide resolved
src/qobj/functions.jl Show resolved Hide resolved
test/core-test/quantum_objects.jl Show resolved Hide resolved
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

Successfully merging this pull request may close these issues.

Support observable list for expect
2 participants