Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Mutable Arithmetics #57

Merged
merged 3 commits into from
Dec 2, 2022
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.1.3"

[deps]
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
QUBOTools = "60eb5b62-0a39-4ddc-84c5-97d2adff9319"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

Expand Down
17 changes: 10 additions & 7 deletions src/compiler/build.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using MutableArithmetics
const MA = MutableArithmetics

function toqubo_build!(model::VirtualQUBOModel{T}, ::AbstractArchitecture) where {T}
# -*- Assemble Objective Function -*-
H = sum(
[
model.f
[model.ρ[ci] * g for (ci, g) in model.g]
[model.θ[vi] * h for (vi, h) in model.h]
];
init = zero(PBO.PBF{VI,T}),
H = MA.@rewrite(
model.f +
sum(model.ρ[ci] * g for (ci, g) in model.g; init = zero(PBO.PBF{VI,T})) +
sum(model.ρ[vi] * h for (vi, h) in model.h; init = zero(PBO.PBF{VI,T}))
)

# -*- Quadratization Step -*-
Expand All @@ -24,6 +24,9 @@ function toqubo_build!(model::VirtualQUBOModel{T}, ::AbstractArchitecture) where
a = SAT{T}[]
b = zero(T)

# ve tamanho do H e faz sizehint


for (ω, c) in H
if isempty(ω)
b += c
Expand Down
252 changes: 251 additions & 1 deletion src/lib/pbo/PBF.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Base: isiterable

using MutableArithmetics
const MA = MutableArithmetics

@doc raw"""
"""
function _parseterm end
Expand Down Expand Up @@ -317,4 +320,251 @@ function Base.convert(U::Type{<:T}, f::PBF{<:Any,T}) where {T}
else
error("Can't convert non-constant Pseudo-boolean Function to scalar type '$U'")
end
end
end


# -*- MA Mutable Arithmetics -*-

MA.mutability(::Type{<:PBF{S,T}}) where {S,T} = MA.IsMutable()


# -*- MA Mutable Copy: -*-
function MA.mutable_copy(f::PBF{S,T}) where{S,T}
f_copy = PBO.PBF{S,T}()
for (ω,c) in f
f_copy[ω] = MA.copy_if_mutable(f[ω])
end
return f_copy
end

# -*- MA isequal_canonical: -*-
function MA.isequal_canonical(
f::PBF{S,T},
g::PBF{S,T}
) where {S,T}

return f == g

end


# -*- MA Arithmetic: zero,one -*-
function MA.operate!(
::typeof(zero),
f::PBF{S,T}
) where {S,T}

empty!(f)

return f
end

function MA.operate!(
::typeof(one),
f::PBF{S,T}
) where {S,T}

empty!(f)
f[nothing] = one(T)

return f
end


# -*- MA Arithmetic: (+) -*-

function MA.operate!(
::typeof(+),
f::PBF{S,T},
g::PBF{S,T}
) where {S,T}

for(ω, c) in g
f[ω] += c
end

return f
end

function MA.operate!(
::typeof(+),
f::PBF{<:Any,T},
c::T
) where {T}
if iszero(c)
return f
end

f[nothing] += c

return f
end

# -*- MA Arithmetic: (-) -*-


function MA.operate!(
::typeof(-),
f::PBF{S,T}
) where {S,T}

for(ω, c) in f
f[ω] = -c
end

return f
end

function MA.operate!(
::typeof(-),
f::PBF{S,T},
g::PBF{S,T}
) where {S,T}

for(ω, c) in g
f[ω] -= c
end

return f
end


function MA.operate!(
::typeof(-),
f::PBF{<:Any,T},
c::T
) where {T}
if iszero(c)
return f
end

f[nothing] -= c

return f
end

# -*- MA Arithmetic: (*) -*-

function MA.operate!(
pedromxavier marked this conversation as resolved.
Show resolved Hide resolved
::typeof(*),
f::PBF{S,T},
g::PBF{S,T}
) where {S,T}

h = PBF{S,T}()

if isempty(f) || isempty(g)
return h
else
for (ωᵢ, cᵢ) in f, (ωⱼ, cⱼ) in g
h[union(ωᵢ, ωⱼ)] += cᵢ * cⱼ
end

return h
end

end

function MA.operate!(
::typeof(*),
f::PBF{S,T},
a::T
) where {S,T}

if iszero(a)
empty!(f)
else
for(ω, c) in f
f[ω] = c*a
end
end

return f
end

# -*- MA Arithmetic: (add_mul) -*-
function MA.operate!(
::typeof(add_mul),
f::PBF{S,T},
m::T,
g::PBF{S,T}
) where {S,T}

for(ω, c) in g
f[ω] += m*c
end
return f

end

# -*- MA Arithmetic: (/) -*-

function MA.operate!(
::typeof(/),
f::PBF{S,T},
a::T
) where {S,T}

if iszero(a)
throw(DivideError())
else
for(ω, c) in f
f[ω] = c / a
end
end

return f
end

# -*- MA Arithmetic: (^) -*-
function MA.operate!(
::typeof(^),
f::PBF{S,T},
n::Integer
) where {S,T}

if n < 0
throw(DivideError())
elseif n == 0
return MA.operate!(one, f)
elseif n == 1
return f
elseif n == 2
for (ω,c) in f
f[ω] = c*c
end
else
for (ω,c) in f
f[ω] = c*c
end

if iseven(n)
return f^(n ÷ 2)
else
return f^(n ÷ 2) * f
end
end
end


function MA.operate_to!(
output::PBF{S,T},
op::Union{typeof(+),typeof(-)},
f::PBF{S,T},
g::PBF{S,T},
) where {S,T}
empty!(output)
MA.operate!(+, output, f)
MA.operate!(op, output, g)
return output
end



# -*- MA Arithmetic: Evaluation -*-



# -*- MA Type conversion -*-


1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Anneal = "e4d9eb7f-b088-426e-aeb5-1c0dae3d8abb"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand Down
Loading