def test_random_device_placer_small_device(): topo = cirq.TiltedSquareLattice(3, 3) qubits = sorted(topo.nodes_to_gridqubits().values()) circuit = cirq.experiments.random_rotations_between_grid_interaction_layers_circuit( qubits, depth=8, two_qubit_op_factory=lambda a, b, _: cirq.SQRT_ISWAP(a, b) ) qp = cg.RandomDevicePlacer() with pytest.raises(cg.CouldNotPlaceError): qp.place_circuit( circuit, problem_topology=topo, shared_rt_info=cg.SharedRuntimeInfo(run_id='1', device=cg.Foxtail), rs=np.random.RandomState(1), )
def test_hardcoded_qubit_placer(): rainbow_record = cg.SimulatedProcessorWithLocalDeviceRecord('rainbow') rainbow_device = rainbow_record.get_device() rainbow_graph = rainbow_device.metadata.nx_graph hardcoded = cg.HardcodedQubitPlacer(_all_offset_placements(rainbow_graph)) topo = cirq.TiltedSquareLattice(3, 2) circuit = cirq.experiments.random_rotations_between_grid_interaction_layers_circuit( qubits=sorted(topo.nodes_as_gridqubits()), depth=4) shared_rt_info = cg.SharedRuntimeInfo(run_id='example', device=rainbow_device) rs = np.random.RandomState(10) placed_c, placement = hardcoded.place_circuit( circuit, problem_topology=topo, shared_rt_info=shared_rt_info, rs=rs) cirq.is_valid_placement(rainbow_graph, topo.graph, placement) assert isinstance(placed_c, cirq.FrozenCircuit)
def test_device_missing_metadata(): class BadDevice(cirq.Device): pass topo = cirq.TiltedSquareLattice(3, 3) qubits = sorted(topo.nodes_to_gridqubits().values()) circuit = cirq.experiments.random_rotations_between_grid_interaction_layers_circuit( qubits, depth=8, two_qubit_op_factory=lambda a, b, _: cirq.SQRT_ISWAP(a, b)) qp = cg.RandomDevicePlacer() with pytest.raises(ValueError): qp.place_circuit( circuit, problem_topology=topo, shared_rt_info=cg.SharedRuntimeInfo(run_id='1', device=BadDevice()), rs=np.random.RandomState(1), )
def test_random_device_placer_tilted_square_lattice(): topo = cirq.TiltedSquareLattice(4, 2) qubits = sorted(topo.nodes_to_gridqubits().values()) circuit = cirq.experiments.random_rotations_between_grid_interaction_layers_circuit( qubits, depth=8, two_qubit_op_factory=lambda a, b, _: cirq.SQRT_ISWAP(a, b) ) assert not all(q in cg.Sycamore23.qubit_set() for q in circuit.all_qubits()) qp = cg.RandomDevicePlacer() circuit2, mapping = qp.place_circuit( circuit, problem_topology=topo, shared_rt_info=cg.SharedRuntimeInfo(run_id='1', device=cg.Sycamore23), rs=np.random.RandomState(1), ) assert circuit is not circuit2 assert circuit != circuit2 assert all(q in cg.Sycamore23.qubit_set() for q in circuit2.all_qubits()) for k, v in mapping.items(): assert k != v
def _all_offset_placements(device_graph, offset=(4, 2), min_sidelength=2, max_sidelength=5): # Generate candidate tilted square lattice topologies sidelens = list( itertools.product(range(min_sidelength, max_sidelength + 1), repeat=2)) topos = [ cirq.TiltedSquareLattice(width, height) for width, height in sidelens ] # Make placements using TiltedSquareLattice.nodes_to_gridqubits offset parameter placements = { topo: topo.nodes_to_gridqubits(offset=offset) for topo in topos } # Only allow placements that are valid on the device graph placements = { topo: mapping for topo, mapping in placements.items() if cirq.is_valid_placement(device_graph, topo.graph, mapping) } return placements