from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer, IBMQ, BasicAer, assemble, transpile
from qiskit.visualization import plot_bloch_multivector,plot_bloch_vector, plot_histogram
from qiskit.quantum_info import Statevector
import numpy as np 
import matplotlib

backend = BasicAer.get_backend('qasm_simulator')
shots = 1024
style = {'backgroundcolor': 'lightyellow'} # Style of the circuits

qreg = QuantumRegister(2) 
creg = ClassicalRegister(1) 

qc = QuantumCircuit(qreg, creg)

qc.x(1) # Applying X gate on qubit 2 to get |1>

qc.barrier()
qc.draw(output='mpl', style=style) 

# Applying Hadamard Gate
for qubit in range(2):
    qc.h(qubit)

qc.barrier()
qc.draw(output='mpl', style=style) 

# Balanced Oracle
qc.cx(0,1)
qc.barrier()
for qubit in range(1):
    qc.h(qubit)
qc.measure(0,0)
qc.draw(output='mpl', style=style) 

transpiled_dj_circuit = transpile(qc, backend)
qobj = assemble(transpiled_dj_circuit)
results = backend.run(qobj).result()
answer = results.get_counts()
plot_histogram(answer)

## Re-initialising
qreg = QuantumRegister(2) 
creg = ClassicalRegister(1) 

qc = QuantumCircuit(qreg, creg)

qc.x(1) # Applying X gate on qubit 2 to get |1>

qc.barrier()
# qc.draw(output='mpl', style=style) 

# Applying Hadamard Gate
for qubit in range(2):
    qc.h(qubit)

qc.barrier()

# Constant Oracle
if qc[1]==1:
    qc.x(1)

for qubit in range(1):
    qc.h(qubit)
qc.measure(0,0)
qc.draw(output='mpl', style=style) 


transpiled_dj_circuit = transpile(qc, backend)
qobj = assemble(transpiled_dj_circuit)
results = backend.run(qobj).result()
answer = results.get_counts()
plot_histogram(answer)