def _get_circuit( problem: ProblemT, gammas: Sequence[float], betas: Sequence[float], device_name: str ) -> Tuple[List[cirq.Qid], cirq.Circuit, List[cirq.Qid]]: if isinstance(problem, HardwareGridProblem): initial_qubits = [cirq.GridQubit(r, c) for r, c in problem.coordinates] circuit, final_qubits = get_compiled_hardware_grid_circuit( problem=problem, qubits=initial_qubits, gammas=gammas, betas=betas, non_negligible=False) elif isinstance(problem, SKProblem): initial_qubits = place_line_on_device( device_name=device_name, n=problem.graph.number_of_nodes(), line_placement_strategy='mixed') circuit, final_qubits = get_compiled_sk_model_circuit( problem=problem, qubits=initial_qubits, gammas=gammas, betas=betas, non_negligible=False) elif isinstance(problem, ThreeRegularProblem): device = recirq.get_device_obj_by_name(device_name=device_name) (initial_qubits, circuit, final_qubits) = get_compiled_3_regular_maxcut_circuit(problem=problem, device=device, gammas=gammas, betas=betas) else: raise ValueError("Unknown problem: {}".format(problem)) return initial_qubits, circuit, final_qubits
def test_on_device(n, method): err_graph = _fake_calib_data() path = place_line_on_device('Sycamore23', n=n, line_placement_strategy=method, err_graph=err_graph) if n == 13: if method == 'greedy' or method == 'mst': assert path is None return assert nx.is_simple_path(err_graph, path)
async def collect_data(task: PrecomputedDataCollectionTask, base_dir=None, problem_generation_base_dir=None, precomputation_base_dir=None, ): """Collect and save data for the experiment specified by params. The associated problem generation data must already exist. """ if base_dir is None: base_dir = DEFAULT_BASE_DIR if problem_generation_base_dir is None: problem_generation_base_dir = DEFAULT_PROBLEM_GENERATION_BASE_DIR if precomputation_base_dir is None: precomputation_base_dir = DEFAULT_PRECOMPUTATION_BASE_DIR if recirq.exists(task, base_dir=base_dir): print(f"{task.fn} already exists. Skipping.") return precompute_task = task.precomputation_task generation_task = precompute_task.generation_task problem = recirq.load(generation_task, base_dir=problem_generation_base_dir)[ 'problem'] # type: ProblemT optimum = recirq.load(precompute_task, base_dir=precomputation_base_dir)[ 'optimum'] # type: OptimizationResult sampler = recirq.get_sampler_by_name(device_name=task.device_name) device = recirq.get_device_obj_by_name(device_name=task.device_name) try: if isinstance(problem, HardwareGridProblem): initial_qubits = [cirq.GridQubit(r, c) for r, c in problem.coordinates] circuit, final_qubits = get_compiled_hardware_grid_circuit( problem=problem, qubits=initial_qubits, gammas=optimum.gammas, betas=optimum.betas, non_negligible=False) elif isinstance(problem, SKProblem): initial_qubits = place_line_on_device( device_name=task.device_name, n=problem.graph.number_of_nodes(), line_placement_strategy='mixed') circuit, final_qubits = get_compiled_sk_model_circuit( problem=problem, qubits=initial_qubits, gammas=optimum.gammas, betas=optimum.betas, non_negligible=False) elif isinstance(problem, ThreeRegularProblem): initial_qubits, circuit, final_qubits = get_compiled_3_regular_maxcut_circuit( problem=problem, device=device, gammas=optimum.gammas, betas=optimum.betas) else: raise ValueError("Unknown problem: {}".format(problem)) except BadlyStructuredCircuitError: print("!!!! Badly structured circuit: {}".format(task)) # TODO https://github.com/quantumlib/Cirq/issues/2553 return if not task.structured: # Left align circuit = compile_to_non_negligible(circuit) circuit = cirq.Circuit(circuit.all_operations()) if task.echoed: assert task.structured raise NotImplementedError("To be implemented in follow-up PR") violation_indices = find_circuit_structure_violations(circuit) circuit.program_id = task.fn result = await sampler.run_async(program=circuit, repetitions=task.n_shots) bitstrings = result.measurements['z'] recirq.save(task=task, data={ 'bitstrings': recirq.BitArray(bitstrings), 'qubits': initial_qubits, 'final_qubits': final_qubits, 'circuit': circuit, 'violation_indices': violation_indices, }, base_dir=base_dir) print(f"{task.fn} complete.")
async def collect_p1_landscape_data( task: P1LandscapeDataCollectionTask, base_dir=None, problem_generation_base_dir=None) -> None: if base_dir is None: base_dir = DEFAULT_BASE_DIR if problem_generation_base_dir is None: problem_generation_base_dir = DEFAULT_PROBLEM_GENERATION_BASE_DIR if recirq.exists(task, base_dir=base_dir): print(f"{task.fn} already exists. Skipping.") return generation_task = task.generation_task problem = recirq.load(generation_task, base_dir=problem_generation_base_dir)[ 'problem'] # type: ProblemT sampler = recirq.get_sampler_by_name(device_name=task.device_name) device = recirq.get_device_obj_by_name(device_name=task.device_name) if isinstance(problem, HardwareGridProblem): initial_qubits = [cirq.GridQubit(r, c) for r, c in problem.coordinates] circuit, final_qubits = get_compiled_hardware_grid_circuit( problem=problem, qubits=initial_qubits, gammas=[task.gamma], betas=[task.beta], non_negligible=False) elif isinstance(problem, SKProblem): initial_qubits = place_line_on_device( device_name=task.device_name, n=problem.graph.number_of_nodes(), line_placement_strategy='mixed') circuit, final_qubits = get_compiled_sk_model_circuit( problem=problem, qubits=initial_qubits, gammas=[task.gamma], betas=[task.beta], non_negligible=False) elif isinstance(problem, ThreeRegularProblem): initial_qubits, circuit, final_qubits = get_compiled_3_regular_maxcut_circuit( problem=problem, device=device, gammas=[task.gamma], betas=[task.beta]) else: raise ValueError("Unknown problem: {}".format(problem)) flipped_circuit = circuit.copy() measurement_op = flipped_circuit[-1].operations[0] flipped_circuit = flipped_circuit[:-1] flipped_circuit.append(cirq.Moment(cirq.X.on_each(*measurement_op.qubits))) flipped_circuit.append( measurement_op.gate.with_bits_flipped(*range(problem.graph.number_of_nodes())).on( *measurement_op.qubits)) unmodified_n_shots = task.n_shots // 2 flipped_n_shots = task.n_shots - unmodified_n_shots t0 = timeit.default_timer() circuit.program_id = task.fn unmodified_result = await sampler.run_async(program=circuit, repetitions=unmodified_n_shots) circuit.program_id = task.fn + '-flip' flipped_result = await sampler.run_async(program=flipped_circuit, repetitions=flipped_n_shots) t1 = timeit.default_timer() result = unmodified_result + flipped_result execution_time = t1 - t0 print(f'Circuit execution time: {execution_time} s') t0 = timeit.default_timer() bitstrings = result.measurements['z'] recirq.save(task=task, data={ 'bitstrings': recirq.BitArray(bitstrings), 'qubits': initial_qubits, 'final_qubits': final_qubits, 'execution_time': execution_time }, base_dir=base_dir) t1 = timeit.default_timer() print(f'Time to save bitstrings: {t1 - t0} s') print()