Deutsch-Jozsa Algorithm
- Tags: Quantum Computing, CalcCompl
- Source: qiskit, Learn Quantum Computing with Python and Q# [Ap.D]
- see Jupiter Notebook Extract
The first example of a quantum algorithm performing better than the best classical algorithm.
The algorithm can be applied to an oracle \(U_{f}\) where \(f\) is either constant or balanced and decide in \(O(1)\) whether it’s one or the other, in a single application.
Algorithm
- prepare two qubits,
control
andtarget
, in the \(| 0 \rangle \otimes |0\rangle\) state - prepare the state \(| + - \rangle\)1
- apply the
oracle
\(U_{f}\) to input state \(| +- \rangle\) - measure
control
in the \(X\text{-basis}\)- if 0 then \(f\) is constant, otherwise \(f\) is balanced
The algorithm can be extended to \(n\) qubits with functions of form \[f(x_{0}, x_{1},\cdots,x_{n})\] and n-qubits oracles \[U_{f}|x_{0} x_{1}\cdots x_{n}y\rangle = | x_{0} x_{1}\cdots x_{n}\rangle \otimes | f(x_{0}, x_{1},\cdots,x_{n}) \oplus y\rangle\]

Figure 1: Circuit for Deutsch-Jozsa
Code
Algorithm
namespace DeutschJozsa {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Diagnostics;
operation CheckIfOracleIsBalanced(oracle : ((Qubit, Qubit) => Unit))
: Bool {
use control = Qubit();
use target = Qubit();
H(control);
X(target);
H(target);
oracle(control, target);
H(target);
X(target);
return MResetX(control) == One;
}
operation RunDeutschJozsaAlgorithm() : Unit {
Fact(not CheckIfOracleIsBalanced(ApplyZeroOracle),
"Test failed for zero oracle.");
Fact(not CheckIfOracleIsBalanced(ApplyOneOracle),
"Test failed for one oracle.");
Fact(CheckIfOracleIsBalanced(ApplyIdOracle),
"Test failed for id oracle.");
Fact(CheckIfOracleIsBalanced(ApplyNotOracle),
"Test failed for not oracle.");
Message("All tests passed!");
}
}
Oracles
namespace DeutschJozsa {
open Microsoft.Quantum.Intrinsic;
operation ApplyZeroOracle(control : Qubit, target : Qubit) : Unit {
}
operation ApplyOneOracle(control : Qubit, target : Qubit) : Unit {
X(target);
}
operation ApplyIdOracle(control : Qubit, target : Qubit) : Unit {
CNOT(control, target);
}
operation ApplyNotOracle(control : Qubit, target : Qubit) : Unit {
X(control);
CNOT(control, target);
X(control);
}
}
-
shortand for \(| + \rangle \otimes | - \rangle\) ↩︎