Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Replace previous Ansatz for Quantum struct #5

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ version = "0.1.0"
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Tenet = "85d41934-b9cd-44e1-8730-56d86f15f3ec"
ValSplit = "0625e100-946b-11ec-09cd-6328dd093154"

[compat]
Tenet = "0.5"
ValSplit = "0.1"
julia = "1.9"
15 changes: 13 additions & 2 deletions src/Quantum/Ansatz.jl → src/Ansatz.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Tenet

abstract type Ansatz <: Tenet.AbstractTensorNetwork end
abstract type Ansatz end

abstract type Socket end
struct Scalar <: Socket end
Expand All @@ -10,14 +10,25 @@ struct Operator <: Socket end
function socket end
socket(::A) where {A<:Ansatz} = socket(A)

function socket(q::Quantum)
_sites = sites(q)
if isempty(_sites)
Scalar()
elseif all(!isdual, _sites)
State()
else
Operator()
end
end

abstract type Boundary end
struct Open <: Boundary end
struct Periodic <: Boundary end

function boundary end
boundary(::A) where {A<:Ansatz} = boundary(A)

Base.summary(io::IO, tn::A) where {A<:Ansatz} = print(io, "$A(n=$(length(tensors(tn))))")
Base.summary(io::IO, tn::A) where {A<:Ansatz} = print(io, "$A (n=$(length(tensors(tn))))")
Base.show(io::IO, tn::A) where {A<:Ansatz} = Base.summary(io, tn)

# helpers
Expand Down
File renamed without changes.
9 changes: 7 additions & 2 deletions src/Qrochet.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
module Qrochet

include("Quantum/Ansatz.jl")
include("Quantum.jl")
export Site, site_str, isdual
export ninputs, noutputs, sites
export Quantum

include("Ansatz.jl")
export socket, Scalar, State, Operator
export boundary, Open, Periodic
export Product
export MatrixProduct

include("Quantum/Product.jl")
include("Ansatz/Product.jl")
export Product

end
82 changes: 82 additions & 0 deletions src/Quantum.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Tenet

"""
Site(id[, dual = false])
site"id"

Represents a physical index.

# Unresolved questions

- Should we store here some information about quantum numbers?
"""
struct Site
id::Int
dual::Bool

Site(id, dual = false) = new(id, dual)
end

isdual(site::Site) = site.dual
Base.show(io::IO, site::Site) = print(io, "$(site.id)$(site.dual ? "†" : "")")
Base.adjoint(site::Site) = Site(site.id, !site.dual)

macro site_str(str)
m = match(r"^(\d+)(')$", str)
if isnothing(m)
error("Invalid site string: $str")
end

id = parse(Int, m.captures[1])
dual = m.captures[2] == "'"

quote
Site($id, $dual)
end
end

"""
Quantum

Tensor Network with a notion of "causality". This leads to the notion of sites and directionality (input/output).

# Notes

- Indices are referenced by `Site`s.
"""
struct Quantum
tn::TensorNetwork

# WARN keep them synchronized
siteinds::Dict{Site,Symbol}
sitetensors::Dict{Site,Tensor}

function Quantum(tn::TensorNetwork, sites::Dict{Site,Symbol})
for (site, index) in sites
if !haskey(tn.indexmap, index)
error("Index $index not found in TensorNetwork")
elseif index ∉ inds(tn, set = :open)
error("Index $index must be open")
end
end

sitetensors = map(sites) do (site, index)
site => tn[index]
end |> Dict{Site,Tensor}

new(tn, sites, sitetensors)
end
end

# TODO (@mofeing) Return `copy`?
Tenet.TensorNetwork(q::Quantum) = q.tn

ninputs(q::Quantum) = count(isdual, keys(q.sites))
noutputs(q::Quantum) = count(!isdual, keys(q.sites))

Base.summary(io::IO, q::Quantum) = print(io, "$(length(q.tn.tensormap))-tensors Quantum")
Base.show(io::IO, q::Quantum) = print(io, "Quantum (inputs=$(ninputs(q)), outputs=$(noutputs(q)))")

sites(tn::Quantum) = collect(keys(tn.siteinds))

Base.getindex(q::Quantum, site::Site) = q.siteinds[site]
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Test
using Qrochet
using OMEinsum

@testset "Unit tests" verbose = true begin end

Expand Down
Loading