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 _get_device_calibration(device_name: str): """Get device calibration. Use an LRU cache to avoid repeated calls to the web interface. It's possible this is not what you want. TODO: move to recirq.engine_utils. """ processor_id = recirq.get_processor_id_by_device_name(device_name) if processor_id is None: # TODO: https://github.com/quantumlib/ReCirq/issues/14 device_obj = recirq.get_device_obj_by_name(device_name) dummy_graph = ccr.gridqubits_to_graph_device(device_obj.qubits) nx.set_edge_attributes(dummy_graph, name='weight', values=0.01) return dummy_graph calibration = cg.get_engine_calibration(processor_id) err_graph = calibration_data_to_graph(calibration) return err_graph
async def collect_readout_calibration(task: ReadoutCalibrationTask, base_dir=None): if base_dir is None: base_dir = DEFAULT_BASE_DIR if recirq.exists(task, base_dir=base_dir): print(f"{task.fn} already exists. Skipping.") return sampler = recirq.get_sampler_by_name(device_name=task.device_name) device = recirq.get_device_obj_by_name(device_name=task.device_name) readout_calibration_result = await _estimate_single_qubit_readout_errors_async( sampler=sampler, qubits=sorted(device.qubits), repetitions=task.n_shots) recirq.save(task=task, data={ 'calibration': readout_calibration_result, }, base_dir=base_dir)
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.")
def _get_device_graph(device_name: str) -> nx.Graph: """Helper function to get the qubit connectivity for a given named device""" device = recirq.get_device_obj_by_name(device_name) device_graph = ccr.gridqubits_to_graph_device(device.qubits) return device_graph
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()
def test_device_obj(): assert recirq.get_device_obj_by_name('Sycamore23') \ == cirq.google.Sycamore23