Skip to content

Commit

Permalink
update (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
timbode authored Dec 12, 2024
1 parent d566c42 commit 30f4b73
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QAOA"
uuid = "814bfb56-6688-4460-8fd4-bc1ff2cd6cd4"
authors = ["Tim Bode, Dmitry Bagrets, Aditi Misra-Spieldenner, Tobias Stollenwerk, Frank K. Wilhelm"]
version = "1.3.2"
version = "1.3.3"

[deps]
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
Expand Down
4 changes: 2 additions & 2 deletions src/QAOA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using OrdinaryDiffEq
import Distributions, Random

include("problem.jl")
export Problem, TensorProblem
export Problem

include("optimization.jl")
export cost_function, optimize_parameters
Expand All @@ -21,7 +21,7 @@ export sherrington_kirkpatrick, partition_problem, max_cut, min_vertex_cover
include("circuit.jl")

include("mean_field.jl")
export evolve!, evolve, expectation, mean_field_solution
export evolve!, expectation, mean_field_solution

include("fluctuations.jl")
export evolve_fluctuations
Expand Down
127 changes: 0 additions & 127 deletions src/mean_field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,6 @@ function magnetization(S::Matrix{<:Real}, h::Vector{<:Real}, J::Matrix{<:Real})
h + [sum([J[i, j] * S[3, j] for j in 1:size(S)[2]]) for i in 1:size(S)[2]]
end

"""
magnetization(S::Matrix{<:Real}, h::Vector{<:Real}, J::Matrix{<:Real})
Third dispatch for the magnetization vector.
### Notes
- The magnetization vector in the most general case is defined as
``m_i(t) = h_i + \\sum_{j=1}^N \\left( J_{ij} n_j^z(t) + sum_{j=1}^N J_{ijk} n_j^z(t)n_k^z(t) + \\cdots \\right)``.
"""
function magnetization(S::Vector{<:Real}, tensor)
mag = zeros(size(S)[1])
for (idxs, val) in tensor
for i in idxs
# remove the current spin from the list
the_other_idxs = filter!(idx -> idx != i, collect(idxs))
# special case are local fields
the_other_idxs = the_other_idxs == [] ? [0] : the_other_idxs
mag[i] += val * prod([get(S, j, 1.0) for j in the_other_idxs])
end
end
mag
end

"""
V_P(alpha::Real)
Expand Down Expand Up @@ -131,108 +106,6 @@ function evolve!(S::Vector{<:Vector{<:Vector{<:Real}}}, h::Vector{<:Real}, J::Ma
end
end


"""
evolve(h::Vector{<:Real}, J::Matrix{<:Real}, T_final::Float64, schedule::Function; rtol=1e-4, atol=1e-6)
Evolves the mean-field equations of motion for a given system.
# Input
- `h::Vector{<:Real}`: External magnetic field vector.
- `J::Matrix{<:Real}`: Interaction matrix.
- `T_final::Float64`: Final time of evolution.
- `schedule::Function`: Scheduling function for the evolution.
- `rtol`: Relative tolerance for the ODE solver (default: `1e-4`).
- `atol`: Absolute tolerance for the ODE solver (default: `1e-6`).
# Output
- `sol`: Solution object from the ODE solver containing the time evolution of the system.
# Notes
- This is the third dispatch of `evolve`, which directly solves the full mean-field equations of motion for a system described by an external magnetic field `h` and an interaction matrix `J` over a time interval from `0.0` to `T_final`. The evolution is controlled by a scheduling function `schedule(t)`, which interpolates between the different dynamical regimes.
- The initial state `S₀` is assumed to be the vector `[1.0, 0.0, 0.0]` for each spin.
- The function uses the `Tsit5()` solver from the `DifferentialEquations.jl` package to solve the ODE.
# Example
```julia
h = [0.5, -0.5, 0.3]
J = [0.0 0.1 0.2; 0.1 0.0 0.3; 0.2 0.3 0.0]
T_final = 10.0
schedule(t) = t / T_final
sol = evolve_mean_field(h, J, T_final, schedule)
```
"""
function evolve(h::Vector{<:Real}, J::Matrix{<:Real}, T_final::Float64, schedule::Function; rtol=1e-4, atol=1e-6)

function mf_eom(dS, S, _, t)
magnetization = h + [sum([J[i, j] * S[3, j] for j in 1:size(S)[2]]) for i in 1:size(S)[2]]
dS .= reduce(hcat, [[-2 * schedule(t) * magnetization[i] * S[2, i],
-2 * (1 - schedule(t)) * S[3, i] + 2 * schedule(t) * magnetization[i] * S[1, i],
2 * (1 - schedule(t)) * S[2, i]] for i in 1:size(S)[2]])
end

S₀ = reduce(hcat, [[1., 0., 0.] for _ in 1:size(h)[1]])
prob = ODEProblem(mf_eom, S₀, (0.0, T_final))
sol = solve(prob, Tsit5(), reltol=rtol, abstol=atol)
sol
end

"""
evolve(tensor_problem::TensorProblem, T_final::Float64, schedule_x::Function, schedule_z::Function; rtol=1e-4, atol=1e-6)
Evolves a mean-field approximation of a quantum system described by `tensor_problem` over time using specified scheduling functions for the x and z components of the Hamiltonian.
# Input
- `tensor_problem::TensorProblem`: A structured type containing the Hamiltonian tensors and the number of qubits.
- `T_final::Float64`: The final time up to which the system should be evolved.
- `schedule_x::Function`: A function defining the time-dependence of the x component of the Hamiltonian.
- `schedule_z::Function`: A function defining the time-dependence of the z component of the Hamiltonian.
- `rtol::Float64`: (optional) The relative tolerance for the ODE solver. Defaults to 1e-4.
- `atol::Float64`: (optional) The absolute tolerance for the ODE solver. Defaults to 1e-6.
# Output
- `sol`: The solution object from the ODE solver, containing the time evolution of the system's state.
# Notes
- This is the fourth dispatch of `evolve`, which directly solves the full mean-field equations of motion for a system described by the tensors `xtensor` and `ztensor` over a time interval from `0.0` to `T_final`. The evolution is controlled by the scheduling functions `schedule_x(t)` and `schedule_z(t)`, which interpolate between the different dynamical regimes.
- The initial state `S₀` is set to have all qubits in the state `[1, 0, 0]`. The differential equations are solved using the `Tsit5()` solver from the `DifferentialEquations.jl`` package, with specified relative and absolute tolerances.
# Example
```julia
# Define your tensor problem and scheduling functions
xtensor = Dict[(1,) => 1.0, (2,) => 1.0]
ztensor = Dict[(1, 2) => 1.0]
tensor_problem = TensorProblem(xtensor, ztensor, num_qubits)
T_final = 10.0
schedule(t) = t / T_final
schedule_x(t) = 1 - schedule(t)
schedule_z(t) = schedule(t)
# Evolve the system
sol = evolve(tensor_problem, T_final, schedule_x, schedule_z)
```
"""
function evolve(tensor_problem::TensorProblem, T_final::Float64, schedule_x::Function, schedule_z::Function; rtol=1e-4, atol=1e-6)
@unpack_TensorProblem tensor_problem

function mf_eom(dS, S, _, t)
magnetization_x = magnetization(S[1, :], xtensor)
magnetization_z = magnetization(S[3, :], ztensor)

dnx(i) = -2 * schedule_z(t) * magnetization_z[i] * S[2, i]
dny(i) = -2 * schedule_x(t) * magnetization_x[i] * S[3, i] + 2 * schedule_z(t) * magnetization_z[i] * S[1, i]
dnz(i) = 2 * schedule_x(t) * magnetization_x[i] * S[2, i]
dS .= reduce(hcat, [[dnx(i), dny(i), dnz(i)] for i in 1:size(S)[2]])
end

S₀ = reduce(hcat, [[1., 0., 0.] for _ in 1:num_qubits])
prob = ODEProblem(mf_eom, S₀, (0.0, T_final))
sol = solve(prob, Tsit5(), reltol=rtol, abstol=atol)
sol
end


"""
expectation(S::Vector{<:Vector{<:Real}}, h::Vector{<:Real}, J::Matrix{<:Real})
Expand Down
25 changes: 0 additions & 25 deletions src/problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,5 @@ end



"""
TensorProblem(num_qubits::Int, xtensor::Dict{Tuple, Float64}, ztensor::Dict{Tuple, Float64})
A container holding the basic properties of the QAOA circuit.
$(TYPEDFIELDS)
"""
@with_kw struct TensorProblem
"The number of qubits of the QAOA circuit."
num_qubits::Int

"The coupling tensor of the driver Hamiltonian."
xtensor

"The coupling tensor of the problem Hamiltonian."
ztensor

TensorProblem(num_qubits, xtensor, ztensor) = new(num_qubits, xtensor, ztensor)
TensorProblem(num_qubits, ztensor) = new(num_qubits, Dict((1,) => 0.0), ztensor)
end







2 comments on commit 30f4b73

@timbode
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/121250

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.3 -m "<description of version>" 30f4b73d28fbfc6541f9e506d93cb08afef805bd
git push origin v1.3.3

Please sign in to comment.