microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dbwy/random_seed

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/DeutschJozsaNISQ.qs

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