QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.
https://yaoquantum.org
Other
924 stars 122 forks source link

specify matblock printing #386

Closed GiggleLiu closed 2 years ago

GiggleLiu commented 2 years ago

Example

julia> function rydberg_chain(nbits::Int; Ω::Number=0.0, Δ::Real=0.0, V::Real=0.0, r::Real=0.0)
              Pr = matblock(sparse([3], [3], [1.0+0im], 3, 3); nlevel=3, tag="|r⟩⟨r|")
              Z1r = matblock(sparse([2, 3], [2, 3], [1.0+0im, -1.0], 3, 3); nlevel=3)
              X1r = matblock(sparse([2, 3], [3, 2], [1.0+0im, 1.0], 3, 3); nlevel=3, tag="|1⟩⟨r| + |r⟩⟨1|")
              X01 = matblock(sparse([1, 2], [2, 1], [1.0+0im, 1.0], 3, 3); nlevel=3, tag="|1⟩⟨0| + |0⟩⟨1|")
              Y1r = matblock(sparse([3, 2], [2, 3], [1.0im, -1.0im], 3, 3); nlevel=3, tag="i|1⟩⟨0| - i|0⟩⟨1|")
              # single site term in {|1>, |r>}.
              h = Add(nbits; nlevel=3)
              !isapprox(Δ, 0; atol=1e-12) && push!(h, (-Δ) * sum([put(nbits, i=>Pr) for i=1:nbits]))
              #!iszero(Δ) && push!(h, (-Δ/2) * sum([put(nbits, i=>Z1r) for i=1:nbits]))
              !isapprox(real(Ω), 0; atol=1e-12) && push!(h, real(Ω)/2 * sum([put(nbits, i=>X1r) for i=1:nbits]))
              !isapprox(imag(Ω), 0; atol=1e-12) && push!(h, imag(Ω)/2 * sum([put(nbits, i=>Y1r) for i=1:nbits]))
              # interaction
              !isapprox(V, 0; atol=1e-12) && nbits > 1 && push!(h, V * sum([put(nbits, (i,i+1)=>kron(Pr, Pr)) for i=1:nbits-1]))
              # Raman term
              !isapprox(r, 0; atol=1e-12) && push!(h, r * sum([put(nbits, i=>X01) for i=1:nbits]))
              return h
          end

julia> h1 = rydberg_chain(2; Ω=Ω, Δ=Δf*Ω, V)
nqudits: 2
+
├─ [scale: -0.377371] +
│     ├─ put on (1)
│     │  └─ |r⟩⟨r|
│     └─ put on (2)
│        └─ |r⟩⟨r|
├─ [scale: 0.5] +
│     ├─ put on (1)
│     │  └─ |1⟩⟨r| + |r⟩⟨1|
│     └─ put on (2)
│        └─ |1⟩⟨r| + |r⟩⟨1|
└─ [scale: 1000.0] +
      └─ put on (1, 2)
         └─ kron
            ├─ 1=>|r⟩⟨r|
            └─ 2=>|r⟩⟨r|
codecov[bot] commented 2 years ago

Codecov Report

Merging #386 (c575106) into master (fc3046e) will increase coverage by 1.19%. The diff coverage is 96.77%.

@@            Coverage Diff             @@
##           master     #386      +/-   ##
==========================================
+ Coverage   87.47%   88.67%   +1.19%     
==========================================
  Files          72       73       +1     
  Lines        3849     4327     +478     
==========================================
+ Hits         3367     3837     +470     
- Misses        482      490       +8     
Impacted Files Coverage Δ
lib/YaoBlocks/src/YaoBlocks.jl 100.00% <ø> (ø)
lib/YaoBlocks/src/autodiff/mat_back.jl 91.66% <0.00%> (ø)
lib/YaoBlocks/src/composite/composite.jl 75.00% <ø> (-5.00%) :arrow_down:
lib/YaoBlocks/src/layout.jl 82.99% <ø> (ø)
lib/YaoBlocks/src/primitive/primitive.jl 100.00% <ø> (ø)
lib/YaoBlocks/src/primitive/rotation_gate.jl 91.48% <ø> (+1.74%) :arrow_up:
lib/YaoSym/src/symengine/instruct.jl 71.42% <66.66%> (ø)
lib/YaoArrayRegister/src/instruct.jl 89.80% <87.50%> (-0.05%) :arrow_down:
lib/YaoBlocks/src/composite/tag/dagger.jl 86.36% <92.85%> (+11.36%) :arrow_up:
lib/YaoArrayRegister/src/qudit_instruct.jl 95.55% <95.55%> (ø)
... and 27 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 058a156...c575106. Read the comment docs.

Roger-luo commented 2 years ago

I like this feature, LGTM