# !pip3 install qiskit

import numpy as np
import qiskit as q

### build a circuit for 3 qubits 

qr = q.QuantumRegister(3)
circ = q.QuantumCircuit(qr)
### add the first H gate (in qiskit east significant bit has the lowest index) 
circ.h(qr[2])
### add the controlled phase gate 
circ.cp(np.pi/2, qr[1], qr[2]) # based on qubit 1 apply 2pi/2**(k-1) rotation to qubit 2
### add the next cp gate
circ.cp(np.pi/4, qr[0], qr[2]) # based on qubit 0 apply 2pi/2**(k-1) rotation to qubit 2

### repeat the process for qubit 1
circ.h(qr[1])
circ.cp(np.pi/2, qr[0], qr[1])

### add the final h gate
circ.h(qr[0])

### finally swap the bits 0 th, and 2nd qubit 

circ.swap(qr[0], qr[2])

circ.draw()

from google.colab import drive
drive.mount('/content/drive')

# !pip install pylatexenc

path='/content/drive/My Drive/Colab Notebooks/'
style = {'backgroundcolor': 'lavender', 'dpi':200, 'subfontsize':10}


circ.draw('mpl', scale=0.8, style=style, filename=path+'qfouriert_3bits.png')

qr = q.QuantumRegister(3)
circ1 = q.QuantumCircuit(qr)

### encode the state 110 at first
circ1.x(qr[2])
circ1.x(qr[1]) 

### repeat what's done before

### add the first H gate (in qiskit east significant bit has the lowest index) 
circ1.h(qr[2])
### add the controlled phase gate 
circ1.cp(np.pi/2, qr[1], qr[2]) # based on qubit 1 apply 2pi/2**(k-1) rotation to qubit 2
### add the next cp gate
circ1.cp(np.pi/4, qr[0], qr[2]) # based on qubit 0 apply 2pi/2**(k-1) rotation to qubit 2

### repeat the process for qubit 1
circ1.h(qr[1])
circ1.cp(np.pi/2, qr[0], qr[1])

### add the final h gate
circ1.h(qr[0])

### finally swap the bits 0 th, and 2nd qubit 

circ1.swap(qr[0], qr[2])

circ1.draw()

circ1.save_statevector()

qasm_sim = q.Aer.get_backend('qasm_simulator')


statevector = qasm_sim.run(circ1).result().get_statevector()
q.visualization.plot_bloch_multivector(statevector)

qr = q.QuantumRegister(3)
circ2 = q.QuantumCircuit(qr)

### encode the state 101 at first
circ2.x(qr[2])
circ2.x(qr[0]) 

### repeat what's done before

### add the first H gate (in qiskit east significant bit has the lowest index) 
circ2.h(qr[2])
### add the controlled phase gate 
circ2.cp(np.pi/2, qr[1], qr[2]) # based on qubit 1 apply 2pi/2**(k-1) rotation to qubit 2
### add the next cp gate
circ2.cp(np.pi/4, qr[0], qr[2]) # based on qubit 0 apply 2pi/2**(k-1) rotation to qubit 2

### repeat the process for qubit 1
circ2.h(qr[1])
circ2.cp(np.pi/2, qr[0], qr[1])

### add the final h gate
circ2.h(qr[0])

### finally swap the bits 0 th, and 2nd qubit 

circ2.swap(qr[0], qr[2])

circ2.draw()

circ2.save_statevector()

qasm_sim = q.Aer.get_backend('qasm_simulator')


statevector2 = qasm_sim.run(circ2).result().get_statevector()
q.visualization.plot_bloch_multivector(statevector2)

