#!pip install qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute, dagcircuit 
import numpy as np

from qiskit.visualization import plot_histogram
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
"""
we need to input how many steps the quantum system will take in total similar to CRW
if no of qubits is n then then the graph will have 2^(n-1) nodes or points to shift. here the first qubit will play the role of coin as in CRW.
"""
n_steps = 1 #Number of iterations
qubits= QuantumRegister(4, name = 'qubits')
cbits= ClassicalRegister(4, name= 'cbits')

#increment gate
print(len(qubits))
inc = QuantumCircuit (qubits,name='inc') #Circuit for inc operator
inc.rcccx( qubits[0],qubits[1],qubits[2],qubits[3])
inc.ccx(qubits[0],qubits[1],qubits[2])
inc.cx(qubits[0],qubits[1])
inc.draw()















inc= inc.to_instruction()
#decrement gate
dec= QuantumCircuit (qubits,name='dec') #Circuit for dec operator
dec.x(qubits[0])
dec.x(qubits[1])
dec.x(qubits[2])
dec.rcccx( qubits[0],qubits[1],qubits[2],qubits[3])
dec.x(qubits[2])
dec.ccx(qubits[0],qubits[1],qubits[2])
dec.x(qubits[1])
dec.cx(qubits[0],qubits[1])
dec.x(qubits[0])
dec.draw()




dec= dec.to_instruction()
#Main Circuit

circ = QuantumCircuit (qubits, cbits) #Main circuit
#Fixing the coin spin either 0 or 1
#circ.x(qubits[0])

for i in range(n_steps):
 
 circ.h (qubits[0]) #Coin step
 circ.append (inc, [qubits[0],qubits[1],qubits[2],qubits[3]]) #Shift step
 circ.append (dec, [qubits[0],qubits[1],qubits[2],qubits[3]]) #Shift step

circ.measure ([qubits[0],qubits[1],qubits[2],qubits[3]], [cbits[0],cbits[1],cbits[2],cbits[3]])

# Draw the circuit
circ.draw()


simulator = Aer.get_backend('qasm_simulator')
job = execute(circ, simulator, shots=1000)
result = job.result()
counts = result.get_counts(circ)
print("\nTotal counts are:",counts)
# Plot a histogram
plot_histogram(counts)
