0% found this document useful (0 votes)
19 views4 pages

QCEXP@9

The document outlines the implementation of the Deutsch-Jozsa algorithm, a quantum algorithm that can determine if a black-box function is constant or balanced with only one query. It describes the steps involved, including the use of Hadamard gates and quantum superposition, and provides Python code using Qiskit to simulate the algorithm. The significance of the algorithm is highlighted, emphasizing its demonstration of quantum speedup compared to classical approaches.

Uploaded by

rahulcounsellor1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

QCEXP@9

The document outlines the implementation of the Deutsch-Jozsa algorithm, a quantum algorithm that can determine if a black-box function is constant or balanced with only one query. It describes the steps involved, including the use of Hadamard gates and quantum superposition, and provides Python code using Qiskit to simulate the algorithm. The significance of the algorithm is highlighted, emphasizing its demonstration of quantum speedup compared to classical approaches.

Uploaded by

rahulcounsellor1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Mitesh Singh B22 2101111 AI&DS

Experiment 9
Aim: To write a program to implement Deutsch-Jozsa’s algorithm
LO Mapped: LO3
LO Description: To understand quantum entanglement, quantum algorithms
Theory:
The Deutsch-Jozsa algorithm is a quantum algorithm that distinguishes between constant and
balanced functions in one query, regardless of the number of input bits. It was the first real
demonstration of a quantum speedup over classical algorithms.
Problem statement
n
You are given a black-box function f : { 0 ,1 } →{0 , 1} which is guaranteed to be either:

● Constant: returns the same value (either 0 or 1) for all inputs


● Balanced: returns 0 for half the inputs and 1 for the other half

The goal is to determine if f is constant or balanced.

Classical approach

In the worst case, you would have to evaluate f (x) on 2n−1 +1 inputs to be sure.

Quantum approach
It requires only 1 query, regardless of the number of inputs.
Steps
Assume we have n input qubits and 1 output qubit:

1) Initial state
● Input qubits: ¿ 0 ⟩⊗n
● Output qubit: ¿ 1 ⟩
● So, the state is:
⊗n
¿ 0 ⟩ ⊗∨1 ⟩
2) Apply Hadamard gates
● Apply Hadamard gates to all qubits to create a superposition
● Input qubits become:

1
√❑
● Output qubits become:

1
√❑
Mitesh Singh B22 2101111 AI&DS

3) Apply the oracle U f


● The oracle transforms the state like this:

U f ∨x ⟩∨ y ⟩=¿ x ⟩∨ y ⊕ f (x)⟩

● Due to the superposition and the way the output qubit was prepared, this encodes f (x) as a
phase:

¿x⟩ ( ¿ 0 ⊕ f ( x) ⟩−¿1
√❑
⨁ f (x)⟩
)
● So, the system becomes:

1
√❑
● The output qubit is now separable — we focus only on the input qubits
4) Apply Hadamard gates to the input qubits
● Now apply Hadamard gates to all n input qubits
● This causes interference based on the values of f (x)
5) Measure the input qubits
● If the result is all zeroes, then f is constant
● If the result is anything else, then f is balanced

Significance
● It only uses 1 query v/s exponential queries in classical computing
● It exploits quantum parallelism and interference
● While it has limited practical application, it demonstrates the principle of quantum speedup
● It also serves as a foundation for more practical algorithms such as Grover’s and Shor’s
algorithm
Code / Output:
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator

def deutsch_jozsa_algorithm(oracle, n):


"""
Implements the Deutsch-Jozsa algorithm for an n-bit function.
:param oracle: A quantum circuit implementing the oracle function
f(x)
:param n: Number of input qubits
"""
# Create a quantum circuit with n input qubits + 1 output qubit and
n classical bits for measurement
qc = QuantumCircuit(n + 1, n)

# Step 1: Initialise the last qubit to |1>


qc.x(n)

# Step 2: Apply Hadamard gates to all qubits


qc.h(range(n + 1))

# Step 3: Apply the oracle


qc.append(oracle, range(n + 1))
Mitesh Singh B22 2101111 AI&DS

# Step 4: Apply Hadamard gates to the first n qubits


qc.h(range(n))

# Step 5: Measure the first n qubits


qc.measure(range(n), range(n))

# Simulate the circuit


simulator = AerSimulator()
transpiled_circuit = transpile(qc, simulator)
result = simulator.run(transpiled_circuit, shots=1024).result()
counts = result.get_counts()

# Output results
print("Measurement outcomes:", counts)
print("Quantum Circuit:")
print(qc.draw(output='text'))
return counts

# Example oracles

def constant_oracle(n):
"""Returns a constant oracle (f(x) = 0 for all x)."""
oracle = QuantumCircuit(n + 1)
# Does nothing to the circuit (f(x) = 0)
return oracle.to_instruction()

def balanced_oracle(n):
"""Returns a balanced oracle (f(x) = x_0 XOR x_1 XOR ... XOR
x_n)."""
oracle = QuantumCircuit(n + 1)
for i in range(n):
oracle.cx(i, n)
return oracle.to_instruction()

if __name__ == "__main__":
n = 3 # Number of input qubits

print("Running Deutsch-Jozsa with constant oracle:")


deutsch_jozsa_algorithm(constant_oracle(n), n)

print("\nRunning Deutsch-Jozsa with balanced oracle:")


deutsch_jozsa_algorithm(balanced_oracle(n), n)
Mitesh Singh B22 2101111 AI&DS

Conclusion: Thus, we have successfully written a program to implement Deutsch-Jozsa’s algorithm

You might also like