microsoft/qdk

Public

mirrored from https://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/BernsteinVazirani.qs

163lines · modecode

1/// # Sample
2/// Bernstein-Vazirani algorithm
3///
4/// # Description
5/// The Bernstein-Vazirani algorithm determines the value of a bit string
6/// encoded in a function.
7///
8/// This Q# program implements the Bernstein-Vazirani algorithm.
9namespace Sample {
10 import Std.Arrays.*;
11 import Std.Convert.*;
12 import Std.Diagnostics.*;
13 import Std.Math.*;
14 import Std.Measurement.*;
15
16 @EntryPoint()
17 operation Main() : Int[] {
18 // Consider a function 𝑓(𝑥⃗) on bitstrings 𝑥⃗ = (𝑥₀, …, 𝑥ₙ₋₁) of the form
19 // 𝑓(𝑥⃗) ≔ Σᵢ 𝑥ᵢ 𝑟ᵢ
20 // where 𝑟⃗ = (𝑟₀, …, 𝑟ₙ₋₁) is an unknown bitstring that determines the
21 // parity of 𝑓.
22
23 // The Bernstein–Vazirani algorithm allows determining 𝑟 given a
24 // quantum operation that implements
25 // |𝑥〉|𝑦〉 ↦ |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉.
26
27 // The entry point function of this program, `Main`, shows how to use
28 // the `BernsteinVazirani` operation to determine the value of various
29 // integers whose bits describe 𝑟.
30 let nQubits = 10;
31
32 // Use the Bernstein–Vazirani algorithm to determine the bit strings
33 // that various integers represent.
34 let integers = [127, 238, 512];
35 mutable decodedIntegers = [];
36 for integer in integers {
37 // Create an operation that encodes a bit string represented by an
38 // integer as a parity operation.
39 let parityOperation = EncodeIntegerAsParityOperation(integer);
40
41 // Use the parity operation as input to the Bernstein-Vazirani
42 // algorithm to determine the bit string.
43 let decodedBitString = BernsteinVazirani(parityOperation, nQubits);
44 let decodedInteger = ResultArrayAsInt(decodedBitString);
45 Fact(
46 decodedInteger == integer,
47 $"Decoded integer {decodedInteger}, but expected {integer}."
48 );
49
50 Message($"Successfully decoded bit string as int: {decodedInteger}");
51 set decodedIntegers += [decodedInteger];
52 }
53
54 return decodedIntegers;
55 }
56
57 /// # Summary
58 /// This operation implements the Bernstein-Vazirani quantum algorithm.
59 /// This algorithm computes for a given Boolean function that is promised to
60 /// be a parity 𝑓(𝑥₀, …, 𝑥ₙ₋₁) = Σᵢ 𝑟ᵢ 𝑥ᵢ a result in the form of a bit
61 /// vector (𝑟₀, …, 𝑟ₙ₋₁) corresponding to the parity function.
62 /// Note that it is promised that the function is actually a parity
63 /// function.
64 ///
65 /// # Input
66 /// ## Uf
67 /// A quantum operation that implements |𝑥〉|𝑦〉 ↦ |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉,
68 /// where 𝑓 is a Boolean function that implements a parity Σᵢ 𝑟ᵢ 𝑥ᵢ.
69 /// ## n
70 /// The number of bits in the input register |𝑥〉.
71 ///
72 /// # Output
73 /// An array of type `Result[]` that contains the parity 𝑟⃗ = (𝑟₀, …, 𝑟ₙ₋₁).
74 ///
75 /// # See Also
76 /// - For details see Section 1.4.3 of Nielsen & Chuang.
77 ///
78 /// # References
79 /// - [ *Ethan Bernstein and Umesh Vazirani*,
80 /// SIAM J. Comput., 26(5), 1411–1473, 1997 ]
81 /// (https://doi.org/10.1137/S0097539796300921)
82 operation BernsteinVazirani(Uf : ((Qubit[], Qubit) => Unit), n : Int) : Result[] {
83 // We allocate n + 1 clean qubits. Note that the function Uf is defined
84 // on inputs of the form (x, y), where x has n bits and y has 1 bit.
85 use queryRegister = Qubit[n];
86 use target = Qubit();
87
88 // The last qubit needs to be flipped so that the function will actually
89 // be computed into the phase when Uf is applied.
90 X(target);
91
92 within {
93 // Now, a Hadamard transform is applied to each of the qubits. As
94 // the last step before the measurement, a Hadamard transform is
95 // applied to all qubits except last one. We could apply the
96 // transform to the last qubit also, but this would not affect the
97 // final outcome.
98 // We use a within-apply block to ensure that the Hadamard transform
99 // is correctly inverted.
100 ApplyToEachA(H, queryRegister);
101 } apply {
102 H(target);
103 // We now apply Uf to the n+1 qubits, computing
104 // |x, y〉 ↦ |x, y ⊕ f(x)〉.
105 Uf(queryRegister, target);
106 }
107
108 // Measure all qubits and reset them to the |0〉 state so that they can
109 // be safely deallocated at the end of the block.
110 let resultArray = MResetEachZ(queryRegister);
111
112 // Finally, the last qubit, which held the y-register, is reset.
113 Reset(target);
114
115 // The result is already contained in resultArray so no further
116 // post-processing is necessary.
117 return resultArray;
118 }
119
120 /// # Summary
121 /// Given an integer that can be represented as a bit string
122 /// 𝑟⃗ = (r₀, …, rₙ₋₁), this operation applies a unitary 𝑈 that acts on 𝑛 + 1
123 /// qubits as:
124 /// 𝑈 |𝑥〉|𝑦〉 = |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉
125 /// where 𝑓(𝑥) = Σᵢ 𝑥ᵢ 𝑟ᵢ mod 2.
126 ///
127 /// # Input
128 /// ## bitStringAsInt
129 /// An integer that can be represented as a bit string 𝑟⃗ used to define the
130 /// function 𝑓.
131 /// ## xRegister
132 /// Represents the |𝑥〉 register that 𝑈 acts on.
133 /// ## yQubit
134 /// Represents the |𝑦〉 qubit that 𝑈 acts on.
135 operation ApplyParityOperation(
136 bitStringAsInt : Int,
137 xRegister : Qubit[],
138 yQubit : Qubit
139 ) : Unit {
140 // `xRegister` muts have enough qubits to represent the integer.
141 let requiredBits = BitSizeI(bitStringAsInt);
142 let availableQubits = Length(xRegister);
143 Fact(
144 availableQubits >= requiredBits,
145 $"Integer value {bitStringAsInt} requires {requiredBits} bits to be represented but the quantum register only has {availableQubits} qubits"
146 );
147
148 // Apply the quantum operations that encode the bit string.
149 for index in IndexRange(xRegister) {
150 if ((bitStringAsInt &&& 2^index) != 0) {
151 CNOT(xRegister[index], yQubit);
152 }
153 }
154 }
155
156 /// # Summary
157 /// Returns black-box operations (Qubit[], Qubit) => () of the form
158 /// U_f |𝑥〉|𝑦〉 = |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉.
159 /// We define 𝑓 by providing the bit string 𝑟⃗ as an integer.
160 function EncodeIntegerAsParityOperation(bitStringAsInt : Int) : (Qubit[], Qubit) => Unit {
161 return ApplyParityOperation(bitStringAsInt, _, _);
162 }
163}
164