boschmitt / tweedledum

C++17 Library for analysis, compilation/synthesis, and optimization of quantum circuits
MIT License
97 stars 36 forks source link

Failure reading DIMACS files with 10 variables or more #185

Open natanaeljr opened 11 months ago

natanaeljr commented 11 months ago

Describe the bug

Reading DIMACS files fails with files having more than 10 variables from CNF.

Reproducible example code

main.cpp:

#include <lorina/common.hpp>
#include <tweedledum/Synthesis/pkrm_synth.h>
#include <kitty/dynamic_truth_table.hpp>
#include <mockturtle/networks/xag.hpp>
#include <mockturtle/io/dimacs_reader.hpp>
#include <tweedledum/Utils/Classical/xag_simulate.h>
#include <lorina/dimacs.hpp>

using namespace tweedledum;

int main()
{
  mockturtle::xag_network xag;
  lorina::return_code ret = lorina::read_dimacs("graphE.cnf", mockturtle::dimacs_reader(xag));
  if (ret != lorina::return_code::success)
    return 1;
  std::vector<kitty::dynamic_truth_table> truth_tables = xag_simulate(xag);
  Circuit circuit = pkrm_synth(truth_tables[0]);
  return 0;
}

graphE.cnf:

p cnf 12 31
1 2 3 0
4 5 6 0
7 8 9 0
10 11 12 0
-1 -2 0
-1 -3 0
-2 -3 0
-4 -5 0
-4 -6 0
-5 -6 0
-7 -8 0
-7 -9 0
-8 -9 0
-10 -11 0
-10 -12 0
-11 -12 0
-1 -4 0
-2 -5 0
-3 -6 0
-1 -7 0
-2 -8 0
-3 -9 0
-10 -1 0
-2 -11 0
-3 -12 0
-4 -7 0
-5 -8 0
-6 -9 0
-4 -10 0
-5 -11 0
-6 -12 0

Expected behavior

The DIMACS file is successfully read and the circuit is synthetized.

Information

Additional context

Suggested fix for this issue:

diff --git a/external/lorina/lorina/dimacs.hpp b/external/lorina/lorina/dimacs.hpp
index dc44b9a..e8bd60f 100644
--- a/external/lorina/lorina/dimacs.hpp
+++ b/external/lorina/lorina/dimacs.hpp
@@ -99,7 +99,7 @@ public:
 namespace dimacs_regex
 {
 static std::regex problem_spec( R"(^p\s+([cd]nf)\s+([0-9]+)\s+([0-9]+)$)" );
-static std::regex clause( R"(((-?[1-9]+)+ +)+0)" );
+static std::regex clause( R"(((-?[1-9][0-9]*)+ +)+0)" );

 } // namespace dimacs_regex