当前位置:网站首页>Q_04_05 使用Qubits
Q_04_05 使用Qubits
2022-08-09 13:02:00 【MOVCat】
使用Qubits
现在已经看到了Q#语言的各种不同部分,让我们深入了解它,并了解如何使用量子比特本身。
分配Qubits
首先,为了获得我们可以在Q#中使用的量子比特,我们在一个using
块中分配量子比特:
using (register = Qubit[5]) {
// Do stuff...
}
以这种方式分配的任何量子位都以 状态开始;在上面的例子中, register
因此处于状态
。在using
块的末尾,由该块分配的任何量子位将立即解除分配,并且不能继续使用。
警告
目标机器希望在取消分配之前,量子位立即处于 状态,以便它们可以被重用并提供给其他using
块进行分配。尽可能使用单一操作将任何已分配的量子位返回到 。如果需要, 重置操作可用于测量量子位,并使用该测量结果确保量子位返回到
。这样的测量将破坏剩余量子的任何纠缠。
原始门
一旦分配,量子位就可以传递给功能和操作。从某种意义上说,Q#程序可以用qubit来做这些,因为可以采取的行动都被定义为操作。我们将在Primitive Operations和Functions中更详细地看到这些操作,但现在我们提到一些可用于与量子比特交互的有用的基本操作。
首先,单Q位Pauli运算符X,Y和Z在Q#中由原始操作X
, Y
和Z
,每个(Qubit => () : Adjoint, Controlled)
类型为(Qubit => () : Adjoint, Controlled)
。正如原始操作和函数中所描述的那样,我们可以将X看作是一个位翻转操作或NOT门。这让我们可以为 形成一些经典位串s:
operation PrepareBitString(bitstring : Bool[], register : Qubit[]) : () {
body {
let nQubits = Length(register);
for (idxQubit in 0..nQubits - 1) {
if (bitstring[idxQubit]) {
X(register[idxQubit]);
}
}
}
adjoint auto
controlled auto
controlled adjoint auto
}
operation Example() : () {
body {
using (register = Qubit[8]) {
PrepareBitString(
[true; true; false; false; true; false; false; true],
register
);
// 此时,register现在具有状态 |11001001〉.
}
}
}
Tip
稍后,我们将看到更简洁的书写这种操作的方式,不需要手动流量控制。
我们还可以准备诸如 来表示的Hadamard变换H
H : (Qubit => () : Adjoint, Controlled)
:
operation PreparePlusMinusState(bitstring : Bool[], register : Qubit[]) : () {
body {
// First, get a computational basis state of the form
// |s_0 s_1 ... s_n〉 by using PrepareBitString, above.
PrepareBitString(bitstring, register);
// Next, we use that |+〉 = H|0〉 and |-〉 = H|1〉 to
// prepare the state we want.
for (idxQubit in 0..Length(register) - 1) {
H(register[idxQubit]);
}
}
}
测量
使用Measure
操作,这是一个内置的原始门,我们可以从Qubit
类型的对象中提取经典信息,并将经典值作为结果赋值,其Result
保留为Result
,表示结果不再是量子态。Measure
的输入是Bloch球体上的Pauli轴,由Pauli
类型的对象(例如PauliX
)和类型为Qubit
的对象Qubit
。
一个简单的例子是下面的操作,它在 ket0
状态下创建一个量子位,然后对它应用一个Hadamard门H
,然后在PauliZ
基础上测量结果。
operation MeasurementOneQubit () : Result {
body {
mutable result = Zero;
// The following using block creates a fresh qubit and initializes it
// in the |0〉 state.
using (qubits = Qubit[1]) {
let qubit = qubits[0];
// We apply a Hadamard operation H to the state, thereby creating the
// state 1/sqrt(2)(|0〉+|1〉).
H(qubit);
// Now we measure the qubit in Z-basis.
set result = M(qubit);
// As the qubit is now in an eigenstate of the measurement operator,
// we reset the qubit before releasing it.
if (result == One) {
X(qubit);
}
}
// Finally, we return the result of the measurement.
return result;
}
}
下面的操作给出了一个稍微复杂的例子,当以指定的泡Qubit[]
测量时,返回类型为Qubit[]
的寄存器中所有量子位的布尔值为true
状态为零,否则为false
。
operation AllMeasurementsZero (qs : Qubit[], pauli : Pauli) : Bool {
body {
mutable value = true;
for (i in 0..Length(qs)-1) {
if ( Measure([pauli], [qs[i]]) == One ) {
set value = false;
}
}
return value;
}
}
Q#语言允许经典控制流对量子位测量结果的依赖性。这反过来又能够实现强大的概率小工具,可以降低实施单元的计算成本。举例来说,在Q#中实现所谓的Repeat-Until-Success很容易,这些概率电路在基本门电路方面具有预期的低成本,但真正的成本取决于实际运行和实际交错各种可能的分支。
为了促进Repeat-Until-Success(RUS)模式,Q#支持该构造
repeat {
statement1
}
until (expression)
fixup {
statement2
}
其中statement1
和statement2
可以是任何有效的Q#语句,并expression
任何有效的表达式,其计算结果为Bool
类型的值。在一个典型的用例中,下面的电路在Bloch球体上围绕
无理轴进行旋转。这是通过使用已知的RUS模式完成的:
operation RUScircuit (qubit : Qubit) : () {
body {
using(ancillas = Qubit[2]) {
ApplyToEachA(H, ancillas);
mutable finished = false;
repeat {
(Controlled X)(ancillas, qubit);
S(qubit);
(Controlled X)(ancillas, qubit);
Z(qubit);
}
until(finished)
fixup {
if AllMeasurementsZero(ancillas, Xpauli) {
set finished = true;
}
}
}
}
}
此示例显示了使用finished
的可变变量,该变量位于整个repeat-until-fixup循环的范围内,并在循环之前初始化并在修正步骤中进行更新。
最后,我们给出一个RUS模式的例子来准备量子态 ,从 状态开始。另请参阅canon提供的单元测试示例 :
operation RepeatUntilSuccessStatePreparation( target : Qubit ) : () {
body {
using( qubits = Qubit[1] ) {
let ancilla = qubits[0];
H(ancilla);
repeat {
// We expect target and ancilla qubit to be in |+* state.
AssertProb(
[PauliX], [target], Zero, 1.0,
"target qubit should be in the |+* state", 1e-10 );
AssertProb(
[PauliX], [ancilla], Zero, 1.0,
"ancilla qubit should be in the |+* state", 1e-10 );
(Adjoint T)(ancilla);
CNOT(target,ancilla);
T(ancilla);
// The probability of measuring |+* state on ancilla is 3/4.
AssertProb(
[PauliX], [ancilla], Zero, ToDouble(3) / ToDouble(4),
"Error: the probability to measure |+* in the first
ancilla must be 3/4",
1e-10);
// If we get measurement outcome Zero, we prepare the required state
let outcome = Measure([PauliX], [ancilla]);
}
until( outcome == Zero )
fixup {
// Bring ancilla and target back to |+* state
if( outcome == One ) {
Z(ancilla);
X(target);
H(target);
}
}
// Return ancilla back to Zero state
H(ancilla);
}
}
}
此操作中显示的显着编程特性是涉及量子操作的循环的更复杂的fixup
部分,以及使用AssertProb
语句来确定在程序中某些指定点处测量量子态的概率。另请参阅测试和调试以获取有关Assert
和AssertProb
语句的更多信息。
边栏推荐
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- [FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
- Standing wave ratio calculation method
- 5G 联通网管设计思路
- Data Mining-06
- Time series analysis course lab report
- Process/Thread related in Sandbox - 1
- 搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
- 剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
- FFmpeg多媒体文件处理(FFMPEG日志系统)
猜你喜欢
5G China unicom repeater network management protocol real-time requirements
微服务+微信小程序实现社区服务
面试攻略系列(四)-- 你不知道的大厂面试
WSA工具箱安装应用商店提示无法工作怎么解决?
第六届“强网杯”全国网络安全挑战赛
Explanation of RTSP protocol
5G China unicom AP:B SMS ASCII 转码要求
为什么文字不贴合边
FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
随机推荐
How to reduce the size of desktop icons after the computer is reinstalled
gin的中间件和路由分组
JS轮播图实现
5G China unicom repeater network management protocol real-time requirements
GET POST PUT DELETE request in GIN
Rmarkdown Tutorial
Standing wave ratio calculation method
[极客大挑战 2019]Upload
GIN文件上传与返回
Realization of RTSP Protocol
电脑重装系统还原0x80070005错误如何解决
jenkins api创建自定义pipeline
kustomize entry example and basic syntax instructions
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
Unity3d_API_Gyroscope 陀螺仪的接口
LeetCode 37.解数独
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
glibc memory management model freeing C library memory cache
第六届“强网杯”全国网络安全挑战赛