Пример #1
0
def test_random_instances(target_space):

    # can happen that a tests fails, just start again ... if all tests fail: start to worry
    # it happens from time to time that UPS can not disentangle
    # it will throw the error/
    # OpenVQEException: Could not disentangle the given state after 100 restarts
    qubits = len(target_space)
    coeffs = numpy.random.uniform(0, 1, qubits)

    wfn = QubitWaveFunction()
    for i, c in enumerate(coeffs):
        wfn += c * QubitWaveFunction.from_string("1.0|" + target_space[i] +
                                                 ">")
    wfn = wfn.normalize()

    try:
        UPS = UnaryStatePrep(target_space=target_space)
        U = UPS(wfn=wfn)

        # now the coeffs are normalized
        bf2c = dict()
        for i, c in enumerate(coeffs):
            bf2c[target_space[i]] = coeffs[i]

        wfn2 = tequila.simulators.simulator_api.simulate(U,
                                                         initial_state=0,
                                                         variables=None)

        for k, v in wfn.items():
            assert (numpy.isclose(wfn2[k], v))

    except TequilaUnaryStateException:
        print("caught a tolerated excpetion")
Пример #2
0
    def _evaluate_angles(
            self, wfn: QubitWaveFunction) -> typing.Dict[sympy.Symbol, float]:
        # coeffs need to be normalized
        wfn = wfn.normalize()

        # initialize the map that will substitute the abstract_coefficients with the QubitWaveFunction coefficients
        subs = dict()
        for key, ac in self._abstract_coeffs.items():
            coeff = 0.0
            if key in wfn:
                coeff = wfn[key]
            if coeff.imag != 0.0:
                raise TequilaException(
                    "UnaryStatePrep currently only possible for real coefficients"
                )
            subs[ac] = sympy.Float(float(coeff.real))
        result = dict()
        if (self._use_symbolic_solver):
            # fails a lot of times
            # better don't use
            # get the angle variables from the symbolic equations for the angles
            for symbol, eq in self._angle_equations.items():
                result[symbol] = float(eq.evalf(subs=subs))
        else:
            # numeric solution (more stable)
            # integrated repeat loop for the case that the randomly generated guess is especially bad
            count = 0
            solutions = []
            while (count < self.max_repeat and len(solutions) == 0):
                try:
                    # same substitution map as before, but initialized as list of tuples
                    subsx = [x for x in subs.items()]
                    equations = [eq.subs(subsx) for eq in self._equations]
                    guess = numpy.random.uniform(0.1, 0.9 * 2 * numpy.pi,
                                                 len(self._abstract_angles))
                    solutions = sympy.nsolve(equations, self._abstract_angles,
                                             guess)
                    count += 1
                except:
                    count += 1

            if (len(solutions) == 0):
                raise TequilaUnaryStateException(
                    "Failed to numerically solve for angles")

            for i, symbol in enumerate(self._abstract_angles):
                result[symbol] = float(solutions[i].evalf(subs=subs))

        return result