Ejemplo n.º 1
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, numbering=self.numbering)
Ejemplo n.º 2
0
    def convert_measurements(self, backend_result) -> QubitWaveFunction:
        """0.
        :param backend_result: array from pyquil as list of lists of integers.
        :return: backend_result in Tequila format.
        """
        def string_to_array(s):
            listing = []
            for letter in s:
                if letter not in [',', ' ', '[', ']', '.']:
                    listing.append(int(letter))
            return listing

        result = QubitWaveFunction()
        bit_dict = {}
        for b in backend_result:
            try:
                bit_dict[str(b)] += 1
            except:
                bit_dict[str(b)] = 1

        for k, v in bit_dict.items():
            arr = string_to_array(k)
            result._state[BitString.from_array(arr)] = v
        return result
Ejemplo n.º 3
0
def strip_sympy_zeros(wfn: QubitWaveFunction):
    result = QubitWaveFunction()
    for k, v in wfn.items():
        if v != 0:
            result[k] = v
    return result
Ejemplo n.º 4
0
    def apply_on_standard_basis(cls, gate: QGate, basisfunction: BitString,
                                qubits: dict, variables) -> QubitWaveFunction:

        basis_array = basisfunction.array
        if gate.is_controlled():
            do_apply = True
            check = [basis_array[qubits[c]] == 1 for c in gate.control]
            for c in check:
                if not c:
                    do_apply = False
            if not do_apply:
                return QubitWaveFunction.from_int(basisfunction)

        if len(gate.target) > 1:
            raise Exception(
                "Multi-targets not supported for symbolic simulators")

        result = QubitWaveFunction()
        for tt in gate.target:
            t = qubits[tt]
            qt = basis_array[t]
            a_array = copy.deepcopy(basis_array)
            a_array[t] = (a_array[t] + 1) % 2
            current_state = QubitWaveFunction.from_int(basisfunction)
            altered_state = QubitWaveFunction.from_int(
                BitString.from_array(a_array))

            fac1 = None
            fac2 = None
            if gate.name == "H":
                fac1 = (sympy.Integer(-1)**qt *
                        sympy.sqrt(sympy.Rational(1 / 2)))
                fac2 = (sympy.sqrt(sympy.Rational(1 / 2)))
            elif gate.name.upper() == "CNOT" or gate.name.upper() == "X":
                fac2 = sympy.Integer(1)
            elif gate.name.upper() == "Y":
                fac2 = sympy.I * sympy.Integer(-1)**(qt)
            elif gate.name.upper() == "Z":
                fac1 = sympy.Integer(-1)**(qt)
            elif gate.name.upper() == "RX":
                angle = sympy.Rational(1 / 2) * gate.parameter(variables)
                fac1 = sympy.cos(angle)
                fac2 = -sympy.sin(angle) * sympy.I
            elif gate.name.upper() == "RY":
                angle = -sympy.Rational(1 / 2) * gate.parameter(variables)
                fac1 = sympy.cos(angle)
                fac2 = +sympy.sin(angle) * sympy.Integer(-1)**(qt + 1)
            elif gate.name.upper() == "RZ":
                angle = sympy.Rational(1 / 2) * gate.parameter(variables)
                fac1 = sympy.exp(-angle * sympy.I * sympy.Integer(-1)**(qt))
            else:
                raise Exception("Gate is not known to simulators, " +
                                str(gate))

            if fac1 is None or fac1 == 0:
                result += fac2 * altered_state
            elif fac2 is None or fac2 == 0:
                result += fac1 * current_state
            elif fac1 is None and fac2 is None:
                raise Exception("???")
            else:
                result += fac1 * current_state + fac2 * altered_state

        return result
Ejemplo n.º 5
0
def test_pauli_gates(paulis, qubit, init):
    iwfn = QubitWaveFunction.from_int(i=init, n_qubits=qubit + 1)
    wfn = simulate(paulis[0](qubit), initial_state=init)
    iwfn = iwfn.apply_qubitoperator(paulis[1](qubit))
    assert (iwfn.isclose(wfn))
Ejemplo n.º 6
0
import tequila.simulators.simulator_api
from tequila.apps import UnaryStatePrep
from tequila.apps.unary_state_prep import TequilaUnaryStateException
import numpy
from tequila import BitString
from tequila.simulators.simulator_api import BackendCircuitSymbolic
from tequila.wavefunction.qubit_wavefunction import QubitWaveFunction
import pytest


@pytest.mark.parametrize(
    "target_space",
    [['00', '11'], ['01', '11'], ['001', '010', '100'], ['0011', '1010'],
     QubitWaveFunction.from_string("1.0|00> - 2.0|11>")])
def test_construction(target_space: list):
    print("ts=", target_space)
    UPS = UnaryStatePrep(target_space=target_space)


@pytest.mark.parametrize(
    "target_space",
    [['01', '10'], ['001', '010', '100'], ['0011', '0110', '1100', '1001']])
def test_unary_states(target_space: list):
    UPS = UnaryStatePrep(target_space=target_space)
    qubits = len(target_space)
    coeff = 1.0 / numpy.sqrt(
        qubits
    )  # fails for the 3-Qubit Case because the wrong sign is picked in the solution
    coeffs = [coeff for i in range(qubits)]

    wfn = QubitWaveFunction()