Skip to content

Commit 3ffdd3d

Browse files
Change block diagonal form structure (#349)
* Change block diagonal form structure * Add changelog
1 parent 91cf211 commit 3ffdd3d

File tree

7 files changed

+82
-47
lines changed

7 files changed

+82
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

10+
- Change the structure of block diagonalization functions, using `BlockDiagonalForm` struct and changing the function name from `bdf` to `block_diagonal_form`. ([#349])
11+
1012

1113
## [v0.24.0]
1214
Release date: 2024-12-13
@@ -50,6 +52,7 @@ Release date: 2024-11-13
5052
[v0.22.0]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.22.0
5153
[v0.23.0]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.23.0
5254
[v0.23.1]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.23.1
55+
[v0.24.0]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.24.0
5356
[#86]: https://github.com/qutip/QuantumToolbox.jl/issues/86
5457
[#139]: https://github.com/qutip/QuantumToolbox.jl/issues/139
5558
[#292]: https://github.com/qutip/QuantumToolbox.jl/issues/292
@@ -66,3 +69,4 @@ Release date: 2024-11-13
6669
[#342]: https://github.com/qutip/QuantumToolbox.jl/issues/342
6770
[#346]: https://github.com/qutip/QuantumToolbox.jl/issues/346
6871
[#347]: https://github.com/qutip/QuantumToolbox.jl/issues/347
72+
[#349]: https://github.com/qutip/QuantumToolbox.jl/issues/349

docs/src/resources/api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ MultiSiteOperator
258258
DissipativeIsing
259259
```
260260

261+
## [Symmetries and Block Diagonalization](@id doc-API:Symmetries-and-Block-Diagonalization)
262+
263+
```@docs
264+
block_diagonal_form
265+
BlockDiagonalForm
266+
```
267+
261268
## [Miscellaneous](@id doc-API:Miscellaneous)
262269

263270
```@docs

src/QuantumToolbox.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ include("qobj/states.jl")
9090
include("qobj/operators.jl")
9191
include("qobj/superoperators.jl")
9292
include("qobj/synonyms.jl")
93+
include("qobj/block_diagonal_form.jl")
9394

9495
# time evolution
9596
include("time_evolution/time_evolution.jl")
@@ -105,7 +106,6 @@ include("time_evolution/ssesolve.jl")
105106
include("time_evolution/time_evolution_dynamical.jl")
106107

107108
# Others
108-
include("permutation.jl")
109109
include("correlations.jl")
110110
include("spectrum.jl")
111111
include("wigner.jl")

src/permutation.jl

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/qobj/block_diagonal_form.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export BlockDiagonalForm, block_diagonal_form
2+
3+
@doc raw"""
4+
struct BlockDiagonalForm
5+
6+
A type for storing a block-diagonal form of a matrix.
7+
8+
# Fields
9+
- `B::DT`: The block-diagonal matrix. It can be a sparse matrix or a [`QuantumObject`](@ref).
10+
- `P::DT`: The permutation matrix. It can be a sparse matrix or a [`QuantumObject`](@ref).
11+
- `blocks::AbstractVector`: The blocks of the block-diagonal matrix.
12+
- `block_sizes::AbstractVector`: The sizes of the blocks.
13+
"""
14+
struct BlockDiagonalForm{DT,BT<:AbstractVector,BST<:AbstractVector}
15+
B::DT
16+
P::DT
17+
blocks::BT
18+
block_sizes::BST
19+
end
20+
21+
function block_diagonal_form(A::MT) where {MT<:AbstractSparseMatrix}
22+
n = LinearAlgebra.checksquare(A)
23+
24+
G = DiGraph(abs.(A))
25+
idxs_list = connected_components(G)
26+
block_sizes = length.(idxs_list)
27+
28+
P = MT(sparse(1:n, reduce(vcat, idxs_list), ones(n), n, n))
29+
30+
blocks = map(eachindex(idxs_list)) do i
31+
m = block_sizes[i]
32+
idxs = idxs_list[i]
33+
P_i = MT(sparse(1:m, idxs, ones(m), m, n))
34+
return P_i * A * P_i'
35+
end
36+
37+
B = P * A * P'
38+
39+
return BlockDiagonalForm(B, P, blocks, block_sizes)
40+
end
41+
42+
@doc raw"""
43+
block_diagonal_form(A::QuantumObject)
44+
45+
Return the block-diagonal form of a [`QuantumObject`](@ref). This is very useful in the presence of symmetries.
46+
47+
# Arguments
48+
- `A::QuantumObject`: The quantum object.
49+
50+
# Returns
51+
The [`BlockDiagonalForm`](@ref) of `A`.
52+
"""
53+
function block_diagonal_form(
54+
A::QuantumObject{DT,OpType},
55+
) where {DT<:AbstractSparseMatrix,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}}
56+
bdf = block_diagonal_form(A.data)
57+
B = QuantumObject(bdf.B, type = A.type, dims = A.dims)
58+
P = QuantumObject(bdf.P, type = A.type, dims = A.dims)
59+
return BlockDiagonalForm(B, P, bdf.blocks, bdf.block_sizes)
60+
end

test/core-test/permutation.jl renamed to test/core-test/block_diagonal_form.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@testset "Permutation" begin
1+
@testset "Block Diagonal Form" begin
22
# Block Diagonal Form
33
N = 20
44
Δ = 0
@@ -16,15 +16,17 @@
1616
c_ops = [(κ2) * a^2, (κϕ) * ad * a]
1717
L = liouvillian(H, c_ops)
1818

19-
P, L_bd, block_sizes = bdf(L)
20-
blocks_list, block_indices = get_bdf_blocks(L_bd, block_sizes)
19+
bdf = block_diagonal_form(L)
20+
L_bd = bdf.B
21+
block_sizes = bdf.block_sizes
22+
blocks = bdf.blocks
23+
2124
@test size(L_bd) == size(L)
2225
@test length(block_sizes) == 4
23-
@test length(blocks_list) == 4
24-
@test length(block_indices) == 4
26+
@test length(blocks) == 4
2527
@test sum(block_sizes .== 100) == 4
2628

27-
@testset "Type Inference (bdf)" begin
28-
@inferred bdf(L)
29+
@testset "Type Inference (block_diagonal_form)" begin
30+
@inferred block_diagonal_form(L)
2931
end
3032
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const testdir = dirname(@__FILE__)
77

88
# Put core tests in alphabetical order
99
core_tests = [
10+
"block_diagonal_form.jl",
1011
"correlations_and_spectrum.jl",
1112
"dynamical_fock_dimension_mesolve.jl",
1213
"dynamical-shifted-fock.jl",
@@ -15,7 +16,6 @@ core_tests = [
1516
"generalized_master_equation.jl",
1617
"low_rank_dynamics.jl",
1718
"negativity_and_partial_transpose.jl",
18-
"permutation.jl",
1919
"progress_bar.jl",
2020
"quantum_objects.jl",
2121
"quantum_objects_evo.jl",

0 commit comments

Comments
 (0)