QCEXP@8
QCEXP@8
EXPERIMENT 8
THEORY:
Deutsch’s Algorithm is one of the first quantum algorithms that demonstrates quantum
speedup over classical algorithms. It solves the problem of determining whether a given
function is constant or balanced with just one function evaluation, whereas a classical
algorithm would require two evaluations.
Problem Statement
We are given a black-box function (oracle) f: {0,1} → {0,1}, which takes a single-bit input
and produces a single-bit output. The function can be either:
Our goal is to determine whether f is constant or balanced with one function call.
Mathematical Formulation
In the classical case, we need to evaluate both f(0) and f(1) to determine the function type.
In the quantum case, we can use superposition and interference to determine the function
type in a single query.
Uf represents the oracle, which we implement differently for constant and balanced
functions.
CODE:
Qiskit Implementation:-
import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
def deutsch_algorithm(oracle):
qc = QuantumCircuit(2, 1)
qc.h(0)
qc.h(1)
qc.append(oracle.to_instruction(), [0, 1])
qc.h(0)
qc.measure(0, 0)
simulator = AerSimulator()
transpiled_qc = transpile(qc, simulator)
job = simulator.run(transpiled_qc, shots=1024)
result = job.result()
counts = result.get_counts()
def constant_oracle():
qc = QuantumCircuit(2)
return qc
def balanced_oracle():
qc = QuantumCircuit(2)
Mitesh Singh / B22 / 2101111
qc.cx(0, 1)
return qc
IMPLEMENTATION
1. Define the Deutsch Algorithm (deutsch_algorithm)
o Initializes the quantum circuit with 2 qubits and 1 classical bit.
o Applies Hadamard gates to both qubits to create superposition.
o Applies the oracle function (constant or balanced).
o Applies another Hadamard gate to the first qubit to extract information.
o Measures the first qubit and interprets the result.
2. Define the Oracle Functions
o Constant Oracle (constant_oracle): Does nothing since f(x) is the same for
all inputs.
o Balanced Oracle (balanced_oracle): Uses a CNOT (CX) gate to flip the
target qubit when the control is 1.
3. Run Deutsch’s Algorithm
o Executes the circuit on a quantum simulator.
o Outputs either "Constant Function" or "Balanced Function".
Expected Output
Testing Constant Function:
Constant Function
This confirms that Deutsch’s algorithm correctly determines the nature of the function in
just one evaluation, showcasing quantum advantage.
Mitesh Singh / B22 / 2101111
CONCLUSION:-
Deutsch Algorithm provides an elegant demonstration of quantum speedup, leveraging
superposition and interference to solve the problem with just one function evaluation
instead of two. By encoding the function properties into a quantum state and using the
Hadamard transformation, the algorithm efficiently distinguishes between constant and
balanced functions. The implementation showcases how quantum oracles work and how
quantum gates like Hadamard (H) and CNOT (CX) enable computational advantages. This
fundamental concept is extended in the Deutsch-Jozsa Algorithm, which generalizes the
problem for n-bit functions, further highlighting the power of quantum computing in solving
specific problems exponentially faster than classical methods.