%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import *
from qiskit.quantum_info import *
basis_gates = ['id', 'x', 'y', 'z', 's', 't', 'sdg', 'tdg', 'h', 'p', 'sx' ,'r', 'rx', 'ry', 'rz', 'u', 'u1', 'u2', 'u3', 'cx', 'ccx', 'barrier', 'measure', 'snapshot']

or_oracle = QuantumCircuit(3)

# Do not change below this line
or_oracle.draw(output='mpl')

or_tt = ['000', '011', '101', '111']
def check_or_oracle(tt_row):
    check_qc = QuantumCircuit(3)
    for i in range(2):
        if (tt_row[i] == '1'):
            check_qc.x(i)
    check_qc.extend(or_oracle)
    check_qc.measure_all()
    return (execute(check_qc.reverse_bits(),backend=QasmSimulator(), shots=1).result().get_counts().most_frequent() == tt_row)
try:
    assert list(or_oracle.count_ops()) != [], f"Circuit cannot be empty"
    assert 'measure' not in or_oracle.count_ops(), f"Please remove measurements"
    assert set(or_oracle.count_ops().keys()).difference(basis_gates) == set(), f"Only the following basic gates are allowed: {basis_gates}"
    for tt_row in or_tt:     
        assert check_or_oracle(tt_row), f" Input {tt_row[0:2]}: Your encoding is not correct"
    print("Your oracle construction passed all checks")
except AssertionError as e:
    print(f'Your code has an error:  {e.args[0]}')
    
except Exception as e:
    print(f'This error occured: {e.args[0]}')

bv_oracle = QuantumCircuit(3)
bv_oracle.cx(0,2)
bv_oracle.cx(1,2)
bv_oracle.draw('mpl')

bv_circ = QuantumCircuit(3,2)

# Do not remove this line
bv_circ.draw(output='mpl')

try:
    assert list(bv_circ.count_ops()) != [], f"Circuit cannot be empty"
    assert set(bv_circ.count_ops().keys()).difference(basis_gates) == set(), f"Only the following basic gates are allowed: {basis_gates}"
    counts = execute(bv_circ.reverse_bits(), backend=QasmSimulator(), shots=8192).result().get_counts()
    assert list(counts.keys()) == ['11'], "Your circuit did not produce the right answer"
    print(" Your circuit produced the correct output. Please submit for evaluation.")
except AssertionError as e:
    print(f'Your code has an error:  {e.args[0]}')
    
except Exception as e:
    print(f'This error occured: {e.args[0]}')
plot_histogram(counts)