HHL算法的QISKit实现

本实现基于IBM QISKit 0.7.0版本,python 3.7版本。

HHL Experiment (Quantum Algorithm for linear systems of equations)

This experiment is implemented based on IBM QISKit version 0.7.0. It is a quantum implementation of finding x ⃗ \vec{x} x satisfying A x ⃗ = b ⃗ A\vec{x}=\vec{b} Ax =b , and only for the case when A A A is a 2 by 2 matrx, while b ⃗ \vec{b} b is a 2 by 1 vector.

The whole process can be divided into several steps:

  1. Quantum phase estimation
  2. Controlled rotation
  3. Inverse quantum phase estimation

The quantum circuit for this experiment is (from reference3):

The first qubit ∣ x 1 ⟩ |x_1 \rangle x1 is the ancilla register for controlled rotation. The second and third qubit ∣ x 2 x 3 ⟩ |x_2x_3\rangle x2x3 (the register C) will save the superposition of the eigenvalues of A, after the quantum phase estimation. The forth qubit ∣ x 4 ⟩ |x_4 \rangle x4 is used to save ∣ b ⟩ |b \rangle b, and after the whole process of HHL, it will save the approximate value of x ⃗ \vec{x} x .

# Import packages
%matplotlib inline
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute
from qiskit import BasicAer
from math import pi
from qiskit.tools.visualization import plot_histogram

import warnings  # Ignore the warning message
warnings.filterwarnings('ignore')

The known A A A and b ⃗ \vec{b} b are set like this in this experiment:

A = 1 2 ( 3 1 1 3 ) A=\frac{1}{2}\begin{pmatrix} 3 & 1\\ 1 & 3 \end{pmatrix} A=21(3113) and b ⃗ = ( b 1 b 2 ) \vec{b}=\begin{pmatrix} b_1\\ b_2 \end{pmatrix} b =(b1b2).

b ⃗ \vec{b} b can be encoded in a quantum state that ∣ b ⟩ = b 1 ∣ 0 ⟩ + b 2 ∣ 1 ⟩ |b \rangle = b_1|0 \rangle+b_2|1 \rangle b=b10+b21, which fulfills b 1 2 + b 2 2 = 1 b_1^2+b_2^2=1 b12+b22=1.

And the eigenvalues of A are λ 1 = 1 \lambda_1=1 λ1=1 and λ 2 = 2 \lambda_2=2 λ2=2 with corresponding eigenvectors ∣ u 1 ⟩ |u_1 \rangle u1 and ∣ u 2 ⟩ |u_2 \rangle u2. λ 1 \lambda_1 λ1 and λ 2 \lambda_2 λ2 can be encoded by ∣ x 2 x 3 ⟩ = ∣ 01 ⟩ |x_2x_3 \rangle=|01 \rangle x2x3=01 and ∣ x 2 x 3 ⟩ = ∣ 10 ⟩ |x_2x_3 \rangle=|10 \rangle x2x3=10 respectively. (Actually all 2 by 2 matrics with eigenvalues 1 and 2 can be realized by this circuit.)

Step 1. Apply quantum phase estimation

This step is to determine the eigenvalues of A.

1.1 Initialization

Here we use ∣ b ⟩ = ∣ 1 ⟩ |b \rangle =|1 \rangle b=1 as an example, and the initial value for ∣ x 1 x 2 x 3 x 4 ⟩ |x_1x_2x_3x_4 \rangle x1x2x3x4 is ∣ 0001 ⟩ |0001 \rangle 0001.

1.2 Create superposition

Apply Hadamard gate to the ancilla register and the register C.

1.3 Apply controlled-U gate

The controlled-U gates in this process can be implemented by phase shift gates, with phases e x p ( i A t 0 / 4 ) exp(iAt_0/4) exp(iAt0/4) and e x p ( i A t 0 / 2 ) exp(iAt_0/2) exp(iAt0/2) (here we let t 0 = 2 π t_0=2\pi t0=2π):

The picture above is from https://www.youtube.com/watch?v=v1AUILJz3RU.

1.4 Inverse Fourier transform

Apply inverse Fourier transform to the register C.

## 1.1 Initialization
n = 4 # Set the number of qubits in the register
q = QuantumRegister(n, 'q') # Create a Quantum Register with n qubits.
register = QuantumCircuit(q) # Create a Quantum Circuit acting on the q register
register.x(q[3]) 

## 1.2 Create superposition
register.h(q[1]) 
register.h(q[2])

## 1.3 Apply controlled-U gate
register.u1(pi/2, q[2])
register.u1(pi, q[1])
register.cx(q[2], q[3]) #additional, still need to figure out why

## 1.4 Inverse quantum Fourier transform
register.swap(q[1], q[2])
register.h(q[2]) #iQFT
register.cu1(-pi/2, q[1], q[2])
register.h(q[1])

After the phase estimation the state of ∣ x 1 x 2 x 3 ⟩ |x_1x_2x_3 \rangle x1x2x3 becomes β 1 ∣ 01 ⟩ ∣ u 1 ⟩ + β 2 ∣ 10 ⟩ ∣ u 2 ⟩ \beta_1|01 \rangle|u_1 \rangle+\beta_2|10 \rangle|u_2 \rangle β101u1+β210u2, where β 1 \beta_1 β1 and β 2 \beta_2 β2 are expansion coefficients of ∣ b ⟩ |b \rangle b in A A A's eigenbasis.

Step 2. Controlled rotation

You can find more mathematical derivations about this step here.

2.1 Get λ 1 − 1 \lambda_1^{-1} λ11 and λ 2 − 1 \lambda_2^{-1} λ21 using a SWAP gate

After the SWAP gate, the state of ∣ x 1 x 2 x 3 ⟩ |x_1x_2x_3 \rangle x1x2x3 becomes β 1 ∣ 10 ⟩ ∣ u 1 ⟩ + β 2 ∣ 01 ⟩ ∣ u 2 ⟩ \beta_1|10 \rangle|u_1 \rangle+\beta_2|01 \rangle|u_2 \rangle β110u1+β201u2. We can now interpret ∣ x 2 x 3 ⟩ = ∣ 10 ⟩ |x_2x_3 \rangle=|10 \rangle x2x3=10 as the state encoding the inverted eigenvalue 2 λ 1 − 1 = 2 2\lambda_1^{-1}=2 2λ11=2 and ∣ x 2 x 3 ⟩ = ∣ 01 ⟩ |x_2x_3 \rangle=|01 \rangle x2x3=01 as that encoding 2 λ 2 − 1 = 1 2\lambda_2^{-1}=1 2λ21=1.

Here in our implementation, the matix A have eignvalues 1 and 2. I also give an example of getting λ 1 − 1 \lambda_1^{-1} λ11 and λ 2 − 1 \lambda_2^{-1} λ21 when the eigenvalues are 1 and 3. Thus we use A = ( 2 1 1 2 ) A=\begin{pmatrix} 2 & 1\\ 1 & 2 \end{pmatrix} A=(2112) as the input matrix instead. In this example, we need to use a X gate on ∣ x 2 ⟩ |x_2 \rangle x2 to instead the SWAP gate between ∣ x 2 ⟩ |x_2 \rangle x2 and ∣ x 3 ⟩ |x_3\rangle x3.

2.2 Apply rotation R y R_y Ry

This process is to save λ j − 1 \lambda_j^{-1} λj1 into the amplitudes of the ancilla register.

R y ( θ ) = ( c o s ( θ 2 ) − s i n ( θ 2 ) s i n ( θ 2 ) c o s ( θ 2 ) ) R_y(\theta)=\begin{pmatrix} cos(\frac{\theta}{2}) & -sin(\frac{\theta}{2})\\ sin(\frac{\theta}{2})& cos(\frac{\theta}{2}) \end{pmatrix} Ry(θ)=(cos(2θ)sin(2θ)sin(2θ)cos(2θ)), and the r r r in the R y R_y Ry gate is a parameter that ranges between l o g 2 2 π log_22\pi log22π and l o g 2 2 π / ω log_22\pi/\omega log22π/ω with ω \omega ω being the minimum angle that can be resolved. Here we choose r=4, because when x=4, the system has a good performance on both the fidelity and the probability, as is shown in the picture below (from reference 3):

A more detailed explanation for r r r can be found in reference 3.

## 2.1 SWAP
# register.swap(q[1], q[2])  # for eigenvalues 1 and 2
register.x(q[1])  # for eigen values 1 and 3

## 2.2 rotation R_y
register.cu3(pi/8, 0, 0, q[1], q[0])
register.cu3(pi/16, 0, 0, q[2], q[0]) 

Step 3. Uncompute

This process is to do the inverse of all operations before R y R_y Ry, which is denoted by U † U^\dagger U in the circuit.

# register.swap(q[1], q[2])  # for eigenvalues 1 and 2
register.x(q[1])  # for eigenvalues 1 and 3
register.h(q[1]) #iQFT
register.cu1(pi/2, q[1], q[2])
register.h(q[2])
register.swap(q[1], q[2])

register.cx(q[2], q[3]) #additional, still need to figure out why
register.u1(-pi, q[1])
register.u1(-pi/2, q[2])

register.h(q[2]) 
register.h(q[1]) 

Step 4. Measurement

4.1 Quantum circuit

# Create a Classical Register with n bits.
c = ClassicalRegister(n, '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")

4.2 Run on local simulator

The final state for the ∣ x 1 x 2 x 3 x 4 ⟩ |x_1x_2x_3x_4 \rangle x1x2x3x4 should be a ∣ 0000 ⟩ + b ∣ 0001 ⟩ + c ∣ 1000 ⟩ + d ∣ 1001 ⟩ a|0000\rangle+b|0001\rangle+c|1000\rangle+d|1001\rangle a0000+b0001+c1000+d1001 (The ∣ x 2 x 3 ⟩ |x_2x_3 \rangle x2x3 is always ∣ 00 ⟩ |00\rangle 00). Because we can only get the ∣ x ⟩ |x \rangle x when the ancilla qubit, ∣ x 1 ⟩ = ∣ 1 ⟩ |x_1 \rangle=|1\rangle x1=1, so we only care about the last two possible states of ∣ x 1 x 2 x 3 x 4 ⟩ |x_1x_2x_3x_4 \rangle x1x2x3x4.

Note that the rightmost qubit in the result is q0 ( ∣ x 1 ⟩ |x_1 \rangle x1), the leftmost is q3 ( ∣ x 4 ⟩ |x_4 \rangle x4). Thus in this implementation the two states that we need to pay attention to is ‘0001’ and ‘1001’. The ratio of these two probabilities, which is ∣ c / d ∣ 2 |c/d|^2 c/d2, is close to ∣ x 1 / x 2 ∣ 2 |x_1/x_2|^2 x1/x22 ( x ⃗ = ( x 1    x 2 ) T \vec{x} = (x_1\; x_2)^T x =(x1x2)T).

# 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=8192)

# Grab the results from the job.
result_sim = job_sim.result()

counts = result_sim.get_counts(qc)
print(counts)
x1_square = counts['0001']
x2_square = counts['1001']
p = x1_square/x2_square
print('probability of state 0001:', x1_square)
print('probability of state 1001:', x2_square)
print('the ratio of two probabilities', p)
plot_histogram(result_sim.get_counts())
{'0000': 3, '0001': 85, '1000': 7809, '1001': 295}
probability of state 0001: 85
probability of state 1001: 295
the ratio of two probabilities 0.288135593220339

Reference:

  1. Aram W Harrow, Avinatan Hassidim, and Seth Lloyd. Quantum algorithm for linear systems ofequations. Physical review letters, 103(15):150502, 2009.
  2. Yudong Cao, Anmer Daskin, Steven Frankel, and Sabre Kais. Quantum circuit design forsolving linear systems of equations.Molecular Physics, 110(15-16):1675–1680, 2012.
  3. Yudong Cao, Anmer Daskin, Steven Frankel, and Sabre Kais. Quantum circuits for solvinglinear systems of equations.arXiv preprint arXiv:1110.2232v3, 2013.
  4. Jian Pan, Yudong Cao, Xiwei Yao, Zhaokai Li, Chenyong Ju, Hongwei Chen, Xinhua Peng,Sabre Kais, and Jiangfeng Du. Experimental realization of quantum algorithm for solving linearsystems of equations.Physical Review A, 89(2):022313, 2014.
  5. X-D Cai, Christian Weedbrook, Z-E Su, M-C Chen, Mile Gu, M-J Zhu, Li Li, Nai-Le Liu,Chao-Yang Lu, and Jian-Wei Pan. Experimental quantum computing to solve systems of linearequations.Physical review letters, 110(23):230501, 2013.
  6. The implementation of HHL on QISKit

你可能感兴趣的:(量子机器学习)