void apply_lnn( circuit& circ, const circuit& base, lnn_optimization_mode operation, gate::line allocation[]) { unsigned index = 0; foreach( const gate& g, base) { if(is_toffoli(g)) { //do nothing for this gate } else { gate_details gate_d= calculate_gate_details(g, operation, allocation, base); //std::cout << gate_d.distance << std::endl; if(gate_d.control_exists) { unsigned abs_dist = abs(gate_d.distance); if(abs_dist > 0) { index+=abs_dist; adding_swap_gates(circ, gate_d , index, operation, allocation); if(operation == LNN_OPTIMIZATION_NAIVE) index+=abs_dist; //remove old gate circ.remove_gate_at(index+1); } else { if(operation == LNN_OPTIMIZATION_LOCAL_REORDER) { circ.insert_gate(index) = gate_d.mod_gate; circ.remove_gate_at(index+1); } } } index++; } } }
void adding_swap_gates(circuit& circ, struct gate_details gate_details, unsigned index, lnn_optimization_mode operation, gate::line allocation[]) { gate cur_gate = gate_details.mod_gate; gate::line target_line = gate_details.target; int distance = gate_details.distance; unsigned abs_dist = abs(distance); //swap infront for(unsigned i = 0; i< abs_dist; i++) { cur_gate.remove_target(target_line); unsigned tmptar = target_line; if(distance>0) { tmptar--; insert_swap_gate(circ, index-abs_dist+i, target_line, tmptar ); if(operation == LNN_OPTIMIZATION_LOCAL_REORDER) { gate::line tmp = allocation[target_line]; allocation[target_line] = allocation[tmptar]; allocation[tmptar] = tmp; } target_line--; } else { tmptar++; insert_swap_gate(circ, index-abs_dist+i, target_line, tmptar); if(operation == LNN_OPTIMIZATION_LOCAL_REORDER) { gate::line tmp = allocation[target_line]; allocation[target_line] = allocation[tmptar]; allocation[tmptar] = tmp; } target_line++; } cur_gate.add_target(target_line); } //now a gate with lnn distance from 0 can be added circ.insert_gate(index) = cur_gate; //swap behind if(operation == LNN_OPTIMIZATION_NAIVE) { for(unsigned i = 0; i< abs(distance); i++) { //invers to the other for loop if(distance>0 ) { insert_swap_gate(circ, index+1+i, target_line, target_line+1 ); target_line++; } else { insert_swap_gate(circ, index+1+i, target_line, target_line-1); target_line--; } } } }