def forward_backward_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, ): _seed_random() start = now() mapping_procedure_calls = 0 best_cnot_number = float("inf") while ( # We want to have at least 2 allowed calls to the mapping procedure. allowed_calls_to_mapping - mapping_procedure_calls > 1 and best_cnot_number > 0): mapping, nbcalls = initial_mapping_from_iterative_forward_backward( circuit, hardware, wrap_iterative_mapping_algorithm, maximum_mapping_procedure_calls=(allowed_calls_to_mapping - mapping_procedure_calls), ) mapping_procedure_calls += nbcalls modified_circuit, _ = ha_mapping(circuit, mapping, hardware) op_count = modified_circuit.count_ops() best_cnot_number = min( best_cnot_number, 3 * op_count.get("swap", 0) + op_count.get("cx", 0)) return best_cnot_number, now() - start
def random_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, allowed_calls_to_mapping_checkpoints: ty.List[int], ): _seed_random() best_random_cnot_number = float("inf") cnots_results = [] times = [] start = now() for i in range(allowed_calls_to_mapping): mapping = { qubit: i for qubit, i in zip(circuit.qubits, permutation(range(hardware.qubit_number))) } modified_circuit, _ = ha_mapping(circuit, mapping, hardware) op_count = modified_circuit.count_ops() best_random_cnot_number = min( best_random_cnot_number, 3 * op_count.get("swap", 0) + op_count.get("cx", 0)) if i + 1 in allowed_calls_to_mapping_checkpoints: cnots_results.append(best_random_cnot_number) times.append(now() - start) return cnots_results, times
def forward_backward_annealing_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, ): _seed_random() start = now() mapping, nbcals = initial_mapping_from_iterative_forward_backward( circuit, hardware, wrap_iterative_mapping_algorithm, maximum_mapping_procedure_calls=allowed_calls_to_mapping, ) if allowed_calls_to_mapping - nbcals > 2: n = allowed_calls_to_mapping - nbcals mapping, cost, iteration_number = get_initial_mapping_from_annealing( get_mapping_cost, circuit, hardware, max_steps=n, initial_mapping=mapping, temp_begin=1.0, schedule_func=lambda t: 10**(-6 / n) * t, ) modified_circuit, _ = ha_mapping(circuit, mapping, hardware) op_count = modified_circuit.count_ops() return 3 * op_count.get("swap", 0) + op_count.get("cx", 0), now() - start
def cost_function(mapping, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture): mapped_circuit, final_mapping = ha_mapping(circuit, mapping, hardware) count = mapped_circuit.count_ops() assert ("cx" in count) != ("cnot" in count) return (3 * count.get("swap", 0) + count.get("cx", 0) + count.get("cnot", 0) + 4 * count.get("bridge", 0))
def annealing_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, ): _seed_random() start = now() mapping, cost, iteration_number = get_initial_mapping_from_annealing( get_mapping_cost, circuit, hardware, max_steps=allowed_calls_to_mapping - 1, temp_begin=1.0, schedule_func=lambda t: 10**(-6 / (allowed_calls_to_mapping - 1)) * t, ) modified_circuit, _ = ha_mapping(circuit, mapping, hardware) op_count = modified_circuit.count_ops() return 3 * op_count.get("swap", 0) + op_count.get("cx", 0), now() - start
def using_only_swap_strategy( circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, mapping: ty.Dict[Qubit, int], ): start = now() modified_circuit, _ = ha_mapping( circuit, mapping, hardware, swap_cost_heuristic=sabre_heuristic, get_distance_matrix=get_distance_matrix_swap_number, get_candidates=get_all_swap_candidates, ) duration = now() - start cnot_count = 3 * modified_circuit.count_ops().get( "swap", 0) + modified_circuit.count_ops().get("cx", 0) # print(modified_circuit.draw("text")) return cnot_count, duration
def forward_backward_neighbour_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, ): _seed_random() start = now() mapping = get_random_mapping(circuit) cnots = [] while allowed_calls_to_mapping > 1: mapping, nbcalls = initial_mapping_from_iterative_forward_backward( circuit, hardware, wrap_iterative_mapping_algorithm, maximum_mapping_procedure_calls=allowed_calls_to_mapping, initial_mapping=mapping, ) allowed_calls_to_mapping -= nbcalls modified_circuit, _ = ha_mapping(circuit, mapping, hardware) op_count = modified_circuit.count_ops() cnots.append(3 * op_count.get("swap", 0) + op_count.get("cx", 0)) mapping = get_neighbour_random(mapping) return min(cnots), now() - start
def annealing_sabre_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, ): _seed_random() start = now() mapping = initial_mapping_from_sabre(circuit, hardware, wrap_iterative_mapping_algorithm) # We want at least 2 steps in the annealing procedure. if allowed_calls_to_mapping > 4: n = allowed_calls_to_mapping - 3 mapping, cost, iteration_number = get_initial_mapping_from_annealing( get_mapping_cost, circuit, hardware, max_steps=n, initial_mapping=mapping, temp_begin=1.0, schedule_func=lambda t: 10**(-6 / n) * t, ) modified_circuit, _ = ha_mapping(circuit, mapping, hardware) op_count = modified_circuit.count_ops() return 3 * op_count.get("swap", 0) + op_count.get("cx", 0), now() - start
def sabre_strategy_results( allowed_calls_to_mapping: int, circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, allowed_calls_to_mapping_checkpoints: ty.List[int], ): _seed_random() best_random_cnot_number = float("inf") cnots = [] times = [] start = now() for i in range(int(ceil(allowed_calls_to_mapping / 2))): initial_mapping = initial_mapping_from_sabre( circuit, hardware, wrap_iterative_mapping_algorithm) modified_circuit, _ = ha_mapping(circuit, initial_mapping, hardware) op_count = modified_circuit.count_ops() best_random_cnot_number = min( best_random_cnot_number, 3 * op_count.get("swap", 0) + op_count.get("cx", 0)) if (2 * (i + 1) in allowed_calls_to_mapping_checkpoints or 2 * i + 1 in allowed_calls_to_mapping_checkpoints): cnots.append(best_random_cnot_number) times.append(now() - start) return cnots, times
def mapping_algorithm( quantum_circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, mapping: ty.Dict[Qubit, int], ): return ha_mapping(quantum_circuit, mapping, hardware)
def get_mapping_cost(mapping, quantum_circuit, hardware) -> float: mapped_quantum_circuit, _ = ha_mapping(quantum_circuit, mapping, hardware) return mapped_quantum_circuit.size() - quantum_circuit.size()
def wrap_iterative_mapping_algorithm( quantum_circuit: QuantumCircuit, hardware: IBMQHardwareArchitecture, mapping, ): return ha_mapping(quantum_circuit, mapping, hardware)
def mapping_algorithm(circuit, hardware, mapping): return ha_mapping(circuit, mapping, hardware)