Ejemplo n.º 1
0
def test_compile_y(target, control, power):

    circuit = gates.Y(target=target, control=control, power=power)
    equivalent_circuit_y = compile_y(circuit)

    equivalent_y = gates.Rz(target=target, control=None, angle=-numpy.pi / 2) + \
                   gates.X(target=target, control=control, power=power) + \
                   gates.Rz(target=target, control=None, angle=numpy.pi / 2)

    assert (equivalent_circuit_y == equivalent_y)
Ejemplo n.º 2
0
def test_compile_ry(target, control, angle):

    circuit = gates.Ry(target=target, control=control, angle=angle)
    equivalent_circuit = compile_ry(circuit)

    equivalent_ry = gates.Rz(target=target, control=None, angle=-numpy.pi / 2) + \
                    gates.Rx(target=target, control=control, angle=angle) + \
                    gates.Rz(target=target, control=None, angle=numpy.pi / 2)

    assert (equivalent_circuit == equivalent_ry)
Ejemplo n.º 3
0
def test_basis_change(simulator):
    for angle in list(uniform(0, 2 * pi, 5)):
        EX = simulate(ExpectationValue(U=gates.Rx(target=0, angle=angle), H=PX(0)), backend=simulator)
        EY = simulate(ExpectationValue(U=gates.Rx(target=0, angle=angle), H=PY(0)), backend=simulator)
        EZ = simulate(ExpectationValue(U=gates.Rx(target=0, angle=angle), H=PZ(0)), backend=simulator)

        EXX = simulate(ExpectationValue(U=gates.Rx(target=0, angle=angle) + change_basis(target=0, axis=0),
                                   H=PZ(0)), backend=simulator)
        EYY = simulate(ExpectationValue(U=gates.Rx(target=0, angle=angle) + change_basis(target=0, axis=1),
                                   H=PZ(0)), backend=simulator)
        EZZ = simulate(ExpectationValue(U=gates.Rx(target=0, angle=angle) + change_basis(target=0, axis=2),
                                   H=PZ(0)), backend=simulator)

        assert (isclose(EX, EXX, atol=1.e-4))
        assert (isclose(EY, EYY, atol=1.e-4))
        assert (isclose(EZ, EZZ, atol=1.e-4))

    for i, gate in enumerate([gates.Rx, gates.Ry, gates.Rz]):
        angle = uniform(0, 2 * pi)
        U1 = gate(target=0, angle=angle)
        U2 = change_basis(target=0, axis=i) + gates.Rz(target=0, angle=angle) + change_basis(target=0, axis=i,
                                                                                             daggered=True)
        wfn1 = simulate(U1, backend=simulator)
        wfn2 = simulate(U2, backend=simulator)
        assert (isclose(numpy.abs(wfn1.inner(wfn2)) ** 2, 1.0, atol=1.e-4))

        if simulator == "qiskit":
            return # initial state not yet supported
        wfn1 = simulate(U1, initial_state=1, backend=simulator)
        wfn2 = simulate(U2, initial_state=1, backend=simulator)
        assert (isclose(numpy.abs(wfn1.inner(wfn2)) ** 2, 1.0, atol=1.e-4))
Ejemplo n.º 4
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))
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def test_gradient_UHZH_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.Rz(
            target=qubit, control=control, angle=angle) + gates.H(target=qubit)
    else:
        U = gates.H(target=qubit) + gates.Rz(
            target=qubit, angle=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)))