-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDeutschJozsaCode.ans
More file actions
63 lines (56 loc) · 2.11 KB
/
Copy pathDeutschJozsaCode.ans
File metadata and controls
63 lines (56 loc) · 2.11 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
// This programming assignment is focused on debugging Deutsch-Jozsa algorithm.
// The code contains exactly 7 bugs. Find them all!
// Phase oracle implementing a constant function f(x) = 0
operation PhaseOracleZero (inputRegister : Qubit[]) : Unit {
// Do nothing!
}
// Marking oracle implementing a balanced function f(x) = xₖ (the value of k-th bit)
operation MarkingOracleKthBit (inputRegister : Qubit[], target : Qubit, k : Int) : Unit {
Controlled X([inputRegister[k]], target);
}
operation ApplyMarkingOracleAsPhaseOracle (
markingOracle : ((Qubit[], Qubit) => Unit),
inputRegister : Qubit[]
) : Unit {
use target = Qubit();
// Put the target into the |-⟩ state
X(target);
H(target);
// Apply the marking oracle; since the target is in the |-⟩ state,
// this will apply a -1 factor to the states that satisfy the oracle condition
markingOracle(inputRegister, target);
H(target);
X(target);
}
operation IsFunctionConstant (nQubits : Int, phaseOracle : (Qubit[] => Unit)) : Bool {
mutable isConstant = true;
use qubits = Qubit[nQubits];
// Apply the H gates, the oracle and the H gates again
within {
ApplyToEachA(H, qubits);
} apply {
phaseOracle(qubits);
}
// Measure all qubits
let measurementResults = MResetEachZ(qubits);
for m in measurementResults {
if m == One {
set isConstant = false;
}
}
return isConstant;
}
function ConstantOrBalanced (value : Bool) : String {
return value ? "constant" | "balanced";
}
@EntryPoint()
operation RunDeutschJozsaAlgorithm () : Unit {
// for constant function
let isZeroConstant = IsFunctionConstant(2, PhaseOracleZero);
Message($"f(x) = 0 classified as {ConstantOrBalanced(isZeroConstant)}");
// for balanced function
let markingOracleSecondBit = MarkingOracleKthBit(_, _, 1);
let phaseOracleSecondBit = ApplyMarkingOracleAsPhaseOracle(markingOracleSecondBit, _);
let isSecondBitConstant = IsFunctionConstant(2, phaseOracleSecondBit);
Message($"f(x) = x₂ classified as {ConstantOrBalanced(isSecondBitConstant)}");
}