def Ising_prog_3(n, t):
    '''Definition of a function to obtain a sequence of gates
       for an Ising chain with H = -0.5*(1/|i-j|)*sigma_x_i*sigma_x_j 
       - 0.5*sigma_x_j - 0.5*sigma_z_j where index i j run from 0th 
       to (n-1)th site and i is always smaller than j. Here 3rd-order
       Trotter approximant is applied. 
       param n: number of qubits (n>=2).
       param t: evolution time.'''
    from pyquil.paulis import exponentiate
    factor = -0.5
    prog = Program()
    # The coefficients of exponential operators
    coef_lis = [7 / 24, 2 / 3, 3 / 4, -2 / 3, -1 / 24, 1]
    for i in np.arange(len(comb_xx(n))):
        prog_ini = Program()
        prog_ini += trotterize(t * factor * coef_lis[0] * comb_xx(n)[i],
                               t * factor * coef_lis[0] * comb_x(n)[i],
                               trotter_order=3)
        prog_ini += exponentiate(t * factor * coef_lis[1] * comb_z(n)[i])
        prog_ini += trotterize(t * factor * coef_lis[2] * comb_xx(n)[i],
                               t * factor * coef_lis[2] * comb_x(n)[i],
                               trotter_order=3)
        prog_ini += exponentiate(t * factor * coef_lis[3] * comb_z(n)[i])
        prog_ini += trotterize(t * factor * coef_lis[4] * comb_xx(n)[i],
                               t * factor * coef_lis[4] * comb_x(n)[i],
                               trotter_order=3)
        prog_ini += exponentiate(t * factor * coef_lis[5] * comb_z(n)[i])
        prog += prog_ini
    return prog
Ejemplo n.º 2
0
def test_trotterize():
    term_one = PauliTerm("X", 0, 1.0)
    term_two = PauliTerm("Z", 0, 1.0)

    with pytest.raises(ValueError):
        trotterize(term_one, term_two, trotter_order=0)
    with pytest.raises(ValueError):
        trotterize(term_one, term_two, trotter_order=5)

    prog = trotterize(term_one, term_one)
    result_prog = Program().inst([H(0), RZ(2.0, 0), H(0), H(0),
                                  RZ(2.0, 0), H(0)])
    assert prog == result_prog

    # trotter_order 1 steps 1
    prog = trotterize(term_one, term_two, trotter_steps=1)
    result_prog = Program().inst([H(0), RZ(2.0, 0), H(0), RZ(2.0, 0)])
    assert prog == result_prog

    # trotter_order 1 steps 2
    prog = trotterize(term_one, term_two, trotter_steps=2)
    result_prog = Program().inst([H(0), RZ(1.0, 0), H(0), RZ(1.0, 0),
                                  H(0), RZ(1.0, 0), H(0), RZ(1.0, 0)])
    assert prog == result_prog

    # trotter_order 2 steps 1
    prog = trotterize(term_one, term_two, trotter_order=2)
    result_prog = Program().inst([H(0), RZ(1.0, 0), H(0), RZ(2.0, 0),
                                  H(0), RZ(1.0, 0), H(0)])
    assert prog == result_prog

    # trotter_order 2 steps 2
    prog = trotterize(term_one, term_two, trotter_order=2, trotter_steps=2)
    result_prog = Program().inst([H(0), RZ(0.5, 0), H(0), RZ(1.0, 0),
                                  H(0), RZ(0.5, 0), H(0),
                                  H(0), RZ(0.5, 0), H(0), RZ(1.0, 0),
                                  H(0), RZ(0.5, 0), H(0)])
    assert prog == result_prog

    # trotter_order 3 steps 1
    prog = trotterize(term_one, term_two, trotter_order=3, trotter_steps=1)
    result_prog = Program().inst([H(0), RZ(14.0 / 24, 0), H(0), RZ(4.0 / 3.0, 0),
                                  H(0), RZ(1.5, 0), H(0), RZ(-4.0 / 3.0, 0),
                                  H(0), RZ(-2.0 / 24, 0), H(0), RZ(2.0, 0)])
    assert prog == result_prog
Ejemplo n.º 3
0
    def error(order, time_step_length):
        a_pauli = time_step_length * sZ(0) * sY(1) * sX(2)
        a_program = a_pauli.program

        b_pauli = time_step_length * sX(0) * sZ(1) * sY(2)
        b_program = b_pauli.program

        num_qubits = len(a_program.get_qubits())
        assert num_qubits == len(b_program.get_qubits())

        a = program_unitary(a_program, num_qubits)
        b = program_unitary(b_program, num_qubits)
        a_plus_b = a + b
        exp_a_plus_b = expmi(time_step_length * a_plus_b)

        trotter_program = trotterize(a_pauli, b_pauli, trotter_order=order)
        trotter = program_unitary(trotter_program, num_qubits)

        return np.linalg.norm(exp_a_plus_b - trotter, np.inf)
def Ising_prog_4(n, t):
    '''Definition of a function to obtain a sequence of gates
       for an Ising chain with H = -0.5*(1/|i-j|)*sigma_x_i*sigma_x_j 
       - 0.5*sigma_x_j - 0.5*sigma_z_j where index i j run from 0th 
       to (n-1)th site and i is always smaller than j. Here 4th-order
       Trotter approximant is applied. 
       param n: number of qubits (n>=2).
       param t: evolution time.'''
    from pyquil.paulis import exponentiate
    factor = -0.5
    prog = Program()
    # The coefficient k2
    coef_k = 1 / (4 - 4**(1 / 3))
    for i in np.arange(len(comb_xx(n))):
        prog_ini = Program()
        prog_ini += trotterize(t * factor * (coef_k / 2) * comb_xx(n)[i],
                               t * factor * (coef_k / 2) * comb_x(n)[i],
                               trotter_order=4)
        prog_ini += exponentiate(t * factor * coef_k * comb_z(n)[i])
        prog_ini += trotterize(t * factor * coef_k * comb_xx(n)[i],
                               t * factor * coef_k * comb_x(n)[i],
                               trotter_order=4)
        prog_ini += exponentiate(t * factor * coef_k * comb_z(n)[i])
        prog_ini += trotterize(
            t * factor * ((1 - 3 * coef_k) / 2) * comb_xx(n)[i],
            t * factor * ((1 - 3 * coef_k) / 2) * comb_x(n)[i],
            trotter_order=4)
        prog_ini += exponentiate(t * factor * (1 - 4 * coef_k) * comb_z(n)[i])
        prog_ini += trotterize(
            t * factor * ((1 - 3 * coef_k) / 2) * comb_xx(n)[i],
            t * factor * ((1 - 3 * coef_k) / 2) * comb_x(n)[i],
            trotter_order=4)
        prog_ini += exponentiate(t * factor * coef_k * comb_z(n)[i])
        prog_ini += trotterize(t * factor * coef_k * comb_xx(n)[i],
                               t * factor * coef_k * comb_x(n)[i],
                               trotter_order=4)
        prog_ini += exponentiate(t * factor * coef_k * comb_z(n)[i])
        prog_ini += trotterize(t * factor * (coef_k / 2) * comb_xx(n)[i],
                               t * factor * (coef_k / 2) * comb_x(n)[i],
                               trotter_order=4)
        prog += prog_ini
    return prog
def test_trotterize():
    q = QubitPlaceholder.register(8)
    term_one = PauliTerm("X", q[0], 1.0)
    term_two = PauliTerm("Z", q[0], 1.0)

    with pytest.raises(ValueError):
        trotterize(term_one, term_two, trotter_order=0)
    with pytest.raises(ValueError):
        trotterize(term_one, term_two, trotter_order=5)

    prog = trotterize(term_one, term_one)
    result_prog = Program().inst(
        [H(q[0]),
         RZ(2.0, q[0]),
         H(q[0]),
         H(q[0]),
         RZ(2.0, q[0]),
         H(q[0])])
    assert address_qubits(prog) == address_qubits(result_prog)

    # trotter_order 1 steps 1
    prog = trotterize(term_one, term_two, trotter_steps=1)
    result_prog = Program().inst(
        [H(q[0]), RZ(2.0, q[0]),
         H(q[0]), RZ(2.0, q[0])])
    assert address_qubits(prog) == address_qubits(result_prog)

    # trotter_order 1 steps 2
    prog = trotterize(term_one, term_two, trotter_steps=2)
    result_prog = Program().inst([
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0]),
        RZ(1.0, q[0])
    ])
    assert address_qubits(prog) == address_qubits(result_prog)

    # trotter_order 2 steps 1
    prog = trotterize(term_one, term_two, trotter_order=2)
    result_prog = Program().inst([
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0]),
        RZ(2.0, q[0]),
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0])
    ])
    assert address_qubits(prog) == address_qubits(result_prog)

    # trotter_order 2 steps 2
    prog = trotterize(term_one, term_two, trotter_order=2, trotter_steps=2)
    result_prog = Program().inst([
        H(q[0]),
        RZ(0.5, q[0]),
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0]),
        RZ(0.5, q[0]),
        H(q[0]),
        H(q[0]),
        RZ(0.5, q[0]),
        H(q[0]),
        RZ(1.0, q[0]),
        H(q[0]),
        RZ(0.5, q[0]),
        H(q[0])
    ])
    assert address_qubits(prog) == address_qubits(result_prog)

    # trotter_order 3 steps 1
    prog = trotterize(term_one, term_two, trotter_order=3, trotter_steps=1)
    result_prog = Program().inst([
        H(q[0]),
        RZ(14.0 / 24, q[0]),
        H(q[0]),
        RZ(4.0 / 3.0, q[0]),
        H(q[0]),
        RZ(1.5, q[0]),
        H(q[0]),
        RZ(-4.0 / 3.0, q[0]),
        H(q[0]),
        RZ(-2.0 / 24, q[0]),
        H(q[0]),
        RZ(2.0, q[0])
    ])
    assert address_qubits(prog) == address_qubits(result_prog)