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

fix issue 432 #435

Merged
merged 4 commits into from
Apr 12, 2024
Merged

fix issue 432 #435

merged 4 commits into from
Apr 12, 2024

Conversation

GiggleLiu
Copy link
Member

@GiggleLiu GiggleLiu commented Oct 28, 2022

fix #432

Change List

  • New exported API sandwich in YaoBlocks for computing <bra| op |ket>.
  • expect function now returns real and errors on complex values.
  • The multiplication <bra| * |ket> now returns a number, and throws an error if the qubit number does not match.

Fixes the type stability issue

julia> using Yao
[ Info: Precompiling Yao [5872b779-8223-5990-8dd0-5abbb0748c8c]

julia> nq = 2
2

julia> O1 = put(nq, 1 => X)
nqubits: 2
put on (1)
└─ X

julia> reg = rand_state(nq)
ArrayReg{2, ComplexF64, Array...}
    active qubits: 2/2
    nlevel: 2

julia> @code_warntype expect(O1, reg)
MethodInstance for YaoAPI.expect(::PutBlock{2, 1, XGate}, ::ArrayReg{2, ComplexF64, Matrix{ComplexF64}})
  from expect(op::AbstractBlock, reg::AbstractRegister) in YaoBlocks at /home/leo/.julia/dev/Yao/lib/YaoBlocks/src/blocktools.jl:95
Arguments
  #self#::Core.Const(YaoAPI.expect)
  op::PutBlock{2, 1, XGate}
  reg::ArrayReg{2, ComplexF64, Matrix{ComplexF64}}
Body::Float64
1%1 = YaoBlocks.sandwich(reg, op, reg)::ComplexF64%2 = Base.broadcasted(YaoBlocks.safe_real, %1)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(YaoBlocks.safe_real), Tuple{ComplexF64}}%3 = Base.materialize(%2)::Float64
└──      return %3

Examples

julia> expect(O1, reg)
0.05754647503710675

julia> sandwich(reg, O1, reg)
0.05754647503710675 + 0.0im

Note

For expect(O1 + O2, reg), the code is still type unstable, this is due to the Add block contains a vector of abstract type.

@codecov
Copy link

codecov bot commented Oct 28, 2022

Codecov Report

Attention: Patch coverage is 96.87500% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 88.31%. Comparing base (e17a32b) to head (430123e).
Report is 6 commits behind head on master.

❗ Current head 430123e differs from pull request most recent head f70f095. Consider uploading reports for the commit f70f095 to get more accurate results

Files Patch % Lines
lib/YaoBlocks/src/utils.jl 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #435      +/-   ##
==========================================
- Coverage   89.03%   88.31%   -0.73%     
==========================================
  Files          83       76       -7     
  Lines        4835     4765      -70     
==========================================
- Hits         4305     4208      -97     
- Misses        530      557      +27     

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

@GiggleLiu GiggleLiu requested a review from Roger-luo October 28, 2022 03:52
Copy link
Member

@Roger-luo Roger-luo left a comment

Choose a reason for hiding this comment

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

LGTM in general, only minor changes is needed.

lib/YaoArrayRegister/src/operations.jl Show resolved Hide resolved
lib/YaoArrayRegister/src/operations.jl Outdated Show resolved Hide resolved
lib/YaoBlocks/src/utils.jl Show resolved Hide resolved
lib/YaoBlocks/src/utils.jl Show resolved Hide resolved
@GiggleLiu
Copy link
Member Author

I think this PR should be merged. Automatic differentiating complex numbers does not make sense.

@GiggleLiu GiggleLiu merged commit 8e878b5 into master Apr 12, 2024
19 of 22 checks passed
@GiggleLiu GiggleLiu deleted the jg/fix432 branch April 12, 2024 03:22
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.

expect(::AbstractBlock, ::ArrayReg) is not recognized as typestable
2 participants