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

Interface UI target #17

Closed
joaquimg opened this issue Feb 8, 2022 · 3 comments · Fixed by #21
Closed

Interface UI target #17

joaquimg opened this issue Feb 8, 2022 · 3 comments · Fixed by #21
Labels
enhancement New feature or request

Comments

@joaquimg
Copy link
Member

joaquimg commented Feb 8, 2022

import MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities

#=
    1 Pure Annealer - working
=#

model = MOI.instantiate(ToQUBO.QuantumAnnealer.Optimizer, with_bridge_type = Float64)

x = MOI.add_variables(model, 2);

for xᵢ in x
    MOI.add_constraint(model, xᵢ, MOI.ZeroOne())
end

# alternatively
#=
x, c = MOI.add_constrained_variables(model, fill(MOI.ZeroOne(), 2))
=#

MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

MOI.set(
    model,
    MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
    MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0, 1.2], x), 0.0),
);

optimize!(model)

#=
    2 Pure Annealer - faile
=#

model = MOI.instantiate(ToQUBO.QuantumAnnealer.Optimizer, with_bridge_type = Float64)

x = MOI.add_variables(model, 2);

# will fail in `optimize!` because there is no auto conversion from Int to Bin
for xᵢ in x
    MOI.add_constraint(model, xᵢ, MOI.Integer())
end

# alternatively
#=
x, c = MOI.add_constrained_variables(model, fill(MOI.Integer(), 2))
=#

MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

MOI.set(
    model,
    MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
    MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0, 1.2], x), 0.0),
);

optimize!(model)


#=
    3 QUBO converter
=#

model = MOI.instantiate(
    ()->ToQUBO.Optimizer(annealer = ToQUBO.QuantumAnnealer.Optimizer),
    with_bridge_type = Float64)

x = MOI.add_variables(model, 2);

# will work because ToQUBO.Optimizer will know how to expand an integer variables
# into binaries
for xᵢ in x
    MOI.add_constraint(model, xᵢ, MOI.Integer())
end

MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

MOI.set(
    model,
    MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
    MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0, 1.2], x), 0.0),
);

optimize!(model)
@pedromxavier
Copy link
Contributor

That looks great! Thanks!

PS: Last example will fail since the variables xᵢ are unbounded. Do you think there is anything we could do about it, appart from yelling at the user about it? I got very interested on the InfiniteOpt.jl package due to quadratures and other stuff I just had in mind before.

@pedromxavier pedromxavier added the enhancement New feature or request label Feb 9, 2022
@joaquimg
Copy link
Member Author

joaquimg commented Feb 9, 2022

My bad writing the reference code, we need the bounds. The converter will return an error saying that as early as possible.

Lets talk about InfinitOpt offline.

@bernalde
Copy link
Collaborator

bernalde commented Feb 9, 2022

I'd be interested in being part of this conversation. You might not know, but the InfintOpt package developer is doing a postdoc at the department I'm currently at (and we're friends). The package is cool too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants