Пример #1
0
def get_hhl_2x2(A, b, r, qubits):
    '''Generate a circuit that implements the full HHL algorithm for the case
    of 2x2 matrices.

    :param A: (numpy.ndarray) A Hermitian 2x2 matrix.
    :param b: (numpy.ndarray) A vector.
    :param r: (float) Parameter to be tuned in the algorithm.
    :param verbose: (bool) Optional information about the wavefunction.

    :return: A Quil program to perform HHL.
    '''
    p = pq.Program()
    p.inst(create_arbitrary_state(b, [qubits[3]]))
    p.inst(H(qubits[1]))
    p.inst(H(qubits[2]))
    p.defgate('CONTROLLED-U0', controlled(scipy.linalg.expm(2j*π*A/4)))
    p.inst(('CONTROLLED-U0', qubits[2], qubits[3]))
    p.defgate('CONTROLLED-U1', controlled(scipy.linalg.expm(2j*π*A/2)))
    p.inst(('CONTROLLED-U1', qubits[1], qubits[3]))
    p.inst(SWAP(qubits[1], qubits[2]))
    p.inst(H(qubits[2]))
    p.defgate('CSdag', controlled(np.array([[1, 0], [0, -1j]])))
    p.inst(('CSdag', qubits[1], qubits[2]))
    p.inst(H(qubits[1]))
    p.inst(SWAP(qubits[1], qubits[2]))
    uncomputation = p.dagger()
    p.defgate('CRy0', controlled(rY(2*π/2**r)))
    p.inst(('CRy0', qubits[1], qubits[0]))
    p.defgate('CRy1', controlled(rY(π/2**r)))
    p.inst(('CRy1', qubits[2], qubits[0]))
    p += uncomputation
    return p
Пример #2
0
def test_phase_estimation():
    phase = 0.75
    precision = 4

    phase_factor = np.exp(1.0j * 2 * np.pi * phase)
    U = np.array([[phase_factor, 0], [0, -1 * phase_factor]])

    trial_prog = phase_estimation(U, precision)

    result_prog = Program()
    ro = result_prog.declare('ro', 'BIT', precision)
    result_prog += [H(i) for i in range(precision)]

    q_out = range(precision, precision + 1)
    for i in range(precision):
        if i > 0:
            U = np.dot(U, U)
        cU = controlled(U)
        name = "CONTROLLED-U{0}".format(2**i)
        result_prog.defgate(name, cU)
        result_prog.inst((name, i) + tuple(q_out))

    result_prog += inverse_qft(range(precision))

    result_prog += [MEASURE(i, ro[i]) for i in range(precision)]

    assert (trial_prog == result_prog)
Пример #3
0
def test_gradient_program():
    f_h = 0.25
    precision = 2

    trial_prog = gradient_program(f_h, precision)

    result_prog = pq.Program([H(0), H(1)])

    phase_factor = np.exp(-1.0j * 2 * np.pi * abs(f_h))
    U = np.array([[phase_factor, 0], [0, phase_factor]])
    q_out = range(precision, precision + 1)
    for i in range(precision):
        if i > 0:
            U = np.dot(U, U)
        cU = controlled(U)
        name = "CONTROLLED-U{0}".format(2**i)
        result_prog.defgate(name, cU)
        result_prog.inst((name, i) + tuple(q_out))

    result_prog.inst([
        H(1),
        CPHASE(1.5707963267948966, 0, 1),
        H(0),
        SWAP(0, 1),
        MEASURE(0, [0]),
        MEASURE(1, [1])
    ])

    assert (trial_prog == result_prog)
Пример #4
0
def get_hhl_2x2_corrected(A, b, r, qubits):
    """ HHL program with bit code corrections on first two H gates """
    p = pq.Program()
    p.inst(create_arbitrary_state(b, [qubits[3]]))
    
    # PHASE ESTIMATION
    bit_code_H(p, qubits[1], qubits)  
    bit_code_H(p, qubits[2], qubits)    
    p.defgate('CONTROLLED-U0', controlled(scipy.linalg.expm(2j*π*A/4)))
    p.inst(('CONTROLLED-U0', qubits[2], qubits[3]))
    p.defgate('CONTROLLED-U1', controlled(scipy.linalg.expm(2j*π*A/2)))
    p.inst(('CONTROLLED-U1', qubits[1], qubits[3]))
    p.inst(SWAP(qubits[1], qubits[2]))
    p.inst(H(qubits[2]))
    p.defgate('CSdag', controlled(np.array([[1, 0], [0, -1j]])))
    p.inst(('CSdag', qubits[1], qubits[2]))
    p.inst(H(qubits[1]))
    p.inst(SWAP(qubits[1], qubits[2]))

    p.defgate('CRy0', controlled(rY(2*π/2**r)))
    p.inst(('CRy0', qubits[1], qubits[0]))
    p.defgate('CRy1', controlled(rY(π/2**r)))
    p.inst(('CRy1', qubits[2], qubits[0]))
    
    # HARD CODE THE INVERSE PHASE ESTIMATION :'(
    p.inst(SWAP(qubits[1], qubits[2]))
    p.inst(H(qubits[1]))
    p.defgate('CSdag-INV', controlled(np.array([[1, 0], [0, 1j]])))
    p.inst(('CSdag-INV', qubits[1], qubits[2]))
    p.inst(H(qubits[2]))
    p.inst(SWAP(qubits[1], qubits[2]))
    p.defgate('CONTROLLED-U1-INV', controlled(scipy.linalg.expm(-2j*π*A/2)))
    p.inst(('CONTROLLED-U1-INV', qubits[1], qubits[3]))
    p.defgate('CONTROLLED-U0-INV', controlled(scipy.linalg.expm(-2j*π*A/4)))
    p.inst(('CONTROLLED-U0-INV', qubits[2], qubits[3]))    
    p.inst(H(qubits[2]))
    p.inst(H(qubits[1]))

    # undoes create_arbitrary_state
    p.inst(RY(π/2, qubits[3]))
    p.inst(H(qubits[3]))
    return p
In |psi_0> we have a superposition of all possible time steps between 0 and T, 
so you have a superposition of all possible evolutions and a suitable choice of
number of timesteps T and total evolution time t_0 allow to encode binary representations of the eigenvalues. 

For the final step, you apply an inverse Fourier transform that writes the phases into new reg
"""

# In a 2 by 2 case, the circuit is simplified. Given that the matrix A has eigenvalues that are powers of 2
# We choose T = 4, t_0 = 2pi to obtain exact results with just two controlled evolutions

# Superposition
hhl += H(1)
hhl += H(2)

# Controlled-U0
hhl.defgate('CONTROLLED-U0', controlled(scipy.linalg.expm(2j * np.pi * A / 4)))
hhl += ('CONTROLLED-U0', 2, 3)
# Controlled-U1
hhl.defgate('CONTROLLED-U1', controlled(scipy.linalg.expm(2j * np.pi * A / 2)))
hhl += ('CONTROLLED-U1', 1, 3)

# Apply the Quantum Inverse fourier transform to write the phase to a register
hhl += SWAP(1, 2)
hhl += H(2)
hhl.defgate('CSdag', controlled(np.array([[1, 0], [0, -1j]])))
hhl += ('CSdag', 1, 2)
hhl += H(1)

hhl += SWAP(1, 2)
uncomputation = hhl.dagger()
Пример #6
0
"""
Harrow-Hassidim-Lloyd (HHL) algorithm.
"""

qvm_server, quilc_server, fc = init_qvm_and_quilc("")
qc = get_qc("6q-qvm", connection=fc)
pi = np.pi
A = 0.5*np.array([[3, 1], [1, 3]])
hhl = Program()

# Quantum phase estimation through superposition
hhl += H(1)
hhl += H(2)

# Controlled-U0
hhl.defgate("CONTROLLED-U0", controlled(scipy.linalg.expm(2j*π*A/4)))
hhl += ("CONTROLLED-U0", 2, 3)

# Controlled-U1
hhl.defgate("CONTROLLED-U1", controlled(scipy.linalg.expm(2j*π*A/2)))
hhl += ("CONTROLLED-U1", 1, 3)

# Inverse quantum inverse Fourier transformation
hhl += SWAP(1, 2)
hhl += H(2)
hhl.defgate("CSdag", controlled(np.array([[1, 0], [0, -1j]])))
hhl += ("CSdag", 1, 2)
hhl += H(1)
hhl += SWAP(1, 2)
uncomputation = hhl.dagger()