Skip to content

Commit

Permalink
Linear.to_{qubo,pubo} methods
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Nov 12, 2024
1 parent d052a9b commit bd3567c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions rust/ommx/src/convert/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ impl Linear {
pub fn used_decision_variable_ids(&self) -> BTreeSet<u64> {
self.terms.iter().map(|term| term.id).collect()
}

pub fn to_qubo(&self) -> (BTreeMap<(u64, u64), f64>, f64) {
(
self.terms
.iter()
.map(|term| ((term.id, term.id), term.coefficient))
.collect(),
self.constant,
)
}

pub fn to_pubo(&self) -> BTreeMap<Vec<u64>, f64> {
self.terms
.iter()
.map(|term| (vec![term.id], term.coefficient))
.chain(std::iter::once((vec![], self.constant)))
.collect()
}
}

/// Create a linear function with a single term by regarding the input as the id of the term.
Expand Down Expand Up @@ -279,4 +297,26 @@ mod tests {
assert_eq!(linear.to_string(), "x1 - 1");
assert_eq!(format!("{:.2}", linear), "x1 - 1.00");
}

#[test]
fn to_qubo() {
let linear = super::Linear::new([(1, 1.0), (2, -1.0), (3, -2.0)].into_iter(), 3.0);
let (qubo, constant) = linear.to_qubo();
assert_eq!(qubo.len(), 3);
assert_eq!(qubo[&(1, 1)], 1.0);
assert_eq!(qubo[&(2, 2)], -1.0);
assert_eq!(qubo[&(3, 3)], -2.0);
assert_eq!(constant, 3.0);
}

#[test]
fn to_pubo() {
let linear = super::Linear::new([(1, 1.0), (2, -1.0), (3, -2.0)].into_iter(), 3.0);
let pubo = linear.to_pubo();
assert_eq!(pubo.len(), 4);
assert_eq!(pubo[&vec![1]], 1.0);
assert_eq!(pubo[&vec![2]], -1.0);
assert_eq!(pubo[&vec![3]], -2.0);
assert_eq!(pubo[&vec![]], 3.0);
}
}

0 comments on commit bd3567c

Please sign in to comment.