Skip to content

Instantly share code, notes, and snippets.

@ondrik
Created March 5, 2025 07:01
Show Gist options
  • Save ondrik/b65212419e5a54a5c50f52a785a4daf7 to your computer and use it in GitHub Desktop.
Save ondrik/b65212419e5a54a5c50f52a785a4daf7 to your computer and use it in GitHub Desktop.
BDD a = ...;
BDD b = ...;
BDD gate_x(BDD op, size_t tgt)
{
auto x_op = [](BDD lhs, BDD rhs) {
return BDD(tgt, rhs, lhs);
};
BDD result = bdd_operation(op, tgt, x_op);
return result;
}
BDD gate_h(BDD op, size_t var)
{
auto h_op = [](BDD lhs, BDD rhs) {
auto plus_op = [](LEAF_TYPE lhs, LEAF_TYPE rhs) {
return 1/(sqrt(2)) * (lhs + rhs);
};
auto minus_op = [](LEAF_TYPE lhs, LEAF_TYPE rhs) {
return 1/(sqrt(2)) * (lhs - rhs);
};
BDD new_lhs = binary_apply(lhs, rhs, plus_op);
BDD new_rhs = binary_apply(lhs, rhs, minus_op);
return BDD(tgt, new_lhs, new_rhs);
}
BDD result = bdd_operation(op, tgt, h_op);
return result;
}
BDD gate_ccz(BDD op, size_t ctrl1, size_t ctrl2, size_t tgt)
{
assert(ctrl1 < ctrl2 && ctrl2 < tgt);
BDD result = bdd_operation(op, ctrl1, [](BDD lhs, BDD rhs) {return BDD(ctrl1, lhs, gate_cz(rhs, ctrl2, tgt))});
return result;
}
BDD gate_cz(BDD op, size_t ctrl, size_t tgt)
{
assert(ctrl < tgt);
BDD result = bdd_operation(op, ctrl, [](BDD lhs, BDD rhs) {return BDD(ctrl, lhs, gate_z(rhs, tgt))});
return result;
}
BDD gate_z(BDD op, size_t tgt)
{
auto z_op = [](BDD lhs, BDD rhs) {
auto inv = [](LEAF_TYPE val) { return -val; };
BDD new_rhs = unary_apply(rhs, inv);
return BDD(tgt, lhs, new_rhs);
}
BDD result = bdd_operation(op, tgt, z_op);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment