Пример #1
0
import tequila.simulators.simulator_api
from tequila.circuit import gates
from tequila.objective import Objective, ExpectationValue
from tequila.objective.objective import Variable
from tequila.hamiltonian import paulis
from tequila.circuit.gradient import grad
from tequila import numpy as np
import numpy
import pytest
import tequila as tq
from tequila.simulators.simulator_api import simulate

# Get QC backends for parametrized testing
import select_backends
simulators = select_backends.get()
samplers = select_backends.get(sampler=True)


def test_non_quantum():
    E = tq.Objective()
    E += 1.0
    E = E + 2.0
    E *= 2.0
    E = E * 2.0
    E = 1.0 * E
    E -= 1.0
    E = 1.0 + E
    E = E.apply(lambda x: x / 3.0)
    assert E() == 2.0 * 2.0 * (1.0 + 2.0) / 3.0

Пример #2
0
import tequila as tq
import pytest, numpy

# Backends
import select_backends

backends = select_backends.get()


@pytest.mark.parametrize("backend", backends)
def test_array_computation(backend):
    U = tq.gates.X(target=0)
    hamiltonians = [
        tq.paulis.I(),
        tq.paulis.X(0),
        tq.paulis.Y(0),
        tq.paulis.Z(0)
    ]
    E = tq.ExpectationValue(H=hamiltonians, U=U, shape=[4])
    result = tq.simulate(E, backend=backend)
    assert all(result == numpy.asarray([1.0, 0.0, 0.0, -1.0]))


@pytest.mark.parametrize("backend", backends)
@pytest.mark.parametrize("shape", [(4, ), (2, 2)])
def test_array_shape(backend, shape):
    expected = numpy.asarray([1.0, 0.0, 0.0, -1.0]).reshape(shape)
    U = tq.gates.X(target=0)
    hamiltonians = [
        tq.paulis.I(),
        tq.paulis.X(0),