This repository was archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReferenceImplementation.qs
More file actions
166 lines (135 loc) · 6.19 KB
/
ReferenceImplementation.qs
File metadata and controls
166 lines (135 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all tasks.
// The tasks themselves can be found in Tasks.qs file.
// We recommend that you try to solve the tasks yourself first,
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.PhaseEstimation {
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Characterization;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Oracles;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Convert;
//////////////////////////////////////////////////////////////////
// Part I. Quantum phase estimation (QPE)
//////////////////////////////////////////////////////////////////
// Task 1.1. Inputs to QPE: eigenstates of Z/S/T gates.
operation Eigenstates_ZST_Reference (q : Qubit, state : Int) : Unit
is Adj {
if (state == 1) {
X(q);
}
}
// ------------------------------------------------------
operation UnitaryPowerImpl_Reference (U : (Qubit => Unit is Adj + Ctl), power : Int, q : Qubit) : Unit
is Adj + Ctl {
for (i in 1..power) {
U(q);
}
}
// Task 1.2. Inputs to QPE: powers of Z/S/T gates.
function UnitaryPower_Reference (U : (Qubit => Unit is Adj + Ctl), power : Int) : (Qubit => Unit is Adj + Ctl) {
return UnitaryPowerImpl_Reference(U, power, _);
}
// ------------------------------------------------------
// Task 1.3. Validate inputs to QPE
operation AssertIsEigenstate_Reference (U : (Qubit => Unit), P : (Qubit => Unit is Adj)) : Unit {
using (q = Qubit()) {
// Prepare the state |ψ⟩
P(q);
// Apply the given unitary
U(q);
// If the given state is an eigenstate, the current state of the qubit should be |ψ⟩ (up to a global phase).
// So un-preparing it should bring the state back to |0⟩
Adjoint P(q);
AssertQubit(Zero, q);
}
}
// ------------------------------------------------------
operation Oracle_Reference (U : (Qubit => Unit is Adj + Ctl), power : Int, target : Qubit[]) : Unit
is Adj + Ctl{
for (i in 1 .. power) {
U(target[0]);
}
}
// Task 1.4. QPE for single-qubit unitaries
operation QPE_Reference (U : (Qubit => Unit is Adj + Ctl), P : (Qubit => Unit is Adj), n : Int) : Double {
// Construct a phase estimation oracle from the unitary
let oracle = DiscreteOracle(Oracle_Reference(U, _, _));
// Allocate qubits to hold the eigenstate of U and the phase in a big endian register
using ((eigenstate, phaseRegister) = (Qubit[1], Qubit[n])) {
let phaseRegisterBE = BigEndian(phaseRegister);
// Prepare the eigenstate of U
P(eigenstate[0]);
// Call library
QuantumPhaseEstimation(oracle, eigenstate, phaseRegisterBE);
// Read out the phase
let phase = IntAsDouble(MeasureInteger(BigEndianAsLittleEndian(phaseRegisterBE))) / IntAsDouble(1 <<< n);
ResetAll(eigenstate);
ResetAll(phaseRegister);
return phase;
}
}
//////////////////////////////////////////////////////////////////
// Part II. Iterative phase estimation
//////////////////////////////////////////////////////////////////
// Task 2.1. Single-bit phase estimation
operation SingleBitPE_Reference (U : (Qubit => Unit is Adj + Ctl), P : (Qubit => Unit is Adj)) : Int {
using ((control, eigenstate) = (Qubit(), Qubit())) {
// prepare the eigenstate |ψ⟩
P(eigenstate);
H(control);
Controlled U([control], eigenstate);
H(control);
let eigenvalue = M(control) == Zero ? 1 | -1;
ResetAll([control, eigenstate]);
return eigenvalue;
}
}
// Task 2.2. Two bit phase estimation
operation TwoBitPE_Reference (U : (Qubit => Unit is Adj + Ctl), P : (Qubit => Unit is Adj)) : Double {
// Start by using the same circuit as in task 2.1.
// For eigenvalues +1 and -1, it produces measurement results Zero and One, respectively, 100% of the time;
// for eigenvalues +i and -i, it produces both results with 50% probability, so a different circuit is required.
using ((control, eigenstate) = (Qubit(), Qubit())) {
// prepare the eigenstate |ψ⟩
P(eigenstate);
mutable (measuredZero, measuredOne) = (false, false);
mutable iter = 0;
repeat {
set iter += 1;
H(control);
Controlled U([control], eigenstate);
H(control);
let meas = MResetZ(control);
set (measuredZero, measuredOne) = (measuredZero or meas == Zero, measuredOne or meas == One);
}
// repeat the loop until we get both Zero and One measurement outcomes
// or until we're reasonably certain that we won't get a different outcome
until (iter == 10 or measuredZero and measuredOne);
Reset(eigenstate);
// all measurements yielded Zero => eigenvalue +1
// all measurements yielded One => eigenvalue -1
if (not measuredZero or not measuredOne) {
return measuredOne ? 0.5 | 0.0;
}
}
// To distinguish between eigenvalues i and -i, we need a circuit with an extra S gate on control qubit
using ((control, eigenstate) = (Qubit(), Qubit())) {
// prepare the eigenstate |ψ⟩
P(eigenstate);
H(control);
Controlled U([control], eigenstate);
S(control);
H(control);
let eigenvalue = MResetZ(control) == Zero ? 0.75 | 0.25;
Reset(eigenstate);
return eigenvalue;
}
}
}