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

Change the type of dims for QuantumObject to SVector for type stabilities #217

Merged
merged 15 commits into from
Sep 8, 2024

Conversation

albertomercurio
Copy link
Member

Description

With this PR I made a lot of changes, because I basically changed the internal structure of the QuantumObject constructor.

I changed the type of the dims field from Vector{Int} to SVector{N,Int}, where N is the number of subsystems (e.g., two qubits, a qubit and a cavity….).

This change would fix possible type instabilities for functions like ptrace, where the temporary reshaped array depends on the length of the dims field.

Being a static fixed-length vector, the length of the dims field is already known at compile time, improving the performances.

It would also improve performances on functions dealing with dims, for example dfd_mesolve, where the Hilbert space dimension is changed dynamically during a time evolution.

Important

I kept the support for Vector as input, generating a warning string, and then converting it into a SVector

This PR is just a starting point. Here everything works. In the next PRs I will try to optimize the code to take advantage of the power of SVectors.

Copy link

codecov bot commented Sep 7, 2024

Codecov Report

Attention: Patch coverage is 98.56115% with 2 lines in your changes missing coverage. Please review.

Project coverage is 93.40%. Comparing base (6579bc1) to head (3be1f68).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/qobj/quantum_object.jl 93.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #217      +/-   ##
==========================================
- Coverage   94.24%   93.40%   -0.85%     
==========================================
  Files          29       29              
  Lines        2171     2197      +26     
==========================================
+ Hits         2046     2052       +6     
- Misses        125      145      +20     

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

@albertomercurio
Copy link
Member Author

As an example, I was doing some benchmarks comparing the current implementation of ptrace and the one with SVector.

This is the result with SVector

julia> g = fock(2, 1)
Quantum Object:   type=Ket   dims=[2]   size=(2,)
2-element Vector{ComplexF64}:
 0.0 + 0.0im
 1.0 + 0.0im

julia> e = fock(2, 0)
Quantum Object:   type=Ket   dims=[2]   size=(2,)
2-element Vector{ComplexF64}:
 1.0 + 0.0im
 0.0 + 0.0im

julia> state = normalize(kron(g, e) + kron(e, g))
Quantum Object:   type=Ket   dims=[2, 2]   size=(4,)
4-element Vector{ComplexF64}:
                0.0 + 0.0im
 0.7071067811865475 + 0.0im
 0.7071067811865475 + 0.0im
                0.0 + 0.0im

julia> ptrace(state, 1)
Quantum Object:   type=Operator   dims=[2]   size=(2, 2)   ishermitian=true
2×2 Matrix{ComplexF64}:
 0.5+0.0im  0.0+0.0im
 0.0+0.0im  0.5+0.0im

julia> @code_warntype ptrace(state, 1)
MethodInstance for QuantumToolbox.ptrace(::QuantumObject{Vector{ComplexF64}, KetQuantumObject, 2}, ::Int64)
  from ptrace(QO::QuantumObject, sel::Int64) @ QuantumToolbox ~/.julia/dev/QuantumToolbox/src/qobj/arithmetic_and_attributes.jl:529
Arguments
  #self#::Core.Const(QuantumToolbox.ptrace)
  QO::QuantumObject{Vector{ComplexF64}, KetQuantumObject, 2}
  sel::Int64
Body::Union{QuantumObject{Matrix{ComplexF64}, BraQuantumObject, 1}, QuantumObject{Matrix{ComplexF64}, OperatorQuantumObject, 1}}
1 ─ %1 = QuantumToolbox.SVector(sel)::StaticArraysCore.SVector{1, Int64}
│   %2 = QuantumToolbox.ptrace(QO, %1)::Union{QuantumObject{Matrix{ComplexF64}, BraQuantumObject, 1}, QuantumObject{Matrix{ComplexF64}, OperatorQuantumObject, 1}}
└──      return %2


julia> using BenchmarkTools

julia> @benchmark ptrace($state, 1)
BenchmarkTools.Trial: 10000 samples with 240 evaluations.
 Range (min … max):  306.850 ns … 208.118 μs  ┊ GC (min … max):  0.00% … 99.70%
 Time  (median):     320.371 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   385.384 ns ±   2.132 μs  ┊ GC (mean ± σ):  11.99% ±  5.47%

   ▂▅▄█▆▁                                                        
  ▂██████▇▄▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▃▄▃▂▂▁ ▂
  307 ns           Histogram: frequency by time          469 ns <

 Memory estimate: 736 bytes, allocs estimate: 13.

And this is with the latest stable version

julia> @benchmark ptrace($state, 1)
BenchmarkTools.Trial: 10000 samples with 40 evaluations.
 Range (min … max):  903.350 ns …   4.131 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):       1.137 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):     1.114 μs ± 126.638 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

     ▃▃                              ▂▄▆▇█▇▅▄▂▁                  
  ▂▃▇██▇▄▃▂▂▂▂▂▂▂▂▂▂▁▂▁▂▁▂▁▂▂▂▂▃▃▄▅▇███████████▇▆▅▅▄▄▃▃▃▃▃▂▂▂▂▂ ▄
  903 ns           Histogram: frequency by time         1.27 μs <

 Memory estimate: 1.09 KiB, allocs estimate: 20.

Almost 4 times slower.

docs/make.jl Show resolved Hide resolved
src/QuantumToolbox.jl Outdated Show resolved Hide resolved
test/quantum_objects.jl Outdated Show resolved Hide resolved
test/quantum_objects.jl Outdated Show resolved Hide resolved
test/quantum_objects.jl Outdated Show resolved Hide resolved
test/quantum_objects.jl Outdated Show resolved Hide resolved
@albertomercurio albertomercurio merged commit 4d852a1 into qutip:main Sep 8, 2024
12 of 13 checks passed
albertomercurio added a commit to lgravina1997/QuantumToolbox.jl that referenced this pull request Sep 27, 2024
…tabilities (qutip#217)

* Improve c_ops handling

* Format code

* Change dims to SVector

* Fix JET errors

* Fix dfd_mesolve issues

* Fix entanglement issues

* Fix low rank dynamics

* Fix all tests

* Format code

* Change to StaticArraysCore.jl and minor changes

* Make a few comments on the documentation

* Update some docstring

* Minor changes

* FIx error on Documentation
@albertomercurio albertomercurio deleted the dev/tuple-dims branch September 28, 2024 21:47
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.

2 participants