!python --version
!pip install qiskit

!pip install pylatexenc

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from qiskit.visualization import plot_histogram
from qiskit.visualization import plot_state_city
from random import randrange
from qiskit.tools.monitor import job_monitor

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import random
import math 


#discrete time quantum random walk with a hadamard coin
n=2
steps=1

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)


#initialized
#QC.x(qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])

#print(a)
    
display(QC.draw(output="mpl"))

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)

#discrete time quantum random walk with a hadamard coin
n=2
steps=2

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)


#initialized
#QC.x(qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])

#print(a)
    
display(QC.draw(output="mpl"))

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)

#discrete time quantum random walk with a hadamard coin
n=2
steps=3

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)


#initialized
#QC.x(qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])

#print(a)
    
display(QC.draw(output="mpl"))

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)

from qiskit.quantum_info import DensityMatrix
from qiskit.visualization import plot_state_city

#discrete time quantum random walk with a hadamard coin
n=3
steps=11

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)


#initialized
#QC.x(qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])

#print(a)
    
display(QC.draw(output="mpl"))

state = DensityMatrix.from_instruction(QC)

plot_state_city(state,figsize=(15, 8), color=['midnightblue', 'midnightblue'],
     title="New State City")

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, figsize=(10, 2), color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)

# Import Aer
from qiskit import Aer

# Run the quantum circuit on a statevector simulator backend
backend = Aer.get_backend('statevector_simulator')

# Create a Quantum Program for execution 
job = execute(QC, backend)

result = job.result()

outputstate = result.get_statevector(QC, decimals=3)
print(outputstate)

#discrete time quantum random walk with a hadamard coin
n=6
steps=11

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)


#initialized coin |0>

#QC.x(qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])
        
#print(a)
    
display(QC.draw(output="mpl"))

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, figsize=(10, 2), color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)

#discrete time quantum random walk with a hadamard coin
n=6
steps=11

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)


#initialized coin |1>
QC.x(qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])
        
#print(a)
    
display(QC.draw(output="mpl"))

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, figsize=(10, 2), color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)

from qiskit import *
%matplotlib inline
#discrete time quantum random walk with a hadamard coin
n=6
steps=11

qpos = QuantumRegister(n,'qpos')
qcoin = QuantumRegister(1,'qcoin')
cpos = ClassicalRegister(n,'cr')
QC = QuantumCircuit(qpos, qcoin)

pi=math.pi

#initialized coin Ugate
QC.u(pi/2, 3*pi/2, 0, qcoin[0])

for i in range(steps):

#coin
    QC.h(qcoin[0])
    QC.barrier()

#increment gate operator
    for i in range(n):
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
    QC.barrier()

#decrement gate operator
  
    QC.x(qcoin[0])
    for i in range(n):
        if i+1 < n: QC.x(qpos[i+1:])
        QC.mct([qcoin[0]]+qpos[i+1:], qpos[i], None, mode='noancilla')
        if i+1 < n: QC.x(qpos[i+1:])
    QC.barrier()
a=n/2
p=math.floor(a)
#print(p)
for k in range(n):
    if(k<p):
        QC.swap(qpos[n-1-k],qpos[k])

#print(a)
    
display(QC.draw(output="mpl"))

QC.measure_all()

    # # Execute the circuit on the qasm simulator
    
job = execute(QC,Aer.get_backend('aer_simulator'),shots=10000)
counts = job.result().get_counts(QC)   
print(counts)

plot_histogram(counts, figsize=(10, 2), color='midnightblue', title="New Histogram")

#aer_sim = Aer.get_backend('aer_simulator')
#qobj = assemble(had)
#result = aer_sim.run(qobj).result()
#counts = result.get_counts()
#plot_histogram(counts)