Skip to main content

problemreductions/rules/
qubo_casts.rs

1//! Numeric variant reduction for QUBO.
2
3use crate::impl_variant_reduction;
4use crate::models::algebraic::QUBO;
5use crate::rules::ReductionError;
6use crate::types::i64_to_exact_f64;
7
8impl_variant_reduction!(
9    QUBO,
10    <i64> => <f64>,
11    fields: [num_vars],
12    |src| {
13        let matrix = src
14            .matrix()
15            .iter()
16            .map(|row| {
17                row.iter()
18                    .copied()
19                    .map(i64_to_exact_f64)
20                    .collect::<Result<Vec<_>, _>>()
21            })
22            .collect::<Result<Vec<_>, _>>()
23            .map_err(|error| {
24                ReductionError::inexact_float_conversion::<QUBO<i64>, QUBO<f64>>(error)
25            })?;
26        QUBO::from_matrix(matrix)
27            .map_err(ReductionError::construction::<QUBO<i64>, QUBO<f64>>)?
28    }
29);
30
31#[cfg(test)]
32#[path = "../unit_tests/rules/qubo_casts.rs"]
33mod tests;