def test_idea2(): qc = quantum_circuit([3, 3, 2, 2, 2, 2], [2, 2, 2], 'Test idea 2', True) qc.special_encoding('null', 0, 1) qc.special_encoding('TF', 2, 3) #1 qc.instruct_notes('branch @ 0') qc.add_instruct(ops.branch(3), [0]) #2 qc.instruct_notes('|null> - |T> swap @ 1') swap = ops.swap(3, 0, 2) qc.add_instruct(swap, [1]) #3 qc.instruct_notes('Copy node info to ancillary memory') c32 = ops.copy32() apply_to = (0, 2), (1, 3) qc.add_instruct(ops.copy32(), *apply_to) #4 qc.instruct_notes('evaluate node @ val') qc.add_instruct(ops.SAME(), [2, 3, 4]) #5 qc.instruct_notes('copy val to continue') qc.add_instruct(ops.gates('cnot'), [4, 5]) #6 qc.instruct_notes('CONTINUE CONTROL: load other node') # double_swap = ops.gate_concat(ops.swap(3, 2, 1), ops.gates('not')) dirs5 = {(0, ): ops.gates('not')} ctrl5 = ops.create_control([2, 2], 0, 1, dirs5) qc.add_instruct(ctrl5, [5, 3]) #7 qc.instruct_notes('CONTINUE CONTROL: evaluate node') dirs6 = {(0, ): ops.SAME()} ctrl6 = ops.create_control([2, 2, 2, 2], 0, [1, 2, 3], dirs6) qc.add_instruct(ctrl6, [5, 2, 3, 4]) #8 qc.instruct_notes('Uncopy continue control') qc.add_instruct(ops.gates('cnot'), [4, 5]) #9 qc.instruct_notes('PREPARE MERGE: unevaluate') qc.add_instruct(ops.SAME(), [2, 3, 4]) # qc.instruct_notes('Move amplitudes up one node on the tree') # qc.add_instruct( swap, [1] ) # #9 # qc.instruct_notes('Unbranch @ 0') # qc.add_instruct( ops.branch(3), [0] ) qc.run() qc.index_aide()
def local_search(init, init_cost, max_iter, probA, probB, probC, problem): current = init best_solution = init cheapest_cost = init_cost for _ in range(max_iter): ran = random.uniform(0, 1) if ran < probA: current = operators.swap(best_solution) elif ran < probA + probB: current = operators.three_exchange(best_solution) else: current = operators.one_reinsert(best_solution, problem) feasible, _ = feasibility_check(current, problem) current_cost = cost_function(current, problem) if feasible and current_cost < cheapest_cost: best_solution = current cheapest_cost = current_cost return best_solution, cheapest_cost
def escape_algorithm(init, cheapest_cost, feasible_solutions, infeasible_solutions, problem): new_solution = init probA, probB, probC, probD = 1 / 5, 1 / 5, 1 / 5, 1 / 5 for _ in range(20): ran = random.uniform(0, 1) if ran < probA: current = operators.swap(new_solution) elif ran < probA + probB: current = operators.three_exchange(new_solution) elif ran < probA + probB + probC: current = operators.related_swap(new_solution, problem) elif ran < probA + probB + probC + probD: current = operators.smart_k_reinsert(new_solution, problem) else: current = operators.smart_one_reinsert(new_solution, problem) if tuple(current) in feasible_solutions: feasible = True elif tuple(current) in infeasible_solutions: feasible = False else: feasible, _ = feasibility_check(current, problem) if feasible: new_solution = current if tuple(new_solution) not in feasible_solutions: feasible_solutions[tuple(new_solution)] = cost_function( new_solution, problem) if feasible_solutions[tuple(new_solution)] < cheapest_cost: break else: if tuple(current) not in infeasible_solutions: infeasible_solutions.add(tuple(current)) return new_solution
def local_search_memoization(init, init_cost, feasible_solutions, infeasible_solutions, max_iter, probA, probB, probC, problem): current = init best_solution = init cheapest_cost = init_cost for _ in range(max_iter): ran = random.uniform(0, 1) if ran < probA: current = operators.swap(best_solution) elif ran < probA + probB: current = operators.three_exchange(best_solution) else: current = operators.one_reinsert(best_solution, problem) if tuple(current) in feasible_solutions: feasible = True current_cost = feasible_solutions[tuple(current)] elif tuple(current) in infeasible_solutions: feasible = False else: feasible, _ = feasibility_check(current, problem) current_cost = cost_function(current, problem) if feasible: if tuple(current) not in feasible_solutions: feasible_solutions[tuple(current)] = current_cost if current_cost < cheapest_cost: best_solution = current cheapest_cost = current_cost else: if tuple(current) not in infeasible_solutions: infeasible_solutions.add(tuple(current)) return best_solution, cheapest_cost
def testSwap(): list = genTestList() listUnChanged = list[:] print "TESTING SWAP:\n" print "ORIGINAL LIST:" print list print "SWAPS:" print operators.swap(list, 1, 10) print operators.swap(list, 49, 48) print operators.swap(list, 0, 49) print operators.swap(list, 0, 100) print "CHAINS:" list = operators.swap(list, 7, 25) print list list = operators.swap(list, 0, 5) print list list = operators.swap(list, 0, 49) print list print "SHOULD BE 50:" print len(list) print "SHOULD BE 50:" print len(listUnChanged) removeElements(listUnChanged, list) print "SHOULD BE ZERO:" print len(listUnChanged)