microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/pythontelem

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/DeutschJozsaNISQ.qs

101lines · 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 import Std.Measurement.*;
12
13 @EntryPoint()
14 operation Main() : (Result[], Result[]) {
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 a couple of functions.
29 let balancedResults = DeutschJozsa(SimpleBalancedBoolF, 5);
30 let constantResults = DeutschJozsa(SimpleConstantBoolF, 5);
31 return (balancedResults, constantResults);
32 }
33
34 /// # Summary
35 /// This operation implements the DeutschJozsa algorithm.
36 /// It returns the query register measurement results. If all the measurement
37 /// results are `Zero`, the function is constant. If at least one measurement
38 /// result is `One`, the function is balanced.
39 /// It is assumed that the function is either constant or balanced.
40 ///
41 /// # Input
42 /// ## Uf
43 /// A quantum operation that implements |𝑥〉|𝑦〉 ↦ |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉, where 𝑓 is a
44 /// Boolean function, 𝑥 is an 𝑛 bit register and 𝑦 is a single qubit.
45 /// ## n
46 /// The number of bits in the input register |𝑥〉.
47 ///
48 /// # Output
49 /// An array of measurement results for the query register.
50 /// All `Zero` measurement results indicate that the function is constant.
51 /// At least one `One` measurement result in the array indicates that the
52 /// function is balanced.
53 ///
54 /// # See Also
55 /// - For details see Section 1.4.3 of Nielsen & Chuang.
56 ///
57 /// # References
58 /// - [ *Michael A. Nielsen , Isaac L. Chuang*,
59 /// Quantum Computation and Quantum Information ]
60 /// (http://doi.org/10.1017/CBO9780511976667)
61 operation DeutschJozsa(Uf : ((Qubit[], Qubit) => Unit), n : Int) : Result[] {
62 // We allocate n + 1 clean qubits. Note that the function `Uf` is defined
63 // on inputs of the form (x, y), where x has n bits and y has 1 bit.
64 use queryRegister = Qubit[n];
65 use target = Qubit();
66
67 // The last qubit needs to be flipped so that the function will actually
68 // be computed into the phase when Uf is applied.
69 X(target);
70
71 // Now, a Hadamard transform is applied to each of the qubits.
72 H(target);
73 // We use a within-apply block to ensure that the Hadamard transform is
74 // correctly inverted on the |𝑥〉 register.
75 within {
76 for q in queryRegister {
77 H(q);
78 }
79 } apply {
80 // We apply Uf to the n+1 qubits, computing |𝑥, 𝑦〉 ↦ |𝑥, 𝑦 ⊕ 𝑓(𝑥)〉.
81 Uf(queryRegister, target);
82 }
83
84 // Measure the query register and reset all qubits so they can be safely
85 // deallocated.
86 let results = MResetEachZ(queryRegister);
87 Reset(target);
88 return results;
89 }
90
91 // Simple constant Boolean function
92 operation SimpleConstantBoolF(args : Qubit[], target : Qubit) : Unit {
93 X(target);
94 }
95
96 // Simple balanced Boolean function
97 operation SimpleBalancedBoolF(args : Qubit[], target : Qubit) : Unit {
98 CX(args[0], target);
99 }
100}
101
102