练习 - 使用 Copilot 运行量子程序

已完成

若要开始探索 Azure Quantum 中的 Copilot 和编码,请使用“Quantum Samples”下拉列表中的一个示例

运行量子程序

  1. 导航到 Azure Quantum 中的代码

  2. 选择“Quantum Samples”,然后选择“Random Number Generator”。 以下代码将复制到代码窗口。

    /// # Sample
    /// Quantum Random Number Generator
    ///
    /// # Description
    /// This program implements a quantum ranndom number generator by setting qubits
    /// in superposition and then using the measurement results as random bits.
    namespace Sample {
        open Microsoft.Quantum.Measurement;
        open Microsoft.Quantum.Intrinsic;
    
        @EntryPoint()
        operation Main() : Result[] {
            // Generate 5-bit random number.
            let nBits = 5;
            return GenerateNRandomBits(nBits);
        }
    
        /// # Summary
        /// Generates N random bits.
        operation GenerateNRandomBits(nBits : Int) : Result[] {
            // Allocate N qubits.
            use register = Qubit[nBits];
    
            // Set the qubits into superposition of 0 and 1 using the Hadamard
            // operation `H`.
            for qubit in register {
                H(qubit);
            }
    
            // At this point each has 50% chance of being measured in the |0〉 state
            // and 50% chance of being measured in the |1〉 state.
            // Measure each qubit and reset them all so they can be safely
            // deallocated.
            let results = MeasureEachZ(register);
            ResetAll(register);
            return results;
        }
    }
    
  3. 选择“In-Memory Simulator”

  4. 选择运行

    • 结果将显示在“Results”字段中,结果直方图将显示在代码窗口下方。
    • 可以移动“Select number of shots”的滑块来指定程序运行的次数。
    • “Shots”字段显示每个快照的结果。

若要使用其他模拟器再次运行程序,请执行以下操作:

  1. 选择“In-Memory Simulator”下拉列表,然后选择“Quantinuum H-Series Emulator”

  2. 选择快照数(当前限制为 20),然后选择“Run”。

    • 作业状态将显示在代码窗口顶部。
    • 结果直方图将显示在代码窗口下方。 每个快照的结果目前无法与 Quantinuum H 系列模拟器配合使用。

询问 Copilot

可以向 Azure Quantum 中的 Copilot 提示几乎所有与量子相关的信息。 例如,询问 Copilot 以下问题,并查看发生的情况:

  • “解释 MResetZ 操作”
  • “编写纠缠两个量子位的 Q# 代码”
  • “解释量子干涉”
  • “量子位和经典位有什么区别?”