Quantum Algorithm - Deutsch's Algorithm
Quantum Algorithm - Deutsch's Algorithm
1. Constant function
A function is constant if it produces the same output for all possible inputs.
2. Balanced function
A function is balanced if it produces an equal number of 0 and 1 as output for all possible
inputs.
1. NOT Gate X
NOT gate (X) is a basic logic gate that inverts the input bit. If the input is 0,
the output is 1.If the input is 1, the output is 0.
2. Hadamard Gate H
Hadamard gate (H) transforms a qubit into a superposition of the basis states
∣0⟩ to ∣+⟩ and ∣1⟩ to ∣-⟩ .
eg. H ∣0⟩ = 1/√2 (∣0⟩ + ∣1⟩)
H ∣1⟩ = 1/√2 (∣0⟩ - ∣1⟩)
If Hadamard gate applies to ∣+⟩ and ∣-⟩, H ∣+⟩ to ∣0⟩ and ∣-⟩ to ∣1⟩
eg. H ∣+⟩ = H (1/√2 (∣0⟩ + ∣1⟩)) = ∣0⟩
H ∣-⟩ = H (1/√2 (∣0⟩ - ∣1⟩)) = ∣1⟩
∣y⟩ ∣y ⊕ f(x) ⟩
⊕ = binary sum, ⊗ = tensor product
If x= 1, y=1, 1⊕1=0
If x= 1, y=0 or x=0, y=1 , 1 ⊕ 0 = 0 ⊕ 1 = 1
If x= 0, y=0, 0⊕0=0
∣0⊕ a⟩ - ∣1⊕ a⟩
If a= 0, ∣0⟩ - ∣1⟩
If a= 1, ∣1⟩ - ∣0⟩
So to summarize this, it will become (-1)a (∣0⟩ - ∣1⟩) for both a=0 and a=1.
0 if f(x) is constant
∣0⟩ H H
1 if f(x) is balanced
Uf
∣0⟩ X ∣1⟩ H H
∣0⟩∣0⟩
∣0⟩ NOT ∣0⟩
∣0⟩ ∣1⟩ (initialization)
∣ψ1⟩ = ∣0⟩ ⊗ ∣1⟩
∣ψ2⟩ = H ∣ψ1⟩
= H ∣0⟩ ⊗ H ∣1⟩
= 1/√2 (∣0⟩ + ∣1⟩) ⊗ 1/√2 (∣0⟩ - ∣1⟩)
= 1/2 ( ∣00⟩ - ∣01⟩ + ∣10⟩ - ∣11⟩ )
∣ψ3⟩ = Uf ∣ψ2⟩
= 1/2 ( ∣0⟩ ∣0 ⊕ f(0)⟩ - ∣0⟩ ∣1 ⊕ f(0)⟩ + ∣1⟩ ∣0 ⊕ f(1) ⟩ - ∣1⟩ ∣1 ⊕
f(1)⟩ )
= 1/2 ( ∣0⟩ ( ∣0 ⊕ f(0)⟩ - ∣1 ⊕ f(0)⟩ ) + ∣1⟩ ( ∣0 ⊕ f(1) ⟩ - ∣1 ⊕ f(1)⟩ ) )
Observation
= 1/2 ( ∣0⟩ (-1)f(0) ( ∣0⟩ - ∣1⟩ ) + ∣1⟩ (-1)f(1) ( ∣0⟩ - ∣1⟩ ) )
= 1/2 ( ∣0⟩ (-1)f(0) + ∣1⟩ (-1)f(1) ) ( ∣0⟩ - ∣1⟩ )
= 1/√2 ( ∣0⟩ (-1)f(0) + ∣1⟩ (-1)f(1) ) ⊗ 1/√2 ( ∣0⟩ - ∣1⟩ )
∣ψ4⟩ = H ∣ψ3⟩
n=1
oracle_circuit = oracle(n, is_balanced=False)
for i in range(n+1):
qc.h(i)
qc.append(oracle_circuit, range(n+1))
for i in range(n+1):
qc.h(i)
qc.barrier()
qc.save_statevector()
qc.draw('mpl')
simulator = AerSimulator()
qc = transpile(qc,simulator)
result = simulator.run(qc, shots = 1024).result()
counts = result.get_counts()
plot_histogram(counts)