bool circuit_to_truth_table( const circuit& circ, binary_truth_table& spec, const functor<bool(boost::dynamic_bitset<>&, const circuit&, const boost::dynamic_bitset<>&)>& simulation ) { // number of patterns to check depends on partial or non-partial simulation boost::dynamic_bitset<>::size_type n = ( simulation.settings() && simulation.settings()->get<bool>( "partial", false ) ) ? std::count( circ.constants().begin(), circ.constants().end(), constant() ) : circ.lines(); boost::dynamic_bitset<> input( n, 0u ); do { boost::dynamic_bitset<> output; if ( simulation( output, circ, input ) ) { binary_truth_table::cube_type in_cube, out_cube; bitset_to_vector( in_cube, input ); bitset_to_vector( out_cube, output ); spec.add_entry( in_cube, out_cube ); } else { return false; } } while ( !inc( input ).none() ); // metadata spec.set_inputs( circ.inputs() ); spec.set_outputs( circ.outputs() ); spec.set_constants( circ.constants() ); spec.set_garbage( circ.garbage() ); return true; }
bool xorsat_equivalence_check( const circuit& circ1, const circuit& circ2, const properties::ptr& settings, const properties::ptr& statistics ) { /* settings */ const auto name_mapping = get( settings, "name_mapping", false ); const auto tmpname = get( settings, "tmpname", std::string( "/tmp/test.cnf" ) ); /* timing */ properties_timer t( statistics ); if ( circ1.lines() != circ2.lines() ) { std::cout << "[e] circuits do not have the same number of lines" << std::endl; return false; } circuit id_circ; if ( name_mapping ) { circuit circ2_copy; copy_circuit( circ2, circ2_copy ); const auto operm = derive_output_permutation( circ1.outputs(), circ2.outputs() ); //std::cout << "operm: " << any_join( operm, " " ) << std::endl; const auto idx = circ2_copy.num_gates(); for ( const auto& t : permutation_to_transpositions( operm ) ) { //std::cout << t.first << " " << t.second << std::endl; insert_cnot( circ2_copy, idx, t.first, t.second ); insert_cnot( circ2_copy, idx, t.second, t.first ); insert_cnot( circ2_copy, idx, t.first, t.second ); } id_circ = create_identity_miter( circ1, circ2_copy ); } else { id_circ = create_identity_miter( circ1, circ2 ); } write_to_dimacs( id_circ, tmpname ); return solve_identity_miter( tmpname ); }
unsigned add_line_to_circuit( circuit& circ, const std::string& input, const std::string& output, const constant& c, bool g ) { std::vector<std::string> ins = circ.inputs(); std::vector<std::string> outs = circ.outputs(); std::vector<constant> cs = circ.constants(); std::vector<bool> gar = circ.garbage(); circ.set_lines( circ.lines() + 1u ); ins += input; circ.set_inputs( ins ); outs += output; circ.set_outputs( outs ); cs += c; circ.set_constants( cs ); gar += g; circ.set_garbage( gar ); return circ.lines() - 1u; }
void write_realization( const circuit& circ, std::ostream& os, const write_realization_settings& settings ) { unsigned oldsize = 0; if ( !settings.header.empty() ) { std::string header = settings.header; boost::algorithm::replace_all( header, "\n", "\n# " ); os << "# " << header << std::endl; } if ( !settings.version.empty() ) { os << ".version " << settings.version << std::endl; } os << ".numvars " << circ.lines() << std::endl; std::vector<std::string> variables( circ.lines() ); for ( unsigned i = 0u; i < circ.lines(); ++i ) { variables[i] = boost::str( boost::format( "x%d" ) % i ); } std::vector<std::string> _inputs( circ.inputs().begin(), circ.inputs().end() ); oldsize = _inputs.size(); _inputs.resize( circ.lines() ); for ( unsigned i = oldsize; i < circ.lines(); ++i ) { _inputs[i] = boost::str( boost::format( "i%d" ) % i ); } std::vector<std::string> _outputs( circ.outputs().begin(), circ.outputs().end() ); oldsize = _outputs.size(); _outputs.resize( circ.lines() ); for ( unsigned i = oldsize; i < circ.lines(); ++i ) { _outputs[i] = boost::str( boost::format( "o%d" ) % i ); } os << ".variables " << boost::algorithm::join( variables, " " ) << std::endl; namespace karma = boost::spirit::karma; namespace ascii = boost::spirit::ascii; os << ".inputs"; //std::ostream_iterator<char> outit( os ); //karma::generate_delimited( outit, *( karma::no_delimit['"' << karma::string] << '"' ), ascii::space, _inputs ); for ( const auto& _input : _inputs ) { std::string quote = ( _input.find( " " ) != std::string::npos ) ? "\"" : ""; os << boost::format( " %s%s%s" ) % quote % _input % quote; } os << std::endl; os << ".outputs"; //karma::generate_delimited( outit, *( karma::no_delimit['"' << karma::string] << '"' ), ascii::space, _outputs ); for ( const auto& _output : _outputs ) { std::string quote = ( _output.find( " " ) != std::string::npos ) ? "\"" : ""; os << boost::format( " %s%s%s" ) % quote % _output % quote; } os << std::endl; std::string _constants( circ.lines(), '-' ); std::transform( circ.constants().begin(), circ.constants().end(), _constants.begin(), constant_to_char() ); std::string _garbage( circ.lines(), '-' ); std::transform( circ.garbage().begin(), circ.garbage().end(), _garbage.begin(), garbage_to_char() ); os << ".constants " << _constants << std::endl << ".garbage " << _garbage << std::endl; for ( const auto& bus : circ.inputbuses().buses() ) { std::vector<std::string> lines; std::transform( bus.second.begin(), bus.second.end(), std::back_inserter( lines ), line_to_variable() ); os << ".inputbus " << bus.first << " " << boost::algorithm::join( lines, " " ) << std::endl; } for ( const auto& bus : circ.outputbuses().buses() ) { std::vector<std::string> lines; std::transform( bus.second.begin(), bus.second.end(), std::back_inserter( lines ), line_to_variable() ); os << ".outputbus " << bus.first << " " << boost::algorithm::join( lines, " " ) << std::endl; } for ( const auto& bus : circ.statesignals().buses() ) { std::vector<std::string> lines; std::transform( bus.second.begin(), bus.second.end(), std::back_inserter( lines ), line_to_variable() ); os << ".state " << bus.first << " " << boost::algorithm::join( lines, " " ) << std::endl; } for ( const auto& module : circ.modules() ) { os << ".module " << module.first << std::endl; write_realization_settings module_settings; module_settings.version.clear(); module_settings.header.clear(); write_realization( *module.second, os, module_settings ); } os << ".begin" << std::endl; std::string cmd; for ( const auto& g : circ ) { cmd = settings.type_label( g ); std::vector<std::string> lines; // Peres is special boost::transform( g.controls(), std::back_inserter( lines ), line_to_variable() ); boost::transform( g.targets(), std::back_inserter( lines ), line_to_variable() ); os << cmd << " " << boost::algorithm::join( lines, " " ); boost::optional<const std::map<std::string, std::string>&> annotations = circ.annotations( g ); if ( annotations ) { std::string sannotations; for ( const auto& p : *annotations ) { sannotations += boost::str( boost::format( " %s=\"%s\"" ) % p.first % p.second ); } os << " #@" << sannotations; } os << std::endl; } os << ".end" << std::endl; }