Пример #1
0
    def do_simulate(self, variables, initial_state, *args, **kwargs):
        """
        Internal helper function for performing wavefunction simulation.

        Parameters
        ----------
        variables:
            the variables of parameters in the circuit to use during simulation
        initial_state:
            indicates, in some fashion, the initial state to which the self.circuit is applied.
        args
        kwargs

        Returns
        -------
        QubitWaveFunction:
            The wave function resulting from simulation.

        """
        simulator = pyquil.api.WavefunctionSimulator()
        n_qubits = self.n_qubits
        msb = BitString.from_int(initial_state, nbits=n_qubits)
        iprep = pyquil.Program()
        for i, val in enumerate(msb.array):
            if val > 0:
                iprep += pyquil.gates.X(i)
        backend_result = simulator.wavefunction(iprep + self.circuit,
                                                memory_map=self.resolver)
        return QubitWaveFunction.from_array(arr=backend_result.amplitudes,
                                            numbering=self.numbering)
Пример #2
0
    def do_simulate(self,
                    variables,
                    initial_state=0,
                    *args,
                    **kwargs) -> QubitWaveFunction:
        """
        Internal helper function for performing wavefunction simulation.

        Parameters
        ----------
        variables:
            the variables of parameters in the circuit to use during simulation
        initial_state:
            indicates, in some fashion, the initial state to which the self.circuit is applied.
        args
        kwargs

        Returns
        -------
        QubitWaveFunction:
            The wave function resulting from simulation.

        """
        simulator = cirq.Simulator()
        backend_result = simulator.simulate(program=self.circuit,
                                            param_resolver=self.resolver,
                                            initial_state=initial_state)
        return QubitWaveFunction.from_array(
            arr=backend_result.final_state_vector, numbering=self.numbering)
Пример #3
0
    def do_simulate(self, variables, initial_state, *args, **kwargs):
        """
        Helper function to perform simulation.

        Parameters
        ----------
        variables: dict:
            variables to supply to the circuit.
        initial_state:
            information indicating the initial state on which the circuit should act.
        args
        kwargs

        Returns
        -------
        QubitWaveFunction:
            QubitWaveFunction representing result of the simulation.
        """
        state = self.initialize_state(n_qubits=self.n_qubits)
        lsb = BitStringLSB.from_int(initial_state, nbits=self.n_qubits)
        state.set_computational_basis(
            BitString.from_binary(lsb.binary).integer)
        self.circuit.update_quantum_state(state)

        wfn = QubitWaveFunction.from_array(arr=state.get_vector(),
                                           numbering=self.numbering)
        return wfn
Пример #4
0
    def do_simulate(self, variables, initial_state, *args, **kwargs):
        state = qulacs.QuantumState(self.n_qubits)
        lsb = BitStringLSB.from_int(initial_state, nbits=self.n_qubits)
        state.set_computational_basis(BitString.from_binary(lsb.binary).integer)
        self.circuit.update_quantum_state(state)

        wfn = QubitWaveFunction.from_array(arr=state.get_vector(), numbering=self.numbering)
        return wfn
Пример #5
0
    def do_simulate(self, variables, initial_state=None, *args, **kwargs):
        """
        Helper function to perform simulation.

        Parameters
        ----------
        variables: dict:
            variables to supply to the circuit.
        initial_state:
            information indicating the initial state on which the circuit should act.
        args
        kwargs

        Returns
        -------
        QubitWaveFunction:
            QubitWaveFunction representing result of the simulation.
        """
        n_qubits = max(self.highest_qubit + 1, self.n_qubits,
                       self.abstract_circuit.max_qubit() + 1)
        if initial_state is not None:
            if isinstance(initial_state, int) or isinstance(
                    initial_state, np.int):
                wave = QubitWaveFunction.from_int(i=initial_state,
                                                  n_qubits=n_qubits)
            elif isinstance(initial_state, str):
                wave = QubitWaveFunction.from_string(
                    string=initial_state).to_array()
            elif isinstance(initial_state, QubitWaveFunction):
                wave = initial_state
            elif isinstance(initial_state, np.ndarray):
                wave = QubitWaveFunction.from_array(initial_state)
            else:
                raise TequilaQiboException(
                    'could not understand initial state of type {}'.format(
                        type(initial_state)))
            state = wave.to_array()
            result = self.circuit(state)
        else:
            result = self.circuit()
        back = QubitWaveFunction.from_array(arr=result.numpy())
        return back
Пример #6
0
    def do_sample(self,
                  samples,
                  circuit,
                  noise_model=None,
                  initial_state=None,
                  *args,
                  **kwargs) -> QubitWaveFunction:
        """
        Helper function for performing sampling.

        Parameters
        ----------
        samples: int:
            the number of samples to be taken.
        circuit:
            the circuit to sample from.
        noise_model: optional:
            noise model to be applied to the circuit.
        initial_state:
            sampling supports initial states for qulacs. Indicates the initial state to which circuit is applied.
        args
        kwargs

        Returns
        -------
        QubitWaveFunction:
            the results of sampling, as a Qubit Wave Function.
        """
        n_qubits = max(self.highest_qubit + 1, self.n_qubits,
                       self.abstract_circuit.max_qubit() + 1)
        if initial_state is not None:
            if isinstance(initial_state, int):
                wave = QubitWaveFunction.from_int(i=initial_state,
                                                  n_qubits=n_qubits)
            elif isinstance(initial_state, str):
                wave = QubitWaveFunction.from_string(
                    string=initial_state).to_array()
            elif isinstance(initial_state, QubitWaveFunction):
                wave = initial_state
            elif isinstance(initial_state, np.ndarray):
                wave = QubitWaveFunction.from_array(
                    arr=initial_state,
                    n_qubits=n_qubits)  # silly but necessary
            else:
                raise TequilaQiboException(
                    'received an unusable initial state of type {}'.format(
                        type(initial_state)))
            state = wave.to_array()
            result = circuit(state, nshots=samples)
        else:
            result = circuit(nshots=samples)

        back = self.convert_measurements(backend_result=result)
        return back
Пример #7
0
    def do_simulate(self, variables, initial_state, *args, **kwargs):

        simulator = pyquil.api.WavefunctionSimulator()
        n_qubits = self.n_qubits
        msb = BitString.from_int(initial_state, nbits=n_qubits)
        iprep = pyquil.Program()
        for i, val in enumerate(msb.array):
            if val > 0:
                iprep += pyquil.gates.X(i)
        backend_result = simulator.wavefunction(iprep + self.circuit, memory_map=self.resolver)
        return QubitWaveFunction.from_array(arr=backend_result.amplitudes, numbering=self.numbering)
Пример #8
0
 def do_simulate(self,
                 variables,
                 initial_state=0,
                 *args,
                 **kwargs) -> QubitWaveFunction:
     simulator = cirq.Simulator()
     backend_result = simulator.simulate(program=self.circuit,
                                         param_resolver=self.resolver,
                                         initial_state=initial_state)
     return QubitWaveFunction.from_array(arr=backend_result.final_state,
                                         numbering=self.numbering)
Пример #9
0
    def do_simulate(self,
                    variables,
                    initial_state=0,
                    *args,
                    **kwargs) -> QubitWaveFunction:
        """
        Helper function for performing simulation.
        Parameters
        ----------
        variables:
            variables to pass to the circuit for simulation.
        initial_state:
            indicate initial state on which the unitary self.circuit should act.
        args
        kwargs

        Returns
        -------
        QubitWaveFunction:
            the result of simulation.
        """
        if self.noise_model is None:
            qiskit_backend = self.retrieve_device('statevector_simulator')
        else:
            raise TequilaQiskitException(
                "wave function simulation with noise cannot be performed presently"
            )

        optimization_level = None
        if "optimization_level" in kwargs:
            optimization_level = kwargs['optimization_level']

        opts = None
        if initial_state != 0:
            array = numpy.zeros(shape=[2**self.n_qubits])
            i = BitStringLSB.from_binary(
                BitString.from_int(integer=initial_state,
                                   nbits=self.n_qubits).binary)
            print(initial_state, " -> ", i)
            array[i.integer] = 1.0
            opts = {"initial_statevector": array}
            print(opts)

        backend_result = qiskit.execute(experiments=self.circuit,
                                        optimization_level=optimization_level,
                                        backend=qiskit_backend,
                                        parameter_binds=[self.resolver],
                                        backend_options=opts).result()
        return QubitWaveFunction.from_array(arr=backend_result.get_statevector(
            self.circuit),
                                            numbering=self.numbering)
Пример #10
0
    def do_simulate(self,
                    variables,
                    initial_state=0,
                    *args,
                    **kwargs) -> QubitWaveFunction:
        if self.noise_model is None:
            qiskit_backend = self.get_backend(*args, **kwargs)
            if qiskit_backend != qiskit.Aer.get_backend(
                    name="statevector_simulator"):
                raise TequilaQiskitException(
                    "quiskit_backend for simulations without samples (full wavefunction simulations) need to be the statevector_simulator. Received: qiskit_backend={}"
                    .format(qiskit_backend))
        else:
            raise TequilaQiskitException(
                "wave function simulation with noise cannot be performed presently"
            )

        optimization_level = None
        if "optimization_level" in kwargs:
            optimization_level = kwargs['optimization_level']

        opts = None
        if initial_state != 0:
            array = numpy.zeros(shape=[2**self.n_qubits])
            i = BitStringLSB.from_binary(
                BitString.from_int(integer=initial_state,
                                   nbits=self.n_qubits).binary)
            print(initial_state, " -> ", i)
            array[i.integer] = 1.0
            opts = {"initial_statevector": array}
            print(opts)

        backend_result = qiskit.execute(experiments=self.circuit,
                                        optimization_level=optimization_level,
                                        backend=qiskit_backend,
                                        parameter_binds=[self.resolver],
                                        backend_options=opts).result()
        return QubitWaveFunction.from_array(arr=backend_result.get_statevector(
            self.circuit),
                                            numbering=self.numbering)