Exemplo n.º 1
0
def main(*, num_qubits: int, depth: int, num_circuits: int, seed: int,
         routes: int):
    """Run the quantum volume algorithm with a preset configuration.

    See the calculate_quantum_volume documentation for more details.

    Args:
        num_qubits: Pass-through to calculate_quantum_volume.
        depth: Pass-through to calculate_quantum_volume
        num_circuits: Pass-through to calculate_quantum_volume
        seed: Pass-through to calculate_quantum_volume

    Returns: Pass-through from calculate_quantum_volume.
    """
    device = cirq_google.Bristlecone
    compiler = lambda circuit: cirq_google.optimized_for_xmon(circuit=circuit)
    noisy = cirq.DensityMatrixSimulator(noise=cirq.ConstantQubitNoiseModel(
        qubit_noise_gate=cirq.DepolarizingChannel(p=0.005)))
    calculate_quantum_volume(
        num_qubits=num_qubits,
        depth=depth,
        num_circuits=num_circuits,
        random_state=seed,
        device_graph=routing.gridqubits_to_graph_device(device.qubits),
        samplers=[cirq.Simulator(), noisy],
        routing_attempts=routes,
        compiler=compiler,
    )
Exemplo n.º 2
0
def test_nx_qubit_layout():
    grid_qubit_graph = ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(5, 5))
    pos = ccr.nx_qubit_layout(grid_qubit_graph)
    assert len(pos) == len(grid_qubit_graph)
    for k, (x, y) in pos.items():
        assert x == k.col
        assert y == -k.row
Exemplo n.º 3
0
def test_compile_circuit_with_readout_correction():
    """Tests that we are able to compile a model circuit with readout error
    correction."""
    compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
    router_mock = MagicMock(
        side_effect=lambda circuit, network: ccr.SwapNetwork(circuit, {}))
    a, b, c = cirq.LineQubit.range(3)
    ap, bp, cp = cirq.LineQubit.range(3, 6)
    model_circuit = cirq.Circuit([
        cirq.Moment([cirq.X(a), cirq.Y(b), cirq.Z(c)]),
    ])
    compilation_result = cirq.contrib.quantum_volume.compile_circuit(
        model_circuit,
        device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
        compiler=compiler_mock,
        router=router_mock,
        routing_attempts=1,
        add_readout_error_correction=True,
    )

    assert compilation_result.circuit == cirq.Circuit([
        cirq.Moment([cirq.X(a), cirq.Y(b), cirq.Z(c)]),
        cirq.Moment([cirq.X(a), cirq.X(b), cirq.X(c)]),
        cirq.Moment([cirq.CNOT(a, ap),
                     cirq.CNOT(b, bp),
                     cirq.CNOT(c, cp)]),
        cirq.Moment([cirq.X(a), cirq.X(b), cirq.X(c)]),
    ])
Exemplo n.º 4
0
def test_compile_circuit_multiple_routing_attempts():
    """Tests that we make multiple attempts at routing and keep the best one."""
    qubits = cirq.LineQubit.range(3)
    initial_mapping = dict(zip(qubits, qubits))
    more_operations = cirq.Circuit([
        cirq.X.on_each(qubits),
        cirq.Y.on_each(qubits),
    ])
    more_qubits = cirq.Circuit([
        cirq.X.on_each(cirq.LineQubit.range(4)),
    ])
    well_routed = cirq.Circuit([
        cirq.X.on_each(qubits),
    ])
    router_mock = MagicMock(side_effect=[
        ccr.SwapNetwork(more_operations, initial_mapping),
        ccr.SwapNetwork(well_routed, initial_mapping),
        ccr.SwapNetwork(more_qubits, initial_mapping),
    ])
    compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
    model_circuit = cirq.Circuit([cirq.X.on_each(qubits)])

    compilation_result = cirq.contrib.quantum_volume.compile_circuit(
        model_circuit,
        device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
        compiler=compiler_mock,
        router=router_mock,
        routing_attempts=3,
    )

    assert compilation_result.mapping == initial_mapping
    assert router_mock.call_count == 3
    compiler_mock.assert_called_with(well_routed)
Exemplo n.º 5
0
def test_compile_circuit_replaces_swaps():
    """Tests that the compiler never sees the SwapPermutationGates from the
    router."""
    compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
    a, b, c = cirq.LineQubit.range(3)
    # Create a circuit that will require some swaps.
    model_circuit = cirq.Circuit([
        cirq.Moment([cirq.CNOT(a, b)]),
        cirq.Moment([cirq.CNOT(a, c)]),
        cirq.Moment([cirq.CNOT(b, c)]),
    ])
    compilation_result = cirq.contrib.quantum_volume.compile_circuit(
        model_circuit,
        device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
        compiler=compiler_mock,
        routing_attempts=1,
    )

    # Assert that there were some swaps in the result
    compiler_mock.assert_called_with(compilation_result.circuit)
    assert (len(
        list(
            compilation_result.circuit.findall_operations_with_gate_type(
                cirq.ops.SwapPowGate))) > 0)
    # Assert that there were not SwapPermutations in the result.
    assert (len(
        list(
            compilation_result.circuit.findall_operations_with_gate_type(
                cirq.contrib.acquaintance.SwapPermutationGate))) == 0)
Exemplo n.º 6
0
def test_compile_circuit_router():
    """Tests that the given router is used."""
    router_mock = MagicMock()
    cirq.contrib.quantum_volume.compile_circuit(
        cirq.Circuit(),
        device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
        router=router_mock,
        routing_attempts=1,
    )
    router_mock.assert_called()
Exemplo n.º 7
0
def test_compile_circuit_no_routing_attempts():
    """Tests that setting no routing attempts throws an error."""
    a, b, c = cirq.LineQubit.range(3)
    model_circuit = cirq.Circuit([cirq.Moment([cirq.X(a), cirq.Y(b), cirq.Z(c)])])

    with pytest.raises(AssertionError) as e:
        cirq.contrib.quantum_volume.compile_circuit(
            model_circuit,
            device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
            routing_attempts=0,
        )
    assert e.match('Unable to get routing for circuit')
Exemplo n.º 8
0
def test_calculate_quantum_volume_loop():
    """Test that calculate_quantum_volume is able to run without erring."""
    # Keep test from taking a long time by lowering circuits and routing
    # attempts.
    cirq.contrib.quantum_volume.calculate_quantum_volume(
        num_qubits=5,
        depth=5,
        num_circuits=1,
        routing_attempts=2,
        random_state=1,
        device_graph=ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(3, 3)),
        samplers=[cirq.Simulator()],
    )
Exemplo n.º 9
0
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
Exemplo n.º 10
0
def test_compile_circuit():
    """Tests that we are able to compile a model circuit."""
    compiler_mock = MagicMock(side_effect=lambda circuit: circuit)
    a, b, c = cirq.LineQubit.range(3)
    model_circuit = cirq.Circuit([cirq.Moment([cirq.X(a), cirq.Y(b), cirq.Z(c)])])
    compilation_result = cirq.contrib.quantum_volume.compile_circuit(
        model_circuit,
        device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
        compiler=compiler_mock,
        routing_attempts=1,
    )

    assert len(compilation_result.mapping) == 3
    assert cirq.contrib.routing.ops_are_consistent_with_device_graph(
        compilation_result.circuit.all_operations(),
        cirq.contrib.routing.gridqubits_to_graph_device(FakeDevice().qubits),
    )
    compiler_mock.assert_called_with(compilation_result.circuit)
Exemplo n.º 11
0
def test_calculate_quantum_volume_result_with_device_graph():
    """Test that running the main loop routes the circuit onto the given device
    graph"""
    device_qubits = [cirq.GridQubit(i, j) for i in range(2) for j in range(3)]

    results = cirq.contrib.quantum_volume.calculate_quantum_volume(
        num_qubits=3,
        depth=3,
        num_circuits=1,
        device_graph=ccr.gridqubits_to_graph_device(device_qubits),
        samplers=[cirq.Simulator()],
        routing_attempts=2,
        random_state=1,
    )

    assert len(results) == 1
    assert ccr.ops_are_consistent_with_device_graph(
        results[0].compiled_circuit.all_operations(),
        ccr.get_grid_device_graph(2, 3))
Exemplo n.º 12
0
def test_calculate_quantum_volume_result():
    """Test that running the main loop returns the desired result"""
    results = cirq.contrib.quantum_volume.calculate_quantum_volume(
        num_qubits=3,
        depth=3,
        num_circuits=1,
        device_graph=ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(3, 3)),
        samplers=[cirq.Simulator()],
        routing_attempts=2,
        random_state=1,
    )

    model_circuit = cirq.contrib.quantum_volume.generate_model_circuit(3, 3, random_state=1)
    assert len(results) == 1
    assert results[0].model_circuit == model_circuit
    assert results[0].heavy_set == cirq.contrib.quantum_volume.compute_heavy_set(model_circuit)
    # Ensure that calling to_json on the results does not err.
    buffer = io.StringIO()
    cirq.to_json(results, buffer)
Exemplo n.º 13
0
def _qubit_index_edges(device):
    """Helper function in `_device_to_tket_device`"""
    dev_graph = ccr.gridqubits_to_graph_device(device.qubit_set())
    for n1, n2 in dev_graph.edges:
        yield Node('grid', n1.row, n1.col), Node('grid', n2.row, n2.col)
Exemplo n.º 14
0
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
Exemplo n.º 15
0
def _get_device_graph(device_or_qubits: Any):
    qubits = device_or_qubits if isinstance(device_or_qubits,
                                            list) else device_or_qubits.qubits
    return ccr.gridqubits_to_graph_device(qubits)
Exemplo n.º 16
0
            simulator run extremely slowly for any width/depth of 4 or more,
            because it doubles the circuit size. In reality, the simulator
            shouldn't need to use this larger circuit for the majority of
            operations, since they only come into play at the end.

    Returns: A list of QuantumVolumeResults that contains all of the information
        for running the algorithm and its results.

    """
    circuits = prepare_circuits(num_qubits=num_qubits,
                                depth=depth,
                                num_circuits=num_circuits,
                                random_state=random_state)

    # Get the device graph from the given qubits or device.
    device_graph = None
    if isinstance(device_or_qubits, list):
        device_graph = ccr.gridqubits_to_graph_device(device_or_qubits)
    else:
        device_graph = ccr.xmon_device_to_graph(device_or_qubits)

    return execute_circuits(
        circuits=circuits,
        device_graph=device_graph,
        compiler=compiler,
        samplers=samplers,
        repetitions=repetitions,
        routing_attempts=routing_attempts,
        add_readout_error_correction=add_readout_error_correction,
    )