In [14]:
from IPython.display import Image
Image("images/F2.png",width=300)

Out[14]:
In [15]:
Image("images/dieti.png",width=200)

Out[15]:

# Introduction to Quantum Circuits¶

## Quantum Fourier Transform¶

In [1]:
import numpy as np
from numpy import pi
# importing Qiskit
from qiskit import *
from qiskit.visualization import plot_histogram, plot_bloch_multivector
#config InlineBackend.figure_format = 'svg' # Makes the images look nice


## Quantum Fourier Transform N=3¶

In [2]:
qc = QuantumCircuit(3)
qc.h(2)
qc.cp(pi/2, 1, 2)
qc.cp(pi/4, 0, 2)
qc.h(1)
qc.cp(pi/2, 0, 1)
qc.h(0)
qc.swap(0,2)
qc.draw('mpl')

Out[2]:

### General QFT Function ¶

In [3]:
def qft_rotations(circuit, n):
if n == 0: # Exit function if circuit is empty
return circuit
n -= 1 # Indexes start from 0
circuit.h(n) # Apply the H-gate to the most significant qubit
for qubit in range(n):
circuit.cp(pi/2**(n-qubit), qubit, n)

In [4]:
qc = QuantumCircuit(4)
qft_rotations(qc,4)
qc.draw('mpl')

Out[4]:
In [22]:
def qft_rotations(circuit, n):
"""Performs qft on the first n qubits in circuit (without swaps)"""
if n == 0:
return circuit
n -= 1
circuit.h(n)
for qubit in range(n):
circuit.cp(pi/2**(n-qubit), qubit, n)
qft_rotations(circuit, n)
nb = 6
qc = QuantumCircuit(nb)
qft_rotations(qc,nb)
qc.draw('mpl')

Out[22]:
In [23]:
def swap_registers(circuit, n):
for qubit in range(n//2):
circuit.swap(qubit, n-qubit-1)
return circuit

def qft(circuit, n):
"""QFT on the first n qubits in circuit"""
qft_rotations(circuit, n)
swap_registers(circuit, n)
return circuit

# Let's see how it looks:
qc = QuantumCircuit(6)
qft(qc,6)
qc.draw('mpl')

Out[23]:
In [18]:
# Create the circuit
qc = QuantumCircuit(3)

# Encode the state 7
qc.x(0)
qc.x(1)
qc.x(2)
qc.draw('mpl')

Out[18]:
In [19]:
backend = Aer.get_backend("statevector_simulator")
statevector = execute(qc, backend=backend).result().get_statevector()
plot_bloch_multivector(statevector)

Out[19]:
In [20]:
qft(qc,3)
qc.draw('mpl')

Out[20]:
In [21]:
statevector = execute(qc, backend=backend).result().get_statevector()
plot_bloch_multivector(statevector)
#print(statevector)

Out[21]:

## References¶

1. This jupyter notebook has been adapted from https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html

2. M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, Cambridge Series on Information and the Natural Sciences (Cambridge University Press, Cambridge, 2000).

3. David Deutsch & Richard Jozsa (1992). "Rapid solutions of problems by quantum computation". Proceedings of the Royal Society of London A. 439 (1907): 553–558. doi:10.1098/rspa.1992.0167.

4. N. David Mermin "Quantum Computer Science (An Introduction)", Cambridge (2007)

5. Qiskit: An Open-source Framework for Quantum Computing (2019), doi:10.5281/zenodo.2562110