Пример #1
0
def _get_unrestricted_qvm(connection: ForestConnection,
                          noisy: bool,
                          n_qubits: int = 34) -> QuantumComputer:
    """
    A qvm with a fully-connected topology.

    This is obviously the least realistic QVM, but who am I to tell users what they want.

    Users interested in building their own QuantumComputer from parts may wish to look
    to this function for inspiration, but should not use this private function directly.

    :param connection: The connection to use to talk to external services
    :param noisy: Whether to construct a noisy quantum computer
    :param n_qubits: 34 qubits ought to be enough for anybody.
    :return: A pre-configured QuantumComputer
    """
    fully_connected_device = NxDevice(topology=nx.complete_graph(n_qubits))
    if noisy:
        # note to developers: the noise model specifies noise for each possible gate. In a fully
        # connected topology, there are a lot.
        noise_model = decoherence_noise_with_asymmetric_ro(
            gates=gates_in_isa(fully_connected_device.get_isa()))
    else:
        noise_model = None

    name = f'{n_qubits}q-noisy-qvm' if noisy else f'{n_qubits}q-qvm'
    return QuantumComputer(name=name,
                           qam=QVM(connection=connection,
                                   noise_model=noise_model),
                           device=fully_connected_device,
                           compiler=_get_qvm_compiler_based_on_endpoint(
                               device=fully_connected_device,
                               endpoint=connection.compiler_endpoint))
Пример #2
0
def test_readout_symmetrization(forest):
    device = NxDevice(nx.complete_graph(3))
    noise_model = decoherence_noise_with_asymmetric_ro(gates=gates_in_isa(device.get_isa()))
    qc = QuantumComputer(
        name='testy!',
        qam=QVM(connection=forest, noise_model=noise_model),
        device=device,
        compiler=DummyCompiler()
    )

    prog = Program(I(0), X(1),
                   MEASURE(0, 0),
                   MEASURE(1, 1))
    prog.wrap_in_numshots_loop(1000)

    bs1 = qc.run(prog)
    avg0_us = np.mean(bs1[:, 0])
    avg1_us = 1 - np.mean(bs1[:, 1])
    diff_us = avg1_us - avg0_us
    assert diff_us > 0.03

    bs2 = qc.run_symmetrized_readout(prog, 1000)
    avg0_s = np.mean(bs2[:, 0])
    avg1_s = 1 - np.mean(bs2[:, 1])
    diff_s = avg1_s - avg0_s
    assert diff_s < 0.05
Пример #3
0
def _get_9q_square_qvm(connection: ForestConnection,
                       noisy: bool) -> QuantumComputer:
    """
    A nine-qubit 3x3 square lattice.

    This uses a "generic" lattice not tied to any specific device. 9 qubits is large enough
    to do vaguely interesting algorithms and small enough to simulate quickly.

    Users interested in building their own QuantumComputer from parts may wish to look
    to this function for inspiration, but should not use this private function directly.

    :param connection: The connection to use to talk to external services
    :param noisy: Whether to construct a noisy quantum computer
    :return: A pre-configured QuantumComputer
    """
    nineq_square = nx.convert_node_labels_to_integers(nx.grid_2d_graph(3, 3))
    nineq_device = NxDevice(topology=nineq_square)
    if noisy:
        noise_model = decoherence_noise_with_asymmetric_ro(
            gates=gates_in_isa(nineq_device.get_isa()))
    else:
        noise_model = None

    name = '9q-square-noisy-qvm' if noisy else '9q-square-qvm'
    return QuantumComputer(name=name,
                           qam=QVM(connection=connection,
                                   noise_model=noise_model),
                           device=nineq_device,
                           compiler=_get_qvm_compiler_based_on_endpoint(
                               device=nineq_device,
                               endpoint=connection.compiler_endpoint))
Пример #4
0
def _get_qvm_with_topology(name: str,
                           topology: nx.Graph,
                           noisy: bool = False,
                           requires_executable: bool = True,
                           connection: ForestConnection = None,
                           qvm_type: str = 'qvm') -> QuantumComputer:
    """Construct a QVM with the provided topology.

    :param name: A name for your quantum computer. This field does not affect behavior of the
        constructed QuantumComputer.
    :param topology: A graph representing the desired qubit connectivity.
    :param noisy: Whether to include a generic noise model. If you want more control over
        the noise model, please construct your own :py:class:`NoiseModel` and use
        :py:func:`_get_qvm_qc` instead of this function.
    :param requires_executable: Whether this QVM will refuse to run a :py:class:`Program` and
        only accept the result of :py:func:`compiler.native_quil_to_executable`. Setting this
        to True better emulates the behavior of a QPU.
    :param connection: An optional :py:class:`ForestConnection` object. If not specified,
        the default values for URL endpoints will be used.
    :param qvm_type: The type of QVM. Either 'qvm' or 'pyqvm'.
    :return: A pre-configured QuantumComputer
    """
    # Note to developers: consider making this function public and advertising it.
    device = NxDevice(topology=topology)
    if noisy:
        noise_model = decoherence_noise_with_asymmetric_ro(
            gates=gates_in_isa(device.get_isa()))
    else:
        noise_model = None
    return _get_qvm_qc(name=name,
                       qvm_type=qvm_type,
                       connection=connection,
                       device=device,
                       noise_model=noise_model,
                       requires_executable=requires_executable)
Пример #5
0
def test_NxDevice(isa_dict, noise_model_dict):
    graph = isa_to_graph(ISA.from_dict(isa_dict))
    nxdev = NxDevice(graph)

    device_raw = {'isa': isa_dict,
                  'noise_model': noise_model_dict,
                  'is_online': True,
                  'is_retuning': False}
    dev = Device(DEVICE_FIXTURE_NAME, device_raw)

    nx.is_isomorphic(nxdev.qubit_topology(), dev.qubit_topology())
    isa = nxdev.get_isa()
    assert isa.qubits[0].type == 'Xhalves'
Пример #6
0
def test_NxDevice(isa_dict, noise_model_dict):
    graph = isa_to_graph(ISA.from_dict(isa_dict))
    nxdev = NxDevice(graph)

    device_raw = {
        "isa": isa_dict,
        "noise_model": noise_model_dict,
        "is_online": True,
        "is_retuning": False,
    }
    dev = Device(DEVICE_FIXTURE_NAME, device_raw)

    nx.is_isomorphic(nxdev.qubit_topology(), dev.qubit_topology())
    isa = nxdev.get_isa()
    assert isa.qubits[0].type == "Xhalves"
Пример #7
0
def test_readout_symmetrization(forest):
    device = NxDevice(nx.complete_graph(3))
    noise_model = decoherance_noise_with_asymettric_ro(device.get_isa())
    qc = QuantumComputer(name='testy!',
                         qam=QVM(connection=forest, noise_model=noise_model),
                         device=device)

    prog = Program(I(0), X(1), MEASURE(0, 0), MEASURE(1, 1))

    bs1 = qc.run(prog, [0, 1], 1000)
    avg0_us = np.mean(bs1[:, 0])
    avg1_us = 1 - np.mean(bs1[:, 1])
    diff_us = avg1_us - avg0_us
    print(avg0_us, avg1_us, diff_us)
    assert diff_us > 0.03

    bs2 = qc.run_symmetrized_readout(prog, [0, 1], 1000)
    avg0_s = np.mean(bs2[:, 0])
    avg1_s = 1 - np.mean(bs2[:, 1])
    diff_s = avg1_s - avg0_s
    print(avg0_s, avg1_s, diff_s)
    assert diff_s < 0.05
Пример #8
0
def get_qc(name: str,
           *,
           as_qvm: bool = None,
           noisy: bool = None,
           connection: ForestConnection = None):
    """
    Get a quantum computer.

    A quantum computer is an object of type :py:class:`QuantumComputer` and can be backed
    either by a QVM simulator ("Quantum/Quil Virtual Machine") or a physical Rigetti QPU ("Quantum
    Processing Unit") made of superconducting qubits.

    You can choose the quantum computer to target through a combination of its name and optional
    flags. There are multiple ways to get the same quantum computer. The following are equivalent::

        >>> qc = get_qc("8Q-Agave-noisy-qvm")
        >>> qc = get_qc("8Q-Agave", as_qvm=True, noisy=True)

    and will construct a simulator of the 8q-agave chip with a noise model based on device
    characteristics. We also provide a means for constructing generic quantum simulators that
    are not related to a given piece of Rigetti hardware::

        >>> qc = get_qc("9q-generic-qvm")
        >>> qc = get_qc("9q-generic", as_qvm=True)

    Redundant flags are acceptable, but conflicting flags will raise an exception::

        >>> qc = get_qc("9q-generic-qvm") # qc is fully specified by its name
        >>> qc = get_qc("9q-generic-qvm", as_qvm=True) # redundant, but ok
        >>> qc = get_qc("9q-generic-qvm", as_qvm=False) # Error!

    Use :py:func:`list_quantum_computers` to retrieve a list of known qc names.

    This method is provided as a convenience to quickly construct and use QVM's and QPU's.
    Power users may wish to have more control over the specification of a quantum computer
    (e.g. custom noise models, bespoke topologies, etc.). This is possible by constructing
    a :py:class:`QuantumComputer` object by hand. Please refer to the documentation on
    :py:class:`QuantumComputer` for more information.

    :param name: The name of the desired quantum computer. This should correspond to a name
        returned by :py:func:`list_quantum_computers`. Names ending in "-qvm" will return
        a QVM. Names ending in "-noisy-qvm" will return a QVM with a noise model. Otherwise,
        we will return a QPU with the given name.
    :param as_qvm: An optional flag to force construction of a QVM (instead of a QPU). If
        specified and set to ``True``, a QVM-backed quantum computer will be returned regardless
        of the name's suffix
    :param noisy: An optional flag to force inclusion of a noise model. If
        specified and set to ``True``, a quantum computer with a noise model will be returned
        regardless of the name's suffix. The noise model for QVM's based on a real QPU
        is an empirically parameterized model based on real device noise characteristics.
        The generic QVM noise model is simple T1 and T2 noise plus readout error. See
        :py:func:`decoherance_noise_with_asymmetric_ro`.
    :param connection: An optional :py:class:ForestConnection` object. If not specified,
        the default values for URL endpoints, ping time, and status time will be used. Your
        user id and API key will be read from ~/.pyquil_config. If you deign to change any
        of these parameters, pass your own :py:class:`ForestConnection` object.
    :return:
    """
    if connection is None:
        connection = ForestConnection()

    name, as_qvm, noisy = _parse_name(name, as_qvm, noisy)

    if name == '9q-generic':
        if not as_qvm:
            raise ValueError(
                "The device '9q-generic' is only available as a QVM")

        nineq_square = nx.convert_node_labels_to_integers(
            nx.grid_2d_graph(3, 3))
        nineq_device = NxDevice(topology=nineq_square)
        if noisy:
            noise_model = decoherance_noise_with_asymmetric_ro(
                nineq_device.get_isa())
        else:
            noise_model = None

        return QuantumComputer(name='9q-generic-qvm',
                               qam=QVM(connection=connection,
                                       noise_model=noise_model),
                               device=nineq_device)

    # At least based off a real device.
    device = get_devices(as_dict=True)[name]

    if not as_qvm:
        if noisy is not None and noisy:
            warnings.warn(
                "You have specified `noisy=True`, but you're getting a QPU. This flag "
                "is meant for controling noise models on QVMs.")
        return QuantumComputer(name=name,
                               qam=QPU(device_name=name,
                                       connection=connection),
                               device=device)

    if noisy:
        noise_model = device.noise_model
        name = "{name}-noisy-qvm".format(name=name)
    else:
        noise_model = None
        name = "{name}-qvm".format(name=name)

    return QuantumComputer(name=name,
                           qam=QVM(connection=connection,
                                   noise_model=noise_model),
                           device=device)