# Importing everything
from qiskit import *
from qiskit.visualization import plot_histogram
# For Jupyter Notebooks, change the settings to get nicer images
%config InlineBackend.figure_format = 'svg'

def create_bell_pair(qc, a, b):
    qc.h(a)
    qc.cx(a, b)

def encode_message(qc, qubit, message):
    if message == "00":
        pass
    elif message == "01":
        qc.z(qubit)
    elif message  == "10":
        qc.x(qubit)
    elif message == "11":
        qc.z(qubit)
        qz.x(qubit)
    else:
        print("Enter Valid message please")

def decode_message(qc, a, b):
    qc.cx(a, b)
    qc.h(a)

# Create the quantum circuit with 2 qubits
qc = QuantumCircuit(2)


# First, Eve creates the entangled pair between Alice and Bob
create_bell_pair(qc, 0, 1)
qc.barrier() # This adds a barrier to our circuit. A barrier 
             # separates the gates in our diagram and makes it 
             # clear which part of the circuit is which

# At this point, qubit 0 goes to Alice and qubit 1 goes to Bob

# Next, Alice encodes her message onto qubit 0. In this case,
# we want to send the message '10'. You can try changing this
# value and see how it affects the circuit
message = "10"
encode_message(qc, 0, message)
qc.barrier()
# Alice then sends her qubit to Bob.

# After recieving qubit 0, Bob applies the recovery protocol:
decode_message(qc, 0, 1)

# Finally, Bob measures his qubits to read Alice's message
qc.measure_all()

# Draw our output
qc.draw(output = "mpl")

backend = Aer.get_backend('qasm_simulator')
job_sim = execute(qc, backend, shots=1024)
sim_result = job_sim.result()

measurement_result = sim_result.get_counts(qc)
print(measurement_result)
plot_histogram(measurement_result)

from qiskit import IBMQ
from qiskit.providers.ibmq import least_busy
shots = 256

# Load local account information
IBMQ.load_account()
# Get the least busy backend
provider = IBMQ.get_provider(hub='ibm-q')
backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 
                                       and not x.configuration().simulator 
                                       and x.status().operational==True))
print("least busy backend: ", backend)
# Run our circuit
job = execute(qc, backend=backend, shots=shots)

from qiskit.tools.monitor import job_monitor

job_monitor(job)

result = job.result()
plot_histogram(result.get_counts(qc))

