目前的情况,除非是在相关单位工作,否则一般的开发人员尚无法亲身体验量子计算机。更谈不上学习量子计算机的开发了。
但是通过上面的介绍你可以发现,除非只做一个拿来主义的使用者,否则这些颠覆性的特征将带来算法上的重大改变,不及早的研究、积累,这真的可能成为程序员的一个“灰犀牛”。
除了在实际的量子计算机上实验,目前也有很多软件提供了量子计算的模拟能力,从而可以尝试自己的算法和实验,达到学习的目的。
微软甚至创立了一种新的语言叫“Q#”来应对将来的量子计算,相信应当也不错,不过最近对于非开源的项目还是有些障碍,所以我们来尝试另外一个工具库DLIB。
首先要假设你已经有了基本的开发环境,比如g++/clang。Ubuntu要保证APT包管理的正常使用,macOS则预先安装好Homebrew包管理。
在Ubuntu16上:
apt install libdlib-dev
在macOS:
brew install dlib
DEMO源码本源码来自DLIB的Examples代码quantum_computing_ex.cpp,未做任何修改。
代码中使用上面介绍过的这几个逻辑门,实现了两个最常用的基本算法:Grover Search和Shor ECC校验。这两个算法也是相当有名,网上一搜资料大把,我这半瓶水就不画蛇添足了。
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/* This is an example illustrating the use of the quantum computing simulation classes from the dlib C++ Library. This example assumes you are familiar with quantum computing and Grover's search algorithm and Shor's 9 bit error correcting code in particular. The example shows how to simulate both of these algorithms. The code to simulate Grover's algorithm is primarily here to show you how to make custom quantum gate objects. The Shor ECC example is simpler and uses just the default gates that come with the library.*/
#include <iostream>
#include <complex>
#include <ctime>
#include <dlib/quantum_computing.h>
#include <dlib/string.h>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// This declares a random number generator that we will be using below
dlib::rand rnd;
// ----------------------------------------------------------------------------------------
void shor_encode (
quantum_register& reg
)
/*!
requires
- reg.num_bits() == 1
ensures
- #reg.num_bits() == 9
- #reg == the Shor error coding of the input register
!*/
{
DLIB_CASSERT(reg.num_bits() == 1,"");
quantum_register zeros;
zeros.set_num_bits(8);
reg.append(zeros);
using namespace dlib::quantum_gates;
const gate<1> h = hadamard();
const gate<1> i = noop();
// Note that the expression (h,i) represents the tensor product of the 1 qubit
// h gate with the 1 qubit i gate and larger versions of this expression
// represent even bigger tensor products. So as you see below, we make gates
// big enough to apply to our quantum register by listing out all the gates we
// want to go into the tensor product and then we just apply the resulting gate
// to the quantum register.
// Now apply the gates that constitute Shor's encoding to the input register.
(cnot<3,0>(),i,i,i,i,i).apply_gate_to(reg);
(cnot<6,0>(),i,i).apply_gate_to(reg);
(h,i,i,h,i,i,h,i,i).apply_gate_to(reg);
(cnot<1,0>(),i,cnot<1,0>(),i,cnot<1,0>(),i).apply_gate_to(reg);
(cnot<2,0>(),cnot<2,0>(),cnot<2,0>()).apply_gate_to(reg);
}
// ----------------------------------------------------------------------------------------