Skip to content

Commit

Permalink
Merge pull request qutip#156 from ytdHuang/dev/operator
Browse files Browse the repository at this point in the history
Introduce `commutator`, `fcreate`, and `fdestroy`
  • Loading branch information
ytdHuang authored Jun 4, 2024
2 parents 6f28a5d + ed3a809 commit c6e8b56
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ sigmay
sigmaz
destroy
create
fdestroy
fcreate
eye
qeye
projection
commutator
spre
spost
sprepost
Expand Down
58 changes: 58 additions & 0 deletions src/qobj/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ Functions for generating (common) quantum operators.

export sigmam, sigmap, sigmax, sigmay, sigmaz
export destroy, create, eye, qeye, projection
export fdestroy, fcreate
export commutator
export spre, spost, sprepost, lindblad_dissipator

@doc raw"""
commutator(A::QuantumObject, B::QuantumObject; anti::Bool=false)
Return the commutator (or `anti`-commutator) of the two [`QuantumObject`](@ref):
- commutator (`anti=false`): ``AB-BA``
- anticommutator (`anti=true`): ``AB+BA``
Note that `A` and `B` must be [`Operator`](@ref)
"""
commutator(
A::QuantumObject{<:AbstractArray{T1},OperatorQuantumObject},
B::QuantumObject{<:AbstractArray{T2},OperatorQuantumObject};
anti::Bool = false,
) where {T1,T2} = A * B - (-1)^anti * B * A

@doc raw"""
spre(O::QuantumObject, Id_cache=I(size(O,1)))
Expand Down Expand Up @@ -180,6 +197,47 @@ qeye(
dims::Vector{Int} = [N],
) where {ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = eye(N, type = type, dims = dims)

@doc raw"""
fdestroy(N::Int, j::Int)
Construct a fermionic destruction operator acting on the `j`-th site, where the fock space has totally `N`-sites:
Here, we use the [Jordan-Wigner transformation](https://en.wikipedia.org/wiki/Jordan%E2%80%93Wigner_transformation), namely
```math
d_j = \sigma_z^{\otimes j} \otimes \sigma_{-} \otimes I^{\otimes N-j-1}
```
Note that the site index `j` should satisfy: `0 ≤ j ≤ N - 1`
"""
fdestroy(N::Int, j::Int) = _Jordan_Wigner(N, j, sigmam())

@doc raw"""
fcreate(N::Int, j::Int)
Construct a fermionic creation operator acting on the `j`-th site, where the fock space has totally `N`-sites:
Here, we use the [Jordan-Wigner transformation](https://en.wikipedia.org/wiki/Jordan%E2%80%93Wigner_transformation), namely
```math
d_j^\dagger = \sigma_z^{\otimes j} \otimes \sigma_{+} \otimes I^{\otimes N-j-1}
```
Note that the site index `j` should satisfy: `0 ≤ j ≤ N - 1`
"""
fcreate(N::Int, j::Int) = _Jordan_Wigner(N, j, sigmap())

function _Jordan_Wigner(N::Int, j::Int, op::QuantumObject{<:AbstractArray{T},OperatorQuantumObject}) where {T}
(N < 1) && throw(ArgumentError("The total number of sites (N) cannot be less than 1"))
((j >= N) || (j < 0)) && throw(ArgumentError("The site index (j) should satisfy: 0 ≤ j ≤ N - 1"))

σz = sigmaz().data
Z_tensor = kron(1, 1, fill(σz, j)...)

S = 2^(N - j - 1)
I_tensor = sparse((1.0 + 0.0im) * LinearAlgebra.I, S, S)

return QuantumObject(kron(Z_tensor, op.data, I_tensor); type = Operator, dims = fill(2, N))
end

@doc raw"""
projection(N::Int, i::Int, j::Int)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ core_tests = [
"permutation.jl",
"progress_bar.jl",
"quantum_objects.jl",
"states_and_operators.jl",
"steady_state.jl",
"time_evolution_and_partial_trace.jl",
"wigner.jl",
Expand Down
27 changes: 27 additions & 0 deletions test/states_and_operators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@testset "States and Operators" begin
# test commutation relations for fermionic creation and annihilation operators
sites = 4
SIZE = 2^sites
dims = fill(2, sites)
Q_iden = Qobj(sparse((1.0 + 0.0im) * LinearAlgebra.I, SIZE, SIZE); dims = dims)
Q_zero = Qobj(spzeros(ComplexF64, SIZE, SIZE); dims = dims)
for i in 0:(sites-1)
d_i = fdestroy(sites, i)
@test d_i' fcreate(sites, i)

for j in 0:(sites-1)
d_j = fdestroy(sites, j)

if i == j
@test commutator(d_i, d_j'; anti = true) Q_iden
else
@test commutator(d_i, d_j'; anti = true) Q_zero
end
@test commutator(d_i', d_j'; anti = true) Q_zero
@test commutator(d_i, d_j; anti = true) Q_zero
end
end
@test_throws ArgumentError fdestroy(0, 0)
@test_throws ArgumentError fdestroy(sites, -1)
@test_throws ArgumentError fdestroy(sites, sites)
end

0 comments on commit c6e8b56

Please sign in to comment.