练习 - Azure Quantum 资源估算器入门

已完成

让我们来进行一些使用 Azure Quantum 资源估算器的练习。 在以下示例中,估算 Shor 算法示例的物理资源。

安装 qsharp 和 qsharp 小组件

首先,安装最新的 Azure Quantum qsharpqsharp-widgets 包。

python -m pip install --upgrade qsharp qsharp-widgets 

创建量子算法

  1. 在 VS Code 中,选择“视图 > 命令面板”,然后选择“创建:新 Jupyter Notebook”。

  2. 在笔记本的第一个单元格中,导入 qsharp 包。

    import qsharp
    from qsharp_widgets import EstimateDetails
    
  3. 添加新单元格并复制以下代码。

    %%qsharp
    /// # Sample
    /// Random Bit
    ///
    /// # Description
    /// This Q# program generates a random bit by setting a qubit in a superposition
    /// of the computational basis states |0〉 and |1〉, and returning the measurement
    /// result.
    
        operation RandomBit() : Result {
            // Qubits are only accesible for the duration of the scope where they
            // are allocated and are automatically released at the end of the scope.
            use qubit = Qubit();
    
            // Set the qubit in superposition by applying a Hadamard transformation.
            H(qubit);
    
            // Measure the qubit. There is a 50% probability of measuring either 
            // `Zero` or `One`.
            let result = M(qubit);
    
            // Reset the qubit so it can be safely released.
            Reset(qubit);
            return result;
        }
    

估计量子算法

  1. 现在使用默认假设来估算 RandomBit 操作的物理资源。 添加新单元格并复制以下代码。

    result = qsharp.estimate("RandomBit()")
    result
    

    qsharp.estimate 函数创建一个结果对象,可用于显示整体物理资源计数的表。 第一个表显示了主要物理资源估计值。 RandomBit 操作需要 300 个量子比特,在量子计算机上运行需要 2 微秒。

    物理资源估计
    运行时 2 微秒
    rQOPS 3.00M
    物理量子比特 300
  2. 可以通过折叠包含更多信息的组来检查成本详细信息。 例如,折叠“逻辑量子比特参数”组可看到代码距离为 5,每个逻辑量子比特的物理量子比特数为 50。

    逻辑量子比特参数
    QEC 方案 surface_code
    码距 5
    物理量子比特 50
    逻辑周期时间 2 微秒
    逻辑量子比特错误率 3.00E-5
    交叉预制 0.03
    错误更正阈值 0.01
    逻辑周期时间公式 (4 * twoQubitGateTime + 2 * oneQubitMeasurementTime) * codeDistance
    物理量子比特公式 2 * codeDistance * codeDistance
  3. 可以使用 jobParams 字段访问可传递给作业执行的所有目标参数,并查看假定的默认值:

    result['jobParams']
    
    {'errorBudget': 0.001,
     'qecScheme': {'crossingPrefactor': 0.03,
      'errorCorrectionThreshold': 0.01,
      'logicalCycleTime': '(4 * twoQubitGateTime + 2 * oneQubitMeasurementTime) * codeDistance',
      'name': 'surface_code',
      'physicalQubitsPerLogicalQubit': '2 * codeDistance * codeDistance'},
     'qubitParams': {'instructionSet': 'GateBased',
      'name': 'qubit_gate_ns_e3',
      'oneQubitGateErrorRate': 0.001,
      'oneQubitGateTime': '50 ns',
      'oneQubitMeasurementErrorRate': 0.001,
      'oneQubitMeasurementTime': '100 ns',
      'tGateErrorRate': 0.001,
      'tGateTime': '50 ns',
      'twoQubitGateErrorRate': 0.001,
      'twoQubitGateTime': '50 ns'}}
    

    可以看到,资源估算器采用了 qubit_gate_ns_e3 量子比特模型、surface_code 纠错代码和 0.001 错误预算作为估算的默认值。

更改默认值并估计算法

为程序提交资源估算请求时,可以指定一些可选参数。 以下是可自定义的目标参数:

  • errorBudget - 算法的总体允许错误预算
  • qecScheme - 量子纠错 (QEC) 方案
  • qubitParams - 物理量子比特参数
  • constraints - 组件级约束
  • distillationUnitSpecifications - T 工厂蒸馏算法的规范
  • estimateType - 单一或边界

更改量子比特模型

可以使用基于 Majorana 的量子比特参数 qubitParams“qubit_maj_ns_e6”估算同一算法的成本。

result_maj = qsharp.estimate("RandomBit()", params={
                "qubitParams": {
                    "name": "qubit_maj_ns_e6"
                }})
EstimateDetails(result_maj)

更改量子纠错方案

对于基于 Majorana 的量子比特参数上的同一示例,可以使用已 floque 的 QEC 方案 qecScheme 重新运行资源估算作业。

result_maj = qsharp.estimate("RandomBit()", params={
                "qubitParams": {
                    "name": "qubit_maj_ns_e6"
                },
                "qecScheme": {
                    "name": "floquet_code"
                }})
EstimateDetails(result_maj)

更改错误预算

接下来,使用 10% 的 errorBudget 重新运行同一量子线路。

result_maj = qsharp.estimate("RandomBit()", params={
                "qubitParams": {
                    "name": "qubit_maj_ns_e6"
                },
                "qecScheme": {
                    "name": "floquet_code"
                },
                "errorBudget": 0.1})
EstimateDetails(result_maj)