本实现基于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 Ax⃗=b⃗A\vec{x}=\vec{b}Ax=b, and only for the case when AAA 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 ∣x1⟩|x_1 \rangle∣x1​⟩ is the ancilla register for controlled rotation. The second and third qubit ∣x2x3⟩|x_2x_3\rangle∣x2​x3​⟩ (the register C) will save the superposition of the eigenvalues of A, after the quantum phase estimation. The forth qubit ∣x4⟩|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_histogramimport warnings  # Ignore the warning message
warnings.filterwarnings('ignore')

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

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

b⃗\vec{b}b can be encoded in a quantum state that ∣b⟩=b1∣0⟩+b2∣1⟩|b \rangle = b_1|0 \rangle+b_2|1 \rangle∣b⟩=b1​∣0⟩+b2​∣1⟩, which fulfills b12+b22=1b_1^2+b_2^2=1b12​+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 ∣u1⟩|u_1 \rangle∣u1​⟩ and ∣u2⟩|u_2 \rangle∣u2​⟩. λ1\lambda_1λ1​ and λ2\lambda_2λ2​ can be encoded by ∣x2x3⟩=∣01⟩|x_2x_3 \rangle=|01 \rangle∣x2​x3​⟩=∣01⟩ and ∣x2x3⟩=∣10⟩|x_2x_3 \rangle=|10 \rangle∣x2​x3​⟩=∣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 ∣x1x2x3x4⟩|x_1x_2x_3x_4 \rangle∣x1​x2​x3​x4​⟩ 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 exp(iAt0/4)exp(iAt_0/4)exp(iAt0​/4) and exp(iAt0/2)exp(iAt_0/2)exp(iAt0​/2) (here we let t0=2πt_0=2\pit0​=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])
<qiskit.extensions.standard.h.HGate at 0x222397f0400>

After the phase estimation the state of ∣x1x2x3⟩|x_1x_2x_3 \rangle∣x1​x2​x3​⟩ becomes β1∣01⟩∣u1⟩+β2∣10⟩∣u2⟩\beta_1|01 \rangle|u_1 \rangle+\beta_2|10 \rangle|u_2 \rangleβ1​∣01⟩∣u1​⟩+β2​∣10⟩∣u2​⟩, where β1\beta_1β1​ and β2\beta_2β2​ are expansion coefficients of ∣b⟩|b \rangle∣b⟩ in AAA's eigenbasis.

Step 2. Controlled rotation

You can find more mathematical derivations about this step here.

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

After the SWAP gate, the state of ∣x1x2x3⟩|x_1x_2x_3 \rangle∣x1​x2​x3​⟩ becomes β1∣10⟩∣u1⟩+β2∣01⟩∣u2⟩\beta_1|10 \rangle|u_1 \rangle+\beta_2|01 \rangle|u_2 \rangleβ1​∣10⟩∣u1​⟩+β2​∣01⟩∣u2​⟩. We can now interpret ∣x2x3⟩=∣10⟩|x_2x_3 \rangle=|10 \rangle∣x2​x3​⟩=∣10⟩ as the state encoding the inverted eigenvalue 2λ1−1=22\lambda_1^{-1}=22λ1−1​=2 and ∣x2x3⟩=∣01⟩|x_2x_3 \rangle=|01 \rangle∣x2​x3​⟩=∣01⟩ as that encoding 2λ2−1=12\lambda_2^{-1}=12λ2−1​=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}λ1−1​ and λ2−1\lambda_2^{-1}λ2−1​ when the eigenvalues are 1 and 3. Thus we use A=(2112)A=\begin{pmatrix} 2 &amp; 1\\ 1 &amp; 2 \end{pmatrix}A=(21​12​) as the input matrix instead. In this example, we need to use a X gate on ∣x2⟩|x_2 \rangle∣x2​⟩ to instead the SWAP gate between ∣x2⟩|x_2 \rangle∣x2​⟩ and ∣x3⟩|x_3\rangle∣x3​⟩.

2.2 Apply rotation RyR_yRy​

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

Ry(θ)=(cos(θ2)−sin(θ2)sin(θ2)cos(θ2))R_y(\theta)=\begin{pmatrix} cos(\frac{\theta}{2}) &amp; -sin(\frac{\theta}{2})\\ sin(\frac{\theta}{2})&amp; cos(\frac{\theta}{2}) \end{pmatrix}Ry​(θ)=(cos(2θ​)sin(2θ​)​−sin(2θ​)cos(2θ​)​), and the rrr in the RyR_yRy​ gate is a parameter that ranges between log22πlog_22\pilog2​2π and log22π/ωlog_22\pi/\omegalog2​2π/ω 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 rrr 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])
<qiskit.extensions.standard.cu3.Cu3Gate at 0x22239f26278>

Step 3. Uncompute

This process is to do the inverse of all operations before RyR_yRy​, which is denoted by U†U^\daggerU† 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])
<qiskit.extensions.standard.h.HGate at 0x222398e60b8>

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 ∣x1x2x3x4⟩|x_1x_2x_3x_4 \rangle∣x1​x2​x3​x4​⟩ should be a∣0000⟩+b∣0001⟩+c∣1000⟩+d∣1001⟩a|0000\rangle+b|0001\rangle+c|1000\rangle+d|1001\ranglea∣0000⟩+b∣0001⟩+c∣1000⟩+d∣1001⟩ (The ∣x2x3⟩|x_2x_3 \rangle∣x2​x3​⟩ is always ∣00⟩|00\rangle∣00⟩). Because we can only get the ∣x⟩|x \rangle∣x⟩ when the ancilla qubit, ∣x1⟩=∣1⟩|x_1 \rangle=|1\rangle∣x1​⟩=∣1⟩, so we only care about the last two possible states of ∣x1x2x3x4⟩|x_1x_2x_3x_4 \rangle∣x1​x2​x3​x4​⟩.

Note that the rightmost qubit in the result is q0 (∣x1⟩|x_1 \rangle∣x1​⟩), the leftmost is q3 (∣x4⟩|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/d∣2, is close to ∣x1/x2∣2|x_1/x_2|^2∣x1​/x2​∣2 (x⃗=(x1&ThickSpace;x2)T\vec{x} = (x_1\; x_2)^Tx=(x1​x2​)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

HHL算法的QISKit实现相关推荐

  1. 30-80k/月!影创科技算法岗招聘,含实习生

    影创科技集团招人啦!!! 1 第一步 了解我们 影创科技集团成立于2014年8月,是国内第一.世界一流的混合现实行业领军企业,主要业务为混合现实.人工智能.半导体三大领域的基础科学研究与应用技术及产品 ...

  2. 线性代数应用于计算机科学例子,为什么计算机科学家们应该了解量子计算?(三):算法棱镜折射出的科学...

    译者按: 本文译自 Aram Harrow 的 Why now is the right time to study quantum computing(原文链接), 经 Aram 授权翻译. Ara ...

  3. 通识:量子算法发展的四大阶段

    尽管对于绝大多数人来说,"量子计算"是一个属于科幻世界的遥远词汇,但实际上它已经走过了近四十年的历史.在这四十年里,量子计算及其算法从无到有,从理论概念逐渐变成了现实.我们从量子算 ...

  4. Grover算法试验

    <量子算法与编程入门>4.3 Grover算法 量子搜索算法如何工作 https://quantum.country/search 01 grover算法在Qiskit上试验 grover ...

  5. 量子神经网络:人工智能研究的新范式

    https://www.toutiao.com/a6702017695618957832/ 1 引言 近年来,机器学习技术快速崛起,已经成为大数据时代下的技术基石.机器学习根据已有数据进行学习策略的探 ...

  6. 量子计算技术发展迅猛,商业潜力初现!如何把握量子计算时代的新机遇?

    来源:蓝驰创投 编译:全球君 摘要:通用量子计算机一旦实现,将对通信安全.导航.成像以及人工智能.生物制药.新材料研发等诸多领域产生颠覆性影响,带来国家安全和社会经济发展的极大变革. 通用量子计算机一 ...

  7. 价值2950亿美元的「量子霸权」,技术水平到了哪个阶段

    来源:36Kr 摘要:谁掌握了量子计算机,谁就可能引领下一次信息革命. 当前经典计算体系,并不能解决所有问题.量子计算将给现有的计算理论带来深刻变革,将极大加深人类对物质与信息的理解:将是一种前所未有 ...

  8. 量子计算机就要来了,它真的能改变世界吗?

    来源:网易科技 概要:量子计算机的理论运行速度远远超出任何传统的超级计算机. 在位于纽约市以北约50英里处僻静乡村中的一个小型实验室内,天花板下缠绕着错综复杂的管线和电子设备.这一堆看似杂乱无章的设备 ...

  9. 《基于张量网络的机器学习入门》学习笔记7

    <基于张量网络的机器学习入门>学习笔记7 量子算法 什么是量子算法 三个经典量子算法 Grover算法 背景 基本原理 例题 量子算法 什么是量子算法 例如我们求解一个问题,一个111千克 ...

  10. 带你认识MindSpore量子机器学习库MindQuantum

    摘要:MindSpore在3.28日正式开源了量子机器学习库MindQuantum,本文介绍MindQuantum的关键技术. 本文分享自华为云社区<MindSpore量子机器学习库MindQu ...

最新文章

  1. 【Java】 5.6 类的继承
  2. 2019年最佳Python学习路线
  3. mysql 两张大表关联_详解mysql生产环境如何快速有效的删除大表,附实验说明
  4. Android API之android.view.View.MeasureSpec
  5. 基于广度优先搜索的哈密顿图的判断算法
  6. 前端模板template
  7. python画三色柱状图_python画柱状图--不同颜色并显示数值的方法
  8. 苹果电脑计算机找不到打印机,macbook air电脑关于添加打印机的问题
  9. 【装机】将mbr硬盘转换为gpt
  10. OpenCV 调用手机摄像头
  11. python中scale啥意思_scale什么意思
  12. 【Matlab符号积分和符号微分的实现方法】积分微分
  13. 不联网安装 SQL server 2012 的问题
  14. matplotlib设置颜色、标记、线条,让你的图像更加丰富
  15. 备忘录吕吕没有备忘录十新建_前往地图备忘单
  16. Eigen中四元数Quaterniond的初始
  17. Leetcode刷题——剑指offer_1
  18. android studio 出现: Design editor is unavailable until a successful build 问题
  19. java从项目里下载excel模板
  20. java 验证码_java实现简单的验证码功能

热门文章

  1. matlab和cuda版本对应适配关系
  2. 《windows 程序设计》读书笔记 三
  3. 第一行代码读书笔记___3章
  4. 你知道百度的全景街景地图是怎么做的吗?
  5. 计算机用户文件夹加密,如何加密文件夹?手把手教你给文件夹加密方法
  6. keep跑步记录伪造_Keep、悦跑圈、咕咚、悦动圈,四个跑步App比对
  7. 大学如何自学嵌入式开发?
  8. Cortex m0+笔记
  9. 05Echarts - 折线图(Smoothed Line Chart)
  10. 淘客返利机器人,淘宝,拼多多,京东三合一查券返利机器人搭建教程