|
| 1 | +using Base.Iterators |
| 2 | + |
| 3 | +# changed type tree s.t. AbstractRydberg is a direct subtype of Hamiltonian |
| 4 | +abstract type AbstractRydberg{O <: AbstractOperatorSampler} <: Hamiltonian{2,O} end |
| 5 | + |
| 6 | +struct Rydberg{O,M <: UpperTriangular{Float64},UΩ <: AbstractVector{Float64}, Uδ <: AbstractVector{Float64}, L <: Lattice} <: AbstractRydberg{O} |
| 7 | + op_sampler::O |
| 8 | + V::M |
| 9 | + Ω::UΩ |
| 10 | + δ::Uδ |
| 11 | + lattice::L |
| 12 | + energy_shift::Float64 |
| 13 | +end |
| 14 | + |
| 15 | +nspins(H::Rydberg) = nspins(H.lattice) |
| 16 | + |
| 17 | + |
| 18 | +function make_prob_vector(H::Type{<:AbstractRydberg}, V::UpperTriangular{T}, Ω::AbstractVector{T}, δ::AbstractVector{T}; epsilon=0.0) where T |
| 19 | + @assert length(Ω) == length(δ) == size(V, 1) == size(V, 2) |
| 20 | + @assert (0.0 <= epsilon <= 1.0) "epsilon must be in the range [0, 1]!" |
| 21 | + |
| 22 | + ops = Vector{NTuple{ISING_OP_SIZE, Int}}() |
| 23 | + p = Vector{T}() |
| 24 | + energy_shift = zero(T) |
| 25 | + |
| 26 | + for i in eachindex(Ω) |
| 27 | + if !iszero(Ω[i]) |
| 28 | + push!(ops, makediagonalsiteop(AbstractLTFIM, i)) |
| 29 | + push!(p, Ω[i] / 2) |
| 30 | + energy_shift += Ω[i] / 2 |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + Ns = length(Ω) |
| 35 | + bond_spins = Set{NTuple{2,Int}}() |
| 36 | + coordination_numbers = zeros(Int, Ns) |
| 37 | + for j in axes(V, 2), i in axes(V, 1) |
| 38 | + if i < j && !iszero(V[i, j]) |
| 39 | + push!(bond_spins, (i, j)) |
| 40 | + coordination_numbers[i] += 1 |
| 41 | + coordination_numbers[j] += 1 |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + n = diagonaloperator(H) |
| 46 | + I = Diagonal(LinearAlgebra.I, 2) |
| 47 | + |
| 48 | + # TODO: add fictitious bonds if there's a z-field on an "unbonded" site |
| 49 | + for (site1, site2) in bond_spins |
| 50 | + # by this point we can assume site1 < site2 |
| 51 | + δb1 = δ[site1] / coordination_numbers[site1] |
| 52 | + δb2 = δ[site2] / coordination_numbers[site2] |
| 53 | + local_H = V[site1, site2]*kron(n, n) - δb1*kron(n, I) - δb2*kron(I, n) |
| 54 | + |
| 55 | + p_spins = -diag(local_H) |
| 56 | + C = abs(min(0, minimum(p_spins))) + epsilon*abs(minimum(p_spins[2:end])) |
| 57 | + #dont use the zero matrix element for the epsilon shift |
| 58 | + p_spins .+= C |
| 59 | + energy_shift += C |
| 60 | + |
| 61 | + for (t, p_t) in enumerate(p_spins) |
| 62 | + push!(p, p_t) |
| 63 | + push!(ops, (2, t, length(p), site1, site2)) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + return ops, p, energy_shift |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | +############################################################################### |
| 72 | + |
| 73 | +# function Rydberg(dims::NTuple{D, Int}, R_b, Ω, δ; pbc=true, trunc::Int=0, epsilon::Float64=0) where D |
| 74 | +# if D == 1 |
| 75 | +# lat = Chain(dims[1], 1.0, pbc) |
| 76 | +# elseif D == 2 |
| 77 | +# lat = Rectangle(dims[1], dims[2], 1.0, 1.0, pbc) |
| 78 | +# else |
| 79 | +# error("Unsupported number of dimensions. 1- and 2-dimensional lattices are supported.") |
| 80 | +# end |
| 81 | +# return Rydberg(lat, R_b, Ω, δ; trunc=trunc, epsilon=epsilon) |
| 82 | +# end |
| 83 | + |
| 84 | + |
| 85 | +# We only want one Rydberg() interface, integrated into BloqadeExpr. |
| 86 | +# Probably, we'll want one function generate_interaction_matrix() that creates V and then feeds that matrix into Rydberg(). |
| 87 | +# Check with Phil how that function is coming along. |
| 88 | + |
| 89 | +function Rydberg(lat::Lattice, R_b::Float64, Ω::Float64, δ::Float64; trunc::Int=0, epsilon=0) |
| 90 | + Ns = nspins(lat) |
| 91 | + V = zeros(Float64, Ns, Ns) |
| 92 | + |
| 93 | + if trunc > 0 |
| 94 | + _dist = sort!(collect(Set(lat.distance_matrix))) |
| 95 | + uniq_dist = Vector{Float64}(undef, 0) |
| 96 | + for i in eachindex(_dist) |
| 97 | + if length(uniq_dist) == 0 |
| 98 | + push!(uniq_dist, _dist[i]) |
| 99 | + elseif !(last(uniq_dist) ≈ _dist[i]) |
| 100 | + push!(uniq_dist, _dist[i]) |
| 101 | + end |
| 102 | + end |
| 103 | + smallest_k = sort!(uniq_dist)[1:(trunc+1)] |
| 104 | + dist = copy(lat.distance_matrix) |
| 105 | + for i in eachindex(dist) |
| 106 | + if dist[i] > last(smallest_k) && !(dist[i] ≈ last(smallest_k)) |
| 107 | + dist[i] = zero(dist[i]) |
| 108 | + end |
| 109 | + end |
| 110 | + elseif lat isa Rectangle && all(lat.PBC) |
| 111 | + V = zeros(Ns, Ns) |
| 112 | + K = 3 # figure out an efficient way to set this dynamically |
| 113 | + |
| 114 | + dist = zeros(Ns, Ns) |
| 115 | + for v2 in -K:K, v1 in -K:K |
| 116 | + dV = zeros(Ns, Ns) |
| 117 | + for x2 in axes(dV, 2), x1 in axes(dV, 1) |
| 118 | + i1, j1 = divrem(x1 - 1, lat.n1) |
| 119 | + i2, j2 = divrem(x2 - 1, lat.n1) |
| 120 | + r = [i2 - i1 + v1*lat.n1, j2 - j1 + v2*lat.n2] |
| 121 | + dV[x1, x2] += Ω * (R_b/norm(r, 2))^6 |
| 122 | + end |
| 123 | + # @show v2, v1, maximum(abs, dV) |
| 124 | + |
| 125 | + V += dV |
| 126 | + end |
| 127 | + |
| 128 | + V = (V + V') / 2 # should already be symmetric but just in case |
| 129 | + |
| 130 | + return Rydberg(UpperTriangular(triu!(V, 1)), Ω*ones(Ns), δ*ones(Ns), lat; epsilon=epsilon) |
| 131 | + else |
| 132 | + dist = lat.distance_matrix |
| 133 | + end |
| 134 | + |
| 135 | + @inbounds for i in 1:(Ns-1) |
| 136 | + for j in (i+1):Ns |
| 137 | + # a zero entry in distance_matrix means there should be no bond |
| 138 | + V[i, j] = dist[i, j] != 0.0 ? Ω * (R_b / dist[i, j])^6 : 0.0 |
| 139 | + end |
| 140 | + end |
| 141 | + V = UpperTriangular(triu!(V, 1)) |
| 142 | + |
| 143 | + return Rydberg(V, Ω*ones(Ns), δ*ones(Ns), lat; epsilon=epsilon) |
| 144 | +end |
| 145 | + |
| 146 | +function Rydberg(V::AbstractMatrix{T}, Ω::AbstractVector{T}, δ::AbstractVector{T}, lattice::Lattice; epsilon=zero(T)) where T |
| 147 | + ops, p, energy_shift = make_prob_vector(AbstractRydberg, V, Ω, δ, epsilon=epsilon) |
| 148 | + op_sampler = ImprovedOperatorSampler(AbstractLTFIM, ops, p) |
| 149 | + return Rydberg{typeof(op_sampler), typeof(V), typeof(Ω), typeof(δ), typeof(lattice)}(op_sampler, V, Ω, δ, lattice, energy_shift) |
| 150 | +end |
| 151 | + |
| 152 | +# Check whether the two functions below are needed in updates. |
| 153 | + |
| 154 | +total_hx(H::Rydberg)::Float64 = sum(H.Ω) / 2 |
| 155 | +haslongitudinalfield(H::AbstractRydberg) = !iszero(H.δ) |
0 commit comments