microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
minestarks/circuit-magic

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/DotProductViaPhaseEstimation.qs

165lines · modecode

1// MIT License
2
3// Copyright (c) 2023 KPMG Australia
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22import Std.Math.*;
23import Std.Convert.*;
24
25operation Main() : (Int, Int) {
26 // The angles for inner product. Inner product is meeasured for vectors
27 // (cos(Θ₁/2), sin(Θ₁/2)) and (cos(Θ₂/2), sin(Θ₂/2)).
28 // Number of iterations
29 // Perform measurements
30 Message("Computing inner product of vectors (cos(Θ₁/2), sin(Θ₁/2))⋅(cos(Θ₂/2), sin(Θ₂/2)) ≈ -cos(x𝝅/2ⁿ)");
31 let result = PerformMeasurements();
32 // Return result
33 return (result, 4);
34}
35
36@Config(Adaptive)
37@Config(not HigherLevelConstructs)
38@Config(not FloatingPointComputations)
39operation PerformMeasurements() : Int {
40 // n = 4, so measurementCount = n + 1 = 5
41 return QuantumInnerProduct();
42}
43
44@Config(HigherLevelConstructs)
45@Config(FloatingPointComputations)
46operation PerformMeasurements() : Int {
47 let theta1 = PI() / 7.0;
48 let theta2 = PI() / 5.0;
49 Message($"Θ₁={theta1}, Θ₂={theta2}.");
50
51 // First compute quantum approximation
52 // n = 4 so measurementCount = 5 and 2^n = 16
53 let x = QuantumInnerProduct();
54 let angle = PI() * IntAsDouble(x) / 16.0;
55 let computedInnerProduct = -Cos(angle);
56 Message($"x = {x}, n = 4.");
57
58 // Now compute true inner product
59 let trueInnterProduct = ClassicalInnerProduct();
60
61 Message($"Computed value = {computedInnerProduct}, true value = {trueInnterProduct}");
62
63 return x;
64}
65
66function ClassicalInnerProduct() : Double {
67 let theta1 = PI() / 7.0;
68 let theta2 = PI() / 5.0;
69 return Cos(theta1 / 2.0) * Cos(theta2 / 2.0) + Sin(theta1 / 2.0) * Sin(theta2 / 2.0);
70}
71
72operation QuantumInnerProduct() : Int {
73 //Create target register
74 use TargetReg = Qubit();
75 //Create ancilla register
76 use AncilReg = Qubit();
77 //Run iterative phase estimation
78 let Results = IterativePhaseEstimation(TargetReg, AncilReg);
79 Reset(TargetReg);
80 Reset(AncilReg);
81 return Results;
82}
83
84operation IterativePhaseEstimation(
85 TargetReg : Qubit,
86 AncilReg : Qubit
87) : Int {
88
89 let Measurements = 5; // previously iterationCount (n + 1) with n = 4
90
91 let theta1 = PI() / 7.0;
92 let theta2 = PI() / 5.0;
93
94 use ControlReg = Qubit();
95 mutable MeasureControlReg = [Zero, size = Measurements];
96 mutable bitValue = 0;
97 //Apply to initialise state, this is defined by the angles theta1 and theta2
98 StateInitialisation(TargetReg, AncilReg);
99 for index in 0..Measurements - 1 {
100 H(ControlReg);
101 //Don't apply rotation on first set of oracles
102 if index > 0 {
103 //Loop through previous results
104 for index2 in 0..index - 1 {
105 if MeasureControlReg[Measurements - 1 - index2] == One {
106 //Rotate control qubit dependent on previous measurements and number of measurements
107 let angle = -IntAsDouble(2^(index2)) * PI() / (2.0^IntAsDouble(index));
108 R(PauliZ, angle, ControlReg);
109 }
110 }
111
112 }
113 let powerIndex = (1 <<< (Measurements - 1 - index));
114 //Apply a number of oracles equal to 2^index, where index is the number or measurements left
115 for _ in 1..powerIndex {
116 Controlled GOracle([ControlReg], (TargetReg, AncilReg));
117 }
118 H(ControlReg);
119 //Make a measurement mid circuit
120 set MeasureControlReg w/= (Measurements - 1 - index) <- MResetZ(ControlReg);
121 if MeasureControlReg[Measurements - 1 - index] == One {
122 //Assign bitValue based on previous measurement
123 bitValue += 2^(index);
124 }
125 }
126 return bitValue;
127}
128
129/// # Summary
130/// This is state preperation operator A for encoding the 2D vector (page 7)
131operation StateInitialisation(
132 TargetReg : Qubit,
133 AncilReg : Qubit
134) : Unit is Adj + Ctl {
135
136 let theta1 = PI() / 7.0;
137 let theta2 = PI() / 5.0;
138
139 H(AncilReg);
140 // Arbitrary controlled rotation based on theta. This is vector v.
141 Controlled R([AncilReg], (PauliY, theta1, TargetReg));
142 // X gate on ancilla to change from |+〉 to |-〉.
143 X(AncilReg);
144 // Arbitrary controlled rotation based on theta. This is vector c.
145 Controlled R([AncilReg], (PauliY, theta2, TargetReg));
146 X(AncilReg);
147 H(AncilReg);
148}
149
150operation GOracle(
151 TargetReg : Qubit,
152 AncilReg : Qubit
153) : Unit is Adj + Ctl {
154
155 // Angles inlined
156
157 Z(AncilReg);
158 within {
159 Adjoint StateInitialisation(TargetReg, AncilReg);
160 X(AncilReg);
161 X(TargetReg);
162 } apply {
163 Controlled Z([AncilReg], TargetReg);
164 }
165}
166