Skip to content

Commit

Permalink
Make use of new quantum operator libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Jan 20, 2025
1 parent a16f115 commit e323265
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 38 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ version = "0.4.0"
[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
ITensorQuantumOperatorDefinitions = "fd9b415b-e710-4e2a-b407-cba023081494"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
IsApprox = "28f27b66-4bd8-47e7-9110-e2746eb8bed7"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
QuantumOperatorAlgebra = "c7a6d0f7-daa6-4368-ba67-89ed64127c3b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SerializedElementArrays = "d3ce8812-9567-47e9-a7b5-65a6d70a3065"
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"
Expand All @@ -36,13 +38,15 @@ Adapt = "4.1.0"
ChainRulesCore = "1.10"
Compat = "4.16.0"
HDF5 = "0.14, 0.15, 0.16, 0.17"
ITensorQuantumOperatorDefinitions = "0.1.1"
ITensors = "0.8.0"
IsApprox = "2.0.0"
KrylovKit = "0.8.1, 0.9"
LinearAlgebra = "1.10"
Observers = "0.2"
PackageCompiler = "1, 2"
Printf = "1.10"
QuantumOperatorAlgebra = "0.1.0"
Random = "1.10"
SerializedElementArrays = "0.1.0"
Test = "1.10"
Expand Down
34 changes: 22 additions & 12 deletions src/abstractmps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ using ITensors:
uniqueind,
uniqueinds
## using NDTensors: NDTensors, using_auto_fermion, scalartype, tensor
using ITensors.Ops: Prod
using QuantumOperatorAlgebra: Prod
## using ITensors.QuantumNumbers: QuantumNumbers, removeqn
using ITensors.SiteTypes: SiteTypes, siteinds
using ITensorQuantumOperatorDefinitions: ITensorQuantumOperatorDefinitions, siteinds
## using ITensors.TagSets: TagSets
using LinearAlgebra: LinearAlgebra

Expand Down Expand Up @@ -485,7 +485,7 @@ end
Get the site index (or indices) of MPO `A` that is unique to `A` (not shared with MPS/MPO `B`).
"""
function SiteTypes.siteinds(
function ITensorQuantumOperatorDefinitions.siteinds(
f::Union{typeof(uniqueinds),typeof(uniqueind)},
A::AbstractMPS,
B::AbstractMPS,
Expand All @@ -505,7 +505,7 @@ end
Get the site indices of MPO `A` that are unique to `A` (not shared with MPS/MPO `B`), as a `Vector{<:Index}`.
"""
function SiteTypes.siteinds(
function ITensorQuantumOperatorDefinitions.siteinds(
f::Union{typeof(uniqueinds),typeof(uniqueind)}, A::AbstractMPS, B::AbstractMPS; kwargs...
)
return [siteinds(f, A, B, j; kwargs...) for j in eachindex(A)]
Expand All @@ -517,7 +517,7 @@ end
Get the site index (or indices) of the `j`th MPO tensor of `A` that is shared with MPS/MPO `B`.
"""
function SiteTypes.siteinds(
function ITensorQuantumOperatorDefinitions.siteinds(
f::Union{typeof(commoninds),typeof(commonind)},
A::AbstractMPS,
B::AbstractMPS,
Expand All @@ -533,7 +533,7 @@ end
Get a vector of the site index (or indices) of MPO `A` that is shared with MPS/MPO `B`.
"""
function SiteTypes.siteinds(
function ITensorQuantumOperatorDefinitions.siteinds(
f::Union{typeof(commoninds),typeof(commonind)}, A::AbstractMPS, B::AbstractMPS; kwargs...
)
return [siteinds(f, A, B, j) for j in eachindex(A)]
Expand Down Expand Up @@ -639,7 +639,9 @@ Return the first site Index found on the MPS or MPO
You can choose different filters, like prime level
and tags, with the `kwargs`.
"""
function SiteTypes.siteind(::typeof(first), M::AbstractMPS, j::Integer; kwargs...)
function ITensorQuantumOperatorDefinitions.siteind(
::typeof(first), M::AbstractMPS, j::Integer; kwargs...
)
N = length(M)
(N == 1) && return firstind(M[1]; kwargs...)
if j == 1
Expand All @@ -661,7 +663,7 @@ at the site `j` as an IndexSet.
Optionally filter prime tags and prime levels with
keyword arguments like `plev` and `tags`.
"""
function SiteTypes.siteinds(M::AbstractMPS, j::Integer; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(M::AbstractMPS, j::Integer; kwargs...)
N = length(M)
(N == 1) && return inds(M[1]; kwargs...)
if j == 1
Expand All @@ -674,19 +676,27 @@ function SiteTypes.siteinds(M::AbstractMPS, j::Integer; kwargs...)
return si
end

function SiteTypes.siteinds(::typeof(all), ψ::AbstractMPS, n::Integer; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(
::typeof(all), ψ::AbstractMPS, n::Integer; kwargs...
)
return siteinds(ψ, n; kwargs...)
end

function SiteTypes.siteinds(::typeof(first), ψ::AbstractMPS; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(
::typeof(first), ψ::AbstractMPS; kwargs...
)
return [siteind(first, ψ, j; kwargs...) for j in 1:length(ψ)]
end

function SiteTypes.siteinds(::typeof(only), ψ::AbstractMPS; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(
::typeof(only), ψ::AbstractMPS; kwargs...
)
return [siteind(only, ψ, j; kwargs...) for j in 1:length(ψ)]
end

function SiteTypes.siteinds(::typeof(all), ψ::AbstractMPS; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(
::typeof(all), ψ::AbstractMPS; kwargs...
)
return [siteinds(ψ, j; kwargs...) for j in 1:length(ψ)]
end

Expand Down
4 changes: 2 additions & 2 deletions src/abstractprojmpo/projmposum.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ITensors.Ops: Ops
using QuantumOperatorAlgebra: QuantumOperatorAlgebra

abstract type AbstractSum end

Ops.terms(sum::AbstractSum) = sum.terms
QuantumOperatorAlgebra.terms(sum::AbstractSum) = sum.terms

function set_terms(sum::AbstractSum, terms)
return error("Please implement `set_terms` for the `AbstractSum` type `$(typeof(sum))`.")
Expand Down
1 change: 0 additions & 1 deletion src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export
Apply,
Op,
OpName,
Ops,
Prod,
Scaled,
SiteType,
Expand Down
6 changes: 3 additions & 3 deletions src/imports.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Primarily used to import names into the `ITensorMPS`
# module from submodules or from `ITensors` so they can
# be reexported.
using ITensors.SiteTypes:
using ITensorQuantumOperatorDefinitions:
@OpName_str,
@SiteType_str,
@StateName_str,
Expand All @@ -13,7 +13,7 @@ using ITensors.SiteTypes:
TagType,
ValName,
ops
using ITensors.Ops: Trotter
using QuantumOperatorAlgebra: Trotter

import Base:
# types
Expand Down Expand Up @@ -168,6 +168,6 @@ import ..ITensors:
truncate!,
which_op

import ..ITensors.Ops: params
import QuantumOperatorAlgebra: params

import SerializedElementArrays: disk
14 changes: 8 additions & 6 deletions src/mpo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ using Adapt: adapt
using LinearAlgebra: dot
using Random: Random, AbstractRNG
using ITensors: @ts_str, Algorithm, dag, outer
using ITensors.Ops: OpSum
using ITensors.SiteTypes: SiteTypes, siteind, siteinds
using QuantumOperatorAlgebra: OpSum
using ITensorQuantumOperatorDefinitions:
ITensorQuantumOperatorDefinitions, siteind, siteinds

"""
MPO
Expand Down Expand Up @@ -242,7 +243,8 @@ end
Get the first site Index of the MPO found, by
default with prime level 0.
"""
SiteTypes.siteind(M::MPO, j::Int; kwargs...) = siteind(first, M, j; plev=0, kwargs...)
ITensorQuantumOperatorDefinitions.siteind(M::MPO, j::Int; kwargs...) =
siteind(first, M, j; plev=0, kwargs...)

# TODO: make this return the site indices that would have
# been used to create the MPO? I.e.:
Expand All @@ -252,9 +254,9 @@ SiteTypes.siteind(M::MPO, j::Int; kwargs...) = siteind(first, M, j; plev=0, kwar
Get a Vector of IndexSets of all the site indices of M.
"""
SiteTypes.siteinds(M::MPO; kwargs...) = siteinds(all, M; kwargs...)
ITensorQuantumOperatorDefinitions.siteinds(M::MPO; kwargs...) = siteinds(all, M; kwargs...)

function SiteTypes.siteinds(Mψ::Tuple{MPO,MPS}, n::Int; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(Mψ::Tuple{MPO,MPS}, n::Int; kwargs...)
return siteinds(uniqueinds, Mψ[1], Mψ[2], n; kwargs...)
end

Expand All @@ -265,7 +267,7 @@ function nsites(Mψ::Tuple{MPO,MPS})
return N
end

function SiteTypes.siteinds(Mψ::Tuple{MPO,MPS}; kwargs...)
function ITensorQuantumOperatorDefinitions.siteinds(Mψ::Tuple{MPO,MPS}; kwargs...)
return [siteinds(Mψ, n; kwargs...) for n in 1:nsites(Mψ)]
end

Expand Down
13 changes: 9 additions & 4 deletions src/mps.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Adapt: adapt
using ITensors: hasqns
using ITensors.SiteTypes: SiteTypes, siteind, siteinds, state
using ITensorQuantumOperatorDefinitions:
ITensorQuantumOperatorDefinitions, siteind, siteinds, state
using Random: Random, AbstractRNG

## TODO: Add this back.
Expand Down Expand Up @@ -486,14 +487,17 @@ MPS(sites::Vector{<:Index}, states) = MPS(Float64, sites, states)
Get the first site Index of the MPS. Return `nothing` if none is found.
"""
SiteTypes.siteind(M::MPS, j::Int; kwargs...) = siteind(first, M, j; kwargs...)
ITensorQuantumOperatorDefinitions.siteind(M::MPS, j::Int; kwargs...) =
siteind(first, M, j; kwargs...)

"""
siteind(::typeof(only), M::MPS, j::Int; kwargs...)
Get the only site Index of the MPS. Return `nothing` if none is found.
"""
function SiteTypes.siteind(::typeof(only), M::MPS, j::Int; kwargs...)
function ITensorQuantumOperatorDefinitions.siteind(
::typeof(only), M::MPS, j::Int; kwargs...
)
is = siteinds(M, j; kwargs...)
if isempty(is)
return nothing
Expand All @@ -515,7 +519,8 @@ Get a vector of the only site Index found on each tensor of the MPS. Errors if m
Get a vector of the all site Indices found on each tensor of the MPS. Returns a Vector of IndexSets.
"""
SiteTypes.siteinds(M::MPS; kwargs...) = siteinds(first, M; kwargs...)
ITensorQuantumOperatorDefinitions.siteinds(M::MPS; kwargs...) =
siteinds(first, M; kwargs...)

function replace_siteinds!(M::MPS, sites)
for j in eachindex(M)
Expand Down
24 changes: 17 additions & 7 deletions src/opsum_to_mpo/opsum_to_mpo_generic.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
## using NDTensors: using_auto_fermion
using ITensors.Ops:
Ops, Op, OpSum, Scaled, Sum, argument, coefficient, site, sites, terms, which_op
using ITensors.SiteTypes: has_fermion_string, op
using QuantumOperatorAlgebra:
QuantumOperatorAlgebra,
Op,
OpSum,
Scaled,
Sum,
argument,
coefficient,
site,
sites,
terms,
which_op
using ITensorQuantumOperatorDefinitions: has_fermion_string, op

"""
parity_sign(P)
Expand Down Expand Up @@ -85,10 +95,10 @@ end
add!(os::OpSum, o::Op) = add!(os, Prod{Op}() * o)
add!(os::OpSum, o::Scaled{C,Op}) where {C} = add!(os, Prod{Op}() * o)
add!(os::OpSum, o::Prod{Op}) = add!(os, one(Float64) * o)
add!(os::OpSum, o::Tuple) = add!(os, Ops.op_term(o))
add!(os::OpSum, o::Tuple) = add!(os, QuantumOperatorAlgebra.op_term(o))
add!(os::OpSum, a1::String, args...) = add!(os, (a1, args...))
add!(os::OpSum, a1::Number, args...) = add!(os, (a1, args...))
subtract!(os::OpSum, o::Tuple) = add!(os, -Ops.op_term(o))
subtract!(os::OpSum, o::Tuple) = add!(os, -QuantumOperatorAlgebra.op_term(o))

function isfermionic(t::Vector{Op}, sites)
p = +1
Expand Down Expand Up @@ -188,7 +198,7 @@ function sorteachterm(os::OpSum, sites)
os = copy(os)

for (j, t) in enumerate(os)
if maximum(Ops.sites(t)) > length(sites)
if maximum(QuantumOperatorAlgebra.sites(t)) > length(sites)
error(
"The OpSum contains a term $t that extends beyond the number of sites $(length(sites)).",
)
Expand Down Expand Up @@ -362,7 +372,7 @@ function MPO(
return MPO(eltype, OpSum{C}() + o, s; kwargs...)
end

# Like `Ops.OpSumLike` but without `OpSum` included.
# Like `QuantumOperatorAlgebra.OpSumLike` but without `OpSum` included.
const OpSumLikeWithoutOpSum{C} = Union{
Op,Scaled{C,Op},Sum{Op},Prod{Op},Scaled{C,Prod{Op}},Sum{Scaled{C,Op}}
}
Expand Down
6 changes: 3 additions & 3 deletions src/solvers/timedependentsum.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ITensors: ITensor, inds, permute
using ITensors.Ops: Ops
using QuantumOperatorAlgebra: QuantumOperatorAlgebra

# Represents a time-dependent sum of terms:
#
Expand All @@ -11,7 +11,7 @@ struct TimeDependentSum{Coefficients,Terms}
end

coefficients(expr::TimeDependentSum) = expr.coefficients
Ops.terms(expr::TimeDependentSum) = expr.terms
QuantumOperatorAlgebra.terms(expr::TimeDependentSum) = expr.terms
function Base.copy(expr::TimeDependentSum)
return TimeDependentSum(coefficients(expr), copy.(terms(expr)))
end
Expand Down Expand Up @@ -52,7 +52,7 @@ struct ScaledSum{Coefficients,Terms}
end

coefficients(expr::ScaledSum) = expr.coefficients
Ops.terms(expr::ScaledSum) = expr.terms
QuantumOperatorAlgebra.terms(expr::ScaledSum) = expr.terms

# Apply the scaled sum of terms:
#
Expand Down

0 comments on commit e323265

Please sign in to comment.