量子相位估计算法QISKit实现

量子相位估计算法是HHL算法(解决Ax=b)的重要一环,被广泛应用于各种量子机器学习过程中。本实现基于IBM QISKit 0.7.0版本,python 3.7版本。这篇文章包含了代码在jupyter notebook上运行的结果,但其中几个概率分布和量子电路的图片无法显示,可以通过github链接查阅: https://github.com/yangjy0826/IBM-QISKit/blob/master/qiskit_QPE.ipynb

Quantum Phase Estimation (QPE)

This QPE algorithm is implemented based on IBM QISKit version 0.7.0.

The whole process can be divided into several steps:

  1. Create superposition on the first register
  2. Apply Controlled-U gate
  3. inverse Fouirer transform
  4. Measurement

Other reading materials for Quantum Phase Estimation:

  1. Quantum Computation and Quantum Information, Neilson, from Page 221
  2. Quantum Machine Learning for Data Scientists, form Page 27
  3. UC Berkeley’s course Qubits, Quantum Mechanics, and Computers, lecture note 20.
%matplotlib inline
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute
from qiskit import BasicAer
import math

The quantum circuit for this algorithm is:
circuit
And we shouldfirst repare the quantum registers:

# Set the number of qubits in the register
n = 4  # 1st register
m = 1  # 2nd register

# Create a Quantum Register with n qubits.
q = QuantumRegister(n+m, 'q')

# Create a Quantum Circuit acting on the q register
register = QuantumCircuit(q)

Step 1. Create superposition on the first register

Step 2. Apply Controlled-U gate

# register.x(q[n])
for j in range(n-1,-1,-1):
    # Step 1. Add H gates on all qubits in register1, putting these qubits in superposition:
    register.h(q[j]) 
    # Step 2. Contolled unitary gate C-U applies the unitary operator U on the 2nd qubit if the 1st qubit is 1:
    register.u1(math.pi/float(2**(j)), q[j])
    # register.cx(q[n], q[j])
'''
register.x(q[n])
for j in range(n-1,-1,-1):
    register.h(q[j]) 
    register.u1(math.pi/float(2**(j+1)), q[j])
    register.cx(q[n], q[j])
    register.u1(-math.pi/float(2**(j+1)), q[j])
    register.cx(q[n], q[j])
    register.u1(math.pi/float(2**(j+1)), q[n])
register.draw(output="mpl")
'''
'\nregister.x(q[n])\nfor j in range(n-1,-1,-1):\n    register.h(q[j]) \n    register.u1(math.pi/float(2**(j+1)), q[j])\n    register.cx(q[n], q[j])\n    register.u1(-math.pi/float(2**(j+1)), q[j])\n    register.cx(q[n], q[j])\n    register.u1(math.pi/float(2**(j+1)), q[n])\nregister.draw(output="mpl")\n'

Step 3. Inverse Fourier transform

for j in range(n):
    for k in range(j):
        register.cu1(-math.pi/float(2**(j-k)), q[j], q[k]) 
    register.h(q[j])
def swap(qc, q, i, j):
    qc.cx(q[i], q[j])
    qc.cx(q[j], q[i])
    qc.cx(q[i], q[j])
#swap(register, q, 0, 3)
#swap(register, q, 1, 2)

if n%2==1:
    for i in range(int((n-1)/2)):
        swap(register, q, i, n-i-1)
else:
    for i in range(int(n/2)):
        swap(register, q, i, n-i-1)
register.draw(output="mpl")

png

Step 4. Measurement

# Create a Classical Register with n bits.
c = ClassicalRegister(n+m, 'c')
# Create a Quantum Circuit
measure = QuantumCircuit(q, c)
measure.barrier(q)
# map the quantum measurement to the classical bits
measure.measure(q,c)

# The Qiskit circuit object supports composition using
# the addition operator.
qc = register+measure

#drawing the circuit
qc.draw(output="mpl")

png

# Use Aer's qasm_simulator
backend_sim = BasicAer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.
# We've set the number of repeats of the circuit to be 1024, which is the default.
job_sim = execute(qc, backend_sim, shots=1024)

# Grab the results from the job.
result_sim = job_sim.result()
counts = result_sim.get_counts(qc)
print(counts)
{'01000': 1024}

The result bits are shown in the order qn...q2q1, so we need to change it into the order q1q2...qn, and then divided by 2 n 2^n to get the balue of phase.
e.g. If our result is 01000(q4q3q2q1q0), we should first make it in to the correct order 00010(q0q1q2q3q4). We don’t care about the 2nd register, so the result becomes 0001. After divided by 2 4 2^4 , we get the answer (in decimal) 1/16, which is the phase.

from qiskit.tools.visualization import plot_histogram
plot_histogram(result_sim.get_counts(qc))

png

猜你喜欢

转载自blog.csdn.net/m0_37622530/article/details/87882585
今日推荐