Exemplo n.º 1
0
def test_total_type_jumble(simulator,value1=(numpy.random.randint(10, 1000) / 1000.0 * (numpy.pi / 2.0)),
                value2=(numpy.random.randint(10, 1000) / 1000.0 * (numpy.pi / 2.0))):
    a = Variable('a')
    b = Variable('b')
    values = {a: value1, b: value2}
    H1 = tq.paulis.X(0)
    H2 = tq.paulis.Y(0)
    U1= tq.gates.Ry(angle=a,target=0)
    U2= tq.gates.Rx(angle=b,target=0)
    e1=ExpectationValue(U1,H1)
    e2=ExpectationValue(U2,H2)
    stacked= tq.objective.vectorize([e1, e2])
    stacked = stacked*a*e2
    out=simulate(stacked,variables=values,backend=simulator)
    v1=out[0]
    v2=out[1]
    appendage =  a(values) * -np.sin(b(values))
    an1= np.sin(a(values)) *  appendage
    an2= -np.sin(b(values)) * appendage
    assert np.isclose(v1+v2,an1+an2)
    # not gonna contract, lets make gradient do some real work
    ga=grad(stacked,a)
    gb=grad(stacked,b)
    la=[tq.simulate(x,variables=values) for x in ga]
    print(la)
    lb=[tq.simulate(x,variables=values) for x in gb]
    print(lb)
    tota=np.sum(np.array(la))
    totb=np.sum(np.array(lb))
    gan1= np.cos(a(values)) * appendage + (np.sin(a(values)) * -np.sin(b(values))) - (np.sin(b(values)) * -np.sin(b(values)))
    gan2= np.sin(a(values)) * a(values) * -np.cos(b(values)) + 2 * (-np.cos(b(values)) * appendage)
    assert np.isclose(tota+totb,gan1+gan2)
Exemplo n.º 2
0
def test_really_awfull_thing(simulator, value1=(numpy.random.randint(10, 1000) / 1000.0 * (numpy.pi / 2.0)),
                             value2=(numpy.random.randint(10, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}

    prod = angle1 * angle2
    qubit = 0
    control = None
    H = paulis.Y(qubit=qubit)
    U = gates.Rx(target=qubit, control=control, angle=prod)
    Up = gates.Rx(target=qubit, control=control, angle=prod + np.pi / 2)
    Down = gates.Rx(target=qubit, control=control, angle=prod - np.pi / 2)
    e1 = ExpectationValue(U=U, H=H)
    en1 = simulate(e1, variables=variables, backend=simulator)
    uen = simulate(0.5 * ExpectationValue(Up, H), variables=variables, backend=simulator)
    den = simulate(-0.5 * ExpectationValue(Down, H), variables=variables, backend=simulator)
    an1 = -np.sin(prod(variables=variables))
    anval = prod(variables=variables)
    an2 = angle2(variables=variables)
    added = angle1 * e1
    raised = added.wrap(np.sin)
    dO = grad(raised, 'angle1')
    dE = grad(e1, 'angle1')
    dA = grad(added, 'angle1')
    val = simulate(added, variables=variables, backend=simulator)
    dave = simulate(dA, variables=variables, backend=simulator)
    deval = simulate(dE, variables=variables, backend=simulator)
    doval = simulate(dO, variables=variables, backend=simulator)
    dtrue = np.cos(val) * dave
    assert np.isclose(en1, an1, atol=1.e-4)
    assert np.isclose(deval, an2 * (uen + den), atol=1.e-4)
    assert np.isclose(doval, dtrue, atol=1.e-4)
Exemplo n.º 3
0
def test_mixed_power(
    simulator,
    value1=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0)),
    value2=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(
        target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    H2 = paulis.Y(qubit=qubit)
    U2 = gates.X(target=control) + gates.Rx(
        target=qubit, control=control, angle=angle2)
    e2 = ExpectationValue(U=U2, H=H2)
    added = e1**e2
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator)
    en2 = simulate(e2, variables=variables, backend=simulator)
    an1 = np.sin(angle1(variables=variables))
    an2 = -np.sin(angle2(variables=variables))
    assert np.isclose(val, en1**en2, atol=1.e-4)
    assert np.isclose(val, an1**an2, atol=1.e-4)
Exemplo n.º 4
0
def test_akward_expression(
    simulator,
    value1=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0)),
    value2=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}

    prod = angle1 * angle2
    qubit = 0
    control = None
    H = paulis.Y(qubit=qubit)
    U = gates.Rx(target=qubit, control=control, angle=prod)
    Up = gates.Rx(target=qubit, control=control, angle=prod + np.pi / 2)
    Down = gates.Rx(target=qubit, control=control, angle=prod - np.pi / 2)
    e1 = ExpectationValue(U=U, H=H)
    en1 = simulate(e1, variables=variables, backend=simulator)
    uen = simulate(0.5 * ExpectationValue(Up, H),
                   variables=variables,
                   backend=simulator)
    den = simulate(-0.5 * ExpectationValue(Down, H),
                   variables=variables,
                   backend=simulator)
    an1 = -np.sin(prod(variables=variables))
    anval = prod(variables=variables)
    an2 = angle2(variables=variables)
    added = angle1 * e1
    dO = grad(added, 'angle1')
    dE = grad(e1, 'angle1')
    deval = simulate(dE, variables=variables, backend=simulator)
    doval = simulate(dO, variables=variables, backend=simulator)
    dtrue = angle1(variables=variables) * deval + en1
    assert np.isclose(en1, an1)
    assert np.isclose(deval, an2 * (uen + den), atol=1.e-4)
    assert np.isclose(doval, dtrue, atol=1.e-4)
Exemplo n.º 5
0
def test_stacking():
    a=Variable('a')
    b=Variable('b')
    def f(x):
        return np.cos(x)**2. + np.sin(x)**2.
    funcs=[f,f,f,f]
    vals = {Variable('a'):numpy.random.uniform(0,np.pi),Variable('b'):numpy.random.uniform(0,np.pi)}
    O = VectorObjective(argsets=[[a],[b],[a],[b]],transformations=funcs)
    O1 = O.apply_op_list(funcs)
    O2 = O1/4
    output = simulate(O2,variables=vals)
    assert np.isclose(1.,np.sum(output))
Exemplo n.º 6
0
def test_circuit_from_moments():
    c = QCircuit()
    c += CNOT(target=0, control=(1, 2, 3))
    c += Phase(phi=numpy.pi, target=4)
    c += Rx(angle=Variable('a'), target=[0, 3])
    c += H(target=[0, 1])
    c += Rx(angle=Variable('a'), target=[2, 3])
    ## table[1] should equal 1 at this point, len(moments should be 3)
    c += Z(target=1)
    c += Rx(angle=Variable('a'), target=[0, 3])
    moms = c.moments
    c2 = QCircuit.from_moments(moms)
    assert c == c2
Exemplo n.º 7
0
def test_canonical_moments():
    c = QCircuit()
    c += CNOT(target=0, control=(1, 2, 3))
    c += Rx(angle=Variable('a'), target=[0, 3])
    c += H(target=[0, 1])
    c += Rx(angle=Variable('a'), target=[2, 3])
    c += Rx(angle=Variable('a'), target=[0, 3])
    c += Z(target=1)
    c += Phase(phi=numpy.pi, target=4)
    moms = c.canonical_moments
    assert len(moms) == 6
    assert (moms[0].gates[1].parameter == assign_variable(numpy.pi))
    assert (moms[0].gates[1].target == (4, ))
    assert hasattr(moms[3].gates[0], 'axis')
    assert len(moms[0].qubits) == 5
Exemplo n.º 8
0
def test_gradient_deep_H(simulator, power, controls):
    if controls > 2 and simulator == "qiskit":
        # does not work yet
        return
    qubit = 0
    angle = Variable(name="angle")
    variables = {angle: power}
    control = [i for i in range(1, controls + 1)]
    H = paulis.X(qubit=qubit)

    U = gates.X(target=control) + gates.H(
        target=qubit, control=control, power=angle)

    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    assert (numpy.isclose(E,
                          -numpy.cos(angle(variables) * (numpy.pi)) / 2 + 0.5,
                          atol=1.e-4))
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)

    assert (numpy.isclose(dE,
                          numpy.pi * numpy.sin(angle(variables) * (numpy.pi)) /
                          2,
                          atol=1.e-4))
Exemplo n.º 9
0
def test_gradient_H(simulator, power, controlled):
    qubit = 0
    control = 1
    angle = Variable(name="angle")
    variables = {angle: power}

    H = paulis.X(qubit=qubit)
    if not controlled:
        U = gates.H(target=qubit, power=angle)
    else:
        U = gates.X(target=control) + gates.H(
            target=qubit, control=control, power=angle)

    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    assert (numpy.isclose(E,
                          -numpy.cos(angle(variables) * (numpy.pi)) / 2 + 0.5,
                          atol=1.e-4))
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)

    assert (numpy.isclose(dE,
                          numpy.pi * numpy.sin(angle(variables) * (numpy.pi)) /
                          2,
                          atol=1.e-4))
Exemplo n.º 10
0
def test_gradient_UX_HY_wfnsim(simulator, angle, controlled, silent=True):
    # same as before just with wavefunction simulation

    # case YX
    # U = cos(angle/2) + sin(-angle/2)*i*X
    # O = cos*sin*i*<0|YX|0> + sin*cos*(-i)<0|XY|0>
    #   = 0.5*sin(-angle)*i <0|[YX,XY]|0>
    #   = -sin(angle)

    angle_value = angle
    angle = Variable(name="angle")
    variables = {angle: angle_value}

    qubit = 0
    H = paulis.Y(qubit=qubit)
    if controlled:
        control = 1
        U = gates.X(target=control) + gates.Rx(
            target=qubit, control=control, angle=angle)
    else:
        U = gates.Rx(target=qubit, angle=angle)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables)
    assert (numpy.isclose(E, -numpy.sin(angle(variables)), atol=0.0001))
    assert (numpy.isclose(dE, -numpy.cos(angle(variables)), atol=0.0001))
    if not silent:
        print("E         =", E)
        print("-sin(angle)=", -numpy.sin(angle(variables)))
        print("dE        =", dE)
        print("-cos(angle)=", -numpy.cos(angle(variables)))
Exemplo n.º 11
0
def test_gradient_UY_HX(simulator, angle_value, controlled, silent=True):
    # case X Y
    # U = cos(angle/2) + sin(-angle/2)*i*Y
    # <0|Ud H U |0> = cos^2(angle/2)*<0|X|0>
    # + sin^2(-angle/2) <0|YXY|0>
    # + cos(angle/2)*sin(angle/2)*i<0|XY|0>
    # + sin(-angle/2)*cos(angle/2)*(-i) <0|YX|0>
    # = cos^2*0 + sin^2*0 + cos*sin*i(<0|[XY,YX]|0>)
    # = 0.5*sin(-angle)*i <0|[XY,YX]|0> = -0.5*sin(angle)*i * 2 i <0|Z|0>
    # = sin(angle)

    angle = Variable(name="angle")
    variables = {angle: angle_value}

    qubit = 0
    H = paulis.X(qubit=qubit)
    if controlled:
        control = 1
        U = gates.X(target=control) + gates.Ry(
            target=qubit, control=control, angle=angle)
    else:
        U = gates.X(target=qubit) + gates.X(target=qubit) + gates.Ry(
            target=qubit, angle=angle)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    print("O={type}".format(type=type(O)))
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)
    assert (numpy.isclose(E, numpy.sin(angle(variables)), atol=1.e-4))
    assert (numpy.isclose(dE, numpy.cos(angle(variables)), atol=1.e-4))
    if not silent:
        print("E         =", E)
        print("sin(angle)=", numpy.sin(angle()))
        print("dE        =", dE)
        print("cos(angle)=", numpy.cos(angle()))
Exemplo n.º 12
0
def test_gradient_PHASE_HY(simulator, angle_value, controlled, silent=False):
    angle = Variable(name="angle")
    variables = {angle: angle_value}

    qubit = 0
    H = paulis.Y(qubit=qubit)
    if controlled:
        control = 1
        U = gates.X(target=control) + gates.H(target=qubit) + gates.Phase(
            target=qubit, control=control, phi=angle) + gates.H(target=qubit)
    else:
        U = gates.H(target=qubit) + gates.Phase(
            target=qubit, phi=angle) + gates.H(target=qubit)

    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    dO = grad(objective=O, variable='angle')
    dE = simulate(dO, variables=variables)
    assert (numpy.isclose(E, -numpy.sin(angle(variables)), atol=1.e-4))
    assert (numpy.isclose(dE, -numpy.cos(angle(variables)), atol=1.e-4))
    if not silent:
        print("E         =", E)
        print("-sin(angle)=", -numpy.sin(angle(variables)))
        print("dE        =", dE)
        print("-cos(angle)=", -numpy.cos(angle(variables)))
Exemplo n.º 13
0
def test_conventions():
    qubit = numpy.random.randint(0, 3)
    angle = Variable("angle")

    Rx1 = Rx(target=qubit, angle=angle)
    Rx2 = QCircuit.wrap_gate(
        RotationGateImpl(axis="X", target=qubit, angle=angle))
    Rx3 = QCircuit.wrap_gate(
        RotationGateImpl(axis="x", target=qubit, angle=angle))
    Rx4 = RotationGate(axis=0, target=qubit, angle=angle)
    Rx5 = RotationGate(axis="X", target=qubit, angle=angle)
    Rx6 = RotationGate(axis="x", target=qubit, angle=angle)
    Rx7 = RotationGate(axis=0, target=qubit, angle=angle)
    Rx7.axis = "X"

    ll = [Rx1, Rx2, Rx3, Rx4, Rx5, Rx6, Rx7]
    for l1 in ll:
        for l2 in ll:
            assert (l1 == l2)

    qubit = 2
    for c in [None, 0, 3]:
        for angle in ["angle", 0, 1.234]:
            for axes in [[0, "x", "X"], [1, "y", "Y"], [2, "z", "Z"]]:
                ll = [
                    RotationGate(axis=i, target=qubit, control=c, angle=angle)
                    for i in axes
                ]
                for l1 in ll:
                    for l2 in ll:
                        assert (l1 == l2)
                        l1.axis = axes[numpy.random.randint(0, 2)]
                        assert (l1 == l2)
Exemplo n.º 14
0
def test_rz_phase_flip_1(simulator, p, angle):
    U = gates.X(target=0) + gates.H(1) + gates.CRz(control=0, target=1, angle=Variable('a')) + gates.H(1)
    H = paulis.Z(1) * paulis.I(0)
    O = ExpectationValue(U, H)
    NM = PhaseFlip(p, 2)
    E = simulate(O, backend=simulator, variables={'a': angle}, samples=1000, noise=NM)
    print(E)
    assert (numpy.isclose(E, ((1.0 - 2 * p) ** 2) * numpy.cos(angle), atol=1.e-1))
Exemplo n.º 15
0
def test_gradient():
    a = Variable(name='a')
    variables = {a: 3.0}
    b = a + 2 - 2
    c = (b * 5) / 5
    d = -(-c)

    assert grad(d, a)(variables) == 1.0
Exemplo n.º 16
0
def test_heterogeneous_operations_r(simulator, op, value1=(numpy.random.randint(1, 999) / 1000.0 * (numpy.pi / 2.0)),
                                    value2=(numpy.random.randint(1, 999) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}
    qubit = 0
    control = 1
    H1 = paulis.Y(qubit=qubit)
    U1 = gates.X(target=control) + gates.Rx(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = Objective(args=[e1.args[0], angle2], transformation=op)
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator)
    an1 = -np.sin(angle1(variables=variables))
    an2 = angle2(variables=variables)
    assert np.isclose(val, float(op(en1, an2)), atol=1.e-4)
    assert np.isclose(en1, an1, atol=1.e-4)
Exemplo n.º 17
0
def test_heterogeneous_operations_l(simulator, op, value1=(numpy.random.randint(1, 1000) / 1000.0 * (numpy.pi / 2.0)),
                                    value2=(numpy.random.randint(1, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}
    qubit = 0
    control = 1
    H2 = paulis.X(qubit=qubit)
    U2 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle2)
    e2 = ExpectationValue(U=U2, H=H2)
    added = Objective(args=[angle1, e2.args[0]], transformation=op)
    val = simulate(added, variables=variables, backend=simulator)
    en2 = simulate(e2, variables=variables, backend=simulator)
    an1 = angle1(variables=variables)
    an2 = np.sin(angle2(variables=variables))
    assert np.isclose(val, float(op(an1, en2)), atol=1.e-4)
    assert np.isclose(en2, an2, atol=1.e-4)
Exemplo n.º 18
0
def test_rz_phase_flip_0(simulator, p, angle):
    qubit = 0
    H = paulis.Y(qubit)
    U = gates.H(target=qubit) + gates.Rz(angle=Variable('a'), target=qubit) + gates.H(target=qubit)
    O = ExpectationValue(U=U, H=H)
    NM = PhaseFlip(p, 1)
    E = simulate(O, backend=simulator, variables={'a': angle}, samples=1000, noise=NM)
    assert (numpy.isclose(E, ((-1. + 2 * p) ** 3) * numpy.sin(angle), atol=1.e-1))
Exemplo n.º 19
0
def test_rx_bit_flip_0(simulator, p, angle):
    U = gates.Rx(target=0, angle=Variable('a'))
    H = paulis.Z(0)
    NM = BitFlip(p, 1)

    O = ExpectationValue(U=U, H=H)

    E = simulate(O, backend=simulator, samples=1000, variables={'a': angle}, noise=NM)
    assert (numpy.isclose(E, (1 - 2 * p) * numpy.cos(angle), atol=1.e-1))
Exemplo n.º 20
0
def test_nesting():
    a = Variable(name='a')
    variables = {a: 3.0}
    b = a + 2 - 2
    c = (b * 5) / 5
    d = -(-c)
    e = d**0.5
    f = e**2

    assert np.isclose(a(variables), f(variables))
Exemplo n.º 21
0
def test_gradient_Y(simulator, power, controlled):
    if simulator != "cirq":
        return
    qubit = 0
    control = 1
    angle = Variable(name="angle")
    if controlled:
        U = gates.X(target=control) + gates.Y(target=qubit, power=angle, control=control)
    else:
        U = gates.Y(target=qubit, power=angle)
    angle = Variable(name="angle")
    variables = {angle: power}
    H = paulis.X(qubit=qubit)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)
    assert (numpy.isclose(E, numpy.sin(angle(variables) * (numpy.pi)), atol=1.e-4))
    assert (numpy.isclose(dE, numpy.pi * numpy.cos(angle(variables) * (numpy.pi)), atol=1.e-4))
Exemplo n.º 22
0
def test_rx_bit_flip_0(simulator, p, angle):

    U = gates.Rx(target=0, angle=Variable('a'))
    H = paulis.Z(0)
    NM = BitFlip(p, 1)

    O = ExpectationValue(U=U, H=H)

    E = simulate(O,
                 backend=simulator,
                 samples=1,
                 variables={'a': angle},
                 noise=NM)
Exemplo n.º 23
0
def test_rz_phase_flip_1(simulator, p, angle):

    U = gates.X(target=0) + gates.H(1) + gates.CRz(
        control=0, target=1, angle=Variable('a')) + gates.H(1)
    H = paulis.Z(1) * paulis.I(0)
    O = ExpectationValue(U, H)
    NM = PhaseFlip(p, 2)
    E = simulate(O,
                 backend=simulator,
                 variables={'a': angle},
                 samples=1,
                 noise=NM)
    print(E)
Exemplo n.º 24
0
def test_rz_phase_flip_0(simulator, p, angle):

    qubit = 0
    H = paulis.Y(qubit)
    U = gates.H(target=qubit) + gates.Rz(angle=Variable('a'),
                                         target=qubit) + gates.H(target=qubit)
    O = ExpectationValue(U=U, H=H)
    NM = PhaseFlip(p, 1)
    E = simulate(O,
                 backend=simulator,
                 variables={'a': angle},
                 samples=1,
                 noise=NM)
    print(E)
Exemplo n.º 25
0
def test_l_addition(simulator, value=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    variables = {angle1: value}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = e1 + 1
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator) + 1.
    an1 = np.sin(angle1(variables=variables)) + 1.
    assert np.isclose(val, en1, atol=1.e-4)
    assert np.isclose(val, an1, atol=1.e-4)
Exemplo n.º 26
0
def test_l_division(simulator, value=numpy.random.uniform(0.0, 2.0*numpy.pi, 1)[0]):
    angle1 = Variable(name="angle1")
    variables = {angle1: value}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = e1 / 2
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator) / 2
    an1 = np.sin(value) / 2.
    assert np.isclose(val, en1, atol=1.e-4)
    assert np.isclose(val, an1, atol=1.e-4)
Exemplo n.º 27
0
def test_r_power(simulator, value=numpy.random.uniform(0.1, 1.9*numpy.pi, 1)[0]):
    angle1 = Variable(name="angle1")
    variables = {angle1: value}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = 2 ** e1
    val = simulate(added, variables=variables, backend=simulator)
    en1 = 2 ** simulate(e1, variables=variables, backend=simulator)
    an1 = 2. ** np.sin(angle1(variables=variables))
    assert np.isclose(val, en1, atol=1.e-4)
    assert np.isclose(val, an1, atol=1.e-4)
Exemplo n.º 28
0
def test_rx_bit_flip_1(simulator, p, angle):

    qubit = 1
    U = gates.X(target=0) + gates.CRx(control=0, target=1, angle=Variable('a'))
    H = paulis.Z(1) * paulis.I(0)
    NM = BitFlip(p, 2)

    O = ExpectationValue(U=U, H=H)

    E = simulate(O,
                 backend=simulator,
                 samples=1,
                 variables={'a': angle},
                 noise=NM)
    print(E)
    print(p + numpy.cos(angle) - p * numpy.cos(angle))
Exemplo n.º 29
0
def test_exotic_gradients(gradvar):
    # a and b will fail for autograd not with jax
    a = Variable('a')
    b = Variable('b')
    c = Variable('c')
    d = Variable('d')
    e = Variable('e')
    f = Variable('f')
    variables = {a: 2.0, b: 3.0, c: 4.0, d: 5.0, e: 6.0, f: 7.0}

    t = c * a**b + b / c - Objective(
        args=[c], transformation=np.cos) + f / (d * e) + a * Objective(
            args=[d], transformation=np.exp) / (f + b) + Objective(
                args=[e], transformation=np.tanh) + Objective(
                    args=[f], transformation=np.sinc)
    g = grad(t, gradvar)
    if gradvar == 'a':
        assert np.isclose(
            g(variables),
            c(variables) * b(variables) * (a(variables)**(b(variables) - 1.)) +
            np.exp(d(variables)) / (f(variables) + b(variables)))
    if gradvar == 'b':
        assert np.isclose(
            g(variables),
            (c(variables) * a(variables)**b(variables)) * np.log(a(variables))
            + 1. / c(variables) - a(variables) * np.exp(d(variables)) /
            (f(variables) + b(variables))**2.0)
    if gradvar == 'c':
        assert np.isclose(
            g(variables),
            a(variables)**b(variables) - b(variables) / c(variables)**2. +
            np.sin(c(variables)))
    if gradvar == 'd':
        assert np.isclose(
            g(variables),
            -f(variables) / (np.square(d(variables)) * e(variables)) +
            a(variables) * np.exp(d(variables)) /
            (f(variables) + b(variables)))
    if gradvar == 'e':
        assert np.isclose(
            g(variables), 2. / (1. + np.cosh(2 * e(variables))) -
            f(variables) / (d(variables) * e(variables)**2.))
    if gradvar == 'f':
        assert np.isclose(
            g(variables), 1. / (d(variables) * e(variables)) -
            a(variables) * np.exp(d(variables)) /
            (f(variables) + b(variables))**2. +
            np.cos(np.pi * f(variables)) / f(variables) -
            np.sin(np.pi * f(variables)) / (np.pi * f(variables)**2.))
Exemplo n.º 30
0
def test_gradient_UY_HX_wfnsim(simulator,
                               angle_value,
                               controlled,
                               silent=True):
    # same as before just with wavefunction simulation

    # case X Y
    # U = cos(angle/2) + sin(-angle/2)*i*Y
    # <0|Ud H U |0> = cos^2(angle/2)*<0|X|0>
    # + sin^2(-angle/2) <0|YXY|0>
    # + cos(angle/2)*sin(angle/2)*i<0|XY|0>
    # + sin(-angle/2)*cos(angle/2)*(-i) <0|YX|0>
    # = cos^2*0 + sin^2*0 + cos*sin*i(<0|[XY,YX]|0>)
    # = 0.5*sin(-angle)*i <0|[XY,YX]|0> = -0.5*sin(angle)*i * 2 i <0|Z|0>
    # = sin(angle)

    angle = Variable(name="angle")
    variables = {angle: angle_value}

    qubit = 0
    H = paulis.X(qubit=qubit)
    if controlled:
        control = 1
        U = gates.X(target=control) + gates.Ry(
            target=qubit, control=control, angle=angle)
    else:
        U = gates.Ry(target=qubit, angle=angle)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    dO = grad(objective=O, variable='angle')
    dE = simulate(dO, variables=variables, backend=simulator)
    E = numpy.float(E)  # for isclose
    dE = numpy.float(dE)  # for isclose
    assert (numpy.isclose(E, numpy.sin(angle(variables)), atol=0.0001))
    assert (numpy.isclose(dE, numpy.cos(angle(variables)), atol=0.0001))
    if not silent:
        print("E         =", E)
        print("sin(angle)=", numpy.sin(angle(variables)))
        print("dE        =", dE)
        print("cos(angle)=", numpy.cos(angle(variables)))