Esempio n. 1
0
def test_conversion():
    for i in range(15):
        bita = BitString.from_int(integer=i)
        bita_lsb = BitStringLSB.from_int(integer=i)
        bita_converted = BitString.from_bitstring(other=bita_lsb)
        assert (bita == bita_converted)

    arrays = [[0, 0, 1], [1, 0, 0], [1, 0, 1], [1, 0, 1, 1, 1, 0], [1, 0, 0, 1, 1, 1, 0, 0, 1, 0]]
    for i, arr in enumerate(arrays):
        nbits = len(arr)

        bita = BitString.from_array(array=arr, nbits=nbits)
        bita_lsb = BitStringLSB.from_bitstring(other=bita)
        assert (bita_lsb.array == [x for x in reversed(arr)])
        assert (bita.binary == bita_lsb.binary[::-1])
        assert (bita.integer == bita_lsb.integer)
Esempio n. 2
0
    def convert_measurements(self,
                             backend_result,
                             target_qubits=None) -> QubitWaveFunction:
        """
        map backend results to QubitWaveFunction
        Parameters
        ----------
        backend_result:
            the result returned directly qiskit simulation.
        Returns
        -------
        QubitWaveFunction:
            measurements converted into wave function form.
        """
        qiskit_counts = backend_result.result().get_counts()
        result = QubitWaveFunction()
        # todo there are faster ways
        for k, v in qiskit_counts.items():
            converted_key = BitString.from_bitstring(
                other=BitStringLSB.from_binary(binary=k))
            result._state[converted_key] = v

        if target_qubits is not None:
            mapped_target = [self.qubit_map[q].number for q in target_qubits]
            mapped_full = [
                self.qubit_map[q].number for q in self.abstract_qubits
            ]
            keymap = KeyMapRegisterToSubregister(subregister=mapped_target,
                                                 register=mapped_full)
            result = result.apply_keymap(keymap=keymap)

        return result
Esempio n. 3
0
def test_endianness():
    tests = [
        "000111", "111000", "101010", "010101", "10010010001",
        "111100101000010"
    ]

    for string in tests:
        bits = len(string)
        i1 = BitString.from_int(int(string, 2))
        i2 = BitString.from_binary(binary=string)
        assert (i1 == i2)

        i11 = BitStringLSB.from_int(int(string, 2))
        i22 = BitStringLSB.from_binary(binary=string[::-1])
        assert (i11 == i22)
        assert (i11.integer == i1.integer)
        assert (i22.integer == i2.integer)
        assert (i11.integer == i2.integer)
        assert (i1 == BitString.from_bitstring(i11))
        assert (i1 == BitString.from_bitstring(i22))
        assert (i2 == BitString.from_bitstring(i11))
Esempio n. 4
0
 def convert_measurements(self, backend_result) -> QubitWaveFunction:
     """0.
     :param qiskit_counts: qiskit counts as dictionary, states are binary in little endian (LSB)
     :return: Counts in OpenVQE format, states are big endian (MSB)
     """
     qiskit_counts = backend_result.result().get_counts()
     result = QubitWaveFunction()
     # todo there are faster ways
     for k, v in qiskit_counts.items():
         converted_key = BitString.from_bitstring(
             other=BitStringLSB.from_binary(binary=k))
         result._state[converted_key] = v
     return result
Esempio n. 5
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)
Esempio n. 6
0
def test_constructor():
    for i in range(15):
        bita = BitString.from_int(integer=i)
        bita_lsb = BitStringLSB.from_int(integer=i)
        bitb = BitString.from_int(integer=bita)
        bitc = BitString.from_int(integer=bita_lsb)
        bitd = BitString.from_array(array=bita)
        bite = BitString.from_array(array=bita_lsb)
        bitf = BitString.from_binary(binary=bita)
        bitg = BitString.from_binary(binary=bita_lsb)
        assert (bita == bitb)
        assert (bita == bitc)
        assert (bita == bitd)
        assert (bita == bite)
        assert (bita == bitf)
        assert (bita == bitg)
Esempio n. 7
0
 def convert_measurements(self, backend_result) -> QubitWaveFunction:
     """
     map backend results to QubitWaveFunction
     Parameters
     ----------
     backend_result:
         the result returned directly qiskit simulation.
     Returns
     -------
     QubitWaveFunction:
         measurements converted into wave function form.
     """
     qiskit_counts = backend_result.result().get_counts()
     result = QubitWaveFunction()
     # todo there are faster ways
     for k, v in qiskit_counts.items():
         converted_key = BitString.from_bitstring(
             other=BitStringLSB.from_binary(binary=k))
         result._state[converted_key] = v
     return result
Esempio n. 8
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)
Esempio n. 9
0
 def __call__(self, input_state: BitString, initial_state: int = None) -> BitStringLSB:
     if isinstance(input_state, numbers.Integral):
         return BitStringLSB.from_int(integer=input_state)
     else:
         return BitStringLSB.from_int(integer=input_state.integer, nbits=input_state.nbits)