程式執行
下列範例提供如何實作程式的第一 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 Visual Code 擴充功能的其中一個範例程式。
您可以從 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 內完成。 如需完整的端對端程式,請參閱開始使用Q#程式和 Visual Studio Code。