你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Learn how to run Q# code in a Jupyter Notebook in the Azure Quantum portal. A Jupyter Notebook is a document that contains both rich text and code and can run in your browser using Q# and Python code. 可以直接在 Azure Quantum 门户中创建笔记本,并提供预加载连接信息和预配置 Q# 和 Python 开发环境等功能。
在本文中,你将在Azure 门户中运行一个示例笔记本,该笔记本执行写入和 Python 编写的Q#简单量子随机数生成器。
有关将 Jupyter Notebook 与 Azure Quantum 服务配合使用的详细信息,请参阅 Azure Quantum 工作区中的 Jupyter Notebook。
Prerequisites
在开始之前,需要满足以下先决条件才能在 Azure Quantum 工作区中使用 Jupyter Notebook。
- 具有活动订阅的 Azure 帐户。 If you don’t have an Azure account, register for free and sign up for a pay-as-you-go subscription.
- Azure Quantum 工作区。 请参阅创建 Azure Quantum 工作区。
复制示例笔记本
若要开始,请从笔记本库复制一个示例笔记本。
Log in to the Azure portal and select your Azure Quantum workspace.
Select Notebooks.
In the Sample gallery, locate the Hello, world: Q# notebook tile, select either the IonQ, Quantinuum, or Rigetti provider (the sample code is identical) and select Copy to my notebooks.
The sample notebook can be found under My notebooks and you can now run the notebook.
运行笔记本
若要运行示例笔记本,请针对所选提供程序执行以下步骤。
In My notebooks, select the hello-world-qsharp-ionq notebook.
To run the full program from top to bottom, select Run all.
To walk through the example and run each cell individually from top to bottom, select the cell you want to run and select the run icon.
在 IonQ 上逐步执行程序
The hello world program runs a simple quantum random number generator and displays a histogram of the results.
需要注意的一些事项:
- 1st cell: Preloads your subscription information to connect to the Azure Quantum service.
- 2nd cell: Retrieves the available targets (quantum computers and simulators) in your workspace.
-
3rd cell: Imports the
qsharp
package and sets the base profile for the compiler. -
4th cell: The Q# code that defines the program.
%%qsharp
请注意 magic 命令,该命令允许直接在 Python 笔记本单元中输入Q#代码。 - 5th cell: Compiles the qsharp operation using the base profile to submit to Azure Quantum.
- 第 6 个和第 7 个单元格:设置 target、提交作业和绘图并显示结果。 结果应大致在 0 和 1 之间划分。
Note
如果有任何问题或使用 Azure Quantum 遇到任何问题,可以联系 AzureQuantumInfo@microsoft.com。