-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeutsch_jozsa.qs
42 lines (32 loc) · 1.04 KB
/
deutsch_jozsa.qs
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
namespace DeutschJozsa {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
operation Oracle (target : Qubit[]) : Unit {
// The black box function you wish to test is a constant or balanced function
}
operation DeutschJozsa (register : Qubit[]) : Bool {
let nQubits = Length(register);
use ancilla = Qubit();
// Prepare the initial state
ApplyToEach(H, register);
X(ancilla);
H(ancilla);
// Apply the oracle
Oracle(register + [ancilla]);
// Apply Hadamard gates to all qubits in the register
ApplyToEach(H, register);
// Measure the qubits in the register
let results = MeasureEachZ(register);
// Return true if all measurement results are 0, false otherwise
for result in results
{
if (result == One)
{
return false;
}
}
return true; // All Zero
}
}