예제 #1
0
def test_get_qvm_with_topology_2(forest):
    topo = nx.from_edgelist([
        (5, 6),
        (6, 7),
    ])
    qc = _get_qvm_with_topology(name='test-qvm', topology=topo)
    results = qc.run_and_measure(Program(X(5)), trials=5)
    assert sorted(results.keys()) == [5, 6, 7]
    assert all(x == 1 for x in results[5])
예제 #2
0
def test_get_qvm_with_topology():
    topo = nx.from_edgelist([
        (5, 6),
        (6, 7),
        (10, 11),
    ])
    # Note to developers: perhaps make `get_qvm_with_topology` public in the future
    qc = _get_qvm_with_topology(name='test-qvm', topology=topo)
    assert len(qc.qubits()) == 5
    assert min(qc.qubits()) == 5
예제 #3
0
    def __init__(self, device, *, shots=1000, noisy=False, **kwargs):

        if shots <= 0:
            raise ValueError("Number of shots must be a positive integer.")

        # ignore any 'wires' keyword argument passed to the device
        kwargs.pop("wires", None)
        analytic = kwargs.get("analytic", False)
        timeout = kwargs.pop("timeout", None)

        self._compiled_program = None
        """Union[None, pyquil.ExecutableDesignator]: the latest compiled program. If parametric
        compilation is turned on, this will be a parametric program."""

        self.parametric_compilation = kwargs.get("parametric_compilation", True)

        if self.parametric_compilation:
            self._compiled_program_dict = {}
            """dict[int, pyquil.ExecutableDesignator]: stores circuit hashes associated
                with the corresponding compiled programs."""

            self._parameter_map = {}
            """dict[str, float]: stores the string of symbolic parameters associated with
                their numeric values. This map will be used to bind parameters in a parametric
                program using PyQuil."""

            self._parameter_reference_map = {}
            """dict[str, pyquil.quilatom.MemoryReference]: stores the string of symbolic
                parameters associated with their PyQuil memory references."""

        if analytic:
            raise ValueError("QVM device cannot be run in analytic=True mode.")

        self.connection = super()._get_connection(**kwargs)

        # get the qc
        if isinstance(device, nx.Graph):
            self.qc = _get_qvm_with_topology(
                "device", topology=device, noisy=noisy, connection=self.connection
            )
        elif isinstance(device, str):
            self.qc = get_qc(device, as_qvm=True, noisy=noisy, connection=self.connection)

        num_wires = len(self.qc.qubits())

        super().__init__(num_wires, shots, analytic=analytic, **kwargs)

        if timeout is not None:
            self.qc.compiler.client.timeout = timeout

        self.wiring = {i: q for i, q in enumerate(self.qc.qubits())}
        self.active_reset = False
예제 #4
0
    def __init__(self, device, *, shots=1024, noisy=False, **kwargs):

        if shots <= 0:
            raise ValueError("Number of shots must be a positive integer.")

        # ignore any 'wires' keyword argument passed to the device
        kwargs.pop("wires", None)
        analytic = kwargs.get("analytic", False)

        if analytic:
            raise ValueError("QVM device cannot be run in analytic=True mode.")

        # get the number of wires
        if isinstance(device, nx.Graph):
            # load a QVM based on a graph topology
            num_wires = device.number_of_nodes()
        elif isinstance(device, str):
            # the device string must match a valid QVM device, i.e.
            # N-qvm, or 9q-square-qvm, or Aspen-1-16Q-A
            wire_match = re.search(r"(\d+)(q|Q)", device)

            if wire_match is None:
                # with the current Rigetti naming scheme, this error should never
                # appear as long as the QVM quantum computer has the correct name
                raise ValueError(
                    "QVM device string does not indicate the number of qubits!"
                )

            num_wires = int(wire_match.groups()[0])
        else:
            raise ValueError(
                "Required argument device must be a string corresponding to "
                "a valid QVM quantum computer, or a NetworkX graph object.")

        super().__init__(num_wires, shots, analytic=analytic, **kwargs)

        # get the qc
        if isinstance(device, nx.Graph):
            self.qc = _get_qvm_with_topology("device",
                                             topology=device,
                                             noisy=noisy,
                                             connection=self.connection)
        elif isinstance(device, str):
            self.qc = get_qc(device,
                             as_qvm=True,
                             noisy=noisy,
                             connection=self.connection)

        self.wiring = {i: q for i, q in enumerate(self.qc.qubits())}
        self.active_reset = False
        self.compiled = None
예제 #5
0
def test_get_qvm_with_topology(client_configuration: QCSClientConfiguration):
    topo = nx.from_edgelist([(5, 6), (6, 7), (10, 11)])
    # Note to developers: perhaps make `get_qvm_with_topology` public in the future
    qc = _get_qvm_with_topology(
        name="test-qvm",
        topology=topo,
        noisy=False,
        qvm_type="qvm",
        compiler_timeout=5.0,
        execution_timeout=5.0,
        client_configuration=client_configuration,
    )
    assert len(qc.qubits()) == 5
    assert min(qc.qubits()) == 5
예제 #6
0
def test_get_qvm_with_topology_2(client_configuration: QCSClientConfiguration):
    topo = nx.from_edgelist([(5, 6), (6, 7)])
    qc = _get_qvm_with_topology(
        name="test-qvm",
        topology=topo,
        noisy=False,
        qvm_type="qvm",
        compiler_timeout=5.0,
        execution_timeout=5.0,
        client_configuration=client_configuration,
    )
    results = qc.run(
        qc.compile(
            Program(
                Declare("ro", "BIT", 3),
                X(5),
                MEASURE(5, ("ro", 0)),
                MEASURE(6, ("ro", 1)),
                MEASURE(7, ("ro", 2)),
            ).wrap_in_numshots_loop(5))).readout_data.get("ro")
    assert results.shape == (5, 3)
    assert all(r[0] == 1 for r in results)
예제 #7
0
    def __init__(self, device, *, wires=None, shots=1000, noisy=False, **kwargs):

        if shots is not None and shots <= 0:
            raise ValueError("Number of shots must be a positive integer or None.")

        timeout = kwargs.pop("timeout", None)

        self._compiled_program = None
        """Union[None, pyquil.ExecutableDesignator]: the latest compiled program. If parametric
        compilation is turned on, this will be a parametric program."""

        self.parametric_compilation = kwargs.get("parametric_compilation", True)

        if self.parametric_compilation:
            self._circuit_hash = None
            """None or int: stores the hash of the circuit from the last execution which
            can be used for parametric compilation."""

            self._compiled_program_dict = {}
            """dict[int, pyquil.ExecutableDesignator]: stores circuit hashes associated
                with the corresponding compiled programs."""

            self._parameter_map = {}
            """dict[str, float]: stores the string of symbolic parameters associated with
                their numeric values. This map will be used to bind parameters in a parametric
                program using PyQuil."""

            self._parameter_reference_map = {}
            """dict[str, pyquil.quilatom.MemoryReference]: stores the string of symbolic
                parameters associated with their PyQuil memory references."""

        if shots is None:
            raise ValueError("QVM device cannot be used for analytic computations.")

        self.connection = super()._get_connection(**kwargs)

        # get the qc
        if isinstance(device, nx.Graph):
            self.qc = _get_qvm_with_topology(
                "device", topology=device, noisy=noisy, connection=self.connection
            )
        elif isinstance(device, str):
            self.qc = get_qc(device, as_qvm=True, noisy=noisy, connection=self.connection)

        self.num_wires = len(self.qc.qubits())

        if wires is None:
            # infer the number of modes from the device specs
            # and use consecutive integer wire labels
            wires = range(self.num_wires)

        if isinstance(wires, int):
            raise ValueError(
                "Device has a fixed number of {} qubits. The wires argument can only be used "
                "to specify an iterable of wire labels.".format(self.num_wires)
            )

        if self.num_wires != len(wires):
            raise ValueError(
                "Device has a fixed number of {} qubits and "
                "cannot be created with {} wires.".format(self.num_wires, len(wires))
            )

        super().__init__(wires, shots, **kwargs)

        if timeout is not None:
            self.qc.compiler.client.timeout = timeout

        self.wiring = {i: q for i, q in enumerate(self.qc.qubits())}
        self.active_reset = False
예제 #8
0
from pyquil.api._quantum_computer import _get_qvm_with_topology
from pyquil.device import NxDevice, gates_in_isa
from pyquil import Program, get_qc
from pyquil.gates import *


def make_circuit()-> Program:

    prog = Program()

    prog += X(1)
    prog += X(2)
    prog += CCNOT(1,2,0) # number=8
    # circuit end

    return prog

if __name__ == '__main__':


    #qubits = [0, 1, 2, 3, 4, 5, 6]  # qubits are numbered by octagon
    edges = [(0, 1), (1, 0), (2, 3), (3, 2)] # second octagon

    topo = nx.from_edgelist(edges)
    device = NxDevice(topology=topo)

    qc = _get_qvm_with_topology(name="line",topology=topo)

    print(qc.run_and_measure(make_circuit(),10))

    #pyquil seems should use this way to do the topology
예제 #9
0
    def __init__(self, device, *, shots=1024, noisy=False, **kwargs):

        if shots <= 0:
            raise ValueError("Number of shots must be a positive integer.")

        # ignore any 'wires' keyword argument passed to the device
        kwargs.pop("wires", None)
        analytic = kwargs.get("analytic", False)
        timeout = kwargs.pop("timeout", None)

        self._compiled_program = None
        """Union[None, pyquil.ExecutableDesignator]: the latest compiled program. If parametric
        compilation is turned on, this will be a parametric program."""

        self.parametric_compilation = kwargs.get("parametric_compilation",
                                                 True)

        if self.parametric_compilation:
            self._compiled_program_dict = {}
            """dict[int, pyquil.ExecutableDesignator]: stores circuit hashes associated
                with the corresponding compiled programs."""

            self._parameter_map = {}
            """dict[str, float]: stores the string of symbolic parameters associated with
                their numeric values. This map will be used to bind parameters in a parametric
                program using PyQuil."""

            self._parameter_reference_map = {}
            """dict[str, pyquil.quilatom.MemoryReference]: stores the string of symbolic
                parameters associated with their PyQuil memory references."""

        if analytic:
            raise ValueError("QVM device cannot be run in analytic=True mode.")

        # get the number of wires
        if isinstance(device, nx.Graph):
            # load a QVM based on a graph topology
            num_wires = device.number_of_nodes()
        elif isinstance(device, str):
            # the device string must match a valid QVM device, i.e.
            # N-qvm, or 9q-square-qvm, or Aspen-1-16Q-A
            wire_match = re.search(r"(\d+)(q|Q)", device)

            if wire_match is None:
                # with the current Rigetti naming scheme, this error should never
                # appear as long as the QVM quantum computer has the correct name
                raise ValueError(
                    "QVM device string does not indicate the number of qubits!"
                )

            num_wires = int(wire_match.groups()[0])
        else:
            raise ValueError(
                "Required argument device must be a string corresponding to "
                "a valid QVM quantum computer, or a NetworkX graph object.")

        super().__init__(num_wires, shots, analytic=analytic, **kwargs)

        # get the qc
        if isinstance(device, nx.Graph):
            self.qc = _get_qvm_with_topology("device",
                                             topology=device,
                                             noisy=noisy,
                                             connection=self.connection)
        elif isinstance(device, str):
            self.qc = get_qc(device,
                             as_qvm=True,
                             noisy=noisy,
                             connection=self.connection)

        if timeout is not None:
            self.qc.compiler.client.timeout = timeout

        self.wiring = {i: q for i, q in enumerate(self.qc.qubits())}
        self.active_reset = False