!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
import matplotlib.pyplot as plt

%matplotlib inline
import matplotlib as mpl
from random import randrange
import numpy as np
import networkx as nx
import random
import math 
from qiskit.tools.monitor import job_monitor

from random import randrange
pose=[]
for i in range(400): 
     pose.append([])
#print(pose)
#pose[-1]=1
#print(pose[-1])
pose[0]=200
for k in range(200):
    rand = randrange(0,2)
    if rand==0:
        #print(rand)
        pose[k+1]=pose[k]+1
    if rand==1:
        pose[k+1]=pose[k]-1
#print(pose)
yc=[]
for e in range(400): 
     yc.append([])
for q in range(400):
    yc[q]=0
    for h in range(400):
        if pose[h]==q:
            yc[q]=yc[q]+1
#print((yc))
#print(len(yc))
xc = np.arange(0, 400, 1)
plt.plot(yc)
plt.xlim(150, 250)



#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])

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])

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])

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 QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer,assemble, transpile
from qiskit import BasicAer
from random import randrange
from qiskit.tools.visualization import plot_histogram, plot_state_city
import numpy as np
from qiskit.quantum_info.operators import Operator, Pauli
from qiskit.quantum_info import Operator, Statevector

q = QuantumRegister(6)  # "Qubits q[0] and q[1] for coin 1 and 2", "q[2] and q[3] for player 1" and q[4] and q[5] for player 2" 
c = ClassicalRegister(6)
Q = QuantumCircuit(q,c)

#Coin Operators
# Apply u gate as Hadamard gate to each coin
Q.u(np.pi/2,0,np.pi,q[0])   
Q.u(np.pi/2,0,np.pi,q[1])

#S operator

Q.x(q[0])
Q.x(q[1])
Q.mct([q[0],q[1]],q[3])
Q.mct([q[0],q[1]],q[5])
Q.x(q[0])
Q.x(q[1])
Q.barrier()


Q.x(q[0])
Q.mct([q[0],q[1]],q[2])
Q.mct([q[0],q[1]],q[3])
Q.x(q[0])
Q.barrier()

Q.x(q[1])
Q.mct([q[0],q[1]],q[4])
Q.mct([q[0],q[1]],q[5])
Q.x(q[1])
Q.barrier()

Q.mct([q[0],q[1]],q[2])
Q.mct([q[0],q[1]],q[4])




Q.measure_all()

Q.draw(output='mpl')

# Execute the circuit on the qasm simulator
# Use Aer's qasm_simulator
backend_sim = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.
# We've set the number of repeats of the circuit
# to be 1024, which is the default.
job_sim = execute(Q, backend_sim, shots=1000)

# Grab the results from the job.
result_sim = job_sim.result()
counts = result_sim.get_counts(Q)
print(counts)
    


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


q_o = QuantumRegister(6)  # "Qubits q[0] and q[1] for coin 1 and 2", "q[2] and q[3] for player 1" and q[4] and q[5] for player 2" 
c_o = ClassicalRegister(6)
Q_o = QuantumCircuit(q,c)

#Coin Operators
# Apply u gate as Hadamard gate to each coin
Q_o.u(2*np.pi/3,0,np.pi,q[0])   
Q_o.u(np.pi/2,0,np.pi,q[1])
#S operator

Q_o.x(q[0])
Q_o.x(q[1])
Q_o.mct([q[0],q[1]],q[3])
Q_o.mct([q[0],q[1]],q[5])
Q_o.x(q[0])
Q_o.x(q[1])
Q_o.barrier()


Q_o.x(q[0])
Q_o.mct([q[0],q[1]],q[2])
Q_o.mct([q[0],q[1]],q[3])
Q_o.x(q[0])
Q_o.barrier()

Q_o.x(q[1])
Q_o.mct([q[0],q[1]],q[4])
Q_o.mct([q[0],q[1]],q[5])
Q_o.x(q[1])
Q_o.barrier()

Q_o.mct([q[0],q[1]],q[2])
Q_o.mct([q[0],q[1]],q[4])




Q_o.measure_all()


# Execute the circuit on the qasm simulator
# Use Aer's qasm_simulator
backend_sim_o = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.
# We've set the number of repeats of the circuit
# to be 1024, which is the default.
job_sim_o = execute(Q_o, backend_sim, shots=1000)

# Grab the results from the job.
result_sim_o = job_sim_o.result()
counts_o = result_sim_o.get_counts(Q_o)
print(counts_o)
    


plot_histogram(counts_o, color='midnightblue', title="Decision Histogram")

