microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/second-api-refactor

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/DeutschJozsa.qs

144lines · modecode

1/// # Sample
2/// Deutsch–Jozsa algorithm
3///
4/// # Description
5/// Deutsch–Jozsa is a quantum algorithm that determines whether a given Boolean
6/// function 𝑓 is constant (0 on all inputs or 1 on all inputs) or balanced
7/// (1 for exactly half of the input domain and 0 for the other half).
8///
9/// This Q# program implements the Deutsch–Jozsa algorithm.
10namespace Sample {
11 open Microsoft.Quantum.Diagnostics;
12 open Microsoft.Quantum.Math;
13 open Microsoft.Quantum.Measurement;
14
15 @EntryPoint()
16 operation Main() : (String, Bool)[] {
17 // A Boolean function is a function that maps bitstrings to a bit:
18 // 𝑓 : {0, 1}^n → {0, 1}.
19
20 // We say that 𝑓 is constant if 𝑓(𝑥⃗) = 𝑓(𝑦⃗) for all bitstrings 𝑥⃗ and
21 // 𝑦⃗, and that 𝑓 is balanced if 𝑓 evaluates to true for exactly half of
22 // its inputs.
23
24 // If we are given a function 𝑓 as a quantum operation 𝑈 |𝑥〉|𝑦〉 =
25 // |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉, and are promised that 𝑓 is either constant or is
26 // balanced, then the Deutsch–Jozsa algorithm decides between these
27 // cases with a single application of 𝑈.
28
29 // Here, we demonstrate the use of the Deutsch-Jozsa algorithm by
30 // determining the type (constant or balanced) of various functions.
31 let nameFunctionTypeTuples = [
32 ("SimpleConstantBoolF", SimpleConstantBoolF, true),
33 ("SimpleBalancedBoolF", SimpleBalancedBoolF, false),
34 ("ConstantBoolF", ConstantBoolF, true),
35 ("BalancedBoolF", BalancedBoolF, false)
36 ];
37
38 mutable results = [];
39 for (name, fn, shouldBeConstant) in nameFunctionTypeTuples {
40 let isConstant = DeutschJozsa(fn, 5);
41 if (isConstant != shouldBeConstant) {
42 let shouldBeConstantStr = shouldBeConstant ? "constant" | "balanced";
43 fail $"{name} should be detected as {shouldBeConstantStr}";
44 }
45
46 let isConstantStr = isConstant ? "constant" | "balanced";
47 Message($"{name} is {isConstantStr}");
48 set results += [(name, isConstant)];
49 }
50
51 return results;
52 }
53
54 /// # Summary
55 /// This operation implements the DeutschJozsa algorithm.
56 /// It returns the Boolean value `true` if the function is constant and
57 /// `false` if it is not.
58 /// It is assumed that the function is either constant or balanced.
59 ///
60 /// # Input
61 /// ## Uf
62 /// A quantum operation that implements |𝑥〉|𝑦〉 ↦ |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉, where 𝑓 is a
63 /// Boolean function, 𝑥 is an 𝑛 bit register and 𝑦 is a single qubit.
64 /// ## n
65 /// The number of bits in the input register |𝑥〉.
66 ///
67 /// # Output
68 /// A boolean value `true` that indicates that the function is constant and
69 /// `false` that indicates that the function is balanced.
70 ///
71 /// # See Also
72 /// - For details see Section 1.4.3 of Nielsen & Chuang.
73 ///
74 /// # References
75 /// - [ *Michael A. Nielsen , Isaac L. Chuang*,
76 /// Quantum Computation and Quantum Information ]
77 /// (http://doi.org/10.1017/CBO9780511976667)
78 operation DeutschJozsa(Uf : ((Qubit[], Qubit) => Unit), n : Int) : Bool {
79 // We allocate n + 1 clean qubits. Note that the function `Uf` is defined
80 // on inputs of the form (x, y), where x has n bits and y has 1 bit.
81 use queryRegister = Qubit[n];
82 use target = Qubit();
83
84 // The last qubit needs to be flipped so that the function will actually
85 // be computed into the phase when Uf is applied.
86 X(target);
87
88 // Now, a Hadamard transform is applied to each of the qubits.
89 H(target);
90 // We use a within-apply block to ensure that the Hadamard transform is
91 // correctly inverted on the |𝑥〉 register.
92 within {
93 for q in queryRegister {
94 H(q);
95 }
96 } apply {
97 // We apply Uf to the n+1 qubits, computing |𝑥, 𝑦〉 ↦ |𝑥, 𝑦 ⊕ 𝑓(𝑥)〉.
98 Uf(queryRegister, target);
99 }
100
101 // The following for-loop measures all qubits and resets them to the |0〉
102 // state so that they can be safely deallocated at the end of the block.
103 // The loop also sets `result` to `true` if all measurement results are
104 // `Zero`, i.e. if the function is a constant function, and sets
105 // `result` to `false` if not, which according to the assumption on 𝑓
106 // means that it must be balanced.
107 mutable result = true;
108 for q in queryRegister {
109 if MResetZ(q) == One {
110 set result = false;
111 }
112 }
113
114 // Finally, the last qubit, which held the 𝑦-register, is reset.
115 Reset(target);
116 return result;
117 }
118
119 // Simple constant Boolean function
120 operation SimpleConstantBoolF(args : Qubit[], target : Qubit) : Unit {
121 X(target);
122 }
123
124 // Simple balanced Boolean function
125 operation SimpleBalancedBoolF(args : Qubit[], target : Qubit) : Unit {
126 CX(args[0], target);
127 }
128
129 // A more complex constant Boolean function.
130 // It applies X to every input basis vector.
131 operation ConstantBoolF(args : Qubit[], target : Qubit) : Unit {
132 for i in 0..(2^Length(args)) - 1 {
133 ApplyControlledOnInt(i, X, args, target);
134 }
135 }
136
137 // A more complex balanced Boolean function.
138 // It applies X to half of the input basis vectors.
139 operation BalancedBoolF(args : Qubit[], target : Qubit) : Unit {
140 for i in 0..2..(2^Length(args)) - 1 {
141 ApplyControlledOnInt(i, X, args, target);
142 }
143 }
144}
145