你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

程序执行

以下示例首先概述了程序是如何 Q# 实现的:

/// # Sample
/// Bell States
///
/// # Description
/// Bell states or EPR pairs are specific quantum states of two qubits
/// that represent the simplest (and maximal) examples of quantum entanglement.
///
/// This Q# program implements the four different Bell states.

import Std.Diagnostics.*;
import Std.Measurement.*;

operation Main() : (Result, Result)[] {
    // Allocate the two qubits that will be used to create a Bell state.
    use register = Qubit[2];

    // This array contains a label and a preparation operation for each one
    // of the four Bell states.
    let bellStateTuples = [
        ("|Φ+〉", PreparePhiPlus),
        ("|Φ-〉", PreparePhiMinus),
        ("|Ψ+〉", PreparePsiPlus),
        ("|Ψ-〉", PreparePsiMinus)
    ];

    // Prepare all Bell states, show them using the `DumpMachine` operation
    // and measure the Bell state qubits.
    mutable measurements = [];
    for (label, prepare) in bellStateTuples {
        prepare(register);
        Message($"Bell state {label}:");
        DumpMachine();
        measurements += [(MResetZ(register[0]), MResetZ(register[1]))];
    }
    return measurements;
}

operation PreparePhiPlus(register : Qubit[]) : Unit {
    ResetAll(register);             // |00〉
    H(register[0]);                 // |+0〉
    CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 + |11〉)
}

operation PreparePhiMinus(register : Qubit[]) : Unit {
    ResetAll(register);             // |00〉
    H(register[0]);                 // |+0〉
    Z(register[0]);                 // |-0〉
    CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 - |11〉)
}

operation PreparePsiPlus(register : Qubit[]) : Unit {
    ResetAll(register);             // |00〉
    H(register[0]);                 // |+0〉
    X(register[1]);                 // |+1〉
    CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 + |10〉)
}

operation PreparePsiMinus(register : Qubit[]) : Unit {
    ResetAll(register);             // |00〉
    H(register[0]);                 // |+0〉
    Z(register[0]);                 // |-0〉
    X(register[1]);                 // |-1〉
    CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 - |10〉)
}

此程序实现量子纠缠的四个基本贝尔状态,是包含 Azure Quantum 视觉代码扩展的示例程序之一。

可以从 VS Code QDK 扩展中的内置模拟器运行程序并获取标准输出

Message: Bell state |Φ+〉:

DumpMachine:

 Basis | Amplitude      | Probability | Phase
 -----------------------------------------------
  |00⟩ |  0.7071+0.0000𝑖 |    50.0000% |   0.0000
  |11⟩ |  0.7071+0.0000𝑖 |    50.0000% |   0.0000

Message: Bell state |Φ-〉:

DumpMachine:

 Basis | Amplitude      | Probability | Phase
 -----------------------------------------------
  |00⟩ |  0.7071+0.0000𝑖 |    50.0000% |   0.0000
  |11⟩ | −0.7071+0.0000𝑖 |    50.0000% |  -3.1416

Message: Bell state |Ψ+〉:

DumpMachine:

 Basis | Amplitude      | Probability | Phase
 -----------------------------------------------
  |01⟩ |  0.7071+0.0000𝑖 |    50.0000% |   0.0000
  |10⟩ |  0.7071+0.0000𝑖 |    50.0000% |   0.0000

Message: Bell state |Ψ-〉:

DumpMachine:

 Basis | Amplitude      | Probability | Phase
 -----------------------------------------------
  |01⟩ |  0.7071+0.0000𝑖 |    50.0000% |   0.0000
  |10⟩ | −0.7071+0.0000𝑖 |    50.0000% |  -3.1416

Result: "[(One, One), (Zero, Zero), (One, Zero), (Zero, One)]"
Finished shot 1 of 1

Q# simulation completed.

或使用直方图输出运行模拟器

量子程序作为直方图的输出。

若要在量子硬件上运行程序,首先需要编译程序并将其提交到 Azure Quantum,所有这些都可以从 VS Code 内部完成。 有关完整的端到端过程,请参阅程序和Visual Studio Code入门Q#