# !pip3 install qiskit 

import qiskit as q
import matplotlib.pyplot as plt
from qiskit import IBMQ

qr = q.QuantumRegister(2)
cr = q.ClassicalRegister(2)

circuit = q.QuantumCircuit(qr, cr)
circuit.draw()

circuit.h(qr[0]) # apply hadamard gate on first qubit. 
circuit.h(qr[1]) # apply hadamard gate on second qubit. 
# apply cnot gate.  
display(circuit.draw())

#### see the state-vector 
statevec = q.Aer.get_backend('statevector_simulator')
final_state = q.execute(circuit, statevec).result().get_statevector()
q.visualization.plot_bloch_multivector(final_state) 

circuit.measure(qr, cr)
circuit.draw(scale=2)

#### simulate result on a local computer 

simulator = q.Aer.get_backend(name='qasm_simulator')
results = q.execute(circuit, backend=simulator, ).result()
q.visualization.plot_histogram(results.get_counts(circuit)) 

### try a different circuit where we apply X gate before applying H gate. 

qr1 = q.QuantumRegister(3)
cr1 = q.ClassicalRegister(3)

circuit1 = q.QuantumCircuit(qr1, cr1)
# circuit1.draw()


circuit1.h(qr1[0]) # apply the H gate on q0
circuit1.h(qr1[1]) # apply H gate on q_1
circuit1.h(qr1[2]) # add H gate on q_2

### draw the circuit
circuit1.draw()

#### plt the state vector 

statevec1 = q.Aer.get_backend('statevector_simulator')
final_state1 = q.execute(circuit1, statevec1).result().get_statevector()
q.visualization.plot_bloch_multivector(final_state1) # the state doesn't not change after application of the CNOT gate 
#### unchanged state as expected 

circuit1.measure(qr1, cr1)
circuit1.draw(scale=2)

circuit1.measure(qr1, cr1)
### simulate result on local computer 
simulator1 = q.Aer.get_backend('qasm_simulator')
results1 = q.execute(circuit1, backend=simulator1, ).result()
q.visualization.plot_histogram(results1.get_counts(circuit1)) 

# Now we will use IBM Quantum Computer to see the result 

IBMQ.save_account('your token')
IBMQ.load_account()

provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_valencia') # check which computer has 0 jobs on queue
job = q.execute(circuit1, backend=qcomp)
q_result = job.result()

q.visualization.plot_histogram(q_result.get_counts(circuit1))

