microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/kata-preview

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/algorithms/BellState.qs

61lines · modecode

1/// # Sample
2/// Bell States
3///
4/// # Description
5/// Bell states or EPR pairs are specific quantum states of two qubits
6/// that represent the simplest (and maximal) examples of quantum entanglement.
7///
8/// This Q# program implements the four different Bell states.
9namespace Sample {
10 open Microsoft.Quantum.Diagnostics;
11 open Microsoft.Quantum.Measurement;
12
13 @EntryPoint()
14 operation BellStates() : (Result, Result)[] {
15 // This array contains a label and a preparation operation for each one
16 // of the four Bell states.
17 let bellStateTuples = [
18 ("|Φ+〉", PreparePhiPlus),
19 ("|Φ-〉", PreparePhiMinus),
20 ("|Ψ+〉", PreparePsiPlus),
21 ("|Ψ-〉", PreparePsiMinus)
22 ];
23
24 // Prepare all Bell states, show them using the `DumpMachine` operation
25 // and measure the Bell state qubits.
26 mutable measurements = [];
27 for (label, prepare) in bellStateTuples {
28 // Allocate the two qubits that will be used to create a Bell state.
29 use register = Qubit[2];
30 prepare(register);
31 Message($"Bell state {label}:");
32 DumpMachine();
33 set measurements += [(MResetZ(register[0]), MResetZ(register[1]))];
34 }
35 return measurements;
36 }
37
38 operation PreparePhiPlus(register : Qubit[]) : Unit {
39 H(register[0]); // |+0〉
40 CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 + |11〉)
41 }
42
43 operation PreparePhiMinus(register : Qubit[]) : Unit {
44 H(register[0]); // |+0〉
45 Z(register[0]); // |-0〉
46 CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 - |11〉)
47 }
48
49 operation PreparePsiPlus(register : Qubit[]) : Unit {
50 H(register[0]); // |+0〉
51 X(register[1]); // |+1〉
52 CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 + |10〉)
53 }
54
55 operation PreparePsiMinus(register : Qubit[]) : Unit {
56 H(register[0]); // |+0〉
57 Z(register[0]); // |-0〉
58 X(register[1]); // |-1〉
59 CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 - |10〉)
60 }
61}
62