本文共 17373 字,大约阅读时间需要 57 分钟。
q#量子编程语言
In this article, I’ll introduce you to Q# — the new programming language from Microsoft for quantum computing. We will cover Q# data types, expressions, and statements with the help of code snippets.
在本文中,我将向您介绍Q#,这是Microsoft用于量子计算的新编程语言。 我们将在代码片段的帮助下介绍Q#数据类型,表达式和语句。
For an overview of quantum computing, please visit my earlier article: . There, I also describe how to install Quantum Development Kit (QDK) in Visual Studio 2017.
有关量子计算的概述,请访问我之前的文章: 。 在那里,我还将介绍如何在Visual Studio 2017中安装Quantum Development Kit(QDK)。
According to Microsoft:
根据微软的说法:
Q# is a scalable, multi-paradigm, domain-specific programming language for quantum computing.
Q#是用于量子计算的可伸缩,多范式,特定领域的编程语言。
So, what do these terms actually mean? Let us dive into the details.
那么,这些术语实际上意味着什么? 让我们深入探讨细节。
Scalable
可扩展
Scalable Q# allows us to write code that can be executed on machines of varying computing abilities. We can use it to simulate a few Qubits on our local machine, or even thousands of Qubits for an enterprise level application.
可扩展 Q#允许我们编写可以在各种计算能力的机器上执行的代码。 我们可以使用它来模拟本地计算机上的几个Qubit,或者用于企业级应用程序的数千个Qubit。
Multi-paradigm
多范式
Multi-paradigm Q# is a multi-paradigm programming language. It supports both functional and imperative programming styles. If you are new to programming paradigms, I suggest you refer .
多范式 Q#是一种多范式编程语言。 它支持功能性和命令式编程风格。 如果您不熟悉编程范例,建议您参考。
Domain-specific
特定领域
Domain-specific Q# is a programming language for quantum computing. It is to be used for writing algorithms and code snippets that are executed on quantum processors.
特定领域 Q#是用于量子计算的编程语言。 它用于编写在量子处理器上执行的算法和代码段。
This article will assume you have already installed QDK for Visual Studio 2017. If not, then you can for instructions.
本文将假定您已经安装了适用于Visual Studio 2017的QDK。如果未安装,则可以以获取说明。
After you have successfully installed QDK, we need to verify if Visual Studio 2017 has all the required dependencies installed for Q# development. For this, we will clone and execute the quantum sample programs from GitHub provided by Microsoft.
成功安装QDK后,我们需要验证Visual Studio 2017是否已安装Q#开发所需的所有依赖项。 为此,我们将从Microsoft提供的GitHub中克隆并执行量子示例程序。
Open VS 2017 and navigate to Team >> Manage Connections.
打开VS 2017并导航至团队>>管理连接。
Select Clone under Local Git Repositories and enter the URL: and click “Clone”.
在“本地Git存储库”下选择“克隆”,然后输入URL: : 并单击“克隆”。
The repository will be cloned on your local computer and Visual Studio will switch to the Solution Explorer. It will display all the cloned libraries and samples.
该存储库将被克隆到您的本地计算机上,Visual Studio将切换到解决方案资源管理器。 它将显示所有克隆的库和样本。
Now, open QsharpLibraries.sln solution.
现在,打开QsharpLibraries.sln解决方案。
If you are prompted with the “Install Missing Features” popup box, click “Install” to allow the installation of the necessary features. This will download and install F# and other tools used by some of the samples. Make sure that you are connected to the internet.
如果系统提示您“安装缺少的功能”弹出框,请单击“安装”以允许安装必要的功能。 这将下载并安装某些示例使用的F#和其他工具。 确保您已连接到互联网。
To execute a sample program, right-click on the TeleportationSample project in “Samples > 0.Introduction folder” of QsharpLibraries solution, and then click on “Set as Startup Project” and press F5.
要执行示例程序,请右键单击QsharpLibrari es解决方案的“示例> 0.Introduction文件夹” 中的TeleportationSample项目,然后单击“设置为启动项目”并按F5。
If you can see an output screen similar to the one shown below, then congratulations, your VS 2017 is ready for Q# development.
如果您看到的输出屏幕类似于下图所示,则表示恭喜,您的VS 2017已准备好进行Q#开发。
Note that your output screen may vary because the data being teleported is random. But it should send 8 rounds of data, with all being successfully teleported.
请注意,由于要传送的数据是随机的,因此输出屏幕可能会有所不同。 但是它应该发送8轮数据,所有数据都已成功传送。
Let us understand what are the various type models provided by Q#:
让我们了解一下Q#提供的各种类型模型:
Int: — It represents the 64 -bit signed integer. Notice the upper case ‘I’. This is in contrast to int in C# with lower case ‘i’.
Int:—表示64位有符号整数。 注意大写字母“ I”。 这与C#中的int小写字母“ i”相反。
Double: — It represents double-precision floating point number. This also has a upper case ‘D’ in contrast to double in C#.
Double:—表示双精度浮点数。 与C#中的double相比,它也具有大写字母“ D”。
Bool: — It represents the Boolean type and can take two values — true or false.
Bool:—表示布尔类型,可以采用两个值-true或false。
Qubit: — This represents the Quantum bit. Qubit is the fundamental unit of processing information in quantum computers, similar to a bit in classical computers.
量子位:—表示量子位。 量子位是量子计算机中处理信息的基本单位,类似于经典计算机中的位 。
Result: — This represents the result of a measurement. This can take two possible values Zero or One
结果:—这代表测量结果。 这可以取两个可能的值零或一
We can create an array type of any valid Q# primitive type. Q# does not support rectangular multi-dimensional arrays. Instead, it supports only jagged arrays.
我们可以创建任何有效Q#基本类型的数组类型。 Q#不支持矩形多维数组。 相反,它仅支持锯齿状数组。
Int[], Qubit[][]
Int[], Qubit[][]
By default, all variables in Q# are immutable. Their values cannot be changed after they are bound. So, to create an array whose values can be set, we will use the mutable
keyword:
默认情况下,Q#中的所有变量都是不可变的。 绑定后无法更改其值。 因此,要创建一个可以设置其值的数组,我们将使用mutable
关键字:
mutable myArr = new Int [5];
mutable myArr = new Int [5];
This will create an integer array myArr
of size 5. The elements of a new array are initialized to a type-dependent default value. In this case it will be 0, the default value for an integer type.
这将创建一个大小为5的整数数组myArr
。新数组的元素将初始化为与类型有关的默认值。 在这种情况下,它将为0,这是整数类型的默认值。
Arrays passed as arguments are immutable. All arrays in Q# are zero-based. That is, the first element of an array arr
is always arr[0]
.
作为参数传递的数组是不可变的。 Q#中的所有数组都是从零开始的。 也就是说,数组arr
的第一个元素始终是arr[0]
。
The tuple type represents a tuple of values of any given primitive type. It is represented as (T1, T2, T3,…)
where T1
, T2
, T3
are primitive types. The Q# tuple is immutable. We cannot change the contents of the tuple once it has been created.
元组类型表示任何给定原始类型的值的元组。 它表示为(T1, T2, T3,…)
,其中T1
, T2
, T3
是原始类型。 Q#元组是不可变的。 创建元组后,我们将无法更改其内容。
A tuple expression can contain values of multiple primitive types. So, a tuple of type (Int, Double, Result)
is a valid tuple.
元组表达式可以包含多个基本类型的值。 因此,类型(Int, Double, Result)
的元组是有效的元组。
We can create a tuple with single element also, like (2)
. This is known as a singleton tuple, and it is considered equal to the value of the enclosed type. This property is called singleton tuple equivalence.
我们也可以创建具有单个元素的元组,例如(2)
。 这被称为单例元组,并且被认为等于封闭类型的值。 此属性称为单例元组等效项。
For example, (2)
is a singleton tuple of type Int
, but it is considered equivalent to an integer 2.
例如, (2)
是Int
类型的单例元组,但它被认为等效于整数2。
We can create a user defined type of any primitive type. We can also create an array of user defined types or can also include it in a tuple. User defined types cannot have cyclic dependency on each other. So, it is not possible to create a recursive type structure.
我们可以创建任何原始类型的用户定义类型。 我们还可以创建用户定义类型的数组,也可以将其包含在元组中。 用户定义的类型不能彼此循环依赖。 因此,不可能创建递归类型结构。
A user-defined type is a subtype of the base
type. This means it can be used anywhere a value of the base
type is expected.
用户定义的类型是base
类型的子类型。 这意味着可以在需要base
类型值的任何地方使用它。
A Q# operation is a callable routine, which contains Q# code to carry out a quantum operation. An operation is the basic unit of quantum execution in Q#. The operation can only take single value as input in the form of a tuple. It returns a single value as output, specified after a colon, and may be a tuple.
AQ# 操作是可调用的例程,其中包含Q#代码以执行量子操作。 运算是Q#中量子执行的基本单位。 该操作只能将单个值作为元组的形式输入。 它返回一个值作为输出,在冒号后面指定,并且可以是元组。
An operation has a body section which contains the implementation of the operation. It can also have adjoint, controlled, and controlled adjoint sections. These are used to specify specific variants of appropriate operations. The arguments to an operation are specified as a tuple, within parentheses. The return type of the operation is specified after the colon.
一个操作具有一个主体部分,其中包含该操作的实现。 它还可以具有伴随,受控和受控的伴随节。 这些用于指定适当操作的特定变体。 操作的参数在括号内指定为元组。 操作的返回类型在冒号之后指定。
Refer to a sample operation below:
请参考以下示例操作:
operation AddInteger(a: Int, b: Int): Int { body { mutable c = 0; set c = a + b; return (c); } }
Here, we have an operation AddInteger
which takes a tuple (Int, Int)
as input. It returns an output of type Int
after performing addition operations on input integers.
在这里,我们有一个操作AddInteger
,它以一个元组(Int, Int)
作为输入。 对输入整数执行加法运算后,它将返回Int
类型的输出。
A Q# Function is classical subroutine used within a Quantum algorithm and can only contain classical code (but no quantum operations). Similar to Q# operations, a function will also take a single value as input and returns a single value as output. Both of them can be a tuple. Functions cannot allocate qubits or call operations.
AQ#函数是在Quantum算法中使用的经典子例程,并且只能包含经典代码(但不能包含量子运算)。 与Q#操作类似,函数还将采用单个值作为输入,并返回单个值作为输出。 它们都可以是一个元组。 函数不能分配量子位或调用操作。
Let’s look at a sample function.
让我们看一个示例函数。
function ProductNumber(a: Double, b: Double): Double { mutable c = 0.0; set c = a * b; return (c); }
Here, we have defined a function ProductNumber
, which takes a tuple (Double, Double)
as input and returns an output of type Double
after performing the product of input values. Also, notice that a function does not have a body section, as in the case of an operation.
在这里,我们定义了一个函数ProductNumber
,该函数将一个元组(Double, Double)
作为输入,并在执行输入值的乘积后返回Double
类型的输出。 另外,请注意, 函数不像操作那样具有主体部分。
Let’s take a look at various expressions provided in Q#.
让我们看一下Q#中提供的各种表达式。
There are two types of numeric expressions provided by Q#:
Q#提供两种类型的数值表达式:
To represent a hexadecimal integer, we use the “0x” prefix.
为了表示一个十六进制整数,我们使用“ 0x”前缀。
We can also perform binary operations on numeric expressions to form a new numeric expression. The type of the new expression will be Double
if both input expressions are floating point numbers, or will be an Int
if both are integers.
我们还可以对数字表达式执行二进制运算以形成新的数字表达式。 如果两个输入表达式均为浮点数,则新表达式的类型将为Double
如果两个输入表达式均为整数,则将为Int
。
Apart from binary operations, the numeric expressions also support modulus, power, bitwise AND, bitwise OR, bitwise XOR, and bitwise complement operations.
除了二进制运算,数值表达式还支持模数,幂,按位与,按位或,按位XOR和按位补码运算。
Qubit expressions are the symbols that are bound to qubit values or the elements of a qubit array. Q# does not provide any support for qubit literals.
量子位表达式是绑定到量子位值或量子位数组元素的符号。 Q#不提供对qubit文字的任何支持。
As we have discussed earlier, the primitive type Pauli
can take four possible values: PauliI
, PauliX
, PauliY
and PauliZ
. These all are valid Pauli expressions. We can also create an array of Pauli types, and the array elements are considered as valid Pauli expressions.
如前所述,原始类型Pauli
可以采用四个可能的值: PauliI
, PauliX
, PauliY
和PauliZ
。 这些都是有效的Pauli表达式。 我们还可以创建Pauli类型的数组,并且将数组元素视为有效的Pauli表达式。
The two possible result values Zero
and One
are valid Result expressions. One important point to note is that One
is not the same as integer 1, and Zero
is not same as integer 0. Also, there is no direct conversion between them.
两个可能的结果值Zero
和One
有效的结果表达式。 很重要的一点值得注意的是, One
是不一样的整数1,和Zero
为整0。此外,它们之间没有直接的转换不一样的。
This is in contrast to C# where, boolean true
is considered the same as integer 1 and boolean false
is considered the same as integer 0.
这与C#相反,在C#中,布尔值true
被认为与整数1相同,布尔值false
被认为与整数0相同。
A range expression is represented as start..step..stop
where start
, step
, stop
are all integers. The range expression can take values as start
, start+step
, start+step+step
and so on until stop
is passed.
范围表达式表示为start..step..stop
,其中start
, step
, stop
均为整数。 范围表达式可以采用start
, start+step
, start+step+step
等值,直到通过stop
为止。
If only start
and stop
are mentioned in a range expression, then it will take the value of the step as set to 1 implicitly.
如果在范围表达式中仅提及start
和stop
,则将隐式地将步骤的值设为1。
Let’s understand this with the help of an example:
让我们借助示例了解这一点:
1..3
— this indicates the range 1,2,3
. This gives 1
, 1+1
, 1+1+1
1..3
这表示范围1,2,3
。 这给出1
1+1
1+1+1
1..2..6
indicates the range 1,3,5
, or 1
, 1+2
,1+2+2
1..2..6
表示范围1,3,5
,或1
, 1+2
, 1+2+2
8..-2..3
indicates the range 8,6,4
or 8
, 8+(-2)
, 8+(-2)+(-2)
8..-2..3
表示范围8,6,4
或8
8+(-2)
, 8+(-2)+(-2)
In Q# an array can be represented as a set of element expressions separated by semicolons and enclosed within square brackets. Similar to C#, all elements of an array in Q# should have the same type.
在Q#中,数组可以表示为一组用分号分隔并括在方括号内的元素表达式。 与C#类似,Q#中数组的所有元素应具有相同的类型。
So, [1;2;3]
is a valid array, but [1;2.5;Zero]
is an invalid array.
因此, [1;2;3]
是有效的数组,但是[1;2.5;Zero]
是无效的数组。
We can also use the ‘+’ operator to concatenate two arrays of the same type.
我们也可以使用'+'运算符来连接两个相同类型的数组。
So, [2;4;6] + [8;10;12]
will give [2;4;6;8;10;12]
as output.
因此, [2;4;6] + [8;10;12]
将给出[2;4;6;8;10;12]
作为输出。
To find the length of an array, we use the Length
built-in function.
要查找数组的Length
,我们使用Length
内置函数。
As an example, if myArr
is an integer array having 5 elements, then Length(myArr
) will return 5
as the output.
例如,如果myArr
是具有5个元素的整数数组,则Length(myArr
)将返回5
作为输出。
Symbols in Q# can be mutable or immutable.
Q#中的符号可以是可变的或不可变的。
An immutable symbol cannot be changed after it has been bound. We use the let keyword to define and bind an immutable symbol.
不变符号绑定后不能更改。 我们使用let关键字定义和绑定不可变的符号。
let i=8;
let i=8;
This will bind the symbol i
as an integer with value 8. If we try to reset the value of an immutable expression, we will get a compile time error.
这会将符号i
绑定为具有值8的整数。如果尝试重置不可变表达式的值,则会遇到编译时错误。
Hence set i=10;
will give an error in this case.
因此set i=10;
在这种情况下会给出错误。
A mutable symbol value can be changed after it has been bound. We use the mutable
keyword to define and bind a mutable symbol.
可变符号值绑定后可以更改。 我们使用mutable
关键字定义和绑定可变符号。
mutable i=8;
mutable i=8;
This will bind the symbol i
as an integer with value 8.
这会将符号i
绑定为具有值8的整数。
To change the value of a mutable symbol, we use the set
keyword:
要更改可变符号的值,我们使用set
关键字:
set i=10;
set i=10;
This will update the value of variable i
to 10
这会将变量i
的值更新为10
Q# allows a for-loop to iterate over an integer range. The for statement consists of the keyword for
, followed by an identifier, the keyword in
, a Range expression, and a statement block.
Q#允许for循环在整数范围内迭代。 在for语句由关键字for
,接着的标识符,该关键字in
,一范围的表达,和一个语句块。
A range is specified by the first and last integers in the range, for example: 1..5
represents the range 1, 2, 3, 4, and 5. If a step other than +1 is needed, then three integers with .. between them are used.
范围由该范围内的第一个和最后一个整数指定,例如: 1..5
表示范围1、2、3、4和5。如果需要除+1以外的步长,则使用表示三个整数。在它们之间使用。
So, 1..2..10
is the range 1, 3, 5, 7, and 9. The range is inclusive at both ends.
因此, 1..2..10
是范围1、3、5、7和9。该范围在两端都包括在内。
for(num in 1..2..10) { //Do something }
As the name suggests, this loop will repeat until successful operation occurs. This loop is based on the quantum “repeat until success” pattern. It consists of the keyword repeat
and its statement block, the keyword until
, a Boolean expression, the keyword fixup
, and its statement block .
顾名思义,此循环将重复进行,直到成功执行操作为止。 该循环基于量子的“重复直到成功”模式。 它由关键字repeat
和它的语句块,关键字until
,布尔表达式,关键字fixup
和它的语句块组成。
The statement inside the repeat block is executed, and then the boolean condition is evaluated. If the boolean condition evaluates to true, then the loop terminates. Otherwise, the fixup block is executed and the loop repeats once again.
执行repeat块内的语句,然后评估布尔条件。 如果布尔条件的计算结果为true,则循环终止。 否则,将执行fixup块,并且循环将再次重复。
The fixup block is always required — even if there is no fixup to be done — in which case it will be empty.
始终需要fixup块-即使没有要进行的fixup-在这种情况下,它都是空的。
repeat { //do something } until boolean condition fixup { // do something }
Q# supports if statements for conditional execution, similar to C#. The if statement consists of the keyword if
, followed by a Boolean expression and the statement block. An if block may have an optional else block, which is represented by the keyword else
.
Q#支持条件执行的if语句,类似于C#。 if语句由关键字if
,其后的布尔表达式和语句块组成。 一个if块可以有一个可选的else块,由关键字else
表示。
if (num % 2 == 0) { return true; } else { return false; }
A conditional statement can consist of a series of if-elseif-else chains. The else-if clause is represented by the keyword elif
.
条件语句可以由一系列if-elseif-else链组成。 else-if子句由关键字elif
表示。
if (num == 1) { //do something } elif(num == 2) { //do something } else { //do something }
In this article, we have learned the basics of the Q# programming language. We also installed QDK and verified the Q# execution environment with Visual Studio 2017. Please post your valuable feedback in the comments section and stay tuned for more on Quantum Computing.
在本文中,我们学习了Q#编程语言的基础。 我们还安装了QDK并使用Visual Studio 2017验证了Q#执行环境。请在评论部分中发表您的宝贵反馈,并继续关注有关Quantum Computing的更多信息。
You can always refer to my previous articles .
您随时可以参考我以前的文章。
You can also find this article at
您也可以在找到此文章
Originally published at on Jan 16, 2018.
最初于年1月16日发布在上。
翻译自:
q#量子编程语言
转载地址:http://ucwzd.baihongyu.com/