microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.13

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/DeutschJozsa.qs

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