Example #1
0
    def check_device(self, device):
        """
        Verify if a device is valid.
        Parameters
        ----------
        device:
            a pyquil.api.QuantumComputer, a string which picks one out, or a dictionary that can pass to get_qc.
        Returns
        -------
        None

        """
        if device is None:
            return
        if isinstance(device, str):
            d = device
            if '-qvm' in d.lower():
                d = d[:-4]
            if '-noisy' in d.lower():
                d = d[:-6]
            if d in pyquil.list_quantum_computers():
                return
            else:
                try:
                    get_qc(d)
                    return
                except:
                    try:
                        get_qc(d, as_qvm=True)
                        return
                    except:
                        raise TequilaException(
                            'could not obtain device from string; received {}'.
                            format(device))

        elif isinstance(device, dict):
            try:
                get_qc(**device)
                return
            except:
                raise TequilaException(
                    'could not initialize device from dict; received {}'.
                    format(device))
        elif isinstance(device, pyquil.api.QuantumComputer):
            return

        else:
            raise TequilaException(
                'Uninterpretable object {} of type {} passed to check_device!'.
                format(device, type(device)))
Example #2
0
    def retrieve_device(self, device):
        """
        return an initialized pyquil quantum computer (or None)
        Parameters
        ----------
        device:
            pyquil.api.QuantumComputer, or arguments that can pass to pyquil.get_qc

        Returns
        -------
        pyquil.api.QuantumComputer
            an instantiated device object for pyquil simulation or execution.
        """
        use_device_noise = (self.noise == 'device')
        if device is None:
            return None
        if isinstance(device, str):
            try:
                back = get_qc(device, noisy=use_device_noise)
                return back
            except:
                try:
                    back = get_qc(device, as_qvm=True, noisy=use_device_noise)
                    return back
                except:
                    raise TequilaException(
                        'could not obtain device from string; received {}'.
                        format(device))
        elif isinstance(device, pyquil.api.QuantumComputer):
            return device
        elif isinstance(device, dict):
            try:
                return get_qc(**device)
            except:
                raise TequilaException(
                    'could not initialize device from dict; received {}'.
                    format(device))
        else:
            raise TequilaException(
                'Uninterpretable object {} of type {} passed to check_device!'.
                format(device, type(device)))
Example #3
0
    def __init__(self,
                 abstract_circuit: QCircuit,
                 variables,
                 qubit_map=None,
                 noise=None,
                 device=None,
                 *args,
                 **kwargs):
        """
        Parameters
        ----------
        abstract_circuit: QCircuit:
            Tequila unitary to compile to Pyquil.
        variables: dict:
            values of all variables in the circuit, to compile with.
        qubit_map: dictionary:
            a qubit map which maps the abstract qubits in the abstract_circuit to the qubits on the backend
            there is no need to initialize the corresponding backend types
            the dictionary should simply be {int:int} (preferred) or {int:name}
            if None the default will map to qubits 0 ... n_qubits -1 in the backend
        noise:
            Noise to apply to the circuit.
        device:
            device on which to emulatedly execute all sampling.
        args
        kwargs
        """

        self.op_lookup = {
            'I': (pyquil.gates.I),
            'X': (pyquil.gates.X, pyquil.gates.CNOT, pyquil.gates.CCNOT),
            'Y': (pyquil.gates.Y, ),
            'Z': (pyquil.gates.Z, pyquil.gates.CZ),
            'H': (pyquil.gates.H, ),
            'Rx': pyquil.gates.RX,
            'Ry': pyquil.gates.RY,
            'Rz': pyquil.gates.RZ,
            'Phase': pyquil.gates.PHASE,
            'SWAP': (pyquil.gates.SWAP, pyquil.gates.CSWAP),
        }
        self.match_par_to_dummy = {}
        self.counter = 0
        if device is not None:
            self.compiler_arguments['cc_max'] = True
        super().__init__(abstract_circuit=abstract_circuit,
                         variables=variables,
                         noise=noise,
                         device=device,
                         qubit_map=qubit_map,
                         *args,
                         **kwargs)
        if self.noise is not None:
            self.noise_lookup = {
                'amplitude damp': amp_damp_map,
                'phase damp': phase_damp_map,
                'bit flip': bit_flip_map,
                'phase flip': phase_flip_map,
                'phase-amplitude damp': phase_amp_damp_map,
                'depolarizing': depolarizing_map
            }

            if isinstance(self.noise, str):
                if self.noise == 'device':
                    pass
                else:
                    raise TequilaException(
                        'noise was a string: {}, which is not \'device\'. This is not allowed!'
                        .format(self.noise))

            else:
                self.circuit = self.build_noisy_circuit(
                    self.circuit, self.noise)

        if len(self.match_par_to_dummy.keys()) is None:
            self.match_dummy_to_value = None
            self.resolver = None
        else:
            self.match_dummy_to_value = {
                'theta_{}'.format(str(i)): k
                for i, k in enumerate(self.match_par_to_dummy.keys())
            }
            self.resolver = {
                k: [to_float(v(variables))]
                for k, v in self.match_dummy_to_value.items()
            }
Example #4
0
    def __init__(self,
                 abstract_circuit: QCircuit,
                 variables,
                 use_mapping=True,
                 noise=None,
                 device=None,
                 *args,
                 **kwargs):
        """
        Parameters
        ----------
        abstract_circuit: QCircuit:
            Tequila unitary to compile to Pyquil.
        variables: dict:
            values of all variables in the circuit, to compile with.
        use_mapping: bool:
            whether or not to use a mapping that eliminates unnecessary qubits from the circuit.
        noise:
            Noise to apply to the circuit.
        device:
            device on which to emulatedly execute all sampling.
        args
        kwargs
        """

        self.op_lookup = {
            'I': (pyquil.gates.I),
            'X': (pyquil.gates.X, pyquil.gates.CNOT, pyquil.gates.CCNOT),
            'Y': (pyquil.gates.Y, ),
            'Z': (pyquil.gates.Z, pyquil.gates.CZ),
            'H': (pyquil.gates.H, ),
            'Rx': pyquil.gates.RX,
            'Ry': pyquil.gates.RY,
            'Rz': pyquil.gates.RZ,
            'Phase': pyquil.gates.PHASE,
            'SWAP': (pyquil.gates.SWAP, pyquil.gates.CSWAP),
        }
        self.match_par_to_dummy = {}
        self.counter = 0
        if device is not None:
            self.compiler_arguments['cc_max'] = True
        super().__init__(abstract_circuit=abstract_circuit,
                         variables=variables,
                         noise=noise,
                         device=device,
                         use_mapping=use_mapping,
                         *args,
                         **kwargs)
        if self.noise is not None:
            self.noise_lookup = {
                'amplitude damp': amp_damp_map,
                'phase damp': phase_damp_map,
                'bit flip': bit_flip_map,
                'phase flip': phase_flip_map,
                'phase-amplitude damp': phase_amp_damp_map,
                'depolarizing': depolarizing_map
            }

            if isinstance(self.noise, str):
                if self.noise == 'device':
                    pass
                else:
                    raise TequilaException(
                        'noise was a string: {}, which is not \'device\'. This is not allowed!'
                        .format(self.noise))

            else:
                self.circuit = self.build_noisy_circuit(
                    self.circuit, self.noise)

        if len(self.match_par_to_dummy.keys()) is None:
            self.match_dummy_to_value = None
            self.resolver = None
        else:
            self.match_dummy_to_value = {
                'theta_{}'.format(str(i)): k
                for i, k in enumerate(self.match_par_to_dummy.keys())
            }
            self.resolver = {
                k: [to_float(v(variables))]
                for k, v in self.match_dummy_to_value.items()
            }