7 #ifndef QBPP_MULTIPLIER_HPP
8 #define QBPP_MULTIPLIER_HPP
14 namespace factorization {
27 opt_sol.push_back({x,
eval(a, opt_sol) &
eval(b, opt_sol)});
28 return a * b - 2 * a * x - 2 * b * x + 3 * x;
45 auto val_x =
eval(x, opt_sol);
46 auto val_y =
eval(y, opt_sol);
47 opt_sol.push_back({s, val_x ^ val_y});
48 opt_sol.push_back({c, val_x & val_y});
49 return s + x + y + 4 * c - 4 * c * x - 4 * c * y - 2 * s * x - 2 * s * y +
50 2 * x * y + 4 * c * s;
68 auto val_x =
eval(x, opt_sol);
69 auto val_y =
eval(y, opt_sol);
70 auto val_z =
eval(z, opt_sol);
71 opt_sol.push_back({s, val_x ^ val_y ^ val_z});
72 opt_sol.push_back({c, (val_x & val_y) | (val_y & val_z) | (val_z & val_x)});
73 return s + x + y + z + 4 * c - 4 * c * x - 4 * c * y - 4 * c * z - 2 * s * x -
74 2 * s * y - 2 * s * z + 2 * x * y + 2 * x * z + 2 * y * z + 4 * c * s;
94 template <
typename T,
typename U>
100 throw std::runtime_error(
"Invalid input size");
103 for (
size_t i = 1; i < x.
size(); i++) {
121 template <
typename T,
typename U>
124 int Xsize =
static_cast<int>(x.
size());
125 int Ysize =
static_cast<int>(y.
size());
126 int Zsize =
static_cast<int>(z.
size());
127 if (Xsize + Ysize != Zsize) {
128 throw std::runtime_error(
"Invalid input size");
135 auto p =
var(
"p", Ysize, Xsize);
137 for (
int i = 0; i < Ysize; i++)
138 for (
int j = 0; j < Xsize; j++) {
144 auto s =
var(
"s", Ysize - 1, Xsize);
148 auto c =
var(
"c", Ysize, Xsize);
155 expr +=
adder(P, p[1], s[0], c[0], opt_sol);
157 for (
int i = 1; i < Ysize - 1; ++i) {
160 expr +=
adder(p[i + 1], S, s[i], c[i], opt_sol);
164 MapList map = {{p[0][0], z[0]}};
167 for (
int i = 1; i < Ysize; ++i) {
172 for (
int j = 1; j < Xsize; ++j) {
173 map.push_back({s[Ysize - 2][j], z[j + Ysize - 1]});
176 map.push_back({c[Ysize - 2][Xsize - 1], z[Xsize + Ysize - 1]});
Expr & simplify_as_binary()
Expr & replace(const MapList &map_list)
void push_back(const T &t)
Expr and_gate(const Expr &a, const Expr &b, Var x, MapList &opt_sol)
Function to generate a QUBO expression Expr object for the AND gate.
Expr multiplier(const qbpp::Vector< T > &x, const qbpp::Vector< U > &y, const qbpp::Vector< Var > &z, MapList &opt_sol)
Function to generate QUBO expression for multiplier.
Expr adder(const qbpp::Vector< T > &x, const qbpp::Vector< U > &y, const qbpp::Vector< Var > &s, const qbpp::Vector< Var > &c, MapList &opt_sol)
function to generate QUBO expression for n-bit adder
Expr half_adder(const Expr &x, const Expr &y, Var s, Var c, MapList &opt_sol)
Function to generates a QUBO expression Expr object for the Half Adder.
Expr full_adder(const Expr &x, const Expr &y, const Expr &z, Var s, Var c, MapList &opt_sol)
Function to generate a QUBO expression Expr object for the Full Adder.
Generates a QUBO Expression for the Graph Coloring Problem using QUBO++ library.
Var var(const std::string &var_str)
energy_t eval(const Expr &expr, const Sol &sol)
std::list< std::pair< std::variant< Var, VarInt >, Expr > > MapList
QUBO++, a C++ library for generating expressions for binary and spin variables.